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];
1264 LLVMValueRef is_nan_or_inf
;
1266 /* userclip planes */
1267 while (ucp_enable
) {
1268 unsigned plane_idx
= ffs(ucp_enable
)-1;
1269 ucp_enable
&= ~(1 << plane_idx
);
1272 if (have_cd
&& num_written_clipdistance
) {
1273 LLVMValueRef clipdist
;
1277 *have_clipdist
= TRUE
;
1279 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1281 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1283 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1284 is_nan_or_inf
= lp_build_is_inf_or_nan(gallivm
, vs_type
, clipdist
);
1285 test
= LLVMBuildOr(builder
, test
, is_nan_or_inf
, "");
1286 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1287 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1288 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1290 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1291 indices
[0] = lp_build_const_int32(gallivm
, 0);
1292 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1294 indices
[2] = lp_build_const_int32(gallivm
, 0);
1295 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1296 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1297 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1298 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1300 indices
[2] = lp_build_const_int32(gallivm
, 1);
1301 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1302 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1303 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1304 test
= LLVMBuildFMul(builder
, planes
, cv_y
, "");
1305 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1307 indices
[2] = lp_build_const_int32(gallivm
, 2);
1308 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1309 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1310 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1311 test
= LLVMBuildFMul(builder
, planes
, cv_z
, "");
1312 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1314 indices
[2] = lp_build_const_int32(gallivm
, 3);
1315 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1316 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1317 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1318 test
= LLVMBuildFMul(builder
, planes
, cv_w
, "");
1319 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1321 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1322 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1323 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1324 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1333 * Returns boolean if any clipping has occurred
1334 * Used zero/non-zero i32 value to represent boolean
1337 clipmask_booli32(struct gallivm_state
*gallivm
,
1338 const struct lp_type vs_type
,
1339 LLVMValueRef clipmask_bool_ptr
)
1341 LLVMBuilderRef builder
= gallivm
->builder
;
1342 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
1343 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1344 LLVMValueRef ret
= LLVMConstNull(int32_type
);
1349 * Can do this with log2(vector length) pack instructions and one extract
1350 * (as we don't actually need a or) with sse2 which would be way better.
1352 for (i
=0; i
< vs_type
.length
; i
++) {
1353 temp
= LLVMBuildExtractElement(builder
, clipmask_bool
,
1354 lp_build_const_int32(gallivm
, i
) , "");
1355 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1361 draw_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
1362 struct lp_build_tgsi_context
* bld_base
,
1363 boolean is_vindex_indirect
,
1364 LLVMValueRef vertex_index
,
1365 boolean is_aindex_indirect
,
1366 LLVMValueRef attrib_index
,
1367 LLVMValueRef swizzle_index
)
1369 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1370 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1371 LLVMBuilderRef builder
= gallivm
->builder
;
1372 LLVMValueRef indices
[3];
1374 struct lp_type type
= bld_base
->base
.type
;
1376 if (is_vindex_indirect
|| is_aindex_indirect
) {
1378 res
= bld_base
->base
.zero
;
1379 for (i
= 0; i
< type
.length
; ++i
) {
1380 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1381 LLVMValueRef vert_chan_index
= vertex_index
;
1382 LLVMValueRef attr_chan_index
= attrib_index
;
1383 LLVMValueRef channel_vec
, value
;
1385 if (is_vindex_indirect
) {
1386 vert_chan_index
= LLVMBuildExtractElement(builder
,
1387 vertex_index
, idx
, "");
1389 if (is_aindex_indirect
) {
1390 attr_chan_index
= LLVMBuildExtractElement(builder
,
1391 attrib_index
, idx
, "");
1394 indices
[0] = vert_chan_index
;
1395 indices
[1] = attr_chan_index
;
1396 indices
[2] = swizzle_index
;
1398 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1399 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1400 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1402 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1405 indices
[0] = vertex_index
;
1406 indices
[1] = attrib_index
;
1407 indices
[2] = swizzle_index
;
1409 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1410 res
= LLVMBuildLoad(builder
, res
, "");
1417 draw_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
1418 struct lp_build_tgsi_context
* bld_base
,
1419 LLVMValueRef (*outputs
)[4],
1420 LLVMValueRef emitted_vertices_vec
)
1422 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1423 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1424 struct gallivm_state
*gallivm
= variant
->gallivm
;
1425 LLVMBuilderRef builder
= gallivm
->builder
;
1426 struct lp_type gs_type
= bld_base
->base
.type
;
1427 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1428 lp_int_type(gs_type
), 0);
1429 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1430 LLVMValueRef next_prim_offset
=
1431 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1432 LLVMValueRef io
= variant
->io_ptr
;
1434 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1436 for (i
= 0; i
< gs_type
.length
; ++i
) {
1437 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1438 LLVMValueRef currently_emitted
=
1439 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1440 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1441 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1444 convert_to_aos(gallivm
, io
, indices
,
1446 gs_info
->num_outputs
, gs_type
,
1451 draw_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
1452 struct lp_build_tgsi_context
* bld_base
,
1453 LLVMValueRef verts_per_prim_vec
,
1454 LLVMValueRef emitted_prims_vec
)
1456 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1457 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1458 struct gallivm_state
*gallivm
= variant
->gallivm
;
1459 LLVMBuilderRef builder
= gallivm
->builder
;
1460 LLVMValueRef prim_lengts_ptr
=
1461 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1464 for (i
= 0; i
< bld_base
->base
.type
.length
; ++i
) {
1465 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1466 LLVMValueRef prims_emitted
=
1467 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1468 LLVMValueRef store_ptr
;
1469 LLVMValueRef num_vertices
=
1470 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1472 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1473 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1474 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1475 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1480 draw_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
1481 struct lp_build_tgsi_context
* bld_base
,
1482 LLVMValueRef total_emitted_vertices_vec
,
1483 LLVMValueRef emitted_prims_vec
)
1485 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1486 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1487 struct gallivm_state
*gallivm
= variant
->gallivm
;
1488 LLVMBuilderRef builder
= gallivm
->builder
;
1489 LLVMValueRef emitted_verts_ptr
=
1490 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1491 LLVMValueRef emitted_prims_ptr
=
1492 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1493 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1495 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &zero
, 0, "");
1496 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &zero
, 0, "");
1498 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1499 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1503 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1506 struct gallivm_state
*gallivm
= variant
->gallivm
;
1507 LLVMContextRef context
= gallivm
->context
;
1508 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1509 LLVMTypeRef arg_types
[10];
1510 unsigned num_arg_types
=
1511 elts
? Elements(arg_types
) : Elements(arg_types
) - 1;
1512 LLVMTypeRef func_type
;
1513 LLVMValueRef context_ptr
;
1514 LLVMBasicBlockRef block
;
1515 LLVMBuilderRef builder
;
1516 struct lp_type vs_type
;
1517 LLVMValueRef end
, start
;
1518 LLVMValueRef count
, fetch_elts
, fetch_elt_max
, fetch_count
;
1519 LLVMValueRef vertex_id_offset
;
1520 LLVMValueRef stride
, step
, io_itr
;
1521 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1522 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1523 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1524 struct draw_context
*draw
= llvm
->draw
;
1525 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1527 struct lp_build_context bld
;
1528 struct lp_build_loop_state lp_loop
;
1529 const int vector_length
= lp_native_vector_width
/ 32;
1530 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1531 LLVMValueRef fetch_max
;
1532 struct lp_build_sampler_soa
*sampler
= 0;
1533 LLVMValueRef ret
, clipmask_bool_ptr
;
1534 const struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
1535 struct draw_llvm_variant_key
*key
= &variant
->key
;
1536 /* If geometry shader is present we need to skip both the viewport
1537 * transformation and clipping otherwise the inputs to the geometry
1538 * shader will be incorrect.
1540 const boolean bypass_viewport
= gs
|| key
->bypass_viewport
;
1541 const boolean enable_cliptest
= !gs
&& (key
->clip_xy
||
1544 LLVMValueRef variant_func
;
1545 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1546 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1547 boolean have_clipdist
= FALSE
;
1548 struct lp_bld_tgsi_system_values system_values
;
1550 memset(&system_values
, 0, sizeof(system_values
));
1553 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1554 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1555 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1557 arg_types
[i
++] = LLVMPointerType(int32_type
, 0);/* fetch_elts */
1558 arg_types
[i
++] = int32_type
; /* fetch_elt_max */
1560 arg_types
[i
++] = int32_type
; /* start */
1561 arg_types
[i
++] = int32_type
; /* fetch_count / count */
1562 arg_types
[i
++] = int32_type
; /* stride */
1563 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1564 arg_types
[i
++] = int32_type
; /* instance_id */
1565 arg_types
[i
++] = int32_type
; /* vertex_id_offset */
1567 func_type
= LLVMFunctionType(int32_type
, arg_types
, num_arg_types
, 0);
1569 variant_func
= LLVMAddFunction(gallivm
->module
,
1570 elts
? "draw_llvm_shader_elts" : "draw_llvm_shader",
1574 variant
->function_elts
= variant_func
;
1576 variant
->function
= variant_func
;
1578 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1579 for (i
= 0; i
< num_arg_types
; ++i
)
1580 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1581 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1582 LLVMNoAliasAttribute
);
1584 context_ptr
= LLVMGetParam(variant_func
, 0);
1585 io_ptr
= LLVMGetParam(variant_func
, 1);
1586 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1587 stride
= LLVMGetParam(variant_func
, 5 + (elts
? 1 : 0));
1588 vb_ptr
= LLVMGetParam(variant_func
, 6 + (elts
? 1 : 0));
1589 system_values
.instance_id
= LLVMGetParam(variant_func
, 7 + (elts
? 1 : 0));
1590 vertex_id_offset
= LLVMGetParam(variant_func
, 8 + (elts
? 1 : 0));
1592 lp_build_name(context_ptr
, "context");
1593 lp_build_name(io_ptr
, "io");
1594 lp_build_name(vbuffers_ptr
, "vbuffers");
1595 lp_build_name(stride
, "stride");
1596 lp_build_name(vb_ptr
, "vb");
1597 lp_build_name(system_values
.instance_id
, "instance_id");
1598 lp_build_name(vertex_id_offset
, "vertex_id_offset");
1601 fetch_elts
= LLVMGetParam(variant_func
, 3);
1602 fetch_elt_max
= LLVMGetParam(variant_func
, 4);
1603 fetch_count
= LLVMGetParam(variant_func
, 5);
1604 lp_build_name(fetch_elts
, "fetch_elts");
1605 lp_build_name(fetch_elt_max
, "fetch_elt_max");
1606 lp_build_name(fetch_count
, "fetch_count");
1607 start
= count
= NULL
;
1610 start
= LLVMGetParam(variant_func
, 3);
1611 count
= LLVMGetParam(variant_func
, 4);
1612 lp_build_name(start
, "start");
1613 lp_build_name(count
, "count");
1614 fetch_elts
= fetch_count
= NULL
;
1621 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1622 builder
= gallivm
->builder
;
1623 LLVMPositionBuilderAtEnd(builder
, block
);
1625 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1627 memset(&vs_type
, 0, sizeof vs_type
);
1628 vs_type
.floating
= TRUE
; /* floating point values */
1629 vs_type
.sign
= TRUE
; /* values are signed */
1630 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1631 vs_type
.width
= 32; /* 32-bit float */
1632 vs_type
.length
= vector_length
;
1634 /* hold temporary "bool" clipmask */
1635 clipmask_bool_ptr
= lp_build_alloca(gallivm
, lp_build_int_vec_type(gallivm
, vs_type
), "");
1636 LLVMBuildStore(builder
, lp_build_zero(gallivm
, lp_int_type(vs_type
)), clipmask_bool_ptr
);
1638 /* code generated texture sampling */
1639 sampler
= draw_llvm_sampler_soa_create(
1640 draw_llvm_variant_key_samplers(key
),
1646 count
= fetch_count
;
1649 end
= lp_build_add(&bld
, start
, count
);
1652 step
= lp_build_const_int32(gallivm
, vector_length
);
1654 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1656 lp_build_loop_begin(&lp_loop
, gallivm
, zero
);
1658 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1659 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][LP_MAX_VECTOR_WIDTH
/ 32] = { { 0 } };
1661 LLVMValueRef clipmask
; /* holds the clipmask value */
1662 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1664 io_itr
= lp_loop
.counter
;
1666 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1668 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1669 io_itr
, io
, lp_loop
.counter
);
1671 system_values
.vertex_id
= lp_build_zero(gallivm
, lp_type_uint_vec(32, 32*vector_length
));
1672 for (i
= 0; i
< vector_length
; ++i
) {
1673 LLVMValueRef vert_index
=
1674 LLVMBuildAdd(builder
,
1676 lp_build_const_int32(gallivm
, i
), "");
1677 LLVMValueRef true_index
=
1678 LLVMBuildAdd(builder
, start
, vert_index
, "");
1679 LLVMValueRef vertex_id
;
1681 /* make sure we're not out of bounds which can happen
1682 * if fetch_count % 4 != 0, because on the last iteration
1683 * a few of the 4 vertex fetches will be out of bounds */
1684 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1687 LLVMValueRef fetch_ptr
;
1688 LLVMValueRef index_overflowed
;
1689 LLVMValueRef index_ptr
=
1692 lp_build_vec_type(gallivm
, lp_type_int(32)), "");
1693 struct lp_build_if_state if_ctx
;
1694 index_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
1695 true_index
, fetch_elt_max
,
1696 "index_overflowed");
1698 lp_build_if(&if_ctx
, gallivm
, index_overflowed
);
1700 /* Generate maximum possible index so that
1701 * generate_fetch can treat it just like
1702 * any other overflow and return zeros.
1703 * We don't have to worry about the restart
1704 * primitive index because it has already been
1708 lp_build_const_int32(gallivm
, 0xffffffff);
1709 LLVMBuildStore(builder
, val
, index_ptr
);
1711 lp_build_else(&if_ctx
);
1714 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1715 &true_index
, 1, "");
1716 val
= LLVMBuildLoad(builder
, fetch_ptr
, "");
1717 LLVMBuildStore(builder
, val
, index_ptr
);
1719 lp_build_endif(&if_ctx
);
1720 true_index
= LLVMBuildLoad(builder
, index_ptr
, "true_index");
1722 /* in the paths with elts vertex id has to be unaffected by the
1723 * index bias and because indices inside our elements array have
1724 * already had index bias applied we need to subtract it here to
1725 * get back to the original index.
1726 * in the linear paths vertex id has to be unaffected by the
1727 * original start index and because we abuse the 'start' variable
1728 * to either represent the actual start index or the index at which
1729 * the primitive was split (we split rendering into chunks of at
1730 * most 4095-vertices) we need to back out the original start
1731 * index out of our vertex id here.
1733 vertex_id
= LLVMBuildSub(builder
, true_index
, vertex_id_offset
, "");
1735 system_values
.vertex_id
= LLVMBuildInsertElement(
1737 system_values
.vertex_id
, vertex_id
,
1738 lp_build_const_int32(gallivm
, i
), "");
1740 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1741 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1742 LLVMValueRef vb_index
=
1743 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1744 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1745 generate_fetch(gallivm
, draw
, vbuffers_ptr
,
1746 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1747 system_values
.instance_id
);
1750 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1751 draw
->pt
.nr_vertex_elements
, vs_type
);
1753 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1754 generate_vs(variant
,
1762 key
->clamp_vertex_color
);
1764 if (pos
!= -1 && cv
!= -1) {
1765 /* store original positions in clip before further manipulation */
1766 store_clip(gallivm
, vs_type
, io
, outputs
, 0, cv
);
1767 store_clip(gallivm
, vs_type
, io
, outputs
, 1, pos
);
1770 if (enable_cliptest
) {
1771 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1772 /* allocate clipmask, assign it integer type */
1773 clipmask
= generate_clipmask(llvm
,
1782 context_ptr
, &have_clipdist
);
1783 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1784 /* store temporary clipping boolean value */
1785 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1788 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1791 /* do viewport mapping */
1792 if (!bypass_viewport
) {
1793 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1797 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1800 /* store clipmask in vertex header,
1801 * original positions in clip
1802 * and transformed positions in data
1804 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
1805 vs_info
->num_outputs
, vs_type
,
1808 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
1810 sampler
->destroy(sampler
);
1812 /* return clipping boolean value for function */
1813 ret
= clipmask_booli32(gallivm
, vs_type
, clipmask_bool_ptr
);
1815 LLVMBuildRet(builder
, ret
);
1817 gallivm_verify_function(gallivm
, variant_func
);
1821 struct draw_llvm_variant_key
*
1822 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1825 struct draw_llvm_variant_key
*key
;
1826 struct draw_sampler_static_state
*draw_sampler
;
1828 key
= (struct draw_llvm_variant_key
*)store
;
1830 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1832 /* Presumably all variants of the shader should have the same
1833 * number of vertex elements - ie the number of shader inputs.
1834 * NOTE: we NEED to store the needed number of needed inputs
1835 * here, not the number of provided elements to match keysize
1836 * (and the offset of sampler state in the key).
1838 key
->nr_vertex_elements
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
1839 assert(key
->nr_vertex_elements
<= llvm
->draw
->pt
.nr_vertex_elements
);
1841 /* will have to rig this up properly later */
1842 key
->clip_xy
= llvm
->draw
->clip_xy
;
1843 key
->clip_z
= llvm
->draw
->clip_z
;
1844 key
->clip_user
= llvm
->draw
->clip_user
;
1845 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1846 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
1847 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1848 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1849 key
->has_gs
= llvm
->draw
->gs
.geometry_shader
!= NULL
;
1850 key
->num_outputs
= draw_total_vs_outputs(llvm
->draw
);
1853 /* All variants of this shader will have the same value for
1854 * nr_samplers. Not yet trying to compact away holes in the
1857 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1858 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
1859 key
->nr_sampler_views
=
1860 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
1863 key
->nr_sampler_views
= key
->nr_samplers
;
1866 draw_sampler
= draw_llvm_variant_key_samplers(key
);
1868 memcpy(key
->vertex_element
,
1869 llvm
->draw
->pt
.vertex_element
,
1870 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1872 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
1874 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1875 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
1876 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
1878 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1879 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
1880 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
1888 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
1891 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
1893 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
1894 debug_printf("clip_xy = %u\n", key
->clip_xy
);
1895 debug_printf("clip_z = %u\n", key
->clip_z
);
1896 debug_printf("clip_user = %u\n", key
->clip_user
);
1897 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
1898 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
1899 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
1900 debug_printf("has_gs = %u\n", key
->has_gs
);
1901 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
1903 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
1904 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
1905 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
1906 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
1907 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
1910 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1911 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
1917 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1918 unsigned shader_stage
,
1920 uint32_t width
, uint32_t height
, uint32_t depth
,
1921 uint32_t first_level
, uint32_t last_level
,
1922 const void *base_ptr
,
1923 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1924 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1925 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
1928 struct draw_jit_texture
*jit_tex
;
1930 assert(shader_stage
== PIPE_SHADER_VERTEX
||
1931 shader_stage
== PIPE_SHADER_GEOMETRY
);
1933 if (shader_stage
== PIPE_SHADER_VERTEX
) {
1934 assert(sview_idx
< Elements(draw
->llvm
->jit_context
.textures
));
1936 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
1937 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
1938 assert(sview_idx
< Elements(draw
->llvm
->gs_jit_context
.textures
));
1940 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
1946 jit_tex
->width
= width
;
1947 jit_tex
->height
= height
;
1948 jit_tex
->depth
= depth
;
1949 jit_tex
->first_level
= first_level
;
1950 jit_tex
->last_level
= last_level
;
1951 jit_tex
->base
= base_ptr
;
1953 for (j
= first_level
; j
<= last_level
; j
++) {
1954 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
1955 jit_tex
->row_stride
[j
] = row_stride
[j
];
1956 jit_tex
->img_stride
[j
] = img_stride
[j
];
1962 draw_llvm_set_sampler_state(struct draw_context
*draw
,
1963 unsigned shader_type
)
1967 if (shader_type
== PIPE_SHADER_VERTEX
) {
1968 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
1969 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
1971 if (draw
->samplers
[i
]) {
1972 const struct pipe_sampler_state
*s
1973 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
1974 jit_sam
->min_lod
= s
->min_lod
;
1975 jit_sam
->max_lod
= s
->max_lod
;
1976 jit_sam
->lod_bias
= s
->lod_bias
;
1977 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1980 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
1981 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
1982 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
1984 if (draw
->samplers
[i
]) {
1985 const struct pipe_sampler_state
*s
1986 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
1987 jit_sam
->min_lod
= s
->min_lod
;
1988 jit_sam
->max_lod
= s
->max_lod
;
1989 jit_sam
->lod_bias
= s
->lod_bias
;
1990 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1998 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
2000 struct draw_llvm
*llvm
= variant
->llvm
;
2002 if (variant
->function_elts
) {
2003 gallivm_free_function(variant
->gallivm
,
2004 variant
->function_elts
, variant
->jit_func_elts
);
2007 if (variant
->function
) {
2008 gallivm_free_function(variant
->gallivm
,
2009 variant
->function
, variant
->jit_func
);
2012 gallivm_destroy(variant
->gallivm
);
2014 remove_from_list(&variant
->list_item_local
);
2015 variant
->shader
->variants_cached
--;
2016 remove_from_list(&variant
->list_item_global
);
2017 llvm
->nr_variants
--;
2023 * Create LLVM types for various structures.
2026 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
2028 struct gallivm_state
*gallivm
= var
->gallivm
;
2029 LLVMTypeRef texture_type
, sampler_type
, context_type
;
2031 texture_type
= create_jit_texture_type(gallivm
, "texture");
2032 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2034 context_type
= create_gs_jit_context_type(gallivm
,
2035 var
->shader
->base
.vector_length
,
2036 texture_type
, sampler_type
,
2037 "draw_gs_jit_context");
2038 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2040 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
2044 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
2046 if (!variant
->context_ptr_type
)
2047 create_gs_jit_types(variant
);
2048 return variant
->context_ptr_type
;
2052 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2053 struct lp_type gs_type
)
2055 struct gallivm_state
*gallivm
= variant
->gallivm
;
2056 LLVMBuilderRef builder
= gallivm
->builder
;
2057 struct lp_type mask_type
= lp_int_type(gs_type
);
2058 LLVMValueRef num_prims
;
2059 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
2062 num_prims
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
),
2063 variant
->num_prims
);
2064 for (i
= 0; i
<= gs_type
.length
; i
++) {
2065 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
2066 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, idx
, idx
, "");
2068 mask_val
= lp_build_compare(gallivm
, mask_type
,
2069 PIPE_FUNC_GREATER
, num_prims
, mask_val
);
2075 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2076 struct draw_gs_llvm_variant
*variant
)
2078 struct gallivm_state
*gallivm
= variant
->gallivm
;
2079 LLVMContextRef context
= gallivm
->context
;
2080 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2081 LLVMTypeRef arg_types
[6];
2082 LLVMTypeRef func_type
;
2083 LLVMValueRef variant_func
;
2084 LLVMValueRef context_ptr
;
2085 LLVMValueRef prim_id_ptr
;
2086 LLVMBasicBlockRef block
;
2087 LLVMBuilderRef builder
;
2088 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2089 struct lp_build_sampler_soa
*sampler
= 0;
2090 struct lp_build_context bld
;
2091 struct lp_bld_tgsi_system_values system_values
;
2092 struct lp_type gs_type
;
2094 struct draw_gs_llvm_iface gs_iface
;
2095 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2096 LLVMValueRef consts_ptr
;
2097 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2098 struct lp_build_mask_context mask
;
2099 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2100 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2102 memset(&system_values
, 0, sizeof(system_values
));
2104 assert(variant
->vertex_header_ptr_type
);
2106 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2107 arg_types
[1] = variant
->input_array_type
; /* input */
2108 arg_types
[2] = variant
->vertex_header_ptr_type
; /* vertex_header */
2109 arg_types
[3] = int32_type
; /* num_prims */
2110 arg_types
[4] = int32_type
; /* instance_id */
2111 arg_types
[5] = LLVMPointerType(
2112 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2114 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
2116 variant_func
= LLVMAddFunction(gallivm
->module
, "draw_geometry_shader",
2118 variant
->function
= variant_func
;
2120 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2122 for (i
= 0; i
< Elements(arg_types
); ++i
)
2123 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2124 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
2125 LLVMNoAliasAttribute
);
2127 context_ptr
= LLVMGetParam(variant_func
, 0);
2128 input_array
= LLVMGetParam(variant_func
, 1);
2129 io_ptr
= LLVMGetParam(variant_func
, 2);
2130 num_prims
= LLVMGetParam(variant_func
, 3);
2131 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2132 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2134 lp_build_name(context_ptr
, "context");
2135 lp_build_name(input_array
, "input");
2136 lp_build_name(io_ptr
, "io");
2137 lp_build_name(num_prims
, "num_prims");
2138 lp_build_name(system_values
.instance_id
, "instance_id");
2139 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2141 variant
->context_ptr
= context_ptr
;
2142 variant
->io_ptr
= io_ptr
;
2143 variant
->num_prims
= num_prims
;
2145 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2146 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2147 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2148 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2149 gs_iface
.input
= input_array
;
2150 gs_iface
.variant
= variant
;
2156 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2157 builder
= gallivm
->builder
;
2158 LLVMPositionBuilderAtEnd(builder
, block
);
2160 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2162 memset(&gs_type
, 0, sizeof gs_type
);
2163 gs_type
.floating
= TRUE
; /* floating point values */
2164 gs_type
.sign
= TRUE
; /* values are signed */
2165 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2166 gs_type
.width
= 32; /* 32-bit float */
2167 gs_type
.length
= vector_length
;
2169 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2171 /* code generated texture sampling */
2172 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
,
2175 mask_val
= generate_mask_value(variant
, gs_type
);
2176 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2178 if (gs_info
->uses_primid
) {
2179 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");;
2182 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2183 tgsi_dump(tokens
, 0);
2184 draw_gs_llvm_dump_variant_key(&variant
->key
);
2187 lp_build_tgsi_soa(variant
->gallivm
,
2196 &llvm
->draw
->gs
.geometry_shader
->info
,
2197 (const struct lp_build_tgsi_gs_iface
*)&gs_iface
);
2199 sampler
->destroy(sampler
);
2201 lp_build_mask_end(&mask
);
2203 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2205 gallivm_verify_function(gallivm
, variant_func
);
2209 struct draw_gs_llvm_variant
*
2210 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2211 unsigned num_outputs
,
2212 const struct draw_gs_llvm_variant_key
*key
)
2214 struct draw_gs_llvm_variant
*variant
;
2215 struct llvm_geometry_shader
*shader
=
2216 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2217 LLVMTypeRef vertex_header
;
2219 variant
= MALLOC(sizeof *variant
+
2220 shader
->variant_key_size
-
2221 sizeof variant
->key
);
2222 if (variant
== NULL
)
2225 variant
->llvm
= llvm
;
2226 variant
->shader
= shader
;
2228 variant
->gallivm
= gallivm_create();
2230 create_gs_jit_types(variant
);
2232 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2234 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2236 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2238 draw_gs_llvm_generate(llvm
, variant
);
2240 gallivm_compile_module(variant
->gallivm
);
2242 variant
->jit_func
= (draw_gs_jit_func
)
2243 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2245 variant
->list_item_global
.base
= variant
;
2246 variant
->list_item_local
.base
= variant
;
2247 /*variant->no = */shader
->variants_created
++;
2248 variant
->list_item_global
.base
= variant
;
2254 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2256 struct draw_llvm
*llvm
= variant
->llvm
;
2258 if (variant
->function
) {
2259 gallivm_free_function(variant
->gallivm
,
2260 variant
->function
, variant
->jit_func
);
2263 gallivm_destroy(variant
->gallivm
);
2265 remove_from_list(&variant
->list_item_local
);
2266 variant
->shader
->variants_cached
--;
2267 remove_from_list(&variant
->list_item_global
);
2268 llvm
->nr_gs_variants
--;
2272 struct draw_gs_llvm_variant_key
*
2273 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2276 struct draw_gs_llvm_variant_key
*key
;
2277 struct draw_sampler_static_state
*draw_sampler
;
2279 key
= (struct draw_gs_llvm_variant_key
*)store
;
2281 key
->num_outputs
= draw_total_gs_outputs(llvm
->draw
);
2283 /* All variants of this shader will have the same value for
2284 * nr_samplers. Not yet trying to compact away holes in the
2287 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2288 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2289 key
->nr_sampler_views
=
2290 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2293 key
->nr_sampler_views
= key
->nr_samplers
;
2296 draw_sampler
= key
->samplers
;
2298 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2300 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2301 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2302 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2304 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2305 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2306 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2313 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2316 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2318 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2319 debug_printf("sampler[%i].src_format = %s\n", i
,
2320 util_format_name(sampler
[i
].texture_state
.format
));