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"
34 #include "gallivm/lp_bld_arit.h"
35 #include "gallivm/lp_bld_arit_overflow.h"
36 #include "gallivm/lp_bld_logic.h"
37 #include "gallivm/lp_bld_const.h"
38 #include "gallivm/lp_bld_swizzle.h"
39 #include "gallivm/lp_bld_struct.h"
40 #include "gallivm/lp_bld_type.h"
41 #include "gallivm/lp_bld_flow.h"
42 #include "gallivm/lp_bld_debug.h"
43 #include "gallivm/lp_bld_tgsi.h"
44 #include "gallivm/lp_bld_printf.h"
45 #include "gallivm/lp_bld_intr.h"
46 #include "gallivm/lp_bld_init.h"
47 #include "gallivm/lp_bld_type.h"
48 #include "gallivm/lp_bld_pack.h"
49 #include "gallivm/lp_bld_format.h"
51 #include "tgsi/tgsi_exec.h"
52 #include "tgsi/tgsi_dump.h"
54 #include "util/u_math.h"
55 #include "util/u_pointer.h"
56 #include "util/u_string.h"
57 #include "util/u_simple_list.h"
64 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
,
68 struct draw_gs_llvm_iface
{
69 struct lp_build_tgsi_gs_iface base
;
71 struct draw_gs_llvm_variant
*variant
;
75 static INLINE
const struct draw_gs_llvm_iface
*
76 draw_gs_llvm_iface(const struct lp_build_tgsi_gs_iface
*iface
)
78 return (const struct draw_gs_llvm_iface
*)iface
;
82 * Create LLVM type for draw_vertex_buffer.
85 create_jit_dvbuffer_type(struct gallivm_state
*gallivm
,
86 const char *struct_name
)
88 LLVMTargetDataRef target
= gallivm
->target
;
89 LLVMTypeRef dvbuffer_type
;
90 LLVMTypeRef elem_types
[DRAW_JIT_DVBUFFER_NUM_FIELDS
];
91 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
93 elem_types
[DRAW_JIT_DVBUFFER_MAP
] =
94 LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
95 elem_types
[DRAW_JIT_DVBUFFER_SIZE
] = int32_type
;
97 dvbuffer_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
98 Elements(elem_types
), 0);
100 #if HAVE_LLVM < 0x0300
101 LLVMAddTypeName(gallivm
->module
, struct_name
, dvbuffer_type
);
103 /* Make sure the target's struct layout cache doesn't return
104 * stale/invalid data.
106 LLVMInvalidateStructLayout(gallivm
->target
, dvbuffer_type
);
109 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, map
,
110 target
, dvbuffer_type
,
111 DRAW_JIT_DVBUFFER_MAP
);
112 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, size
,
113 target
, dvbuffer_type
,
114 DRAW_JIT_DVBUFFER_SIZE
);
116 return dvbuffer_type
;
120 * Create LLVM type for struct draw_jit_texture
123 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
125 LLVMTargetDataRef target
= gallivm
->target
;
126 LLVMTypeRef texture_type
;
127 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
128 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
130 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
131 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
132 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
133 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
134 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
135 elem_types
[DRAW_JIT_TEXTURE_BASE
] =
136 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
137 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
138 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
139 elem_types
[DRAW_JIT_TEXTURE_MIP_OFFSETS
] =
140 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
142 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
143 Elements(elem_types
), 0);
145 #if HAVE_LLVM < 0x0300
146 LLVMAddTypeName(gallivm
->module
, struct_name
, texture_type
);
148 /* Make sure the target's struct layout cache doesn't return
149 * stale/invalid data.
151 LLVMInvalidateStructLayout(gallivm
->target
, texture_type
);
154 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
155 target
, texture_type
,
156 DRAW_JIT_TEXTURE_WIDTH
);
157 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
158 target
, texture_type
,
159 DRAW_JIT_TEXTURE_HEIGHT
);
160 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
161 target
, texture_type
,
162 DRAW_JIT_TEXTURE_DEPTH
);
163 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
164 target
, texture_type
,
165 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
166 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
167 target
, texture_type
,
168 DRAW_JIT_TEXTURE_LAST_LEVEL
);
169 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, base
,
170 target
, texture_type
,
171 DRAW_JIT_TEXTURE_BASE
);
172 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
173 target
, texture_type
,
174 DRAW_JIT_TEXTURE_ROW_STRIDE
);
175 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
176 target
, texture_type
,
177 DRAW_JIT_TEXTURE_IMG_STRIDE
);
178 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, mip_offsets
,
179 target
, texture_type
,
180 DRAW_JIT_TEXTURE_MIP_OFFSETS
);
182 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
189 * Create LLVM type for struct draw_jit_sampler
192 create_jit_sampler_type(struct gallivm_state
*gallivm
, const char *struct_name
)
194 LLVMTargetDataRef target
= gallivm
->target
;
195 LLVMTypeRef sampler_type
;
196 LLVMTypeRef elem_types
[DRAW_JIT_SAMPLER_NUM_FIELDS
];
198 elem_types
[DRAW_JIT_SAMPLER_MIN_LOD
] =
199 elem_types
[DRAW_JIT_SAMPLER_MAX_LOD
] =
200 elem_types
[DRAW_JIT_SAMPLER_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
201 elem_types
[DRAW_JIT_SAMPLER_BORDER_COLOR
] =
202 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
204 sampler_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
205 Elements(elem_types
), 0);
207 #if HAVE_LLVM < 0x0300
208 LLVMAddTypeName(gallivm
->module
, struct_name
, sampler_type
);
210 /* Make sure the target's struct layout cache doesn't return
211 * stale/invalid data.
213 LLVMInvalidateStructLayout(gallivm
->target
, sampler_type
);
216 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, min_lod
,
217 target
, sampler_type
,
218 DRAW_JIT_SAMPLER_MIN_LOD
);
219 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, max_lod
,
220 target
, sampler_type
,
221 DRAW_JIT_SAMPLER_MAX_LOD
);
222 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, lod_bias
,
223 target
, sampler_type
,
224 DRAW_JIT_SAMPLER_LOD_BIAS
);
225 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, border_color
,
226 target
, sampler_type
,
227 DRAW_JIT_SAMPLER_BORDER_COLOR
);
229 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler
, target
, sampler_type
);
236 * Create LLVM type for struct draw_jit_context
239 create_jit_context_type(struct gallivm_state
*gallivm
,
240 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
241 const char *struct_name
)
243 LLVMTargetDataRef target
= gallivm
->target
;
244 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
245 LLVMTypeRef elem_types
[DRAW_JIT_CTX_NUM_FIELDS
];
246 LLVMTypeRef context_type
;
248 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
249 LP_MAX_TGSI_CONST_BUFFERS
);
250 elem_types
[1] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
251 DRAW_TOTAL_CLIP_PLANES
), 0);
252 elem_types
[2] = LLVMPointerType(float_type
, 0); /* viewport */
253 elem_types
[3] = LLVMArrayType(texture_type
,
254 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
255 elem_types
[4] = LLVMArrayType(sampler_type
,
256 PIPE_MAX_SAMPLERS
); /* samplers */
257 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
258 Elements(elem_types
), 0);
259 #if HAVE_LLVM < 0x0300
260 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
262 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
265 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
266 target
, context_type
, DRAW_JIT_CTX_CONSTANTS
);
267 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
268 target
, context_type
, DRAW_JIT_CTX_PLANES
);
269 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewport
,
270 target
, context_type
, DRAW_JIT_CTX_VIEWPORT
);
271 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
272 target
, context_type
,
273 DRAW_JIT_CTX_TEXTURES
);
274 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
275 target
, context_type
,
276 DRAW_JIT_CTX_SAMPLERS
);
277 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
278 target
, context_type
);
285 * Create LLVM type for struct draw_gs_jit_context
288 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
289 unsigned vector_length
,
290 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
291 const char *struct_name
)
293 LLVMTargetDataRef target
= gallivm
->target
;
294 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
295 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
296 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
297 LLVMTypeRef context_type
;
299 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
300 LP_MAX_TGSI_CONST_BUFFERS
);
301 elem_types
[1] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
302 DRAW_TOTAL_CLIP_PLANES
), 0);
303 elem_types
[2] = LLVMPointerType(float_type
, 0); /* viewport */
305 elem_types
[3] = LLVMArrayType(texture_type
,
306 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
307 elem_types
[4] = LLVMArrayType(sampler_type
,
308 PIPE_MAX_SAMPLERS
); /* samplers */
310 elem_types
[5] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
311 elem_types
[6] = LLVMPointerType(LLVMVectorType(int_type
,
313 elem_types
[7] = LLVMPointerType(LLVMVectorType(int_type
,
316 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
317 Elements(elem_types
), 0);
318 #if HAVE_LLVM < 0x0300
319 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
321 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
324 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
325 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
326 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
327 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
328 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewport
,
329 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
330 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
331 target
, context_type
,
332 DRAW_GS_JIT_CTX_TEXTURES
);
333 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
334 target
, context_type
,
335 DRAW_GS_JIT_CTX_SAMPLERS
);
336 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
337 target
, context_type
,
338 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
339 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
340 target
, context_type
,
341 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
342 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
343 target
, context_type
,
344 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
345 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
346 target
, context_type
);
353 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
355 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
356 LLVMTypeRef input_array
;
358 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
359 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
360 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
361 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
367 * Create LLVM type for struct pipe_vertex_buffer
370 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
,
371 const char *struct_name
)
373 LLVMTargetDataRef target
= gallivm
->target
;
374 LLVMTypeRef elem_types
[4];
378 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
380 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
382 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
383 Elements(elem_types
), 0);
384 #if HAVE_LLVM < 0x0300
385 LLVMAddTypeName(gallivm
->module
, struct_name
, vb_type
);
387 LLVMInvalidateStructLayout(gallivm
->target
, vb_type
);
390 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
392 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
395 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
402 * Create LLVM type for struct vertex_header;
405 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
407 LLVMTargetDataRef target
= gallivm
->target
;
408 LLVMTypeRef elem_types
[4];
409 LLVMTypeRef vertex_header
;
410 char struct_name
[24];
412 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
414 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
415 elem_types
[DRAW_JIT_VERTEX_CLIP
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
416 elem_types
[DRAW_JIT_VERTEX_PRE_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
417 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
419 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
420 Elements(elem_types
), 0);
421 #if HAVE_LLVM < 0x0300
422 LLVMAddTypeName(gallivm
->module
, struct_name
, vertex_header
);
424 LLVMInvalidateStructLayout(gallivm
->target
, vertex_header
);
427 /* these are bit-fields and we can't take address of them
428 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
429 target, vertex_header,
430 DRAW_JIT_VERTEX_CLIPMASK);
431 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
432 target, vertex_header,
433 DRAW_JIT_VERTEX_EDGEFLAG);
434 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
435 target, vertex_header,
436 DRAW_JIT_VERTEX_PAD);
437 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
438 target, vertex_header,
439 DRAW_JIT_VERTEX_VERTEX_ID);
441 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
442 target
, vertex_header
,
443 DRAW_JIT_VERTEX_CLIP
);
444 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, pre_clip_pos
,
445 target
, vertex_header
,
446 DRAW_JIT_VERTEX_PRE_CLIP_POS
);
447 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
448 target
, vertex_header
,
449 DRAW_JIT_VERTEX_DATA
);
451 assert(LLVMABISizeOfType(target
, vertex_header
) ==
452 offsetof(struct vertex_header
, data
[data_elems
]));
454 return vertex_header
;
459 * Create LLVM types for various structures.
462 create_jit_types(struct draw_llvm_variant
*variant
)
464 struct gallivm_state
*gallivm
= variant
->gallivm
;
465 LLVMTypeRef texture_type
, sampler_type
, context_type
, buffer_type
,
468 texture_type
= create_jit_texture_type(gallivm
, "texture");
469 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
471 context_type
= create_jit_context_type(gallivm
, texture_type
, sampler_type
,
473 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
475 buffer_type
= create_jit_dvbuffer_type(gallivm
, "draw_vertex_buffer");
476 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
478 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
479 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
484 get_context_ptr_type(struct draw_llvm_variant
*variant
)
486 if (!variant
->context_ptr_type
)
487 create_jit_types(variant
);
488 return variant
->context_ptr_type
;
493 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
495 if (!variant
->buffer_ptr_type
)
496 create_jit_types(variant
);
497 return variant
->buffer_ptr_type
;
502 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
504 if (!variant
->vb_ptr_type
)
505 create_jit_types(variant
);
506 return variant
->vb_ptr_type
;
510 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
512 if (!variant
->vertex_header_ptr_type
)
513 create_jit_types(variant
);
514 return variant
->vertex_header_ptr_type
;
519 * Create per-context LLVM info.
522 draw_llvm_create(struct draw_context
*draw
)
524 struct draw_llvm
*llvm
;
526 llvm
= CALLOC_STRUCT( draw_llvm
);
534 llvm
->nr_variants
= 0;
535 make_empty_list(&llvm
->vs_variants_list
);
537 llvm
->nr_gs_variants
= 0;
538 make_empty_list(&llvm
->gs_variants_list
);
545 * Free per-context LLVM info.
548 draw_llvm_destroy(struct draw_llvm
*llvm
)
550 /* XXX free other draw_llvm data? */
556 * Create LLVM-generated code for a vertex shader.
558 struct draw_llvm_variant
*
559 draw_llvm_create_variant(struct draw_llvm
*llvm
,
561 const struct draw_llvm_variant_key
*key
)
563 struct draw_llvm_variant
*variant
;
564 struct llvm_vertex_shader
*shader
=
565 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
566 LLVMTypeRef vertex_header
;
568 variant
= MALLOC(sizeof *variant
+
569 shader
->variant_key_size
-
570 sizeof variant
->key
);
574 variant
->llvm
= llvm
;
576 variant
->gallivm
= gallivm_create();
578 create_jit_types(variant
);
580 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
582 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
584 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
586 draw_llvm_generate(llvm
, variant
, FALSE
); /* linear */
587 draw_llvm_generate(llvm
, variant
, TRUE
); /* elts */
589 gallivm_compile_module(variant
->gallivm
);
591 variant
->jit_func
= (draw_jit_vert_func
)
592 gallivm_jit_function(variant
->gallivm
, variant
->function
);
594 variant
->jit_func_elts
= (draw_jit_vert_func_elts
)
595 gallivm_jit_function(variant
->gallivm
, variant
->function_elts
);
597 variant
->shader
= shader
;
598 variant
->list_item_global
.base
= variant
;
599 variant
->list_item_local
.base
= variant
;
600 /*variant->no = */shader
->variants_created
++;
601 variant
->list_item_global
.base
= variant
;
608 generate_vs(struct draw_llvm_variant
*variant
,
609 LLVMBuilderRef builder
,
610 struct lp_type vs_type
,
611 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
612 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
613 const struct lp_bld_tgsi_system_values
*system_values
,
614 LLVMValueRef context_ptr
,
615 struct lp_build_sampler_soa
*draw_sampler
,
616 boolean clamp_vertex_color
)
618 struct draw_llvm
*llvm
= variant
->llvm
;
619 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
620 LLVMValueRef consts_ptr
= draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
621 struct lp_build_sampler_soa
*sampler
= 0;
623 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
624 tgsi_dump(tokens
, 0);
625 draw_llvm_dump_variant_key(&variant
->key
);
628 if (llvm
->draw
->num_sampler_views
&& llvm
->draw
->num_samplers
)
629 sampler
= draw_sampler
;
631 lp_build_tgsi_soa(variant
->gallivm
,
634 NULL
/*struct lp_build_mask_context *mask*/,
640 &llvm
->draw
->vs
.vertex_shader
->info
,
645 unsigned chan
, attrib
;
646 struct lp_build_context bld
;
647 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
648 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
650 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
651 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
652 if (outputs
[attrib
][chan
]) {
653 switch (info
->output_semantic_name
[attrib
]) {
654 case TGSI_SEMANTIC_COLOR
:
655 case TGSI_SEMANTIC_BCOLOR
:
656 if (clamp_vertex_color
) {
657 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
658 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
659 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
662 case TGSI_SEMANTIC_FOG
:
663 if (chan
== 1 || chan
== 2)
664 LLVMBuildStore(builder
, bld
.zero
, outputs
[attrib
][chan
]);
666 LLVMBuildStore(builder
, bld
.one
, outputs
[attrib
][chan
]);
676 generate_fetch(struct gallivm_state
*gallivm
,
677 struct draw_context
*draw
,
678 LLVMValueRef vbuffers_ptr
,
680 struct pipe_vertex_element
*velem
,
683 LLVMValueRef instance_id
)
685 const struct util_format_description
*format_desc
=
686 util_format_description(velem
->src_format
);
687 LLVMValueRef zero
= LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
));
688 LLVMBuilderRef builder
= gallivm
->builder
;
689 LLVMValueRef indices
=
690 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
691 velem
->vertex_buffer_index
, 0);
692 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
694 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
695 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
696 LLVMValueRef map_ptr
= draw_jit_dvbuffer_map(gallivm
, vbuffer_ptr
);
697 LLVMValueRef buffer_size
= draw_jit_dvbuffer_size(gallivm
, vbuffer_ptr
);
699 LLVMValueRef buffer_overflowed
;
700 LLVMValueRef needed_buffer_size
;
701 LLVMValueRef temp_ptr
=
702 lp_build_alloca(gallivm
,
703 lp_build_vec_type(gallivm
, lp_float32_vec4_type()), "");
704 LLVMValueRef ofbit
= NULL
;
705 struct lp_build_if_state if_ctx
;
707 if (velem
->instance_divisor
) {
708 /* Index is equal to the start instance plus the number of current
709 * instance divided by the divisor. In this case we compute it as:
710 * index = start_instance + (instance_id / divisor)
712 LLVMValueRef current_instance
;
713 index
= lp_build_const_int32(gallivm
, draw
->start_instance
);
714 current_instance
= LLVMBuildUDiv(builder
, instance_id
,
715 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
717 index
= lp_build_uadd_overflow(gallivm
, index
, current_instance
, &ofbit
);
720 stride
= lp_build_umul_overflow(gallivm
, vb_stride
, index
, &ofbit
);
721 stride
= lp_build_uadd_overflow(gallivm
, stride
, vb_buffer_offset
, &ofbit
);
722 stride
= lp_build_uadd_overflow(
724 lp_build_const_int32(gallivm
, velem
->src_offset
), &ofbit
);
725 needed_buffer_size
= lp_build_uadd_overflow(
727 lp_build_const_int32(gallivm
,
728 util_format_get_blocksize(velem
->src_format
)),
731 buffer_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
732 needed_buffer_size
, buffer_size
,
733 "buffer_overflowed");
734 buffer_overflowed
= LLVMBuildOr(builder
, buffer_overflowed
, ofbit
, "");
736 lp_build_printf(gallivm
, "vbuf index = %u, vb_stride is %u\n",
738 lp_build_printf(gallivm
, " vb_buffer_offset = %u, src_offset is %u\n",
740 lp_build_const_int32(gallivm
, velem
->src_offset
));
741 lp_build_print_value(gallivm
, " blocksize = ",
742 lp_build_const_int32(
744 util_format_get_blocksize(velem
->src_format
)));
745 lp_build_printf(gallivm
, " instance_id = %u\n", instance_id
);
746 lp_build_printf(gallivm
, " stride = %u\n", stride
);
747 lp_build_printf(gallivm
, " buffer size = %u\n", buffer_size
);
748 lp_build_printf(gallivm
, " needed_buffer_size = %u\n", needed_buffer_size
);
749 lp_build_print_value(gallivm
, " buffer overflowed = ", buffer_overflowed
);
752 lp_build_if(&if_ctx
, gallivm
, buffer_overflowed
);
755 lp_build_const_vec(gallivm
, lp_float32_vec4_type(), 0);
756 LLVMBuildStore(builder
, val
, temp_ptr
);
758 lp_build_else(&if_ctx
);
761 map_ptr
= LLVMBuildGEP(builder
, map_ptr
, &stride
, 1, "");
763 val
= lp_build_fetch_rgba_aos(gallivm
,
765 lp_float32_vec4_type(),
768 LLVMBuildStore(builder
, val
, temp_ptr
);
770 lp_build_endif(&if_ctx
);
772 *res
= LLVMBuildLoad(builder
, temp_ptr
, "aos");
776 convert_to_soa(struct gallivm_state
*gallivm
,
777 LLVMValueRef (*src_aos
)[LP_MAX_VECTOR_WIDTH
/ 32],
778 LLVMValueRef (*dst_soa
)[TGSI_NUM_CHANNELS
],
779 unsigned num_attribs
, const struct lp_type soa_type
)
782 struct lp_type aos_channel_type
= soa_type
;
784 debug_assert(TGSI_NUM_CHANNELS
== 4);
785 debug_assert((soa_type
.length
% TGSI_NUM_CHANNELS
) == 0);
787 aos_channel_type
.length
>>= 1;
789 for (i
= 0; i
< num_attribs
; ++i
) {
790 LLVMValueRef aos_channels
[TGSI_NUM_CHANNELS
];
791 unsigned pixels_per_channel
= soa_type
.length
/ TGSI_NUM_CHANNELS
;
793 for (j
= 0; j
< TGSI_NUM_CHANNELS
; ++j
) {
794 LLVMValueRef channel
[LP_MAX_VECTOR_LENGTH
] = { 0 };
796 assert(pixels_per_channel
<= LP_MAX_VECTOR_LENGTH
);
798 for (k
= 0; k
< pixels_per_channel
; ++k
) {
799 channel
[k
] = src_aos
[i
][j
+ TGSI_NUM_CHANNELS
* k
];
802 aos_channels
[j
] = lp_build_concat(gallivm
, channel
, aos_channel_type
, pixels_per_channel
);
805 lp_build_transpose_aos(gallivm
, soa_type
, aos_channels
, dst_soa
[i
]);
811 store_aos(struct gallivm_state
*gallivm
,
816 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
817 LLVMBuilderRef builder
= gallivm
->builder
;
818 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
819 LLVMValueRef indices
[3];
821 indices
[0] = lp_build_const_int32(gallivm
, 0);
823 indices
[2] = lp_build_const_int32(gallivm
, 0);
825 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
826 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
829 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
832 /* Unaligned store due to the vertex header */
833 lp_set_store_alignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
837 * Adjust the mask to architecture endianess. The mask will the store in struct:
839 * struct vertex_header {
840 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
841 * unsigned edgeflag:1;
842 * unsigned have_clipdist:1;
843 * unsigned vertex_id:16;
847 * On little-endian machine nothing needs to done, however on bit-endian machine
848 * the mask's fields need to be adjusted with the algorithm:
850 * uint32_t reverse (uint32_t x)
852 * return (x >> 16) | // vertex_id
853 * ((x & 0x3fff) << 18) | // clipmask
854 * ((x & 0x4000) << 3) | // have_clipdist
855 * ((x & 0x8000) << 1); // edgeflag
859 adjust_mask(struct gallivm_state
*gallivm
,
862 #ifdef PIPE_ARCH_BIG_ENDIAN
863 LLVMBuilderRef builder
= gallivm
->builder
;
864 LLVMValueRef vertex_id
;
865 LLVMValueRef clipmask
;
866 LLVMValueRef have_clipdist
;
867 LLVMValueRef edgeflag
;
869 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
870 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
871 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
872 have_clipdist
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
873 have_clipdist
= LLVMBuildShl(builder
, have_clipdist
, lp_build_const_int32(gallivm
, 3), "");
874 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
875 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
877 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
878 mask
= LLVMBuildOr(builder
, mask
, have_clipdist
, "");
879 mask
= LLVMBuildOr(builder
, mask
, edgeflag
, "");
885 store_aos_array(struct gallivm_state
*gallivm
,
886 struct lp_type soa_type
,
888 LLVMValueRef
*indices
,
892 LLVMValueRef clipmask
,
893 boolean have_clipdist
)
895 LLVMBuilderRef builder
= gallivm
->builder
;
896 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
897 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
898 LLVMValueRef linear_inds
[LP_MAX_VECTOR_WIDTH
/ 32];
899 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
900 int vector_length
= soa_type
.length
;
903 debug_assert(TGSI_NUM_CHANNELS
== 4);
905 for (i
= 0; i
< vector_length
; i
++) {
906 linear_inds
[i
] = lp_build_const_int32(gallivm
, i
);
908 inds
[i
] = indices
[i
];
910 inds
[i
] = linear_inds
[i
];
912 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
916 /* store vertex header for each of the n vertices */
917 LLVMValueRef val
, cliptmp
;
918 int vertex_id_pad_edgeflag
;
920 /* If this assertion fails, it means we need to update the bit twidding
921 * code here. See struct vertex_header in draw_private.h.
923 assert(DRAW_TOTAL_CLIP_PLANES
==14);
924 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
925 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
927 vertex_id_pad_edgeflag
|= 1 << (DRAW_TOTAL_CLIP_PLANES
+1);
928 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
), vertex_id_pad_edgeflag
);
929 /* OR with the clipmask */
930 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
931 for (i
= 0; i
< vector_length
; i
++) {
932 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
933 val
= LLVMBuildExtractElement(builder
, cliptmp
, linear_inds
[i
], "");
934 val
= adjust_mask(gallivm
, val
);
935 LLVMBuildStore(builder
, val
, id_ptr
);
937 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
938 io_ptrs
[i
], inds
[i
], val
);
943 /* store for each of the n vertices */
944 for (i
= 0; i
< vector_length
; i
++) {
945 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
951 convert_to_aos(struct gallivm_state
*gallivm
,
953 LLVMValueRef
*indices
,
954 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
955 LLVMValueRef clipmask
,
957 struct lp_type soa_type
,
958 boolean have_clipdist
)
960 LLVMBuilderRef builder
= gallivm
->builder
;
961 unsigned chan
, attrib
, i
;
964 lp_build_printf(gallivm
, " # storing begin\n");
966 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
967 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
968 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
969 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
970 if (outputs
[attrib
][chan
]) {
971 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
972 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
974 lp_build_printf(gallivm
, "output %d : %d ",
975 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
977 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
979 lp_build_print_value(gallivm
, "val = ", out
);
982 LLVMBuildBitCast(builder
, out
, lp_build_int_vec_type(gallivm
, soa_type
), "");
984 lp_build_print_value(gallivm
, " ival = ", iv
);
995 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
996 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
998 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
1000 for (i
= 0; i
< soa_type
.length
; ++i
) {
1001 aos
[i
] = lp_build_extract_range(gallivm
,
1002 soa
[i
% TGSI_NUM_CHANNELS
],
1003 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1008 store_aos_array(gallivm
,
1014 clipmask
, have_clipdist
);
1017 lp_build_printf(gallivm
, " # storing end\n");
1023 * Stores original vertex positions in clip coordinates
1026 store_clip(struct gallivm_state
*gallivm
,
1027 const struct lp_type vs_type
,
1028 LLVMValueRef io_ptr
,
1029 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1030 boolean pre_clip_pos
, int idx
)
1032 LLVMBuilderRef builder
= gallivm
->builder
;
1033 LLVMValueRef soa
[4];
1034 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
1035 LLVMValueRef indices
[2];
1036 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1037 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1038 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1042 indices
[1] = lp_build_const_int32(gallivm
, 0);
1044 for (i
= 0; i
< vs_type
.length
; i
++) {
1045 inds
[i
] = lp_build_const_int32(gallivm
, i
);
1046 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1049 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
1050 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
1051 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
1052 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
1054 if (!pre_clip_pos
) {
1055 for (i
= 0; i
< vs_type
.length
; i
++) {
1056 clip_ptrs
[i
] = draw_jit_header_clip(gallivm
, io_ptrs
[i
]);
1059 for (i
= 0; i
< vs_type
.length
; i
++) {
1060 clip_ptrs
[i
] = draw_jit_header_pre_clip_pos(gallivm
, io_ptrs
[i
]);
1064 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
1065 for (i
= 0; i
< vs_type
.length
; ++i
) {
1066 aos
[i
] = lp_build_extract_range(gallivm
,
1067 soa
[i
% TGSI_NUM_CHANNELS
],
1068 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1072 for (j
= 0; j
< vs_type
.length
; j
++) {
1073 LLVMTypeRef clip_ptr_type
= LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4), 0);
1074 LLVMValueRef clip_ptr
;
1076 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
1077 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
1079 /* Unaligned store */
1080 lp_set_store_alignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
1086 * Transforms the outputs for viewport mapping
1089 generate_viewport(struct draw_llvm_variant
*variant
,
1090 LLVMBuilderRef builder
,
1091 struct lp_type vs_type
,
1092 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1093 LLVMValueRef context_ptr
)
1096 struct gallivm_state
*gallivm
= variant
->gallivm
;
1097 struct lp_type f32_type
= vs_type
;
1098 const unsigned pos
= draw_current_shader_position_output(variant
->llvm
->draw
);
1099 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1100 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn*/
1101 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1102 LLVMValueRef vp_ptr
= draw_jit_context_viewport(gallivm
, context_ptr
);
1104 /* for 1/w convention*/
1105 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1106 LLVMBuildStore(builder
, out3
, outputs
[pos
][3]);
1108 /* Viewport Mapping */
1109 for (i
=0; i
<3; i
++) {
1110 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[pos
][i
], ""); /*x0 x1 .. xn*/
1113 LLVMValueRef scale_i
;
1114 LLVMValueRef trans_i
;
1117 index
= lp_build_const_int32(gallivm
, i
);
1118 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1120 index
= lp_build_const_int32(gallivm
, i
+4);
1121 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1123 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1124 LLVMBuildLoad(builder
, scale_i
, "scale"));
1125 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1126 LLVMBuildLoad(builder
, trans_i
, "trans"));
1129 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1131 out
= LLVMBuildFMul(builder
, out
, scale
, "");
1132 /* add translation */
1133 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
1135 /* store transformed outputs */
1136 LLVMBuildStore(builder
, out
, outputs
[pos
][i
]);
1143 * Returns clipmask as nxi32 bitmask for the n vertices
1146 generate_clipmask(struct draw_llvm
*llvm
,
1147 struct gallivm_state
*gallivm
,
1148 struct lp_type vs_type
,
1149 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1154 unsigned ucp_enable
,
1155 LLVMValueRef context_ptr
,
1156 boolean
*have_clipdist
)
1158 LLVMBuilderRef builder
= gallivm
->builder
;
1159 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1160 LLVMValueRef test
, temp
;
1161 LLVMValueRef zero
, shift
;
1162 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1163 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1164 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1165 struct lp_type f32_type
= vs_type
;
1166 struct lp_type i32_type
= lp_int_type(vs_type
);
1167 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1168 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1169 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1170 bool have_cd
= false;
1173 cd
[0] = draw_current_shader_clipdistance_output(llvm
->draw
, 0);
1174 cd
[1] = draw_current_shader_clipdistance_output(llvm
->draw
, 1);
1176 if (cd
[0] != pos
|| cd
[1] != pos
)
1179 if (num_written_clipdistance
&& !clip_user
) {
1181 ucp_enable
= (1 << num_written_clipdistance
) - 1;
1184 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1185 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1186 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1187 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1190 * load clipvertex and position from correct locations.
1191 * if they are the same just load them once.
1193 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1194 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1195 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1196 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1198 if (clip_user
&& cv
!= pos
) {
1199 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1200 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1201 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1202 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1210 /* Cliptest, for hardwired planes */
1213 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1215 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1219 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1220 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1221 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1222 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1223 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1226 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1227 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1228 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1229 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1232 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1233 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1234 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1235 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1236 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1240 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1243 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1244 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1245 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1249 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1250 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1251 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1252 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1255 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1256 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1257 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1258 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1262 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1263 LLVMValueRef indices
[3];
1265 /* userclip planes */
1266 while (ucp_enable
) {
1267 unsigned plane_idx
= ffs(ucp_enable
)-1;
1268 ucp_enable
&= ~(1 << plane_idx
);
1271 if (have_cd
&& num_written_clipdistance
) {
1272 LLVMValueRef clipdist
;
1276 *have_clipdist
= TRUE
;
1278 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1280 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1282 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1283 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1284 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1285 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1287 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1288 indices
[0] = lp_build_const_int32(gallivm
, 0);
1289 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1291 indices
[2] = lp_build_const_int32(gallivm
, 0);
1292 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1293 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1294 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1295 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1297 indices
[2] = lp_build_const_int32(gallivm
, 1);
1298 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1299 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1300 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1301 test
= LLVMBuildFMul(builder
, planes
, cv_y
, "");
1302 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1304 indices
[2] = lp_build_const_int32(gallivm
, 2);
1305 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1306 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1307 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1308 test
= LLVMBuildFMul(builder
, planes
, cv_z
, "");
1309 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1311 indices
[2] = lp_build_const_int32(gallivm
, 3);
1312 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1313 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1314 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1315 test
= LLVMBuildFMul(builder
, planes
, cv_w
, "");
1316 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1318 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1319 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1320 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1321 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1330 * Returns boolean if any clipping has occurred
1331 * Used zero/non-zero i32 value to represent boolean
1334 clipmask_booli32(struct gallivm_state
*gallivm
,
1335 const struct lp_type vs_type
,
1336 LLVMValueRef clipmask_bool_ptr
)
1338 LLVMBuilderRef builder
= gallivm
->builder
;
1339 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
1340 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1341 LLVMValueRef ret
= LLVMConstNull(int32_type
);
1346 * Can do this with log2(vector length) pack instructions and one extract
1347 * (as we don't actually need a or) with sse2 which would be way better.
1349 for (i
=0; i
< vs_type
.length
; i
++) {
1350 temp
= LLVMBuildExtractElement(builder
, clipmask_bool
,
1351 lp_build_const_int32(gallivm
, i
) , "");
1352 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1358 draw_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
1359 struct lp_build_tgsi_context
* bld_base
,
1360 boolean is_indirect
,
1361 LLVMValueRef vertex_index
,
1362 LLVMValueRef attrib_index
,
1363 LLVMValueRef swizzle_index
)
1365 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1366 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1367 LLVMBuilderRef builder
= gallivm
->builder
;
1368 LLVMValueRef indices
[3];
1370 struct lp_type type
= bld_base
->base
.type
;
1374 res
= bld_base
->base
.zero
;
1375 for (i
= 0; i
< type
.length
; ++i
) {
1376 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1377 LLVMValueRef vert_chan_index
= LLVMBuildExtractElement(builder
,
1378 vertex_index
, idx
, "");
1379 LLVMValueRef channel_vec
, value
;
1380 indices
[0] = vert_chan_index
;
1381 indices
[1] = attrib_index
;
1382 indices
[2] = swizzle_index
;
1384 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1385 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1386 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1388 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1391 indices
[0] = vertex_index
;
1392 indices
[1] = attrib_index
;
1393 indices
[2] = swizzle_index
;
1395 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1396 res
= LLVMBuildLoad(builder
, res
, "");
1403 draw_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
1404 struct lp_build_tgsi_context
* bld_base
,
1405 LLVMValueRef (*outputs
)[4],
1406 LLVMValueRef emitted_vertices_vec
)
1408 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1409 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1410 struct gallivm_state
*gallivm
= variant
->gallivm
;
1411 LLVMBuilderRef builder
= gallivm
->builder
;
1412 struct lp_type gs_type
= bld_base
->base
.type
;
1413 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1414 lp_int_type(gs_type
), 0);
1415 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1416 LLVMValueRef next_prim_offset
=
1417 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1418 LLVMValueRef io
= variant
->io_ptr
;
1420 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1422 for (i
= 0; i
< gs_type
.length
; ++i
) {
1423 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1424 LLVMValueRef currently_emitted
=
1425 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1426 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1427 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1430 convert_to_aos(gallivm
, io
, indices
,
1432 gs_info
->num_outputs
, gs_type
,
1437 draw_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
1438 struct lp_build_tgsi_context
* bld_base
,
1439 LLVMValueRef verts_per_prim_vec
,
1440 LLVMValueRef emitted_prims_vec
)
1442 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1443 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1444 struct gallivm_state
*gallivm
= variant
->gallivm
;
1445 LLVMBuilderRef builder
= gallivm
->builder
;
1446 LLVMValueRef prim_lengts_ptr
=
1447 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1450 for (i
= 0; i
< bld_base
->base
.type
.length
; ++i
) {
1451 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1452 LLVMValueRef prims_emitted
=
1453 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1454 LLVMValueRef store_ptr
;
1455 LLVMValueRef num_vertices
=
1456 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1458 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1459 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1460 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1461 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1466 draw_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
1467 struct lp_build_tgsi_context
* bld_base
,
1468 LLVMValueRef total_emitted_vertices_vec
,
1469 LLVMValueRef emitted_prims_vec
)
1471 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1472 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1473 struct gallivm_state
*gallivm
= variant
->gallivm
;
1474 LLVMBuilderRef builder
= gallivm
->builder
;
1475 LLVMValueRef emitted_verts_ptr
=
1476 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1477 LLVMValueRef emitted_prims_ptr
=
1478 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1479 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1481 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &zero
, 0, "");
1482 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &zero
, 0, "");
1484 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1485 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1489 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1492 struct gallivm_state
*gallivm
= variant
->gallivm
;
1493 LLVMContextRef context
= gallivm
->context
;
1494 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1495 LLVMTypeRef arg_types
[10];
1496 unsigned num_arg_types
=
1497 elts
? Elements(arg_types
) : Elements(arg_types
) - 1;
1498 LLVMTypeRef func_type
;
1499 LLVMValueRef context_ptr
;
1500 LLVMBasicBlockRef block
;
1501 LLVMBuilderRef builder
;
1502 struct lp_type vs_type
;
1503 LLVMValueRef end
, start
;
1504 LLVMValueRef count
, fetch_elts
, fetch_elt_max
, fetch_count
;
1505 LLVMValueRef vertex_id_offset
;
1506 LLVMValueRef stride
, step
, io_itr
;
1507 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1508 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1509 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1510 struct draw_context
*draw
= llvm
->draw
;
1511 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1513 struct lp_build_context bld
;
1514 struct lp_build_loop_state lp_loop
;
1515 const int vector_length
= lp_native_vector_width
/ 32;
1516 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1517 LLVMValueRef fetch_max
;
1518 struct lp_build_sampler_soa
*sampler
= 0;
1519 LLVMValueRef ret
, clipmask_bool_ptr
;
1520 const struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
1521 struct draw_llvm_variant_key
*key
= &variant
->key
;
1522 /* If geometry shader is present we need to skip both the viewport
1523 * transformation and clipping otherwise the inputs to the geometry
1524 * shader will be incorrect.
1526 const boolean bypass_viewport
= gs
|| key
->bypass_viewport
;
1527 const boolean enable_cliptest
= !gs
&& (key
->clip_xy
||
1530 LLVMValueRef variant_func
;
1531 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1532 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1533 boolean have_clipdist
= FALSE
;
1534 struct lp_bld_tgsi_system_values system_values
;
1536 memset(&system_values
, 0, sizeof(system_values
));
1539 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1540 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1541 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1543 arg_types
[i
++] = LLVMPointerType(int32_type
, 0);/* fetch_elts */
1544 arg_types
[i
++] = int32_type
; /* fetch_elt_max */
1546 arg_types
[i
++] = int32_type
; /* start */
1547 arg_types
[i
++] = int32_type
; /* fetch_count / count */
1548 arg_types
[i
++] = int32_type
; /* stride */
1549 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1550 arg_types
[i
++] = int32_type
; /* instance_id */
1551 arg_types
[i
++] = int32_type
; /* vertex_id_offset */
1553 func_type
= LLVMFunctionType(int32_type
, arg_types
, num_arg_types
, 0);
1555 variant_func
= LLVMAddFunction(gallivm
->module
,
1556 elts
? "draw_llvm_shader_elts" : "draw_llvm_shader",
1560 variant
->function_elts
= variant_func
;
1562 variant
->function
= variant_func
;
1564 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1565 for (i
= 0; i
< num_arg_types
; ++i
)
1566 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1567 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1568 LLVMNoAliasAttribute
);
1570 context_ptr
= LLVMGetParam(variant_func
, 0);
1571 io_ptr
= LLVMGetParam(variant_func
, 1);
1572 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1573 stride
= LLVMGetParam(variant_func
, 5 + (elts
? 1 : 0));
1574 vb_ptr
= LLVMGetParam(variant_func
, 6 + (elts
? 1 : 0));
1575 system_values
.instance_id
= LLVMGetParam(variant_func
, 7 + (elts
? 1 : 0));
1576 vertex_id_offset
= LLVMGetParam(variant_func
, 8 + (elts
? 1 : 0));
1578 lp_build_name(context_ptr
, "context");
1579 lp_build_name(io_ptr
, "io");
1580 lp_build_name(vbuffers_ptr
, "vbuffers");
1581 lp_build_name(stride
, "stride");
1582 lp_build_name(vb_ptr
, "vb");
1583 lp_build_name(system_values
.instance_id
, "instance_id");
1584 lp_build_name(vertex_id_offset
, "vertex_id_offset");
1587 fetch_elts
= LLVMGetParam(variant_func
, 3);
1588 fetch_elt_max
= LLVMGetParam(variant_func
, 4);
1589 fetch_count
= LLVMGetParam(variant_func
, 5);
1590 lp_build_name(fetch_elts
, "fetch_elts");
1591 lp_build_name(fetch_elt_max
, "fetch_elt_max");
1592 lp_build_name(fetch_count
, "fetch_count");
1593 start
= count
= NULL
;
1596 start
= LLVMGetParam(variant_func
, 3);
1597 count
= LLVMGetParam(variant_func
, 4);
1598 lp_build_name(start
, "start");
1599 lp_build_name(count
, "count");
1600 fetch_elts
= fetch_count
= NULL
;
1607 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1608 builder
= gallivm
->builder
;
1609 LLVMPositionBuilderAtEnd(builder
, block
);
1611 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1613 memset(&vs_type
, 0, sizeof vs_type
);
1614 vs_type
.floating
= TRUE
; /* floating point values */
1615 vs_type
.sign
= TRUE
; /* values are signed */
1616 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1617 vs_type
.width
= 32; /* 32-bit float */
1618 vs_type
.length
= vector_length
;
1620 /* hold temporary "bool" clipmask */
1621 clipmask_bool_ptr
= lp_build_alloca(gallivm
, lp_build_int_vec_type(gallivm
, vs_type
), "");
1622 LLVMBuildStore(builder
, lp_build_zero(gallivm
, lp_int_type(vs_type
)), clipmask_bool_ptr
);
1624 /* code generated texture sampling */
1625 sampler
= draw_llvm_sampler_soa_create(
1626 draw_llvm_variant_key_samplers(key
),
1632 count
= fetch_count
;
1635 end
= lp_build_add(&bld
, start
, count
);
1638 step
= lp_build_const_int32(gallivm
, vector_length
);
1640 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1642 lp_build_loop_begin(&lp_loop
, gallivm
, zero
);
1644 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1645 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][LP_MAX_VECTOR_WIDTH
/ 32] = { { 0 } };
1647 LLVMValueRef clipmask
; /* holds the clipmask value */
1648 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1650 io_itr
= lp_loop
.counter
;
1652 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1654 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1655 io_itr
, io
, lp_loop
.counter
);
1657 system_values
.vertex_id
= lp_build_zero(gallivm
, lp_type_uint_vec(32, 32*vector_length
));
1658 for (i
= 0; i
< vector_length
; ++i
) {
1659 LLVMValueRef vert_index
=
1660 LLVMBuildAdd(builder
,
1662 lp_build_const_int32(gallivm
, i
), "");
1663 LLVMValueRef true_index
=
1664 LLVMBuildAdd(builder
, start
, vert_index
, "");
1665 LLVMValueRef vertex_id
;
1667 /* make sure we're not out of bounds which can happen
1668 * if fetch_count % 4 != 0, because on the last iteration
1669 * a few of the 4 vertex fetches will be out of bounds */
1670 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1673 LLVMValueRef fetch_ptr
;
1674 LLVMValueRef index_overflowed
;
1675 LLVMValueRef index_ptr
=
1678 lp_build_vec_type(gallivm
, lp_type_int(32)), "");
1679 struct lp_build_if_state if_ctx
;
1680 index_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
1681 true_index
, fetch_elt_max
,
1682 "index_overflowed");
1684 lp_build_if(&if_ctx
, gallivm
, index_overflowed
);
1686 /* Generate maximum possible index so that
1687 * generate_fetch can treat it just like
1688 * any other overflow and return zeros.
1689 * We don't have to worry about the restart
1690 * primitive index because it has already been
1694 lp_build_const_int32(gallivm
, 0xffffffff);
1695 LLVMBuildStore(builder
, val
, index_ptr
);
1697 lp_build_else(&if_ctx
);
1700 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1701 &true_index
, 1, "");
1702 val
= LLVMBuildLoad(builder
, fetch_ptr
, "");
1703 LLVMBuildStore(builder
, val
, index_ptr
);
1705 lp_build_endif(&if_ctx
);
1706 true_index
= LLVMBuildLoad(builder
, index_ptr
, "true_index");
1708 /* in the paths with elts vertex id has to be unaffected by the
1709 * index bias and because indices inside our elements array have
1710 * already had index bias applied we need to subtract it here to
1711 * get back to the original index.
1712 * in the linear paths vertex id has to be unaffected by the
1713 * original start index and because we abuse the 'start' variable
1714 * to either represent the actual start index or the index at which
1715 * the primitive was split (we split rendering into chunks of at
1716 * most 4095-vertices) we need to back out the original start
1717 * index out of our vertex id here.
1719 vertex_id
= LLVMBuildSub(builder
, true_index
, vertex_id_offset
, "");
1721 system_values
.vertex_id
= LLVMBuildInsertElement(
1723 system_values
.vertex_id
, vertex_id
,
1724 lp_build_const_int32(gallivm
, i
), "");
1726 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1727 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1728 LLVMValueRef vb_index
=
1729 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1730 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1731 generate_fetch(gallivm
, draw
, vbuffers_ptr
,
1732 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1733 system_values
.instance_id
);
1736 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1737 draw
->pt
.nr_vertex_elements
, vs_type
);
1739 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1740 generate_vs(variant
,
1748 key
->clamp_vertex_color
);
1750 if (pos
!= -1 && cv
!= -1) {
1751 /* store original positions in clip before further manipulation */
1752 store_clip(gallivm
, vs_type
, io
, outputs
, 0, cv
);
1753 store_clip(gallivm
, vs_type
, io
, outputs
, 1, pos
);
1756 if (enable_cliptest
) {
1757 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1758 /* allocate clipmask, assign it integer type */
1759 clipmask
= generate_clipmask(llvm
,
1768 context_ptr
, &have_clipdist
);
1769 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1770 /* store temporary clipping boolean value */
1771 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1774 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1777 /* do viewport mapping */
1778 if (!bypass_viewport
) {
1779 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1783 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1786 /* store clipmask in vertex header,
1787 * original positions in clip
1788 * and transformed positions in data
1790 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
1791 vs_info
->num_outputs
, vs_type
,
1794 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
1796 sampler
->destroy(sampler
);
1798 /* return clipping boolean value for function */
1799 ret
= clipmask_booli32(gallivm
, vs_type
, clipmask_bool_ptr
);
1801 LLVMBuildRet(builder
, ret
);
1803 gallivm_verify_function(gallivm
, variant_func
);
1807 struct draw_llvm_variant_key
*
1808 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1811 struct draw_llvm_variant_key
*key
;
1812 struct draw_sampler_static_state
*draw_sampler
;
1814 key
= (struct draw_llvm_variant_key
*)store
;
1816 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1818 /* Presumably all variants of the shader should have the same
1819 * number of vertex elements - ie the number of shader inputs.
1820 * NOTE: we NEED to store the needed number of needed inputs
1821 * here, not the number of provided elements to match keysize
1822 * (and the offset of sampler state in the key).
1824 key
->nr_vertex_elements
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
1825 assert(key
->nr_vertex_elements
<= llvm
->draw
->pt
.nr_vertex_elements
);
1827 /* will have to rig this up properly later */
1828 key
->clip_xy
= llvm
->draw
->clip_xy
;
1829 key
->clip_z
= llvm
->draw
->clip_z
;
1830 key
->clip_user
= llvm
->draw
->clip_user
;
1831 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1832 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
1833 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1834 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1835 key
->has_gs
= llvm
->draw
->gs
.geometry_shader
!= NULL
;
1836 key
->num_outputs
= draw_total_vs_outputs(llvm
->draw
);
1839 /* All variants of this shader will have the same value for
1840 * nr_samplers. Not yet trying to compact away holes in the
1843 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1844 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
1845 key
->nr_sampler_views
=
1846 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
1849 key
->nr_sampler_views
= key
->nr_samplers
;
1852 draw_sampler
= draw_llvm_variant_key_samplers(key
);
1854 memcpy(key
->vertex_element
,
1855 llvm
->draw
->pt
.vertex_element
,
1856 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1858 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
1860 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1861 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
1862 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
1864 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1865 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
1866 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
1874 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
1877 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
1879 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
1880 debug_printf("clip_xy = %u\n", key
->clip_xy
);
1881 debug_printf("clip_z = %u\n", key
->clip_z
);
1882 debug_printf("clip_user = %u\n", key
->clip_user
);
1883 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
1884 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
1885 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
1886 debug_printf("has_gs = %u\n", key
->has_gs
);
1887 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
1889 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
1890 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
1891 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
1892 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
1893 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
1896 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1897 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
1903 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1904 unsigned shader_stage
,
1906 uint32_t width
, uint32_t height
, uint32_t depth
,
1907 uint32_t first_level
, uint32_t last_level
,
1908 const void *base_ptr
,
1909 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1910 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1911 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
1914 struct draw_jit_texture
*jit_tex
;
1916 assert(shader_stage
== PIPE_SHADER_VERTEX
||
1917 shader_stage
== PIPE_SHADER_GEOMETRY
);
1919 if (shader_stage
== PIPE_SHADER_VERTEX
) {
1920 assert(sview_idx
< Elements(draw
->llvm
->jit_context
.textures
));
1922 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
1923 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
1924 assert(sview_idx
< Elements(draw
->llvm
->gs_jit_context
.textures
));
1926 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
1932 jit_tex
->width
= width
;
1933 jit_tex
->height
= height
;
1934 jit_tex
->depth
= depth
;
1935 jit_tex
->first_level
= first_level
;
1936 jit_tex
->last_level
= last_level
;
1937 jit_tex
->base
= base_ptr
;
1939 for (j
= first_level
; j
<= last_level
; j
++) {
1940 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
1941 jit_tex
->row_stride
[j
] = row_stride
[j
];
1942 jit_tex
->img_stride
[j
] = img_stride
[j
];
1948 draw_llvm_set_sampler_state(struct draw_context
*draw
,
1949 unsigned shader_type
)
1953 if (shader_type
== PIPE_SHADER_VERTEX
) {
1954 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
1955 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
1957 if (draw
->samplers
[i
]) {
1958 const struct pipe_sampler_state
*s
1959 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
1960 jit_sam
->min_lod
= s
->min_lod
;
1961 jit_sam
->max_lod
= s
->max_lod
;
1962 jit_sam
->lod_bias
= s
->lod_bias
;
1963 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1966 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
1967 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
1968 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
1970 if (draw
->samplers
[i
]) {
1971 const struct pipe_sampler_state
*s
1972 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
1973 jit_sam
->min_lod
= s
->min_lod
;
1974 jit_sam
->max_lod
= s
->max_lod
;
1975 jit_sam
->lod_bias
= s
->lod_bias
;
1976 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1984 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
1986 struct draw_llvm
*llvm
= variant
->llvm
;
1988 if (variant
->function_elts
) {
1989 gallivm_free_function(variant
->gallivm
,
1990 variant
->function_elts
, variant
->jit_func_elts
);
1993 if (variant
->function
) {
1994 gallivm_free_function(variant
->gallivm
,
1995 variant
->function
, variant
->jit_func
);
1998 gallivm_destroy(variant
->gallivm
);
2000 remove_from_list(&variant
->list_item_local
);
2001 variant
->shader
->variants_cached
--;
2002 remove_from_list(&variant
->list_item_global
);
2003 llvm
->nr_variants
--;
2009 * Create LLVM types for various structures.
2012 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
2014 struct gallivm_state
*gallivm
= var
->gallivm
;
2015 LLVMTypeRef texture_type
, sampler_type
, context_type
;
2017 texture_type
= create_jit_texture_type(gallivm
, "texture");
2018 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2020 context_type
= create_gs_jit_context_type(gallivm
,
2021 var
->shader
->base
.vector_length
,
2022 texture_type
, sampler_type
,
2023 "draw_gs_jit_context");
2024 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2026 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
2030 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
2032 if (!variant
->context_ptr_type
)
2033 create_gs_jit_types(variant
);
2034 return variant
->context_ptr_type
;
2038 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2039 struct lp_type gs_type
)
2041 struct gallivm_state
*gallivm
= variant
->gallivm
;
2042 LLVMBuilderRef builder
= gallivm
->builder
;
2043 LLVMValueRef bits
[16];
2044 struct lp_type mask_type
= lp_int_type(gs_type
);
2045 struct lp_type mask_elem_type
= lp_elem_type(mask_type
);
2046 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
,
2051 assert(gs_type
.length
<= Elements(bits
));
2053 for (i
= gs_type
.length
; i
>= 1; --i
) {
2055 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
2056 bits
[idx
] = lp_build_compare(gallivm
,
2057 mask_elem_type
, PIPE_FUNC_GEQUAL
,
2058 variant
->num_prims
, ind
);
2060 for (i
= 0; i
< gs_type
.length
; ++i
) {
2061 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
2062 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, bits
[i
], ind
, "");
2064 mask_val
= lp_build_compare(gallivm
,
2065 mask_type
, PIPE_FUNC_NOTEQUAL
,
2067 lp_build_const_int_vec(gallivm
, mask_type
, 0));
2073 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2074 struct draw_gs_llvm_variant
*variant
)
2076 struct gallivm_state
*gallivm
= variant
->gallivm
;
2077 LLVMContextRef context
= gallivm
->context
;
2078 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2079 LLVMTypeRef arg_types
[6];
2080 LLVMTypeRef func_type
;
2081 LLVMValueRef variant_func
;
2082 LLVMValueRef context_ptr
;
2083 LLVMValueRef prim_id_ptr
;
2084 LLVMBasicBlockRef block
;
2085 LLVMBuilderRef builder
;
2086 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2087 struct lp_build_sampler_soa
*sampler
= 0;
2088 struct lp_build_context bld
;
2089 struct lp_bld_tgsi_system_values system_values
;
2090 struct lp_type gs_type
;
2092 struct draw_gs_llvm_iface gs_iface
;
2093 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2094 LLVMValueRef consts_ptr
;
2095 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2096 struct lp_build_mask_context mask
;
2097 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2098 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2100 memset(&system_values
, 0, sizeof(system_values
));
2102 assert(variant
->vertex_header_ptr_type
);
2104 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2105 arg_types
[1] = variant
->input_array_type
; /* input */
2106 arg_types
[2] = variant
->vertex_header_ptr_type
; /* vertex_header */
2107 arg_types
[3] = int32_type
; /* num_prims */
2108 arg_types
[4] = int32_type
; /* instance_id */
2109 arg_types
[5] = LLVMPointerType(
2110 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2112 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
2114 variant_func
= LLVMAddFunction(gallivm
->module
, "draw_geometry_shader",
2116 variant
->function
= variant_func
;
2118 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2120 for (i
= 0; i
< Elements(arg_types
); ++i
)
2121 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2122 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
2123 LLVMNoAliasAttribute
);
2125 context_ptr
= LLVMGetParam(variant_func
, 0);
2126 input_array
= LLVMGetParam(variant_func
, 1);
2127 io_ptr
= LLVMGetParam(variant_func
, 2);
2128 num_prims
= LLVMGetParam(variant_func
, 3);
2129 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2130 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2132 lp_build_name(context_ptr
, "context");
2133 lp_build_name(input_array
, "input");
2134 lp_build_name(io_ptr
, "io");
2135 lp_build_name(num_prims
, "num_prims");
2136 lp_build_name(system_values
.instance_id
, "instance_id");
2137 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2139 variant
->context_ptr
= context_ptr
;
2140 variant
->io_ptr
= io_ptr
;
2141 variant
->num_prims
= num_prims
;
2143 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2144 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2145 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2146 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2147 gs_iface
.input
= input_array
;
2148 gs_iface
.variant
= variant
;
2154 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2155 builder
= gallivm
->builder
;
2156 LLVMPositionBuilderAtEnd(builder
, block
);
2158 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2160 memset(&gs_type
, 0, sizeof gs_type
);
2161 gs_type
.floating
= TRUE
; /* floating point values */
2162 gs_type
.sign
= TRUE
; /* values are signed */
2163 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2164 gs_type
.width
= 32; /* 32-bit float */
2165 gs_type
.length
= vector_length
;
2167 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2169 /* code generated texture sampling */
2170 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
,
2173 mask_val
= generate_mask_value(variant
, gs_type
);
2174 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2176 if (gs_info
->uses_primid
) {
2177 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");;
2180 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2181 tgsi_dump(tokens
, 0);
2182 draw_gs_llvm_dump_variant_key(&variant
->key
);
2185 lp_build_tgsi_soa(variant
->gallivm
,
2194 &llvm
->draw
->gs
.geometry_shader
->info
,
2195 (const struct lp_build_tgsi_gs_iface
*)&gs_iface
);
2197 sampler
->destroy(sampler
);
2199 lp_build_mask_end(&mask
);
2201 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2203 gallivm_verify_function(gallivm
, variant_func
);
2207 struct draw_gs_llvm_variant
*
2208 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2209 unsigned num_outputs
,
2210 const struct draw_gs_llvm_variant_key
*key
)
2212 struct draw_gs_llvm_variant
*variant
;
2213 struct llvm_geometry_shader
*shader
=
2214 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2215 LLVMTypeRef vertex_header
;
2217 variant
= MALLOC(sizeof *variant
+
2218 shader
->variant_key_size
-
2219 sizeof variant
->key
);
2220 if (variant
== NULL
)
2223 variant
->llvm
= llvm
;
2224 variant
->shader
= shader
;
2226 variant
->gallivm
= gallivm_create();
2228 create_gs_jit_types(variant
);
2230 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2232 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2234 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2236 draw_gs_llvm_generate(llvm
, variant
);
2238 gallivm_compile_module(variant
->gallivm
);
2240 variant
->jit_func
= (draw_gs_jit_func
)
2241 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2243 variant
->list_item_global
.base
= variant
;
2244 variant
->list_item_local
.base
= variant
;
2245 /*variant->no = */shader
->variants_created
++;
2246 variant
->list_item_global
.base
= variant
;
2252 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2254 struct draw_llvm
*llvm
= variant
->llvm
;
2256 if (variant
->function
) {
2257 gallivm_free_function(variant
->gallivm
,
2258 variant
->function
, variant
->jit_func
);
2261 gallivm_destroy(variant
->gallivm
);
2263 remove_from_list(&variant
->list_item_local
);
2264 variant
->shader
->variants_cached
--;
2265 remove_from_list(&variant
->list_item_global
);
2266 llvm
->nr_gs_variants
--;
2270 struct draw_gs_llvm_variant_key
*
2271 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2274 struct draw_gs_llvm_variant_key
*key
;
2275 struct draw_sampler_static_state
*draw_sampler
;
2277 key
= (struct draw_gs_llvm_variant_key
*)store
;
2279 key
->num_outputs
= draw_total_gs_outputs(llvm
->draw
);
2281 /* All variants of this shader will have the same value for
2282 * nr_samplers. Not yet trying to compact away holes in the
2285 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2286 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2287 key
->nr_sampler_views
=
2288 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2291 key
->nr_sampler_views
= key
->nr_samplers
;
2294 draw_sampler
= key
->samplers
;
2296 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2298 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2299 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2300 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2302 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2303 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2304 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2311 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2314 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2316 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2317 debug_printf("sampler[%i].src_format = %s\n", i
,
2318 util_format_name(sampler
[i
].texture_state
.format
));