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"
46 #include "gallivm/lp_bld_pack.h"
47 #include "gallivm/lp_bld_format.h"
49 #include "tgsi/tgsi_exec.h"
50 #include "tgsi/tgsi_dump.h"
52 #include "util/u_math.h"
53 #include "util/u_pointer.h"
54 #include "util/u_string.h"
55 #include "util/u_simple_list.h"
62 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
,
67 * Create LLVM type for struct draw_jit_texture
70 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
72 LLVMTargetDataRef target
= gallivm
->target
;
73 LLVMTypeRef texture_type
;
74 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
75 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
77 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
78 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
79 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
80 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
81 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
82 elem_types
[DRAW_JIT_TEXTURE_BASE
] =
83 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
84 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
85 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
86 elem_types
[DRAW_JIT_TEXTURE_MIP_OFFSETS
] =
87 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
88 elem_types
[DRAW_JIT_TEXTURE_MIN_LOD
] =
89 elem_types
[DRAW_JIT_TEXTURE_MAX_LOD
] =
90 elem_types
[DRAW_JIT_TEXTURE_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
91 elem_types
[DRAW_JIT_TEXTURE_BORDER_COLOR
] =
92 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
94 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
95 Elements(elem_types
), 0);
97 #if HAVE_LLVM < 0x0300
98 LLVMAddTypeName(gallivm
->module
, struct_name
, texture_type
);
100 /* Make sure the target's struct layout cache doesn't return
101 * stale/invalid data.
103 LLVMInvalidateStructLayout(gallivm
->target
, texture_type
);
106 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
107 target
, texture_type
,
108 DRAW_JIT_TEXTURE_WIDTH
);
109 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
110 target
, texture_type
,
111 DRAW_JIT_TEXTURE_HEIGHT
);
112 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
113 target
, texture_type
,
114 DRAW_JIT_TEXTURE_DEPTH
);
115 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
116 target
, texture_type
,
117 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
118 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
119 target
, texture_type
,
120 DRAW_JIT_TEXTURE_LAST_LEVEL
);
121 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, base
,
122 target
, texture_type
,
123 DRAW_JIT_TEXTURE_BASE
);
124 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
125 target
, texture_type
,
126 DRAW_JIT_TEXTURE_ROW_STRIDE
);
127 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
128 target
, texture_type
,
129 DRAW_JIT_TEXTURE_IMG_STRIDE
);
130 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, mip_offsets
,
131 target
, texture_type
,
132 DRAW_JIT_TEXTURE_MIP_OFFSETS
);
133 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, min_lod
,
134 target
, texture_type
,
135 DRAW_JIT_TEXTURE_MIN_LOD
);
136 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, max_lod
,
137 target
, texture_type
,
138 DRAW_JIT_TEXTURE_MAX_LOD
);
139 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, lod_bias
,
140 target
, texture_type
,
141 DRAW_JIT_TEXTURE_LOD_BIAS
);
142 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, border_color
,
143 target
, texture_type
,
144 DRAW_JIT_TEXTURE_BORDER_COLOR
);
146 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
153 * Create LLVM type for struct draw_jit_texture
156 create_jit_context_type(struct gallivm_state
*gallivm
,
157 LLVMTypeRef texture_type
, const char *struct_name
)
159 LLVMTargetDataRef target
= gallivm
->target
;
160 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
161 LLVMTypeRef elem_types
[5];
162 LLVMTypeRef context_type
;
164 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
165 LP_MAX_TGSI_CONST_BUFFERS
);
166 elem_types
[1] = elem_types
[0]; /* gs_constants */
167 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
168 DRAW_TOTAL_CLIP_PLANES
), 0);
169 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewport */
170 elem_types
[4] = LLVMArrayType(texture_type
,
171 PIPE_MAX_SAMPLERS
); /* textures */
172 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
173 Elements(elem_types
), 0);
174 #if HAVE_LLVM < 0x0300
175 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
177 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
180 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
181 target
, context_type
, 0);
182 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, gs_constants
,
183 target
, context_type
, 1);
184 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
185 target
, context_type
, 2);
186 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
187 target
, context_type
,
188 DRAW_JIT_CTX_TEXTURES
);
189 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
190 target
, context_type
);
197 * Create LLVM type for struct pipe_vertex_buffer
200 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
, const char *struct_name
)
202 LLVMTargetDataRef target
= gallivm
->target
;
203 LLVMTypeRef elem_types
[4];
207 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
209 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0); /* vs_constants */
211 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
212 Elements(elem_types
), 0);
213 #if HAVE_LLVM < 0x0300
214 LLVMAddTypeName(gallivm
->module
, struct_name
, vb_type
);
216 LLVMInvalidateStructLayout(gallivm
->target
, vb_type
);
219 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
221 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
224 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
231 * Create LLVM type for struct vertex_header;
234 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
236 LLVMTargetDataRef target
= gallivm
->target
;
237 LLVMTypeRef elem_types
[4];
238 LLVMTypeRef vertex_header
;
239 char struct_name
[24];
241 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
243 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
244 elem_types
[DRAW_JIT_VERTEX_CLIP
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
245 elem_types
[DRAW_JIT_VERTEX_PRE_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
246 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
248 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
249 Elements(elem_types
), 0);
250 #if HAVE_LLVM < 0x0300
251 LLVMAddTypeName(gallivm
->module
, struct_name
, vertex_header
);
253 LLVMInvalidateStructLayout(gallivm
->target
, vertex_header
);
256 /* these are bit-fields and we can't take address of them
257 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
258 target, vertex_header,
259 DRAW_JIT_VERTEX_CLIPMASK);
260 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
261 target, vertex_header,
262 DRAW_JIT_VERTEX_EDGEFLAG);
263 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
264 target, vertex_header,
265 DRAW_JIT_VERTEX_PAD);
266 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
267 target, vertex_header,
268 DRAW_JIT_VERTEX_VERTEX_ID);
270 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
271 target
, vertex_header
,
272 DRAW_JIT_VERTEX_CLIP
);
273 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, pre_clip_pos
,
274 target
, vertex_header
,
275 DRAW_JIT_VERTEX_PRE_CLIP_POS
);
276 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
277 target
, vertex_header
,
278 DRAW_JIT_VERTEX_DATA
);
280 assert(LLVMABISizeOfType(target
, vertex_header
) ==
281 offsetof(struct vertex_header
, data
[data_elems
]));
283 return vertex_header
;
288 * Create LLVM types for various structures.
291 create_jit_types(struct draw_llvm_variant
*variant
)
293 struct gallivm_state
*gallivm
= variant
->gallivm
;
294 LLVMTypeRef texture_type
, context_type
, buffer_type
, vb_type
;
296 texture_type
= create_jit_texture_type(gallivm
, "texture");
298 context_type
= create_jit_context_type(gallivm
, texture_type
, "draw_jit_context");
299 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
301 buffer_type
= LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
302 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
304 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
305 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
310 get_context_ptr_type(struct draw_llvm_variant
*variant
)
312 if (!variant
->context_ptr_type
)
313 create_jit_types(variant
);
314 return variant
->context_ptr_type
;
319 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
321 if (!variant
->buffer_ptr_type
)
322 create_jit_types(variant
);
323 return variant
->buffer_ptr_type
;
328 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
330 if (!variant
->vb_ptr_type
)
331 create_jit_types(variant
);
332 return variant
->vb_ptr_type
;
336 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
338 if (!variant
->vertex_header_ptr_type
)
339 create_jit_types(variant
);
340 return variant
->vertex_header_ptr_type
;
345 * Create per-context LLVM info.
348 draw_llvm_create(struct draw_context
*draw
)
350 struct draw_llvm
*llvm
;
352 llvm
= CALLOC_STRUCT( draw_llvm
);
360 llvm
->nr_variants
= 0;
361 make_empty_list(&llvm
->vs_variants_list
);
368 * Free per-context LLVM info.
371 draw_llvm_destroy(struct draw_llvm
*llvm
)
373 /* XXX free other draw_llvm data? */
379 * Create LLVM-generated code for a vertex shader.
381 struct draw_llvm_variant
*
382 draw_llvm_create_variant(struct draw_llvm
*llvm
,
384 const struct draw_llvm_variant_key
*key
)
386 struct draw_llvm_variant
*variant
;
387 struct llvm_vertex_shader
*shader
=
388 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
389 LLVMTypeRef vertex_header
;
391 variant
= MALLOC(sizeof *variant
+
392 shader
->variant_key_size
-
393 sizeof variant
->key
);
397 variant
->llvm
= llvm
;
399 variant
->gallivm
= gallivm_create();
401 create_jit_types(variant
);
403 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
405 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
407 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
409 draw_llvm_generate(llvm
, variant
, FALSE
); /* linear */
410 draw_llvm_generate(llvm
, variant
, TRUE
); /* elts */
412 gallivm_compile_module(variant
->gallivm
);
414 variant
->jit_func
= (draw_jit_vert_func
)
415 gallivm_jit_function(variant
->gallivm
, variant
->function
);
417 variant
->jit_func_elts
= (draw_jit_vert_func_elts
)
418 gallivm_jit_function(variant
->gallivm
, variant
->function_elts
);
420 variant
->shader
= shader
;
421 variant
->list_item_global
.base
= variant
;
422 variant
->list_item_local
.base
= variant
;
423 /*variant->no = */shader
->variants_created
++;
424 variant
->list_item_global
.base
= variant
;
431 generate_vs(struct draw_llvm_variant
*variant
,
432 LLVMBuilderRef builder
,
433 struct lp_type vs_type
,
434 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
435 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
436 const struct lp_bld_tgsi_system_values
*system_values
,
437 LLVMValueRef context_ptr
,
438 struct lp_build_sampler_soa
*draw_sampler
,
439 boolean clamp_vertex_color
)
441 struct draw_llvm
*llvm
= variant
->llvm
;
442 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
443 LLVMValueRef consts_ptr
= draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
444 struct lp_build_sampler_soa
*sampler
= 0;
446 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
447 tgsi_dump(tokens
, 0);
450 if (llvm
->draw
->num_sampler_views
&& llvm
->draw
->num_samplers
)
451 sampler
= draw_sampler
;
453 lp_build_tgsi_soa(variant
->gallivm
,
456 NULL
/*struct lp_build_mask_context *mask*/,
463 &llvm
->draw
->vs
.vertex_shader
->info
);
467 unsigned chan
, attrib
;
468 struct lp_build_context bld
;
469 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
470 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
472 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
473 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
474 if (outputs
[attrib
][chan
]) {
475 switch (info
->output_semantic_name
[attrib
]) {
476 case TGSI_SEMANTIC_COLOR
:
477 case TGSI_SEMANTIC_BCOLOR
:
478 if (clamp_vertex_color
) {
479 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
480 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
481 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
484 case TGSI_SEMANTIC_FOG
:
485 if (chan
== 1 || chan
== 2)
486 LLVMBuildStore(builder
, bld
.zero
, outputs
[attrib
][chan
]);
488 LLVMBuildStore(builder
, bld
.one
, outputs
[attrib
][chan
]);
499 generate_fetch(struct gallivm_state
*gallivm
,
500 LLVMValueRef vbuffers_ptr
,
502 struct pipe_vertex_element
*velem
,
505 LLVMValueRef instance_id
)
507 const struct util_format_description
*format_desc
= util_format_description(velem
->src_format
);
508 LLVMValueRef zero
= LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
));
509 LLVMBuilderRef builder
= gallivm
->builder
;
510 LLVMValueRef indices
=
511 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
512 velem
->vertex_buffer_index
, 0);
513 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
515 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
516 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
519 if (velem
->instance_divisor
) {
520 /* array index = instance_id / instance_divisor */
521 index
= LLVMBuildUDiv(builder
, instance_id
,
522 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
526 stride
= LLVMBuildMul(builder
, vb_stride
, index
, "");
528 vbuffer_ptr
= LLVMBuildLoad(builder
, vbuffer_ptr
, "vbuffer");
530 stride
= LLVMBuildAdd(builder
, stride
,
533 stride
= LLVMBuildAdd(builder
, stride
,
534 lp_build_const_int32(gallivm
, velem
->src_offset
),
537 /* lp_build_printf(gallivm, "vbuf index = %d, stride is %d\n", indices, stride);*/
538 vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffer_ptr
, &stride
, 1, "");
540 *res
= lp_build_fetch_rgba_aos(gallivm
,
542 lp_float32_vec4_type(),
548 convert_to_soa(struct gallivm_state
*gallivm
,
549 LLVMValueRef (*src_aos
)[LP_MAX_VECTOR_WIDTH
/ 32],
550 LLVMValueRef (*dst_soa
)[TGSI_NUM_CHANNELS
],
551 unsigned num_attribs
, const struct lp_type soa_type
)
554 struct lp_type aos_channel_type
= soa_type
;
556 debug_assert(TGSI_NUM_CHANNELS
== 4);
557 debug_assert((soa_type
.length
% TGSI_NUM_CHANNELS
) == 0);
559 aos_channel_type
.length
>>= 1;
561 for (i
= 0; i
< num_attribs
; ++i
) {
562 LLVMValueRef aos_channels
[TGSI_NUM_CHANNELS
];
563 unsigned pixels_per_channel
= soa_type
.length
/ TGSI_NUM_CHANNELS
;
565 for (j
= 0; j
< TGSI_NUM_CHANNELS
; ++j
) {
566 LLVMValueRef channel
[LP_MAX_VECTOR_LENGTH
] = { 0 };
568 assert(pixels_per_channel
<= LP_MAX_VECTOR_LENGTH
);
570 for (k
= 0; k
< pixels_per_channel
; ++k
) {
571 channel
[k
] = src_aos
[i
][j
+ TGSI_NUM_CHANNELS
* k
];
574 aos_channels
[j
] = lp_build_concat(gallivm
, channel
, aos_channel_type
, pixels_per_channel
);
577 lp_build_transpose_aos(gallivm
, soa_type
, aos_channels
, dst_soa
[i
]);
583 store_aos(struct gallivm_state
*gallivm
,
588 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
589 LLVMBuilderRef builder
= gallivm
->builder
;
590 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
591 LLVMValueRef indices
[3];
593 indices
[0] = lp_build_const_int32(gallivm
, 0);
595 indices
[2] = lp_build_const_int32(gallivm
, 0);
598 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
601 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
602 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
604 /* Unaligned store due to the vertex header */
605 lp_set_store_alignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
610 store_aos_array(struct gallivm_state
*gallivm
,
611 struct lp_type soa_type
,
616 LLVMValueRef clipmask
,
617 boolean have_clipdist
)
619 LLVMBuilderRef builder
= gallivm
->builder
;
620 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
621 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
622 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
623 int vector_length
= soa_type
.length
;
626 debug_assert(TGSI_NUM_CHANNELS
== 4);
628 for (i
= 0; i
< vector_length
; i
++) {
629 inds
[i
] = lp_build_const_int32(gallivm
, i
);
630 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
634 /* store vertex header for each of the n vertices */
635 LLVMValueRef val
, cliptmp
;
636 int vertex_id_pad_edgeflag
;
638 /* If this assertion fails, it means we need to update the bit twidding
639 * code here. See struct vertex_header in draw_private.h.
641 assert(DRAW_TOTAL_CLIP_PLANES
==14);
642 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
643 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
645 vertex_id_pad_edgeflag
|= 1 << (DRAW_TOTAL_CLIP_PLANES
+1);
646 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
), vertex_id_pad_edgeflag
);
647 /* OR with the clipmask */
648 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
649 for (i
= 0; i
< vector_length
; i
++) {
650 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
651 val
= LLVMBuildExtractElement(builder
, cliptmp
, inds
[i
], "");
652 LLVMBuildStore(builder
, val
, id_ptr
);
654 lp_build_printf(gallivm
, "io = %p, index %d\n, clipmask = %x\n",
655 io_ptrs
[i
], inds
[i
], val
);
660 /* store for each of the n vertices */
661 for (i
= 0; i
< vector_length
; i
++) {
662 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
668 convert_to_aos(struct gallivm_state
*gallivm
,
670 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
671 LLVMValueRef clipmask
,
673 struct lp_type soa_type
,
674 boolean have_clipdist
)
676 LLVMBuilderRef builder
= gallivm
->builder
;
677 unsigned chan
, attrib
, i
;
680 lp_build_printf(gallivm
, " # storing begin\n");
682 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
683 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
684 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
685 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
686 if (outputs
[attrib
][chan
]) {
687 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
688 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
690 lp_build_printf(gallivm
, "output %d : %d ",
691 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
693 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
695 lp_build_print_value(gallivm
, "val = ", out
);
705 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
706 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
708 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
710 for (i
= 0; i
< soa_type
.length
; ++i
) {
711 aos
[i
] = lp_build_extract_range(gallivm
,
712 soa
[i
% TGSI_NUM_CHANNELS
],
713 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
718 store_aos_array(gallivm
,
724 clipmask
, have_clipdist
);
727 lp_build_printf(gallivm
, " # storing end\n");
733 * Stores original vertex positions in clip coordinates
736 store_clip(struct gallivm_state
*gallivm
,
737 const struct lp_type vs_type
,
739 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
740 boolean pre_clip_pos
, int idx
)
742 LLVMBuilderRef builder
= gallivm
->builder
;
744 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
745 LLVMValueRef indices
[2];
746 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
747 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
748 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
752 indices
[1] = lp_build_const_int32(gallivm
, 0);
754 for (i
= 0; i
< vs_type
.length
; i
++) {
755 inds
[i
] = lp_build_const_int32(gallivm
, i
);
756 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
759 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
760 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
761 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
762 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
765 for (i
= 0; i
< vs_type
.length
; i
++) {
766 clip_ptrs
[i
] = draw_jit_header_clip(gallivm
, io_ptrs
[i
]);
769 for (i
= 0; i
< vs_type
.length
; i
++) {
770 clip_ptrs
[i
] = draw_jit_header_pre_clip_pos(gallivm
, io_ptrs
[i
]);
774 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
775 for (i
= 0; i
< vs_type
.length
; ++i
) {
776 aos
[i
] = lp_build_extract_range(gallivm
,
777 soa
[i
% TGSI_NUM_CHANNELS
],
778 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
782 for (j
= 0; j
< vs_type
.length
; j
++) {
783 LLVMTypeRef clip_ptr_type
= LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4), 0);
784 LLVMValueRef clip_ptr
;
786 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
787 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
789 /* Unaligned store */
790 lp_set_store_alignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
796 * Transforms the outputs for viewport mapping
799 generate_viewport(struct draw_llvm_variant
*variant
,
800 LLVMBuilderRef builder
,
801 struct lp_type vs_type
,
802 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
803 LLVMValueRef context_ptr
)
806 struct gallivm_state
*gallivm
= variant
->gallivm
;
807 struct lp_type f32_type
= vs_type
;
808 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
809 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[0][3], ""); /*w0 w1 .. wn*/
810 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
811 LLVMValueRef vp_ptr
= draw_jit_context_viewport(gallivm
, context_ptr
);
813 /* for 1/w convention*/
814 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
815 LLVMBuildStore(builder
, out3
, outputs
[0][3]);
817 /* Viewport Mapping */
818 for (i
=0; i
<3; i
++) {
819 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[0][i
], ""); /*x0 x1 .. xn*/
822 LLVMValueRef scale_i
;
823 LLVMValueRef trans_i
;
826 index
= lp_build_const_int32(gallivm
, i
);
827 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
829 index
= lp_build_const_int32(gallivm
, i
+4);
830 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
832 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
833 LLVMBuildLoad(builder
, scale_i
, "scale"));
834 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
835 LLVMBuildLoad(builder
, trans_i
, "trans"));
838 out
= LLVMBuildFMul(builder
, out
, out3
, "");
840 out
= LLVMBuildFMul(builder
, out
, scale
, "");
841 /* add translation */
842 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
844 /* store transformed outputs */
845 LLVMBuildStore(builder
, out
, outputs
[0][i
]);
852 * Returns clipmask as nxi32 bitmask for the n vertices
855 generate_clipmask(struct draw_llvm
*llvm
,
856 struct gallivm_state
*gallivm
,
857 struct lp_type vs_type
,
858 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
864 LLVMValueRef context_ptr
,
865 boolean
*have_clipdist
)
867 LLVMBuilderRef builder
= gallivm
->builder
;
868 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
869 LLVMValueRef test
, temp
;
870 LLVMValueRef zero
, shift
;
871 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
872 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
873 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
874 struct lp_type f32_type
= vs_type
;
875 struct lp_type i32_type
= lp_int_type(vs_type
);
876 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
877 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
878 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
879 bool have_cd
= false;
882 cd
[0] = draw_current_shader_clipdistance_output(llvm
->draw
, 0);
883 cd
[1] = draw_current_shader_clipdistance_output(llvm
->draw
, 1);
885 if (cd
[0] != pos
|| cd
[1] != pos
)
888 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
889 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
890 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
891 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
894 * load clipvertex and position from correct locations.
895 * if they are the same just load them once.
897 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
898 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
899 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
900 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
902 if (clip_user
&& cv
!= pos
) {
903 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
904 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
905 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
906 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
914 /* Cliptest, for hardwired planes */
917 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
919 test
= LLVMBuildAnd(builder
, test
, temp
, "");
923 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
924 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
925 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
926 test
= LLVMBuildAnd(builder
, test
, temp
, "");
927 mask
= LLVMBuildOr(builder
, mask
, test
, "");
930 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
931 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
932 test
= LLVMBuildAnd(builder
, test
, temp
, "");
933 mask
= LLVMBuildOr(builder
, mask
, test
, "");
936 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
937 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
938 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
939 test
= LLVMBuildAnd(builder
, test
, temp
, "");
940 mask
= LLVMBuildOr(builder
, mask
, test
, "");
944 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
947 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
948 test
= LLVMBuildAnd(builder
, test
, temp
, "");
949 mask
= LLVMBuildOr(builder
, mask
, test
, "");
953 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
954 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
955 test
= LLVMBuildAnd(builder
, test
, temp
, "");
956 mask
= LLVMBuildOr(builder
, mask
, test
, "");
959 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
960 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
961 test
= LLVMBuildAnd(builder
, test
, temp
, "");
962 mask
= LLVMBuildOr(builder
, mask
, test
, "");
966 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
967 LLVMValueRef indices
[3];
969 /* userclip planes */
971 unsigned plane_idx
= ffs(ucp_enable
)-1;
972 ucp_enable
&= ~(1 << plane_idx
);
975 if (have_cd
&& num_written_clipdistance
) {
976 LLVMValueRef clipdist
;
980 *have_clipdist
= TRUE
;
982 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
984 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
986 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
987 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
988 test
= LLVMBuildAnd(builder
, test
, temp
, "");
989 mask
= LLVMBuildOr(builder
, mask
, test
, "");
991 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
992 indices
[0] = lp_build_const_int32(gallivm
, 0);
993 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
995 indices
[2] = lp_build_const_int32(gallivm
, 0);
996 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
997 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
998 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
999 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1001 indices
[2] = lp_build_const_int32(gallivm
, 1);
1002 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1003 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1004 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1005 test
= LLVMBuildFMul(builder
, planes
, cv_y
, "");
1006 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1008 indices
[2] = lp_build_const_int32(gallivm
, 2);
1009 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1010 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1011 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1012 test
= LLVMBuildFMul(builder
, planes
, cv_z
, "");
1013 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1015 indices
[2] = lp_build_const_int32(gallivm
, 3);
1016 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1017 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1018 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1019 test
= LLVMBuildFMul(builder
, planes
, cv_w
, "");
1020 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1022 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1023 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1024 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1025 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1034 * Returns boolean if any clipping has occurred
1035 * Used zero/non-zero i32 value to represent boolean
1038 clipmask_booli32(struct gallivm_state
*gallivm
,
1039 const struct lp_type vs_type
,
1040 LLVMValueRef clipmask_bool_ptr
)
1042 LLVMBuilderRef builder
= gallivm
->builder
;
1043 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
1044 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1045 LLVMValueRef ret
= LLVMConstNull(int32_type
);
1050 * Can do this with log2(vector length) pack instructions and one extract
1051 * (as we don't actually need a or) with sse2 which would be way better.
1053 for (i
=0; i
< vs_type
.length
; i
++) {
1054 temp
= LLVMBuildExtractElement(builder
, clipmask_bool
,
1055 lp_build_const_int32(gallivm
, i
) , "");
1056 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1063 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1066 struct gallivm_state
*gallivm
= variant
->gallivm
;
1067 LLVMContextRef context
= gallivm
->context
;
1068 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1069 LLVMTypeRef arg_types
[8];
1070 LLVMTypeRef func_type
;
1071 LLVMValueRef context_ptr
;
1072 LLVMBasicBlockRef block
;
1073 LLVMBuilderRef builder
;
1074 struct lp_type vs_type
;
1075 LLVMValueRef end
, start
;
1076 LLVMValueRef count
, fetch_elts
, fetch_count
;
1077 LLVMValueRef stride
, step
, io_itr
;
1078 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1079 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1080 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1081 struct draw_context
*draw
= llvm
->draw
;
1082 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1084 struct lp_build_context bld
;
1085 struct lp_build_loop_state lp_loop
;
1086 const int vector_length
= lp_native_vector_width
/ 32;
1087 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1088 LLVMValueRef fetch_max
;
1089 struct lp_build_sampler_soa
*sampler
= 0;
1090 LLVMValueRef ret
, clipmask_bool_ptr
;
1091 const boolean bypass_viewport
= variant
->key
.bypass_viewport
;
1092 const boolean enable_cliptest
= variant
->key
.clip_xy
||
1093 variant
->key
.clip_z
||
1094 variant
->key
.clip_user
;
1095 LLVMValueRef variant_func
;
1096 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1097 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1098 boolean have_clipdist
= FALSE
;
1099 struct lp_bld_tgsi_system_values system_values
;
1101 memset(&system_values
, 0, sizeof(system_values
));
1103 arg_types
[0] = get_context_ptr_type(variant
); /* context */
1104 arg_types
[1] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1105 arg_types
[2] = get_buffer_ptr_type(variant
); /* vbuffers */
1107 arg_types
[3] = LLVMPointerType(int32_type
, 0);/* fetch_elts * */
1109 arg_types
[3] = int32_type
; /* start */
1110 arg_types
[4] = int32_type
; /* fetch_count / count */
1111 arg_types
[5] = int32_type
; /* stride */
1112 arg_types
[6] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1113 arg_types
[7] = int32_type
; /* instance_id */
1115 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
1117 variant_func
= LLVMAddFunction(gallivm
->module
,
1118 elts
? "draw_llvm_shader_elts" : "draw_llvm_shader",
1122 variant
->function_elts
= variant_func
;
1124 variant
->function
= variant_func
;
1126 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1127 for (i
= 0; i
< Elements(arg_types
); ++i
)
1128 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1129 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1130 LLVMNoAliasAttribute
);
1132 context_ptr
= LLVMGetParam(variant_func
, 0);
1133 io_ptr
= LLVMGetParam(variant_func
, 1);
1134 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1135 stride
= LLVMGetParam(variant_func
, 5);
1136 vb_ptr
= LLVMGetParam(variant_func
, 6);
1137 system_values
.instance_id
= LLVMGetParam(variant_func
, 7);
1139 lp_build_name(context_ptr
, "context");
1140 lp_build_name(io_ptr
, "io");
1141 lp_build_name(vbuffers_ptr
, "vbuffers");
1142 lp_build_name(stride
, "stride");
1143 lp_build_name(vb_ptr
, "vb");
1144 lp_build_name(system_values
.instance_id
, "instance_id");
1147 fetch_elts
= LLVMGetParam(variant_func
, 3);
1148 fetch_count
= LLVMGetParam(variant_func
, 4);
1149 lp_build_name(fetch_elts
, "fetch_elts");
1150 lp_build_name(fetch_count
, "fetch_count");
1151 start
= count
= NULL
;
1154 start
= LLVMGetParam(variant_func
, 3);
1155 count
= LLVMGetParam(variant_func
, 4);
1156 lp_build_name(start
, "start");
1157 lp_build_name(count
, "count");
1158 fetch_elts
= fetch_count
= NULL
;
1165 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1166 builder
= gallivm
->builder
;
1167 LLVMPositionBuilderAtEnd(builder
, block
);
1169 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1171 memset(&vs_type
, 0, sizeof vs_type
);
1172 vs_type
.floating
= TRUE
; /* floating point values */
1173 vs_type
.sign
= TRUE
; /* values are signed */
1174 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1175 vs_type
.width
= 32; /* 32-bit float */
1176 vs_type
.length
= vector_length
;
1178 /* hold temporary "bool" clipmask */
1179 clipmask_bool_ptr
= lp_build_alloca(gallivm
, lp_build_int_vec_type(gallivm
, vs_type
), "");
1180 LLVMBuildStore(builder
, lp_build_zero(gallivm
, lp_int_type(vs_type
)), clipmask_bool_ptr
);
1182 /* code generated texture sampling */
1183 sampler
= draw_llvm_sampler_soa_create(
1184 draw_llvm_variant_key_samplers(&variant
->key
),
1192 end
= lp_build_add(&bld
, start
, count
);
1195 step
= lp_build_const_int32(gallivm
, vector_length
);
1197 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1199 lp_build_loop_begin(&lp_loop
, gallivm
, start
);
1201 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1202 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][LP_MAX_VECTOR_WIDTH
/ 32] = { { 0 } };
1204 LLVMValueRef clipmask
; /* holds the clipmask value */
1205 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1208 io_itr
= lp_loop
.counter
;
1210 io_itr
= LLVMBuildSub(builder
, lp_loop
.counter
, start
, "");
1212 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1214 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1215 io_itr
, io
, lp_loop
.counter
);
1217 system_values
.vertex_id
= lp_build_zero(gallivm
, lp_type_uint_vec(32, 32*vector_length
));
1218 for (i
= 0; i
< vector_length
; ++i
) {
1219 LLVMValueRef true_index
=
1220 LLVMBuildAdd(builder
,
1222 lp_build_const_int32(gallivm
, i
), "");
1224 /* make sure we're not out of bounds which can happen
1225 * if fetch_count % 4 != 0, because on the last iteration
1226 * a few of the 4 vertex fetches will be out of bounds */
1227 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1230 LLVMValueRef fetch_ptr
;
1231 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1232 &true_index
, 1, "");
1233 true_index
= LLVMBuildLoad(builder
, fetch_ptr
, "fetch_elt");
1236 system_values
.vertex_id
= LLVMBuildInsertElement(gallivm
->builder
,
1237 system_values
.vertex_id
, true_index
,
1238 lp_build_const_int32(gallivm
, i
), "");
1239 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1240 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1241 LLVMValueRef vb_index
=
1242 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1243 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1244 generate_fetch(gallivm
, vbuffers_ptr
,
1245 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1246 system_values
.instance_id
);
1249 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1250 draw
->pt
.nr_vertex_elements
, vs_type
);
1252 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1253 generate_vs(variant
,
1261 variant
->key
.clamp_vertex_color
);
1263 /* store original positions in clip before further manipulation */
1264 store_clip(gallivm
, vs_type
, io
, outputs
, 0, cv
);
1265 store_clip(gallivm
, vs_type
, io
, outputs
, 1, pos
);
1268 if (enable_cliptest
) {
1269 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1270 /* allocate clipmask, assign it integer type */
1271 clipmask
= generate_clipmask(llvm
,
1275 variant
->key
.clip_xy
,
1276 variant
->key
.clip_z
,
1277 variant
->key
.clip_user
,
1278 variant
->key
.clip_halfz
,
1279 variant
->key
.ucp_enable
,
1280 context_ptr
, &have_clipdist
);
1281 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1282 /* store temporary clipping boolean value */
1283 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1286 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1289 /* do viewport mapping */
1290 if (!bypass_viewport
) {
1291 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1294 /* store clipmask in vertex header,
1295 * original positions in clip
1296 * and transformed positions in data
1298 convert_to_aos(gallivm
, io
, outputs
, clipmask
,
1299 vs_info
->num_outputs
, vs_type
,
1303 lp_build_loop_end_cond(&lp_loop
, end
, step
, LLVMIntUGE
);
1305 sampler
->destroy(sampler
);
1307 /* return clipping boolean value for function */
1308 ret
= clipmask_booli32(gallivm
, vs_type
, clipmask_bool_ptr
);
1310 LLVMBuildRet(builder
, ret
);
1312 gallivm_verify_function(gallivm
, variant_func
);
1316 struct draw_llvm_variant_key
*
1317 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1320 struct draw_llvm_variant_key
*key
;
1321 struct lp_sampler_static_state
*sampler
;
1323 key
= (struct draw_llvm_variant_key
*)store
;
1325 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1327 /* Presumably all variants of the shader should have the same
1328 * number of vertex elements - ie the number of shader inputs.
1330 key
->nr_vertex_elements
= llvm
->draw
->pt
.nr_vertex_elements
;
1332 /* will have to rig this up properly later */
1333 key
->clip_xy
= llvm
->draw
->clip_xy
;
1334 key
->clip_z
= llvm
->draw
->clip_z
;
1335 key
->clip_user
= llvm
->draw
->clip_user
;
1336 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1337 key
->clip_halfz
= !llvm
->draw
->rasterizer
->gl_rasterization_rules
;
1338 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1339 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1342 /* All variants of this shader will have the same value for
1343 * nr_samplers. Not yet trying to compact away holes in the
1346 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1348 sampler
= draw_llvm_variant_key_samplers(key
);
1350 memcpy(key
->vertex_element
,
1351 llvm
->draw
->pt
.vertex_element
,
1352 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1354 memset(sampler
, 0, key
->nr_samplers
* sizeof *sampler
);
1356 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1357 lp_sampler_static_state(&sampler
[i
],
1358 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
],
1359 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
1367 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1368 unsigned sampler_idx
,
1369 uint32_t width
, uint32_t height
, uint32_t depth
,
1370 uint32_t first_level
, uint32_t last_level
,
1371 const void *base_ptr
,
1372 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1373 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1374 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
1377 struct draw_jit_texture
*jit_tex
;
1379 assert(sampler_idx
< Elements(draw
->llvm
->jit_context
.textures
));
1381 jit_tex
= &draw
->llvm
->jit_context
.textures
[sampler_idx
];
1383 jit_tex
->width
= width
;
1384 jit_tex
->height
= height
;
1385 jit_tex
->depth
= depth
;
1386 jit_tex
->first_level
= first_level
;
1387 jit_tex
->last_level
= last_level
;
1388 jit_tex
->base
= base_ptr
;
1390 for (j
= first_level
; j
<= last_level
; j
++) {
1391 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
1392 jit_tex
->row_stride
[j
] = row_stride
[j
];
1393 jit_tex
->img_stride
[j
] = img_stride
[j
];
1399 draw_llvm_set_sampler_state(struct draw_context
*draw
)
1403 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
1404 struct draw_jit_texture
*jit_tex
= &draw
->llvm
->jit_context
.textures
[i
];
1406 if (draw
->samplers
[i
]) {
1407 const struct pipe_sampler_state
*s
1408 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
1409 jit_tex
->min_lod
= s
->min_lod
;
1410 jit_tex
->max_lod
= s
->max_lod
;
1411 jit_tex
->lod_bias
= s
->lod_bias
;
1412 COPY_4V(jit_tex
->border_color
, s
->border_color
.f
);
1419 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
1421 struct draw_llvm
*llvm
= variant
->llvm
;
1423 if (variant
->function_elts
) {
1424 gallivm_free_function(variant
->gallivm
,
1425 variant
->function_elts
, variant
->jit_func_elts
);
1428 if (variant
->function
) {
1429 gallivm_free_function(variant
->gallivm
,
1430 variant
->function
, variant
->jit_func
);
1433 gallivm_destroy(variant
->gallivm
);
1435 remove_from_list(&variant
->list_item_local
);
1436 variant
->shader
->variants_cached
--;
1437 remove_from_list(&variant
->list_item_global
);
1438 llvm
->nr_variants
--;