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_ROW_STRIDE
] =
83 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
84 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
85 elem_types
[DRAW_JIT_TEXTURE_DATA
] =
86 LLVMArrayType(LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0),
87 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
, row_stride
,
122 target
, texture_type
,
123 DRAW_JIT_TEXTURE_ROW_STRIDE
);
124 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
125 target
, texture_type
,
126 DRAW_JIT_TEXTURE_IMG_STRIDE
);
127 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, data
,
128 target
, texture_type
,
129 DRAW_JIT_TEXTURE_DATA
);
130 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, min_lod
,
131 target
, texture_type
,
132 DRAW_JIT_TEXTURE_MIN_LOD
);
133 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, max_lod
,
134 target
, texture_type
,
135 DRAW_JIT_TEXTURE_MAX_LOD
);
136 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, lod_bias
,
137 target
, texture_type
,
138 DRAW_JIT_TEXTURE_LOD_BIAS
);
139 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, border_color
,
140 target
, texture_type
,
141 DRAW_JIT_TEXTURE_BORDER_COLOR
);
143 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
150 * Create LLVM type for struct draw_jit_texture
153 create_jit_context_type(struct gallivm_state
*gallivm
,
154 LLVMTypeRef texture_type
, const char *struct_name
)
156 LLVMTargetDataRef target
= gallivm
->target
;
157 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
158 LLVMTypeRef elem_types
[5];
159 LLVMTypeRef context_type
;
161 elem_types
[0] = LLVMPointerType(float_type
, 0); /* vs_constants */
162 elem_types
[1] = LLVMPointerType(float_type
, 0); /* gs_constants */
163 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
164 DRAW_TOTAL_CLIP_PLANES
), 0);
165 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewport */
166 elem_types
[4] = LLVMArrayType(texture_type
,
167 PIPE_MAX_VERTEX_SAMPLERS
); /* textures */
168 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
169 Elements(elem_types
), 0);
170 #if HAVE_LLVM < 0x0300
171 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
173 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
176 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
177 target
, context_type
, 0);
178 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, gs_constants
,
179 target
, context_type
, 1);
180 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
181 target
, context_type
, 2);
182 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
183 target
, context_type
,
184 DRAW_JIT_CTX_TEXTURES
);
185 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
186 target
, context_type
);
193 * Create LLVM type for struct pipe_vertex_buffer
196 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
, const char *struct_name
)
198 LLVMTargetDataRef target
= gallivm
->target
;
199 LLVMTypeRef elem_types
[4];
203 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
205 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0); /* vs_constants */
207 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
208 Elements(elem_types
), 0);
209 #if HAVE_LLVM < 0x0300
210 LLVMAddTypeName(gallivm
->module
, struct_name
, vb_type
);
212 LLVMInvalidateStructLayout(gallivm
->target
, vb_type
);
215 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
217 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
220 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
227 * Create LLVM type for struct vertex_header;
230 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
232 LLVMTargetDataRef target
= gallivm
->target
;
233 LLVMTypeRef elem_types
[4];
234 LLVMTypeRef vertex_header
;
235 char struct_name
[24];
237 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
239 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
240 elem_types
[DRAW_JIT_VERTEX_CLIP
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
241 elem_types
[DRAW_JIT_VERTEX_PRE_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
242 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
244 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
245 Elements(elem_types
), 0);
246 #if HAVE_LLVM < 0x0300
247 LLVMAddTypeName(gallivm
->module
, struct_name
, vertex_header
);
249 LLVMInvalidateStructLayout(gallivm
->target
, vertex_header
);
252 /* these are bit-fields and we can't take address of them
253 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
254 target, vertex_header,
255 DRAW_JIT_VERTEX_CLIPMASK);
256 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
257 target, vertex_header,
258 DRAW_JIT_VERTEX_EDGEFLAG);
259 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
260 target, vertex_header,
261 DRAW_JIT_VERTEX_PAD);
262 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
263 target, vertex_header,
264 DRAW_JIT_VERTEX_VERTEX_ID);
266 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
267 target
, vertex_header
,
268 DRAW_JIT_VERTEX_CLIP
);
269 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, pre_clip_pos
,
270 target
, vertex_header
,
271 DRAW_JIT_VERTEX_PRE_CLIP_POS
);
272 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
273 target
, vertex_header
,
274 DRAW_JIT_VERTEX_DATA
);
276 assert(LLVMABISizeOfType(target
, vertex_header
) ==
277 offsetof(struct vertex_header
, data
[data_elems
]));
279 return vertex_header
;
284 * Create LLVM types for various structures.
287 create_jit_types(struct draw_llvm_variant
*variant
)
289 struct gallivm_state
*gallivm
= variant
->gallivm
;
290 LLVMTypeRef texture_type
, context_type
, buffer_type
, vb_type
;
292 texture_type
= create_jit_texture_type(gallivm
, "texture");
294 context_type
= create_jit_context_type(gallivm
, texture_type
, "draw_jit_context");
295 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
297 buffer_type
= LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
298 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
300 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
301 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
306 get_context_ptr_type(struct draw_llvm_variant
*variant
)
308 if (!variant
->context_ptr_type
)
309 create_jit_types(variant
);
310 return variant
->context_ptr_type
;
315 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
317 if (!variant
->buffer_ptr_type
)
318 create_jit_types(variant
);
319 return variant
->buffer_ptr_type
;
324 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
326 if (!variant
->vb_ptr_type
)
327 create_jit_types(variant
);
328 return variant
->vb_ptr_type
;
332 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
334 if (!variant
->vertex_header_ptr_type
)
335 create_jit_types(variant
);
336 return variant
->vertex_header_ptr_type
;
341 * Create per-context LLVM info.
344 draw_llvm_create(struct draw_context
*draw
)
346 struct draw_llvm
*llvm
;
348 llvm
= CALLOC_STRUCT( draw_llvm
);
356 llvm
->nr_variants
= 0;
357 make_empty_list(&llvm
->vs_variants_list
);
364 * Free per-context LLVM info.
367 draw_llvm_destroy(struct draw_llvm
*llvm
)
369 /* XXX free other draw_llvm data? */
375 * Create LLVM-generated code for a vertex shader.
377 struct draw_llvm_variant
*
378 draw_llvm_create_variant(struct draw_llvm
*llvm
,
380 const struct draw_llvm_variant_key
*key
)
382 struct draw_llvm_variant
*variant
;
383 struct llvm_vertex_shader
*shader
=
384 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
385 LLVMTypeRef vertex_header
;
387 variant
= MALLOC(sizeof *variant
+
388 shader
->variant_key_size
-
389 sizeof variant
->key
);
393 variant
->llvm
= llvm
;
395 variant
->gallivm
= gallivm_create();
397 create_jit_types(variant
);
399 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
401 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
403 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
405 draw_llvm_generate(llvm
, variant
, FALSE
); /* linear */
406 draw_llvm_generate(llvm
, variant
, TRUE
); /* elts */
408 gallivm_compile_module(variant
->gallivm
);
410 variant
->jit_func
= (draw_jit_vert_func
)
411 gallivm_jit_function(variant
->gallivm
, variant
->function
);
413 variant
->jit_func_elts
= (draw_jit_vert_func_elts
)
414 gallivm_jit_function(variant
->gallivm
, variant
->function_elts
);
416 variant
->shader
= shader
;
417 variant
->list_item_global
.base
= variant
;
418 variant
->list_item_local
.base
= variant
;
419 /*variant->no = */shader
->variants_created
++;
420 variant
->list_item_global
.base
= variant
;
427 generate_vs(struct draw_llvm_variant
*variant
,
428 LLVMBuilderRef builder
,
429 struct lp_type vs_type
,
430 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
431 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
432 const struct lp_bld_tgsi_system_values
*system_values
,
433 LLVMValueRef context_ptr
,
434 struct lp_build_sampler_soa
*draw_sampler
,
435 boolean clamp_vertex_color
)
437 struct draw_llvm
*llvm
= variant
->llvm
;
438 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
439 LLVMValueRef consts_ptr
= draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
440 struct lp_build_sampler_soa
*sampler
= 0;
442 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
443 tgsi_dump(tokens
, 0);
446 if (llvm
->draw
->num_sampler_views
&& llvm
->draw
->num_samplers
)
447 sampler
= draw_sampler
;
449 lp_build_tgsi_soa(variant
->gallivm
,
452 NULL
/*struct lp_build_mask_context *mask*/,
459 &llvm
->draw
->vs
.vertex_shader
->info
);
463 unsigned chan
, attrib
;
464 struct lp_build_context bld
;
465 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
466 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
468 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
469 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
470 if (outputs
[attrib
][chan
]) {
471 switch (info
->output_semantic_name
[attrib
]) {
472 case TGSI_SEMANTIC_COLOR
:
473 case TGSI_SEMANTIC_BCOLOR
:
474 if (clamp_vertex_color
) {
475 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
476 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
477 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
480 case TGSI_SEMANTIC_FOG
:
481 if (chan
== 1 || chan
== 2)
482 LLVMBuildStore(builder
, bld
.zero
, outputs
[attrib
][chan
]);
484 LLVMBuildStore(builder
, bld
.one
, outputs
[attrib
][chan
]);
495 generate_fetch(struct gallivm_state
*gallivm
,
496 LLVMValueRef vbuffers_ptr
,
498 struct pipe_vertex_element
*velem
,
501 LLVMValueRef instance_id
)
503 const struct util_format_description
*format_desc
= util_format_description(velem
->src_format
);
504 LLVMValueRef zero
= LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
));
505 LLVMBuilderRef builder
= gallivm
->builder
;
506 LLVMValueRef indices
=
507 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
508 velem
->vertex_buffer_index
, 0);
509 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
511 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
512 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
515 if (velem
->instance_divisor
) {
516 /* array index = instance_id / instance_divisor */
517 index
= LLVMBuildUDiv(builder
, instance_id
,
518 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
522 stride
= LLVMBuildMul(builder
, vb_stride
, index
, "");
524 vbuffer_ptr
= LLVMBuildLoad(builder
, vbuffer_ptr
, "vbuffer");
526 stride
= LLVMBuildAdd(builder
, stride
,
529 stride
= LLVMBuildAdd(builder
, stride
,
530 lp_build_const_int32(gallivm
, velem
->src_offset
),
533 /* lp_build_printf(gallivm, "vbuf index = %d, stride is %d\n", indices, stride);*/
534 vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffer_ptr
, &stride
, 1, "");
536 *res
= lp_build_fetch_rgba_aos(gallivm
,
538 lp_float32_vec4_type(),
544 convert_to_soa(struct gallivm_state
*gallivm
,
545 LLVMValueRef (*src_aos
)[LP_MAX_VECTOR_WIDTH
/ 32],
546 LLVMValueRef (*dst_soa
)[TGSI_NUM_CHANNELS
],
547 unsigned num_attribs
, const struct lp_type soa_type
)
550 struct lp_type aos_channel_type
= soa_type
;
552 debug_assert(TGSI_NUM_CHANNELS
== 4);
553 debug_assert((soa_type
.length
% TGSI_NUM_CHANNELS
) == 0);
555 aos_channel_type
.length
>>= 1;
557 for (i
= 0; i
< num_attribs
; ++i
) {
558 LLVMValueRef aos_channels
[TGSI_NUM_CHANNELS
];
559 unsigned pixels_per_channel
= soa_type
.length
/ TGSI_NUM_CHANNELS
;
561 for (j
= 0; j
< TGSI_NUM_CHANNELS
; ++j
) {
562 LLVMValueRef channel
[LP_MAX_VECTOR_LENGTH
] = { 0 };
564 assert(pixels_per_channel
<= LP_MAX_VECTOR_LENGTH
);
566 for (k
= 0; k
< pixels_per_channel
; ++k
) {
567 channel
[k
] = src_aos
[i
][j
+ TGSI_NUM_CHANNELS
* k
];
570 aos_channels
[j
] = lp_build_concat(gallivm
, channel
, aos_channel_type
, pixels_per_channel
);
573 lp_build_transpose_aos(gallivm
, soa_type
, aos_channels
, dst_soa
[i
]);
579 store_aos(struct gallivm_state
*gallivm
,
584 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
585 LLVMBuilderRef builder
= gallivm
->builder
;
586 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
587 LLVMValueRef indices
[3];
589 indices
[0] = lp_build_const_int32(gallivm
, 0);
591 indices
[2] = lp_build_const_int32(gallivm
, 0);
594 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
597 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
598 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
600 /* Unaligned store due to the vertex header */
601 lp_set_store_alignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
606 store_aos_array(struct gallivm_state
*gallivm
,
607 struct lp_type soa_type
,
612 LLVMValueRef clipmask
,
613 boolean have_clipdist
)
615 LLVMBuilderRef builder
= gallivm
->builder
;
616 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
617 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
618 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
619 int vector_length
= soa_type
.length
;
622 debug_assert(TGSI_NUM_CHANNELS
== 4);
624 for (i
= 0; i
< vector_length
; i
++) {
625 inds
[i
] = lp_build_const_int32(gallivm
, i
);
626 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
630 /* store vertex header for each of the n vertices */
631 LLVMValueRef val
, cliptmp
;
632 int vertex_id_pad_edgeflag
;
634 /* If this assertion fails, it means we need to update the bit twidding
635 * code here. See struct vertex_header in draw_private.h.
637 assert(DRAW_TOTAL_CLIP_PLANES
==14);
638 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
639 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
641 vertex_id_pad_edgeflag
|= 1 << (DRAW_TOTAL_CLIP_PLANES
+1);
642 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
), vertex_id_pad_edgeflag
);
643 /* OR with the clipmask */
644 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
645 for (i
= 0; i
< vector_length
; i
++) {
646 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
647 val
= LLVMBuildExtractElement(builder
, cliptmp
, inds
[i
], "");
648 LLVMBuildStore(builder
, val
, id_ptr
);
650 lp_build_printf(gallivm
, "io = %p, index %d\n, clipmask = %x\n",
651 io_ptrs
[i
], inds
[i
], val
);
656 /* store for each of the n vertices */
657 for (i
= 0; i
< vector_length
; i
++) {
658 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
664 convert_to_aos(struct gallivm_state
*gallivm
,
666 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
667 LLVMValueRef clipmask
,
669 struct lp_type soa_type
,
670 boolean have_clipdist
)
672 LLVMBuilderRef builder
= gallivm
->builder
;
673 unsigned chan
, attrib
, i
;
676 lp_build_printf(gallivm
, " # storing begin\n");
678 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
679 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
680 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
681 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
682 if (outputs
[attrib
][chan
]) {
683 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
684 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
686 lp_build_printf(gallivm
, "output %d : %d ",
687 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
689 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
691 lp_build_print_value(gallivm
, "val = ", out
);
701 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
702 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
704 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
706 for (i
= 0; i
< soa_type
.length
; ++i
) {
707 aos
[i
] = lp_build_extract_range(gallivm
,
708 soa
[i
% TGSI_NUM_CHANNELS
],
709 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
714 store_aos_array(gallivm
,
720 clipmask
, have_clipdist
);
723 lp_build_printf(gallivm
, " # storing end\n");
729 * Stores original vertex positions in clip coordinates
732 store_clip(struct gallivm_state
*gallivm
,
733 const struct lp_type vs_type
,
735 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
736 boolean pre_clip_pos
, int idx
)
738 LLVMBuilderRef builder
= gallivm
->builder
;
740 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
741 LLVMValueRef indices
[2];
742 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
743 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
744 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
748 indices
[1] = lp_build_const_int32(gallivm
, 0);
750 for (i
= 0; i
< vs_type
.length
; i
++) {
751 inds
[i
] = lp_build_const_int32(gallivm
, i
);
752 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
755 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
756 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
757 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
758 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
761 for (i
= 0; i
< vs_type
.length
; i
++) {
762 clip_ptrs
[i
] = draw_jit_header_clip(gallivm
, io_ptrs
[i
]);
765 for (i
= 0; i
< vs_type
.length
; i
++) {
766 clip_ptrs
[i
] = draw_jit_header_pre_clip_pos(gallivm
, io_ptrs
[i
]);
770 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
771 for (i
= 0; i
< vs_type
.length
; ++i
) {
772 aos
[i
] = lp_build_extract_range(gallivm
,
773 soa
[i
% TGSI_NUM_CHANNELS
],
774 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
778 for (j
= 0; j
< vs_type
.length
; j
++) {
779 LLVMTypeRef clip_ptr_type
= LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4), 0);
780 LLVMValueRef clip_ptr
;
782 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
783 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
785 /* Unaligned store */
786 lp_set_store_alignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
792 * Transforms the outputs for viewport mapping
795 generate_viewport(struct draw_llvm_variant
*variant
,
796 LLVMBuilderRef builder
,
797 struct lp_type vs_type
,
798 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
799 LLVMValueRef context_ptr
)
802 struct gallivm_state
*gallivm
= variant
->gallivm
;
803 struct lp_type f32_type
= vs_type
;
804 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
805 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[0][3], ""); /*w0 w1 .. wn*/
806 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
807 LLVMValueRef vp_ptr
= draw_jit_context_viewport(gallivm
, context_ptr
);
809 /* for 1/w convention*/
810 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
811 LLVMBuildStore(builder
, out3
, outputs
[0][3]);
813 /* Viewport Mapping */
814 for (i
=0; i
<3; i
++) {
815 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[0][i
], ""); /*x0 x1 .. xn*/
818 LLVMValueRef scale_i
;
819 LLVMValueRef trans_i
;
822 index
= lp_build_const_int32(gallivm
, i
);
823 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
825 index
= lp_build_const_int32(gallivm
, i
+4);
826 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
828 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
829 LLVMBuildLoad(builder
, scale_i
, "scale"));
830 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
831 LLVMBuildLoad(builder
, trans_i
, "trans"));
834 out
= LLVMBuildFMul(builder
, out
, out3
, "");
836 out
= LLVMBuildFMul(builder
, out
, scale
, "");
837 /* add translation */
838 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
840 /* store transformed outputs */
841 LLVMBuildStore(builder
, out
, outputs
[0][i
]);
848 * Returns clipmask as nxi32 bitmask for the n vertices
851 generate_clipmask(struct draw_llvm
*llvm
,
852 struct gallivm_state
*gallivm
,
853 struct lp_type vs_type
,
854 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
860 LLVMValueRef context_ptr
,
861 boolean
*have_clipdist
)
863 LLVMBuilderRef builder
= gallivm
->builder
;
864 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
865 LLVMValueRef test
, temp
;
866 LLVMValueRef zero
, shift
;
867 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
868 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
869 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
870 struct lp_type f32_type
= vs_type
;
871 struct lp_type i32_type
= lp_int_type(vs_type
);
872 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
873 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
874 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
875 bool have_cd
= false;
878 cd
[0] = draw_current_shader_clipdistance_output(llvm
->draw
, 0);
879 cd
[1] = draw_current_shader_clipdistance_output(llvm
->draw
, 1);
881 if (cd
[0] != pos
|| cd
[1] != pos
)
884 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
885 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
886 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
887 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
890 * load clipvertex and position from correct locations.
891 * if they are the same just load them once.
893 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
894 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
895 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
896 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
898 if (clip_user
&& cv
!= pos
) {
899 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
900 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
901 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
902 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
910 /* Cliptest, for hardwired planes */
913 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
915 test
= LLVMBuildAnd(builder
, test
, temp
, "");
919 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
920 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
921 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
922 test
= LLVMBuildAnd(builder
, test
, temp
, "");
923 mask
= LLVMBuildOr(builder
, mask
, test
, "");
926 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
927 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
928 test
= LLVMBuildAnd(builder
, test
, temp
, "");
929 mask
= LLVMBuildOr(builder
, mask
, test
, "");
932 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
933 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
934 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
935 test
= LLVMBuildAnd(builder
, test
, temp
, "");
936 mask
= LLVMBuildOr(builder
, mask
, test
, "");
940 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
943 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
944 test
= LLVMBuildAnd(builder
, test
, temp
, "");
945 mask
= LLVMBuildOr(builder
, mask
, test
, "");
949 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
950 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
951 test
= LLVMBuildAnd(builder
, test
, temp
, "");
952 mask
= LLVMBuildOr(builder
, mask
, test
, "");
955 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
956 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
957 test
= LLVMBuildAnd(builder
, test
, temp
, "");
958 mask
= LLVMBuildOr(builder
, mask
, test
, "");
962 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
963 LLVMValueRef indices
[3];
965 /* userclip planes */
967 unsigned plane_idx
= ffs(ucp_enable
)-1;
968 ucp_enable
&= ~(1 << plane_idx
);
971 if (have_cd
&& num_written_clipdistance
) {
972 LLVMValueRef clipdist
;
976 *have_clipdist
= TRUE
;
978 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
980 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
982 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
983 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
984 test
= LLVMBuildAnd(builder
, test
, temp
, "");
985 mask
= LLVMBuildOr(builder
, mask
, test
, "");
987 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
988 indices
[0] = lp_build_const_int32(gallivm
, 0);
989 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
991 indices
[2] = lp_build_const_int32(gallivm
, 0);
992 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
993 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
994 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
995 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
997 indices
[2] = lp_build_const_int32(gallivm
, 1);
998 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
999 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1000 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1001 test
= LLVMBuildFMul(builder
, planes
, cv_y
, "");
1002 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1004 indices
[2] = lp_build_const_int32(gallivm
, 2);
1005 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1006 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1007 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1008 test
= LLVMBuildFMul(builder
, planes
, cv_z
, "");
1009 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1011 indices
[2] = lp_build_const_int32(gallivm
, 3);
1012 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1013 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1014 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1015 test
= LLVMBuildFMul(builder
, planes
, cv_w
, "");
1016 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1018 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1019 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1020 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1021 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1030 * Returns boolean if any clipping has occurred
1031 * Used zero/non-zero i32 value to represent boolean
1034 clipmask_booli32(struct gallivm_state
*gallivm
,
1035 const struct lp_type vs_type
,
1036 LLVMValueRef clipmask_bool_ptr
)
1038 LLVMBuilderRef builder
= gallivm
->builder
;
1039 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
1040 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1041 LLVMValueRef ret
= LLVMConstNull(int32_type
);
1046 * Can do this with log2(vector length) pack instructions and one extract
1047 * (as we don't actually need a or) with sse2 which would be way better.
1049 for (i
=0; i
< vs_type
.length
; i
++) {
1050 temp
= LLVMBuildExtractElement(builder
, clipmask_bool
,
1051 lp_build_const_int32(gallivm
, i
) , "");
1052 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1059 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1062 struct gallivm_state
*gallivm
= variant
->gallivm
;
1063 LLVMContextRef context
= gallivm
->context
;
1064 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1065 LLVMTypeRef arg_types
[8];
1066 LLVMTypeRef func_type
;
1067 LLVMValueRef context_ptr
;
1068 LLVMBasicBlockRef block
;
1069 LLVMBuilderRef builder
;
1070 struct lp_type vs_type
;
1071 LLVMValueRef end
, start
;
1072 LLVMValueRef count
, fetch_elts
, fetch_count
;
1073 LLVMValueRef stride
, step
, io_itr
;
1074 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1075 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1076 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1077 struct draw_context
*draw
= llvm
->draw
;
1078 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1080 struct lp_build_context bld
;
1081 struct lp_build_loop_state lp_loop
;
1082 const int vector_length
= lp_native_vector_width
/ 32;
1083 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1084 LLVMValueRef fetch_max
;
1085 struct lp_build_sampler_soa
*sampler
= 0;
1086 LLVMValueRef ret
, clipmask_bool_ptr
;
1087 const boolean bypass_viewport
= variant
->key
.bypass_viewport
;
1088 const boolean enable_cliptest
= variant
->key
.clip_xy
||
1089 variant
->key
.clip_z
||
1090 variant
->key
.clip_user
;
1091 LLVMValueRef variant_func
;
1092 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1093 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1094 boolean have_clipdist
= FALSE
;
1095 struct lp_bld_tgsi_system_values system_values
;
1097 memset(&system_values
, 0, sizeof(system_values
));
1099 arg_types
[0] = get_context_ptr_type(variant
); /* context */
1100 arg_types
[1] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1101 arg_types
[2] = get_buffer_ptr_type(variant
); /* vbuffers */
1103 arg_types
[3] = LLVMPointerType(int32_type
, 0);/* fetch_elts * */
1105 arg_types
[3] = int32_type
; /* start */
1106 arg_types
[4] = int32_type
; /* fetch_count / count */
1107 arg_types
[5] = int32_type
; /* stride */
1108 arg_types
[6] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1109 arg_types
[7] = int32_type
; /* instance_id */
1111 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
1113 variant_func
= LLVMAddFunction(gallivm
->module
,
1114 elts
? "draw_llvm_shader_elts" : "draw_llvm_shader",
1118 variant
->function_elts
= variant_func
;
1120 variant
->function
= variant_func
;
1122 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1123 for (i
= 0; i
< Elements(arg_types
); ++i
)
1124 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1125 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1126 LLVMNoAliasAttribute
);
1128 context_ptr
= LLVMGetParam(variant_func
, 0);
1129 io_ptr
= LLVMGetParam(variant_func
, 1);
1130 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1131 stride
= LLVMGetParam(variant_func
, 5);
1132 vb_ptr
= LLVMGetParam(variant_func
, 6);
1133 system_values
.instance_id
= LLVMGetParam(variant_func
, 7);
1135 lp_build_name(context_ptr
, "context");
1136 lp_build_name(io_ptr
, "io");
1137 lp_build_name(vbuffers_ptr
, "vbuffers");
1138 lp_build_name(stride
, "stride");
1139 lp_build_name(vb_ptr
, "vb");
1140 lp_build_name(system_values
.instance_id
, "instance_id");
1143 fetch_elts
= LLVMGetParam(variant_func
, 3);
1144 fetch_count
= LLVMGetParam(variant_func
, 4);
1145 lp_build_name(fetch_elts
, "fetch_elts");
1146 lp_build_name(fetch_count
, "fetch_count");
1147 start
= count
= NULL
;
1150 start
= LLVMGetParam(variant_func
, 3);
1151 count
= LLVMGetParam(variant_func
, 4);
1152 lp_build_name(start
, "start");
1153 lp_build_name(count
, "count");
1154 fetch_elts
= fetch_count
= NULL
;
1161 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1162 builder
= gallivm
->builder
;
1163 LLVMPositionBuilderAtEnd(builder
, block
);
1165 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1167 memset(&vs_type
, 0, sizeof vs_type
);
1168 vs_type
.floating
= TRUE
; /* floating point values */
1169 vs_type
.sign
= TRUE
; /* values are signed */
1170 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1171 vs_type
.width
= 32; /* 32-bit float */
1172 vs_type
.length
= vector_length
;
1174 /* hold temporary "bool" clipmask */
1175 clipmask_bool_ptr
= lp_build_alloca(gallivm
, lp_build_int_vec_type(gallivm
, vs_type
), "");
1176 LLVMBuildStore(builder
, lp_build_zero(gallivm
, lp_int_type(vs_type
)), clipmask_bool_ptr
);
1178 /* code generated texture sampling */
1179 sampler
= draw_llvm_sampler_soa_create(
1180 draw_llvm_variant_key_samplers(&variant
->key
),
1188 end
= lp_build_add(&bld
, start
, count
);
1191 step
= lp_build_const_int32(gallivm
, vector_length
);
1193 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1195 lp_build_loop_begin(&lp_loop
, gallivm
, start
);
1197 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1198 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][LP_MAX_VECTOR_WIDTH
/ 32] = { { 0 } };
1200 LLVMValueRef clipmask
; /* holds the clipmask value */
1201 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1204 io_itr
= lp_loop
.counter
;
1206 io_itr
= LLVMBuildSub(builder
, lp_loop
.counter
, start
, "");
1208 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1210 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1211 io_itr
, io
, lp_loop
.counter
);
1213 system_values
.vertex_id
= lp_build_zero(gallivm
, lp_type_uint_vec(32, 32*vector_length
));
1214 for (i
= 0; i
< vector_length
; ++i
) {
1215 LLVMValueRef true_index
=
1216 LLVMBuildAdd(builder
,
1218 lp_build_const_int32(gallivm
, i
), "");
1220 /* make sure we're not out of bounds which can happen
1221 * if fetch_count % 4 != 0, because on the last iteration
1222 * a few of the 4 vertex fetches will be out of bounds */
1223 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1226 LLVMValueRef fetch_ptr
;
1227 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1228 &true_index
, 1, "");
1229 true_index
= LLVMBuildLoad(builder
, fetch_ptr
, "fetch_elt");
1232 system_values
.vertex_id
= LLVMBuildInsertElement(gallivm
->builder
,
1233 system_values
.vertex_id
, true_index
,
1234 lp_build_const_int32(gallivm
, i
), "");
1235 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1236 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1237 LLVMValueRef vb_index
=
1238 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1239 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1240 generate_fetch(gallivm
, vbuffers_ptr
,
1241 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1242 system_values
.instance_id
);
1245 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1246 draw
->pt
.nr_vertex_elements
, vs_type
);
1248 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1249 generate_vs(variant
,
1257 variant
->key
.clamp_vertex_color
);
1259 /* store original positions in clip before further manipulation */
1260 store_clip(gallivm
, vs_type
, io
, outputs
, 0, cv
);
1261 store_clip(gallivm
, vs_type
, io
, outputs
, 1, pos
);
1264 if (enable_cliptest
) {
1265 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1266 /* allocate clipmask, assign it integer type */
1267 clipmask
= generate_clipmask(llvm
,
1271 variant
->key
.clip_xy
,
1272 variant
->key
.clip_z
,
1273 variant
->key
.clip_user
,
1274 variant
->key
.clip_halfz
,
1275 variant
->key
.ucp_enable
,
1276 context_ptr
, &have_clipdist
);
1277 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1278 /* store temporary clipping boolean value */
1279 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1282 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1285 /* do viewport mapping */
1286 if (!bypass_viewport
) {
1287 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1290 /* store clipmask in vertex header,
1291 * original positions in clip
1292 * and transformed positions in data
1294 convert_to_aos(gallivm
, io
, outputs
, clipmask
,
1295 vs_info
->num_outputs
, vs_type
,
1299 lp_build_loop_end_cond(&lp_loop
, end
, step
, LLVMIntUGE
);
1301 sampler
->destroy(sampler
);
1303 /* return clipping boolean value for function */
1304 ret
= clipmask_booli32(gallivm
, vs_type
, clipmask_bool_ptr
);
1306 LLVMBuildRet(builder
, ret
);
1308 gallivm_verify_function(gallivm
, variant_func
);
1312 struct draw_llvm_variant_key
*
1313 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1316 struct draw_llvm_variant_key
*key
;
1317 struct lp_sampler_static_state
*sampler
;
1319 key
= (struct draw_llvm_variant_key
*)store
;
1321 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1323 /* Presumably all variants of the shader should have the same
1324 * number of vertex elements - ie the number of shader inputs.
1326 key
->nr_vertex_elements
= llvm
->draw
->pt
.nr_vertex_elements
;
1328 /* will have to rig this up properly later */
1329 key
->clip_xy
= llvm
->draw
->clip_xy
;
1330 key
->clip_z
= llvm
->draw
->clip_z
;
1331 key
->clip_user
= llvm
->draw
->clip_user
;
1332 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1333 key
->clip_halfz
= !llvm
->draw
->rasterizer
->gl_rasterization_rules
;
1334 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1335 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1338 /* All variants of this shader will have the same value for
1339 * nr_samplers. Not yet trying to compact away holes in the
1342 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1344 sampler
= draw_llvm_variant_key_samplers(key
);
1346 memcpy(key
->vertex_element
,
1347 llvm
->draw
->pt
.vertex_element
,
1348 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1350 memset(sampler
, 0, key
->nr_samplers
* sizeof *sampler
);
1352 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1353 lp_sampler_static_state(&sampler
[i
],
1354 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
],
1355 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
1363 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1364 unsigned sampler_idx
,
1365 uint32_t width
, uint32_t height
, uint32_t depth
,
1366 uint32_t first_level
, uint32_t last_level
,
1367 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1368 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1369 const void *data
[PIPE_MAX_TEXTURE_LEVELS
])
1372 struct draw_jit_texture
*jit_tex
;
1374 assert(sampler_idx
< PIPE_MAX_VERTEX_SAMPLERS
);
1376 jit_tex
= &draw
->llvm
->jit_context
.textures
[sampler_idx
];
1378 jit_tex
->width
= width
;
1379 jit_tex
->height
= height
;
1380 jit_tex
->depth
= depth
;
1381 jit_tex
->first_level
= first_level
;
1382 jit_tex
->last_level
= last_level
;
1384 for (j
= first_level
; j
<= last_level
; j
++) {
1385 jit_tex
->data
[j
] = data
[j
];
1386 jit_tex
->row_stride
[j
] = row_stride
[j
];
1387 jit_tex
->img_stride
[j
] = img_stride
[j
];
1393 draw_llvm_set_sampler_state(struct draw_context
*draw
)
1397 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
1398 struct draw_jit_texture
*jit_tex
= &draw
->llvm
->jit_context
.textures
[i
];
1400 if (draw
->samplers
[i
]) {
1401 const struct pipe_sampler_state
*s
1402 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
1403 jit_tex
->min_lod
= s
->min_lod
;
1404 jit_tex
->max_lod
= s
->max_lod
;
1405 jit_tex
->lod_bias
= s
->lod_bias
;
1406 COPY_4V(jit_tex
->border_color
, s
->border_color
.f
);
1413 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
1415 struct draw_llvm
*llvm
= variant
->llvm
;
1417 if (variant
->function_elts
) {
1418 gallivm_free_function(variant
->gallivm
,
1419 variant
->function_elts
, variant
->jit_func_elts
);
1422 if (variant
->function
) {
1423 gallivm_free_function(variant
->gallivm
,
1424 variant
->function
, variant
->jit_func
);
1427 gallivm_destroy(variant
->gallivm
);
1429 remove_from_list(&variant
->list_item_local
);
1430 variant
->shader
->variants_cached
--;
1431 remove_from_list(&variant
->list_item_global
);
1432 llvm
->nr_variants
--;