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 int_type
= LLVMInt32TypeInContext(gallivm
->context
);
246 LLVMTypeRef elem_types
[DRAW_JIT_CTX_NUM_FIELDS
];
247 LLVMTypeRef context_type
;
249 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
250 LP_MAX_TGSI_CONST_BUFFERS
);
251 elem_types
[1] = LLVMArrayType(int_type
, /* num_vs_constants */
252 LP_MAX_TGSI_CONST_BUFFERS
);
253 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
254 DRAW_TOTAL_CLIP_PLANES
), 0);
255 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewport */
256 elem_types
[4] = LLVMArrayType(texture_type
,
257 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
258 elem_types
[5] = LLVMArrayType(sampler_type
,
259 PIPE_MAX_SAMPLERS
); /* samplers */
260 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
261 Elements(elem_types
), 0);
262 #if HAVE_LLVM < 0x0300
263 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
265 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
268 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
269 target
, context_type
, DRAW_JIT_CTX_CONSTANTS
);
270 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, num_vs_constants
,
271 target
, context_type
, DRAW_JIT_CTX_NUM_CONSTANTS
);
272 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
273 target
, context_type
, DRAW_JIT_CTX_PLANES
);
274 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewport
,
275 target
, context_type
, DRAW_JIT_CTX_VIEWPORT
);
276 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
277 target
, context_type
,
278 DRAW_JIT_CTX_TEXTURES
);
279 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
280 target
, context_type
,
281 DRAW_JIT_CTX_SAMPLERS
);
282 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
283 target
, context_type
);
290 * Create LLVM type for struct draw_gs_jit_context
293 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
294 unsigned vector_length
,
295 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
296 const char *struct_name
)
298 LLVMTargetDataRef target
= gallivm
->target
;
299 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
300 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
301 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
302 LLVMTypeRef context_type
;
304 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
305 LP_MAX_TGSI_CONST_BUFFERS
);
306 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
307 LP_MAX_TGSI_CONST_BUFFERS
);
308 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
309 DRAW_TOTAL_CLIP_PLANES
), 0);
310 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewport */
312 elem_types
[4] = LLVMArrayType(texture_type
,
313 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
314 elem_types
[5] = LLVMArrayType(sampler_type
,
315 PIPE_MAX_SAMPLERS
); /* samplers */
317 elem_types
[6] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
318 elem_types
[7] = LLVMPointerType(LLVMVectorType(int_type
,
320 elem_types
[8] = LLVMPointerType(LLVMVectorType(int_type
,
323 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
324 Elements(elem_types
), 0);
325 #if HAVE_LLVM < 0x0300
326 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
328 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
331 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
332 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
333 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, num_constants
,
334 target
, context_type
, DRAW_GS_JIT_CTX_NUM_CONSTANTS
);
335 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
336 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
337 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewport
,
338 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
339 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
340 target
, context_type
,
341 DRAW_GS_JIT_CTX_TEXTURES
);
342 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
343 target
, context_type
,
344 DRAW_GS_JIT_CTX_SAMPLERS
);
345 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
346 target
, context_type
,
347 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
348 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
349 target
, context_type
,
350 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
351 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
352 target
, context_type
,
353 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
354 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
355 target
, context_type
);
362 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
364 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
365 LLVMTypeRef input_array
;
367 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
368 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
369 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
370 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
376 * Create LLVM type for struct pipe_vertex_buffer
379 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
,
380 const char *struct_name
)
382 LLVMTargetDataRef target
= gallivm
->target
;
383 LLVMTypeRef elem_types
[4];
387 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
389 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
391 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
392 Elements(elem_types
), 0);
393 #if HAVE_LLVM < 0x0300
394 LLVMAddTypeName(gallivm
->module
, struct_name
, vb_type
);
396 LLVMInvalidateStructLayout(gallivm
->target
, vb_type
);
399 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
401 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
404 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
411 * Create LLVM type for struct vertex_header;
414 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
416 LLVMTargetDataRef target
= gallivm
->target
;
417 LLVMTypeRef elem_types
[4];
418 LLVMTypeRef vertex_header
;
419 char struct_name
[24];
421 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
423 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
424 elem_types
[DRAW_JIT_VERTEX_CLIP
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
425 elem_types
[DRAW_JIT_VERTEX_PRE_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
426 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
428 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
429 Elements(elem_types
), 0);
430 #if HAVE_LLVM < 0x0300
431 LLVMAddTypeName(gallivm
->module
, struct_name
, vertex_header
);
433 LLVMInvalidateStructLayout(gallivm
->target
, vertex_header
);
436 /* these are bit-fields and we can't take address of them
437 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
438 target, vertex_header,
439 DRAW_JIT_VERTEX_CLIPMASK);
440 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
441 target, vertex_header,
442 DRAW_JIT_VERTEX_EDGEFLAG);
443 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
444 target, vertex_header,
445 DRAW_JIT_VERTEX_PAD);
446 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
447 target, vertex_header,
448 DRAW_JIT_VERTEX_VERTEX_ID);
450 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
451 target
, vertex_header
,
452 DRAW_JIT_VERTEX_CLIP
);
453 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, pre_clip_pos
,
454 target
, vertex_header
,
455 DRAW_JIT_VERTEX_PRE_CLIP_POS
);
456 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
457 target
, vertex_header
,
458 DRAW_JIT_VERTEX_DATA
);
460 assert(LLVMABISizeOfType(target
, vertex_header
) ==
461 offsetof(struct vertex_header
, data
[data_elems
]));
463 return vertex_header
;
468 * Create LLVM types for various structures.
471 create_jit_types(struct draw_llvm_variant
*variant
)
473 struct gallivm_state
*gallivm
= variant
->gallivm
;
474 LLVMTypeRef texture_type
, sampler_type
, context_type
, buffer_type
,
477 texture_type
= create_jit_texture_type(gallivm
, "texture");
478 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
480 context_type
= create_jit_context_type(gallivm
, texture_type
, sampler_type
,
482 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
484 buffer_type
= create_jit_dvbuffer_type(gallivm
, "draw_vertex_buffer");
485 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
487 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
488 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
493 get_context_ptr_type(struct draw_llvm_variant
*variant
)
495 if (!variant
->context_ptr_type
)
496 create_jit_types(variant
);
497 return variant
->context_ptr_type
;
502 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
504 if (!variant
->buffer_ptr_type
)
505 create_jit_types(variant
);
506 return variant
->buffer_ptr_type
;
511 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
513 if (!variant
->vb_ptr_type
)
514 create_jit_types(variant
);
515 return variant
->vb_ptr_type
;
519 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
521 if (!variant
->vertex_header_ptr_type
)
522 create_jit_types(variant
);
523 return variant
->vertex_header_ptr_type
;
528 * Create per-context LLVM info.
531 draw_llvm_create(struct draw_context
*draw
)
533 struct draw_llvm
*llvm
;
535 llvm
= CALLOC_STRUCT( draw_llvm
);
543 llvm
->nr_variants
= 0;
544 make_empty_list(&llvm
->vs_variants_list
);
546 llvm
->nr_gs_variants
= 0;
547 make_empty_list(&llvm
->gs_variants_list
);
554 * Free per-context LLVM info.
557 draw_llvm_destroy(struct draw_llvm
*llvm
)
559 /* XXX free other draw_llvm data? */
565 * Create LLVM-generated code for a vertex shader.
567 struct draw_llvm_variant
*
568 draw_llvm_create_variant(struct draw_llvm
*llvm
,
570 const struct draw_llvm_variant_key
*key
)
572 struct draw_llvm_variant
*variant
;
573 struct llvm_vertex_shader
*shader
=
574 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
575 LLVMTypeRef vertex_header
;
577 variant
= MALLOC(sizeof *variant
+
578 shader
->variant_key_size
-
579 sizeof variant
->key
);
583 variant
->llvm
= llvm
;
585 variant
->gallivm
= gallivm_create();
587 create_jit_types(variant
);
589 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
591 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
593 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
595 draw_llvm_generate(llvm
, variant
, FALSE
); /* linear */
596 draw_llvm_generate(llvm
, variant
, TRUE
); /* elts */
598 gallivm_compile_module(variant
->gallivm
);
600 variant
->jit_func
= (draw_jit_vert_func
)
601 gallivm_jit_function(variant
->gallivm
, variant
->function
);
603 variant
->jit_func_elts
= (draw_jit_vert_func_elts
)
604 gallivm_jit_function(variant
->gallivm
, variant
->function_elts
);
606 variant
->shader
= shader
;
607 variant
->list_item_global
.base
= variant
;
608 variant
->list_item_local
.base
= variant
;
609 /*variant->no = */shader
->variants_created
++;
610 variant
->list_item_global
.base
= variant
;
617 generate_vs(struct draw_llvm_variant
*variant
,
618 LLVMBuilderRef builder
,
619 struct lp_type vs_type
,
620 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
621 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
622 const struct lp_bld_tgsi_system_values
*system_values
,
623 LLVMValueRef context_ptr
,
624 struct lp_build_sampler_soa
*draw_sampler
,
625 boolean clamp_vertex_color
)
627 struct draw_llvm
*llvm
= variant
->llvm
;
628 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
629 LLVMValueRef consts_ptr
=
630 draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
631 LLVMValueRef num_consts_ptr
=
632 draw_jit_context_num_vs_constants(variant
->gallivm
, context_ptr
);
633 struct lp_build_sampler_soa
*sampler
= 0;
635 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
636 tgsi_dump(tokens
, 0);
637 draw_llvm_dump_variant_key(&variant
->key
);
640 if (llvm
->draw
->num_sampler_views
&& llvm
->draw
->num_samplers
)
641 sampler
= draw_sampler
;
643 lp_build_tgsi_soa(variant
->gallivm
,
646 NULL
/*struct lp_build_mask_context *mask*/,
653 &llvm
->draw
->vs
.vertex_shader
->info
,
658 unsigned chan
, attrib
;
659 struct lp_build_context bld
;
660 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
661 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
663 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
664 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
665 if (outputs
[attrib
][chan
]) {
666 switch (info
->output_semantic_name
[attrib
]) {
667 case TGSI_SEMANTIC_COLOR
:
668 case TGSI_SEMANTIC_BCOLOR
:
669 if (clamp_vertex_color
) {
670 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
671 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
672 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
683 generate_fetch(struct gallivm_state
*gallivm
,
684 struct draw_context
*draw
,
685 LLVMValueRef vbuffers_ptr
,
687 struct pipe_vertex_element
*velem
,
690 LLVMValueRef instance_id
)
692 const struct util_format_description
*format_desc
=
693 util_format_description(velem
->src_format
);
694 LLVMValueRef zero
= LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
));
695 LLVMBuilderRef builder
= gallivm
->builder
;
696 LLVMValueRef indices
=
697 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
698 velem
->vertex_buffer_index
, 0);
699 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
701 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
702 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
703 LLVMValueRef map_ptr
= draw_jit_dvbuffer_map(gallivm
, vbuffer_ptr
);
704 LLVMValueRef buffer_size
= draw_jit_dvbuffer_size(gallivm
, vbuffer_ptr
);
706 LLVMValueRef buffer_overflowed
;
707 LLVMValueRef needed_buffer_size
;
708 LLVMValueRef temp_ptr
=
709 lp_build_alloca(gallivm
,
710 lp_build_vec_type(gallivm
, lp_float32_vec4_type()), "");
711 LLVMValueRef ofbit
= NULL
;
712 struct lp_build_if_state if_ctx
;
714 if (velem
->instance_divisor
) {
715 /* Index is equal to the start instance plus the number of current
716 * instance divided by the divisor. In this case we compute it as:
717 * index = start_instance + (instance_id / divisor)
719 LLVMValueRef current_instance
;
720 index
= lp_build_const_int32(gallivm
, draw
->start_instance
);
721 current_instance
= LLVMBuildUDiv(builder
, instance_id
,
722 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
724 index
= lp_build_uadd_overflow(gallivm
, index
, current_instance
, &ofbit
);
727 stride
= lp_build_umul_overflow(gallivm
, vb_stride
, index
, &ofbit
);
728 stride
= lp_build_uadd_overflow(gallivm
, stride
, vb_buffer_offset
, &ofbit
);
729 stride
= lp_build_uadd_overflow(
731 lp_build_const_int32(gallivm
, velem
->src_offset
), &ofbit
);
732 needed_buffer_size
= lp_build_uadd_overflow(
734 lp_build_const_int32(gallivm
,
735 util_format_get_blocksize(velem
->src_format
)),
738 buffer_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
739 needed_buffer_size
, buffer_size
,
740 "buffer_overflowed");
741 buffer_overflowed
= LLVMBuildOr(builder
, buffer_overflowed
, ofbit
, "");
743 lp_build_printf(gallivm
, "vbuf index = %u, vb_stride is %u\n",
745 lp_build_printf(gallivm
, " vb_buffer_offset = %u, src_offset is %u\n",
747 lp_build_const_int32(gallivm
, velem
->src_offset
));
748 lp_build_print_value(gallivm
, " blocksize = ",
749 lp_build_const_int32(
751 util_format_get_blocksize(velem
->src_format
)));
752 lp_build_printf(gallivm
, " instance_id = %u\n", instance_id
);
753 lp_build_printf(gallivm
, " stride = %u\n", stride
);
754 lp_build_printf(gallivm
, " buffer size = %u\n", buffer_size
);
755 lp_build_printf(gallivm
, " needed_buffer_size = %u\n", needed_buffer_size
);
756 lp_build_print_value(gallivm
, " buffer overflowed = ", buffer_overflowed
);
759 lp_build_if(&if_ctx
, gallivm
, buffer_overflowed
);
762 lp_build_const_vec(gallivm
, lp_float32_vec4_type(), 0);
763 LLVMBuildStore(builder
, val
, temp_ptr
);
765 lp_build_else(&if_ctx
);
768 map_ptr
= LLVMBuildGEP(builder
, map_ptr
, &stride
, 1, "");
770 val
= lp_build_fetch_rgba_aos(gallivm
,
772 lp_float32_vec4_type(),
775 LLVMBuildStore(builder
, val
, temp_ptr
);
777 lp_build_endif(&if_ctx
);
779 *res
= LLVMBuildLoad(builder
, temp_ptr
, "aos");
783 convert_to_soa(struct gallivm_state
*gallivm
,
784 LLVMValueRef (*src_aos
)[LP_MAX_VECTOR_WIDTH
/ 32],
785 LLVMValueRef (*dst_soa
)[TGSI_NUM_CHANNELS
],
786 unsigned num_attribs
, const struct lp_type soa_type
)
789 struct lp_type aos_channel_type
= soa_type
;
791 debug_assert(TGSI_NUM_CHANNELS
== 4);
792 debug_assert((soa_type
.length
% TGSI_NUM_CHANNELS
) == 0);
794 aos_channel_type
.length
>>= 1;
796 for (i
= 0; i
< num_attribs
; ++i
) {
797 LLVMValueRef aos_channels
[TGSI_NUM_CHANNELS
];
798 unsigned pixels_per_channel
= soa_type
.length
/ TGSI_NUM_CHANNELS
;
800 for (j
= 0; j
< TGSI_NUM_CHANNELS
; ++j
) {
801 LLVMValueRef channel
[LP_MAX_VECTOR_LENGTH
] = { 0 };
803 assert(pixels_per_channel
<= LP_MAX_VECTOR_LENGTH
);
805 for (k
= 0; k
< pixels_per_channel
; ++k
) {
806 channel
[k
] = src_aos
[i
][j
+ TGSI_NUM_CHANNELS
* k
];
809 aos_channels
[j
] = lp_build_concat(gallivm
, channel
, aos_channel_type
, pixels_per_channel
);
812 lp_build_transpose_aos(gallivm
, soa_type
, aos_channels
, dst_soa
[i
]);
818 store_aos(struct gallivm_state
*gallivm
,
823 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
824 LLVMBuilderRef builder
= gallivm
->builder
;
825 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
826 LLVMValueRef indices
[3];
828 indices
[0] = lp_build_const_int32(gallivm
, 0);
830 indices
[2] = lp_build_const_int32(gallivm
, 0);
832 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
833 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
836 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
839 /* Unaligned store due to the vertex header */
840 lp_set_store_alignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
844 * Adjust the mask to architecture endianess. The mask will the store in struct:
846 * struct vertex_header {
847 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
848 * unsigned edgeflag:1;
849 * unsigned have_clipdist:1;
850 * unsigned vertex_id:16;
854 * On little-endian machine nothing needs to done, however on bit-endian machine
855 * the mask's fields need to be adjusted with the algorithm:
857 * uint32_t reverse (uint32_t x)
859 * return (x >> 16) | // vertex_id
860 * ((x & 0x3fff) << 18) | // clipmask
861 * ((x & 0x4000) << 3) | // have_clipdist
862 * ((x & 0x8000) << 1); // edgeflag
866 adjust_mask(struct gallivm_state
*gallivm
,
869 #ifdef PIPE_ARCH_BIG_ENDIAN
870 LLVMBuilderRef builder
= gallivm
->builder
;
871 LLVMValueRef vertex_id
;
872 LLVMValueRef clipmask
;
873 LLVMValueRef have_clipdist
;
874 LLVMValueRef edgeflag
;
876 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
877 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
878 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
879 have_clipdist
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
880 have_clipdist
= LLVMBuildShl(builder
, have_clipdist
, lp_build_const_int32(gallivm
, 3), "");
881 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
882 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
884 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
885 mask
= LLVMBuildOr(builder
, mask
, have_clipdist
, "");
886 mask
= LLVMBuildOr(builder
, mask
, edgeflag
, "");
892 store_aos_array(struct gallivm_state
*gallivm
,
893 struct lp_type soa_type
,
895 LLVMValueRef
*indices
,
899 LLVMValueRef clipmask
,
900 boolean have_clipdist
)
902 LLVMBuilderRef builder
= gallivm
->builder
;
903 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
904 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
905 LLVMValueRef linear_inds
[LP_MAX_VECTOR_WIDTH
/ 32];
906 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
907 int vector_length
= soa_type
.length
;
910 debug_assert(TGSI_NUM_CHANNELS
== 4);
912 for (i
= 0; i
< vector_length
; i
++) {
913 linear_inds
[i
] = lp_build_const_int32(gallivm
, i
);
915 inds
[i
] = indices
[i
];
917 inds
[i
] = linear_inds
[i
];
919 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
923 /* store vertex header for each of the n vertices */
924 LLVMValueRef val
, cliptmp
;
925 int vertex_id_pad_edgeflag
;
927 /* If this assertion fails, it means we need to update the bit twidding
928 * code here. See struct vertex_header in draw_private.h.
930 assert(DRAW_TOTAL_CLIP_PLANES
==14);
931 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
932 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
934 vertex_id_pad_edgeflag
|= 1 << (DRAW_TOTAL_CLIP_PLANES
+1);
935 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
), vertex_id_pad_edgeflag
);
936 /* OR with the clipmask */
937 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
938 for (i
= 0; i
< vector_length
; i
++) {
939 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
940 val
= LLVMBuildExtractElement(builder
, cliptmp
, linear_inds
[i
], "");
941 val
= adjust_mask(gallivm
, val
);
942 LLVMBuildStore(builder
, val
, id_ptr
);
944 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
945 io_ptrs
[i
], inds
[i
], val
);
950 /* store for each of the n vertices */
951 for (i
= 0; i
< vector_length
; i
++) {
952 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
958 convert_to_aos(struct gallivm_state
*gallivm
,
960 LLVMValueRef
*indices
,
961 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
962 LLVMValueRef clipmask
,
964 struct lp_type soa_type
,
965 boolean have_clipdist
)
967 LLVMBuilderRef builder
= gallivm
->builder
;
968 unsigned chan
, attrib
, i
;
971 lp_build_printf(gallivm
, " # storing begin\n");
973 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
974 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
975 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
976 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
977 if (outputs
[attrib
][chan
]) {
978 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
979 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
981 lp_build_printf(gallivm
, "output %d : %d ",
982 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
984 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
986 lp_build_print_value(gallivm
, "val = ", out
);
989 LLVMBuildBitCast(builder
, out
, lp_build_int_vec_type(gallivm
, soa_type
), "");
991 lp_build_print_value(gallivm
, " ival = ", iv
);
1002 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
1003 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
1005 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
1007 for (i
= 0; i
< soa_type
.length
; ++i
) {
1008 aos
[i
] = lp_build_extract_range(gallivm
,
1009 soa
[i
% TGSI_NUM_CHANNELS
],
1010 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1015 store_aos_array(gallivm
,
1021 clipmask
, have_clipdist
);
1024 lp_build_printf(gallivm
, " # storing end\n");
1030 * Stores original vertex positions in clip coordinates
1033 store_clip(struct gallivm_state
*gallivm
,
1034 const struct lp_type vs_type
,
1035 LLVMValueRef io_ptr
,
1036 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1037 boolean pre_clip_pos
, int idx
)
1039 LLVMBuilderRef builder
= gallivm
->builder
;
1040 LLVMValueRef soa
[4];
1041 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
1042 LLVMValueRef indices
[2];
1043 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1044 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1045 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1046 LLVMTypeRef clip_ptr_type
=
1047 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
),
1052 indices
[1] = lp_build_const_int32(gallivm
, 0);
1054 for (i
= 0; i
< vs_type
.length
; i
++) {
1055 inds
[i
] = lp_build_const_int32(gallivm
, i
);
1056 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1059 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
1060 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
1061 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
1062 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
1064 if (!pre_clip_pos
) {
1065 for (i
= 0; i
< vs_type
.length
; i
++) {
1066 clip_ptrs
[i
] = draw_jit_header_clip(gallivm
, io_ptrs
[i
]);
1069 for (i
= 0; i
< vs_type
.length
; i
++) {
1070 clip_ptrs
[i
] = draw_jit_header_pre_clip_pos(gallivm
, io_ptrs
[i
]);
1074 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
1075 for (i
= 0; i
< vs_type
.length
; ++i
) {
1076 aos
[i
] = lp_build_extract_range(gallivm
,
1077 soa
[i
% TGSI_NUM_CHANNELS
],
1078 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1082 for (j
= 0; j
< vs_type
.length
; j
++) {
1083 LLVMValueRef clip_ptr
;
1085 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
1086 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
1088 /* Unaligned store */
1089 lp_set_store_alignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
1095 * Transforms the outputs for viewport mapping
1098 generate_viewport(struct draw_llvm_variant
*variant
,
1099 LLVMBuilderRef builder
,
1100 struct lp_type vs_type
,
1101 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1102 LLVMValueRef context_ptr
)
1105 struct gallivm_state
*gallivm
= variant
->gallivm
;
1106 struct lp_type f32_type
= vs_type
;
1107 const unsigned pos
= draw_current_shader_position_output(variant
->llvm
->draw
);
1108 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1109 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn*/
1110 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1111 LLVMValueRef vp_ptr
= draw_jit_context_viewport(gallivm
, context_ptr
);
1113 /* for 1/w convention*/
1114 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1115 LLVMBuildStore(builder
, out3
, outputs
[pos
][3]);
1117 /* Viewport Mapping */
1118 for (i
=0; i
<3; i
++) {
1119 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[pos
][i
], ""); /*x0 x1 .. xn*/
1122 LLVMValueRef scale_i
;
1123 LLVMValueRef trans_i
;
1126 index
= lp_build_const_int32(gallivm
, i
);
1127 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1129 index
= lp_build_const_int32(gallivm
, i
+4);
1130 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1132 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1133 LLVMBuildLoad(builder
, scale_i
, "scale"));
1134 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1135 LLVMBuildLoad(builder
, trans_i
, "trans"));
1138 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1140 out
= LLVMBuildFMul(builder
, out
, scale
, "");
1141 /* add translation */
1142 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
1144 /* store transformed outputs */
1145 LLVMBuildStore(builder
, out
, outputs
[pos
][i
]);
1152 * Returns clipmask as nxi32 bitmask for the n vertices
1155 generate_clipmask(struct draw_llvm
*llvm
,
1156 struct gallivm_state
*gallivm
,
1157 struct lp_type vs_type
,
1158 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1163 unsigned ucp_enable
,
1164 LLVMValueRef context_ptr
,
1165 boolean
*have_clipdist
)
1167 LLVMBuilderRef builder
= gallivm
->builder
;
1168 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1169 LLVMValueRef test
, temp
;
1170 LLVMValueRef zero
, shift
;
1171 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1172 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1173 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1174 struct lp_type f32_type
= vs_type
;
1175 struct lp_type i32_type
= lp_int_type(vs_type
);
1176 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1177 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1178 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1179 bool have_cd
= false;
1182 cd
[0] = draw_current_shader_clipdistance_output(llvm
->draw
, 0);
1183 cd
[1] = draw_current_shader_clipdistance_output(llvm
->draw
, 1);
1185 if (cd
[0] != pos
|| cd
[1] != pos
)
1188 if (num_written_clipdistance
&& !clip_user
) {
1190 ucp_enable
= (1 << num_written_clipdistance
) - 1;
1193 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1194 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1195 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1196 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1199 * load clipvertex and position from correct locations.
1200 * if they are the same just load them once.
1202 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1203 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1204 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1205 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1207 if (clip_user
&& cv
!= pos
) {
1208 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1209 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1210 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1211 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1219 /* Cliptest, for hardwired planes */
1222 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1224 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1228 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1229 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1230 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1231 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1232 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1235 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1236 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1237 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1238 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1241 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1242 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1243 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1244 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1245 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1249 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1252 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1253 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1254 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1258 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1259 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1260 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1261 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1264 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1265 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1266 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1267 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1271 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1272 LLVMValueRef indices
[3];
1273 LLVMValueRef is_nan_or_inf
;
1275 /* userclip planes */
1276 while (ucp_enable
) {
1277 unsigned plane_idx
= ffs(ucp_enable
)-1;
1278 ucp_enable
&= ~(1 << plane_idx
);
1281 if (have_cd
&& num_written_clipdistance
) {
1282 LLVMValueRef clipdist
;
1286 *have_clipdist
= TRUE
;
1288 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1290 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1292 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1293 is_nan_or_inf
= lp_build_is_inf_or_nan(gallivm
, vs_type
, clipdist
);
1294 test
= LLVMBuildOr(builder
, test
, is_nan_or_inf
, "");
1295 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1296 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1297 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1299 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1300 indices
[0] = lp_build_const_int32(gallivm
, 0);
1301 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1303 indices
[2] = lp_build_const_int32(gallivm
, 0);
1304 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1305 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1306 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1307 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1309 indices
[2] = lp_build_const_int32(gallivm
, 1);
1310 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1311 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1312 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1313 test
= LLVMBuildFMul(builder
, planes
, cv_y
, "");
1314 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1316 indices
[2] = lp_build_const_int32(gallivm
, 2);
1317 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1318 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1319 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1320 test
= LLVMBuildFMul(builder
, planes
, cv_z
, "");
1321 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1323 indices
[2] = lp_build_const_int32(gallivm
, 3);
1324 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1325 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1326 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1327 test
= LLVMBuildFMul(builder
, planes
, cv_w
, "");
1328 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1330 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1331 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1332 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1333 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1342 * Returns boolean if any clipping has occurred
1343 * Used zero/non-zero i32 value to represent boolean
1346 clipmask_booli32(struct gallivm_state
*gallivm
,
1347 const struct lp_type vs_type
,
1348 LLVMValueRef clipmask_bool_ptr
)
1350 LLVMBuilderRef builder
= gallivm
->builder
;
1351 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
1352 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1353 LLVMValueRef ret
= LLVMConstNull(int32_type
);
1358 * Can do this with log2(vector length) pack instructions and one extract
1359 * (as we don't actually need a or) with sse2 which would be way better.
1361 for (i
=0; i
< vs_type
.length
; i
++) {
1362 temp
= LLVMBuildExtractElement(builder
, clipmask_bool
,
1363 lp_build_const_int32(gallivm
, i
) , "");
1364 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1370 draw_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
1371 struct lp_build_tgsi_context
* bld_base
,
1372 boolean is_vindex_indirect
,
1373 LLVMValueRef vertex_index
,
1374 boolean is_aindex_indirect
,
1375 LLVMValueRef attrib_index
,
1376 LLVMValueRef swizzle_index
)
1378 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1379 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1380 LLVMBuilderRef builder
= gallivm
->builder
;
1381 LLVMValueRef indices
[3];
1383 struct lp_type type
= bld_base
->base
.type
;
1385 if (is_vindex_indirect
|| is_aindex_indirect
) {
1387 res
= bld_base
->base
.zero
;
1388 for (i
= 0; i
< type
.length
; ++i
) {
1389 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1390 LLVMValueRef vert_chan_index
= vertex_index
;
1391 LLVMValueRef attr_chan_index
= attrib_index
;
1392 LLVMValueRef channel_vec
, value
;
1394 if (is_vindex_indirect
) {
1395 vert_chan_index
= LLVMBuildExtractElement(builder
,
1396 vertex_index
, idx
, "");
1398 if (is_aindex_indirect
) {
1399 attr_chan_index
= LLVMBuildExtractElement(builder
,
1400 attrib_index
, idx
, "");
1403 indices
[0] = vert_chan_index
;
1404 indices
[1] = attr_chan_index
;
1405 indices
[2] = swizzle_index
;
1407 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1408 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1409 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1411 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1414 indices
[0] = vertex_index
;
1415 indices
[1] = attrib_index
;
1416 indices
[2] = swizzle_index
;
1418 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1419 res
= LLVMBuildLoad(builder
, res
, "");
1426 draw_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
1427 struct lp_build_tgsi_context
* bld_base
,
1428 LLVMValueRef (*outputs
)[4],
1429 LLVMValueRef emitted_vertices_vec
)
1431 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1432 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1433 struct gallivm_state
*gallivm
= variant
->gallivm
;
1434 LLVMBuilderRef builder
= gallivm
->builder
;
1435 struct lp_type gs_type
= bld_base
->base
.type
;
1436 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1437 lp_int_type(gs_type
), 0);
1438 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1439 LLVMValueRef next_prim_offset
=
1440 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1441 LLVMValueRef io
= variant
->io_ptr
;
1443 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1445 for (i
= 0; i
< gs_type
.length
; ++i
) {
1446 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1447 LLVMValueRef currently_emitted
=
1448 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1449 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1450 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1453 convert_to_aos(gallivm
, io
, indices
,
1455 gs_info
->num_outputs
, gs_type
,
1460 draw_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
1461 struct lp_build_tgsi_context
* bld_base
,
1462 LLVMValueRef verts_per_prim_vec
,
1463 LLVMValueRef emitted_prims_vec
)
1465 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1466 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1467 struct gallivm_state
*gallivm
= variant
->gallivm
;
1468 LLVMBuilderRef builder
= gallivm
->builder
;
1469 LLVMValueRef prim_lengts_ptr
=
1470 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1473 for (i
= 0; i
< bld_base
->base
.type
.length
; ++i
) {
1474 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1475 LLVMValueRef prims_emitted
=
1476 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1477 LLVMValueRef store_ptr
;
1478 LLVMValueRef num_vertices
=
1479 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1481 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1482 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1483 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1484 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1489 draw_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
1490 struct lp_build_tgsi_context
* bld_base
,
1491 LLVMValueRef total_emitted_vertices_vec
,
1492 LLVMValueRef emitted_prims_vec
)
1494 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1495 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1496 struct gallivm_state
*gallivm
= variant
->gallivm
;
1497 LLVMBuilderRef builder
= gallivm
->builder
;
1498 LLVMValueRef emitted_verts_ptr
=
1499 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1500 LLVMValueRef emitted_prims_ptr
=
1501 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1502 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1504 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &zero
, 0, "");
1505 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &zero
, 0, "");
1507 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1508 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1512 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1515 struct gallivm_state
*gallivm
= variant
->gallivm
;
1516 LLVMContextRef context
= gallivm
->context
;
1517 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1518 LLVMTypeRef arg_types
[10];
1519 unsigned num_arg_types
=
1520 elts
? Elements(arg_types
) : Elements(arg_types
) - 1;
1521 LLVMTypeRef func_type
;
1522 LLVMValueRef context_ptr
;
1523 LLVMBasicBlockRef block
;
1524 LLVMBuilderRef builder
;
1525 struct lp_type vs_type
;
1526 LLVMValueRef end
, start
;
1527 LLVMValueRef count
, fetch_elts
, fetch_elt_max
, fetch_count
;
1528 LLVMValueRef vertex_id_offset
;
1529 LLVMValueRef stride
, step
, io_itr
;
1530 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1531 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1532 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1533 struct draw_context
*draw
= llvm
->draw
;
1534 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1536 struct lp_build_context bld
;
1537 struct lp_build_loop_state lp_loop
;
1538 const int vector_length
= lp_native_vector_width
/ 32;
1539 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1540 LLVMValueRef fetch_max
;
1541 struct lp_build_sampler_soa
*sampler
= 0;
1542 LLVMValueRef ret
, clipmask_bool_ptr
;
1543 const struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
1544 struct draw_llvm_variant_key
*key
= &variant
->key
;
1545 /* If geometry shader is present we need to skip both the viewport
1546 * transformation and clipping otherwise the inputs to the geometry
1547 * shader will be incorrect.
1549 const boolean bypass_viewport
= gs
|| key
->bypass_viewport
;
1550 const boolean enable_cliptest
= !gs
&& (key
->clip_xy
||
1553 LLVMValueRef variant_func
;
1554 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1555 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1556 boolean have_clipdist
= FALSE
;
1557 struct lp_bld_tgsi_system_values system_values
;
1559 memset(&system_values
, 0, sizeof(system_values
));
1562 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1563 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1564 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1566 arg_types
[i
++] = LLVMPointerType(int32_type
, 0);/* fetch_elts */
1567 arg_types
[i
++] = int32_type
; /* fetch_elt_max */
1569 arg_types
[i
++] = int32_type
; /* start */
1570 arg_types
[i
++] = int32_type
; /* fetch_count / count */
1571 arg_types
[i
++] = int32_type
; /* stride */
1572 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1573 arg_types
[i
++] = int32_type
; /* instance_id */
1574 arg_types
[i
++] = int32_type
; /* vertex_id_offset */
1576 func_type
= LLVMFunctionType(int32_type
, arg_types
, num_arg_types
, 0);
1578 variant_func
= LLVMAddFunction(gallivm
->module
,
1579 elts
? "draw_llvm_shader_elts" : "draw_llvm_shader",
1583 variant
->function_elts
= variant_func
;
1585 variant
->function
= variant_func
;
1587 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1588 for (i
= 0; i
< num_arg_types
; ++i
)
1589 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1590 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1591 LLVMNoAliasAttribute
);
1593 context_ptr
= LLVMGetParam(variant_func
, 0);
1594 io_ptr
= LLVMGetParam(variant_func
, 1);
1595 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1596 stride
= LLVMGetParam(variant_func
, 5 + (elts
? 1 : 0));
1597 vb_ptr
= LLVMGetParam(variant_func
, 6 + (elts
? 1 : 0));
1598 system_values
.instance_id
= LLVMGetParam(variant_func
, 7 + (elts
? 1 : 0));
1599 vertex_id_offset
= LLVMGetParam(variant_func
, 8 + (elts
? 1 : 0));
1601 lp_build_name(context_ptr
, "context");
1602 lp_build_name(io_ptr
, "io");
1603 lp_build_name(vbuffers_ptr
, "vbuffers");
1604 lp_build_name(stride
, "stride");
1605 lp_build_name(vb_ptr
, "vb");
1606 lp_build_name(system_values
.instance_id
, "instance_id");
1607 lp_build_name(vertex_id_offset
, "vertex_id_offset");
1610 fetch_elts
= LLVMGetParam(variant_func
, 3);
1611 fetch_elt_max
= LLVMGetParam(variant_func
, 4);
1612 fetch_count
= LLVMGetParam(variant_func
, 5);
1613 lp_build_name(fetch_elts
, "fetch_elts");
1614 lp_build_name(fetch_elt_max
, "fetch_elt_max");
1615 lp_build_name(fetch_count
, "fetch_count");
1616 start
= count
= NULL
;
1619 start
= LLVMGetParam(variant_func
, 3);
1620 count
= LLVMGetParam(variant_func
, 4);
1621 lp_build_name(start
, "start");
1622 lp_build_name(count
, "count");
1623 fetch_elts
= fetch_count
= NULL
;
1630 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1631 builder
= gallivm
->builder
;
1632 LLVMPositionBuilderAtEnd(builder
, block
);
1634 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1636 memset(&vs_type
, 0, sizeof vs_type
);
1637 vs_type
.floating
= TRUE
; /* floating point values */
1638 vs_type
.sign
= TRUE
; /* values are signed */
1639 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1640 vs_type
.width
= 32; /* 32-bit float */
1641 vs_type
.length
= vector_length
;
1643 /* hold temporary "bool" clipmask */
1644 clipmask_bool_ptr
= lp_build_alloca(gallivm
, lp_build_int_vec_type(gallivm
, vs_type
), "");
1645 LLVMBuildStore(builder
, lp_build_zero(gallivm
, lp_int_type(vs_type
)), clipmask_bool_ptr
);
1647 /* code generated texture sampling */
1648 sampler
= draw_llvm_sampler_soa_create(
1649 draw_llvm_variant_key_samplers(key
),
1655 count
= fetch_count
;
1658 end
= lp_build_add(&bld
, start
, count
);
1661 step
= lp_build_const_int32(gallivm
, vector_length
);
1663 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1665 lp_build_loop_begin(&lp_loop
, gallivm
, zero
);
1667 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1668 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][LP_MAX_VECTOR_WIDTH
/ 32] = { { 0 } };
1670 LLVMValueRef clipmask
; /* holds the clipmask value */
1671 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1673 io_itr
= lp_loop
.counter
;
1675 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1677 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1678 io_itr
, io
, lp_loop
.counter
);
1680 system_values
.vertex_id
= lp_build_zero(gallivm
, lp_type_uint_vec(32, 32*vector_length
));
1681 for (i
= 0; i
< vector_length
; ++i
) {
1682 LLVMValueRef vert_index
=
1683 LLVMBuildAdd(builder
,
1685 lp_build_const_int32(gallivm
, i
), "");
1686 LLVMValueRef true_index
=
1687 LLVMBuildAdd(builder
, start
, vert_index
, "");
1688 LLVMValueRef vertex_id
;
1690 /* make sure we're not out of bounds which can happen
1691 * if fetch_count % 4 != 0, because on the last iteration
1692 * a few of the 4 vertex fetches will be out of bounds */
1693 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1696 LLVMValueRef fetch_ptr
;
1697 LLVMValueRef index_overflowed
;
1698 LLVMValueRef index_ptr
=
1701 lp_build_vec_type(gallivm
, lp_type_int(32)), "");
1702 struct lp_build_if_state if_ctx
;
1703 index_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
1704 true_index
, fetch_elt_max
,
1705 "index_overflowed");
1707 lp_build_if(&if_ctx
, gallivm
, index_overflowed
);
1709 /* Generate maximum possible index so that
1710 * generate_fetch can treat it just like
1711 * any other overflow and return zeros.
1712 * We don't have to worry about the restart
1713 * primitive index because it has already been
1717 lp_build_const_int32(gallivm
, 0xffffffff);
1718 LLVMBuildStore(builder
, val
, index_ptr
);
1720 lp_build_else(&if_ctx
);
1723 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1724 &true_index
, 1, "");
1725 val
= LLVMBuildLoad(builder
, fetch_ptr
, "");
1726 LLVMBuildStore(builder
, val
, index_ptr
);
1728 lp_build_endif(&if_ctx
);
1729 true_index
= LLVMBuildLoad(builder
, index_ptr
, "true_index");
1731 /* in the paths with elts vertex id has to be unaffected by the
1732 * index bias and because indices inside our elements array have
1733 * already had index bias applied we need to subtract it here to
1734 * get back to the original index.
1735 * in the linear paths vertex id has to be unaffected by the
1736 * original start index and because we abuse the 'start' variable
1737 * to either represent the actual start index or the index at which
1738 * the primitive was split (we split rendering into chunks of at
1739 * most 4095-vertices) we need to back out the original start
1740 * index out of our vertex id here.
1742 vertex_id
= LLVMBuildSub(builder
, true_index
, vertex_id_offset
, "");
1744 system_values
.vertex_id
= LLVMBuildInsertElement(
1746 system_values
.vertex_id
, vertex_id
,
1747 lp_build_const_int32(gallivm
, i
), "");
1749 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1750 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1751 LLVMValueRef vb_index
=
1752 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1753 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1754 generate_fetch(gallivm
, draw
, vbuffers_ptr
,
1755 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1756 system_values
.instance_id
);
1759 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1760 draw
->pt
.nr_vertex_elements
, vs_type
);
1762 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1763 generate_vs(variant
,
1771 key
->clamp_vertex_color
);
1773 if (pos
!= -1 && cv
!= -1) {
1774 /* store original positions in clip before further manipulation */
1775 store_clip(gallivm
, vs_type
, io
, outputs
, 0, cv
);
1776 store_clip(gallivm
, vs_type
, io
, outputs
, 1, pos
);
1779 if (enable_cliptest
) {
1780 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1781 /* allocate clipmask, assign it integer type */
1782 clipmask
= generate_clipmask(llvm
,
1791 context_ptr
, &have_clipdist
);
1792 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1793 /* store temporary clipping boolean value */
1794 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1797 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1800 /* do viewport mapping */
1801 if (!bypass_viewport
) {
1802 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1806 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1809 /* store clipmask in vertex header,
1810 * original positions in clip
1811 * and transformed positions in data
1813 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
1814 vs_info
->num_outputs
, vs_type
,
1817 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
1819 sampler
->destroy(sampler
);
1821 /* return clipping boolean value for function */
1822 ret
= clipmask_booli32(gallivm
, vs_type
, clipmask_bool_ptr
);
1824 LLVMBuildRet(builder
, ret
);
1826 gallivm_verify_function(gallivm
, variant_func
);
1830 struct draw_llvm_variant_key
*
1831 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1834 struct draw_llvm_variant_key
*key
;
1835 struct draw_sampler_static_state
*draw_sampler
;
1837 key
= (struct draw_llvm_variant_key
*)store
;
1839 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1841 /* Presumably all variants of the shader should have the same
1842 * number of vertex elements - ie the number of shader inputs.
1843 * NOTE: we NEED to store the needed number of needed inputs
1844 * here, not the number of provided elements to match keysize
1845 * (and the offset of sampler state in the key).
1847 key
->nr_vertex_elements
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
1848 assert(key
->nr_vertex_elements
<= llvm
->draw
->pt
.nr_vertex_elements
);
1850 /* will have to rig this up properly later */
1851 key
->clip_xy
= llvm
->draw
->clip_xy
;
1852 key
->clip_z
= llvm
->draw
->clip_z
;
1853 key
->clip_user
= llvm
->draw
->clip_user
;
1854 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1855 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
1856 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1857 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1858 key
->has_gs
= llvm
->draw
->gs
.geometry_shader
!= NULL
;
1859 key
->num_outputs
= draw_total_vs_outputs(llvm
->draw
);
1862 /* All variants of this shader will have the same value for
1863 * nr_samplers. Not yet trying to compact away holes in the
1866 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1867 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
1868 key
->nr_sampler_views
=
1869 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
1872 key
->nr_sampler_views
= key
->nr_samplers
;
1875 draw_sampler
= draw_llvm_variant_key_samplers(key
);
1877 memcpy(key
->vertex_element
,
1878 llvm
->draw
->pt
.vertex_element
,
1879 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1881 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
1883 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1884 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
1885 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
1887 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1888 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
1889 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
1897 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
1900 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
1902 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
1903 debug_printf("clip_xy = %u\n", key
->clip_xy
);
1904 debug_printf("clip_z = %u\n", key
->clip_z
);
1905 debug_printf("clip_user = %u\n", key
->clip_user
);
1906 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
1907 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
1908 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
1909 debug_printf("has_gs = %u\n", key
->has_gs
);
1910 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
1912 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
1913 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
1914 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
1915 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
1916 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
1919 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1920 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
1926 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1927 unsigned shader_stage
,
1929 uint32_t width
, uint32_t height
, uint32_t depth
,
1930 uint32_t first_level
, uint32_t last_level
,
1931 const void *base_ptr
,
1932 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1933 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1934 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
1937 struct draw_jit_texture
*jit_tex
;
1939 assert(shader_stage
== PIPE_SHADER_VERTEX
||
1940 shader_stage
== PIPE_SHADER_GEOMETRY
);
1942 if (shader_stage
== PIPE_SHADER_VERTEX
) {
1943 assert(sview_idx
< Elements(draw
->llvm
->jit_context
.textures
));
1945 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
1946 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
1947 assert(sview_idx
< Elements(draw
->llvm
->gs_jit_context
.textures
));
1949 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
1955 jit_tex
->width
= width
;
1956 jit_tex
->height
= height
;
1957 jit_tex
->depth
= depth
;
1958 jit_tex
->first_level
= first_level
;
1959 jit_tex
->last_level
= last_level
;
1960 jit_tex
->base
= base_ptr
;
1962 for (j
= first_level
; j
<= last_level
; j
++) {
1963 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
1964 jit_tex
->row_stride
[j
] = row_stride
[j
];
1965 jit_tex
->img_stride
[j
] = img_stride
[j
];
1971 draw_llvm_set_sampler_state(struct draw_context
*draw
,
1972 unsigned shader_type
)
1976 if (shader_type
== PIPE_SHADER_VERTEX
) {
1977 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
1978 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
1980 if (draw
->samplers
[i
]) {
1981 const struct pipe_sampler_state
*s
1982 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
1983 jit_sam
->min_lod
= s
->min_lod
;
1984 jit_sam
->max_lod
= s
->max_lod
;
1985 jit_sam
->lod_bias
= s
->lod_bias
;
1986 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1989 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
1990 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
1991 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
1993 if (draw
->samplers
[i
]) {
1994 const struct pipe_sampler_state
*s
1995 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
1996 jit_sam
->min_lod
= s
->min_lod
;
1997 jit_sam
->max_lod
= s
->max_lod
;
1998 jit_sam
->lod_bias
= s
->lod_bias
;
1999 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2007 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
2009 struct draw_llvm
*llvm
= variant
->llvm
;
2011 if (variant
->function_elts
) {
2012 gallivm_free_function(variant
->gallivm
,
2013 variant
->function_elts
, variant
->jit_func_elts
);
2016 if (variant
->function
) {
2017 gallivm_free_function(variant
->gallivm
,
2018 variant
->function
, variant
->jit_func
);
2021 gallivm_destroy(variant
->gallivm
);
2023 remove_from_list(&variant
->list_item_local
);
2024 variant
->shader
->variants_cached
--;
2025 remove_from_list(&variant
->list_item_global
);
2026 llvm
->nr_variants
--;
2032 * Create LLVM types for various structures.
2035 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
2037 struct gallivm_state
*gallivm
= var
->gallivm
;
2038 LLVMTypeRef texture_type
, sampler_type
, context_type
;
2040 texture_type
= create_jit_texture_type(gallivm
, "texture");
2041 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2043 context_type
= create_gs_jit_context_type(gallivm
,
2044 var
->shader
->base
.vector_length
,
2045 texture_type
, sampler_type
,
2046 "draw_gs_jit_context");
2047 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2049 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
2053 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
2055 if (!variant
->context_ptr_type
)
2056 create_gs_jit_types(variant
);
2057 return variant
->context_ptr_type
;
2061 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2062 struct lp_type gs_type
)
2064 struct gallivm_state
*gallivm
= variant
->gallivm
;
2065 LLVMBuilderRef builder
= gallivm
->builder
;
2066 struct lp_type mask_type
= lp_int_type(gs_type
);
2067 LLVMValueRef num_prims
;
2068 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
2071 num_prims
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
),
2072 variant
->num_prims
);
2073 for (i
= 0; i
<= gs_type
.length
; i
++) {
2074 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
2075 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, idx
, idx
, "");
2077 mask_val
= lp_build_compare(gallivm
, mask_type
,
2078 PIPE_FUNC_GREATER
, num_prims
, mask_val
);
2084 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2085 struct draw_gs_llvm_variant
*variant
)
2087 struct gallivm_state
*gallivm
= variant
->gallivm
;
2088 LLVMContextRef context
= gallivm
->context
;
2089 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2090 LLVMTypeRef arg_types
[6];
2091 LLVMTypeRef func_type
;
2092 LLVMValueRef variant_func
;
2093 LLVMValueRef context_ptr
;
2094 LLVMValueRef prim_id_ptr
;
2095 LLVMBasicBlockRef block
;
2096 LLVMBuilderRef builder
;
2097 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2098 struct lp_build_sampler_soa
*sampler
= 0;
2099 struct lp_build_context bld
;
2100 struct lp_bld_tgsi_system_values system_values
;
2101 struct lp_type gs_type
;
2103 struct draw_gs_llvm_iface gs_iface
;
2104 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2105 LLVMValueRef consts_ptr
, num_consts_ptr
;
2106 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2107 struct lp_build_mask_context mask
;
2108 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2109 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2111 memset(&system_values
, 0, sizeof(system_values
));
2113 assert(variant
->vertex_header_ptr_type
);
2115 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2116 arg_types
[1] = variant
->input_array_type
; /* input */
2117 arg_types
[2] = variant
->vertex_header_ptr_type
; /* vertex_header */
2118 arg_types
[3] = int32_type
; /* num_prims */
2119 arg_types
[4] = int32_type
; /* instance_id */
2120 arg_types
[5] = LLVMPointerType(
2121 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2123 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
2125 variant_func
= LLVMAddFunction(gallivm
->module
, "draw_geometry_shader",
2127 variant
->function
= variant_func
;
2129 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2131 for (i
= 0; i
< Elements(arg_types
); ++i
)
2132 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2133 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
2134 LLVMNoAliasAttribute
);
2136 context_ptr
= LLVMGetParam(variant_func
, 0);
2137 input_array
= LLVMGetParam(variant_func
, 1);
2138 io_ptr
= LLVMGetParam(variant_func
, 2);
2139 num_prims
= LLVMGetParam(variant_func
, 3);
2140 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2141 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2143 lp_build_name(context_ptr
, "context");
2144 lp_build_name(input_array
, "input");
2145 lp_build_name(io_ptr
, "io");
2146 lp_build_name(num_prims
, "num_prims");
2147 lp_build_name(system_values
.instance_id
, "instance_id");
2148 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2150 variant
->context_ptr
= context_ptr
;
2151 variant
->io_ptr
= io_ptr
;
2152 variant
->num_prims
= num_prims
;
2154 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2155 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2156 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2157 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2158 gs_iface
.input
= input_array
;
2159 gs_iface
.variant
= variant
;
2165 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2166 builder
= gallivm
->builder
;
2167 LLVMPositionBuilderAtEnd(builder
, block
);
2169 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2171 memset(&gs_type
, 0, sizeof gs_type
);
2172 gs_type
.floating
= TRUE
; /* floating point values */
2173 gs_type
.sign
= TRUE
; /* values are signed */
2174 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2175 gs_type
.width
= 32; /* 32-bit float */
2176 gs_type
.length
= vector_length
;
2178 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2180 draw_gs_jit_context_num_constants(variant
->gallivm
, context_ptr
);
2182 /* code generated texture sampling */
2183 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
,
2186 mask_val
= generate_mask_value(variant
, gs_type
);
2187 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2189 if (gs_info
->uses_primid
) {
2190 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");
2193 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2194 tgsi_dump(tokens
, 0);
2195 draw_gs_llvm_dump_variant_key(&variant
->key
);
2198 lp_build_tgsi_soa(variant
->gallivm
,
2208 &llvm
->draw
->gs
.geometry_shader
->info
,
2209 (const struct lp_build_tgsi_gs_iface
*)&gs_iface
);
2211 sampler
->destroy(sampler
);
2213 lp_build_mask_end(&mask
);
2215 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2217 gallivm_verify_function(gallivm
, variant_func
);
2221 struct draw_gs_llvm_variant
*
2222 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2223 unsigned num_outputs
,
2224 const struct draw_gs_llvm_variant_key
*key
)
2226 struct draw_gs_llvm_variant
*variant
;
2227 struct llvm_geometry_shader
*shader
=
2228 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2229 LLVMTypeRef vertex_header
;
2231 variant
= MALLOC(sizeof *variant
+
2232 shader
->variant_key_size
-
2233 sizeof variant
->key
);
2234 if (variant
== NULL
)
2237 variant
->llvm
= llvm
;
2238 variant
->shader
= shader
;
2240 variant
->gallivm
= gallivm_create();
2242 create_gs_jit_types(variant
);
2244 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2246 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2248 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2250 draw_gs_llvm_generate(llvm
, variant
);
2252 gallivm_compile_module(variant
->gallivm
);
2254 variant
->jit_func
= (draw_gs_jit_func
)
2255 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2257 variant
->list_item_global
.base
= variant
;
2258 variant
->list_item_local
.base
= variant
;
2259 /*variant->no = */shader
->variants_created
++;
2260 variant
->list_item_global
.base
= variant
;
2266 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2268 struct draw_llvm
*llvm
= variant
->llvm
;
2270 if (variant
->function
) {
2271 gallivm_free_function(variant
->gallivm
,
2272 variant
->function
, variant
->jit_func
);
2275 gallivm_destroy(variant
->gallivm
);
2277 remove_from_list(&variant
->list_item_local
);
2278 variant
->shader
->variants_cached
--;
2279 remove_from_list(&variant
->list_item_global
);
2280 llvm
->nr_gs_variants
--;
2284 struct draw_gs_llvm_variant_key
*
2285 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2288 struct draw_gs_llvm_variant_key
*key
;
2289 struct draw_sampler_static_state
*draw_sampler
;
2291 key
= (struct draw_gs_llvm_variant_key
*)store
;
2293 key
->num_outputs
= draw_total_gs_outputs(llvm
->draw
);
2295 /* All variants of this shader will have the same value for
2296 * nr_samplers. Not yet trying to compact away holes in the
2299 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2300 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2301 key
->nr_sampler_views
=
2302 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2305 key
->nr_sampler_views
= key
->nr_samplers
;
2308 draw_sampler
= key
->samplers
;
2310 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2312 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2313 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2314 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2316 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2317 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2318 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2325 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2328 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2330 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2331 debug_printf("sampler[%i].src_format = %s\n", i
,
2332 util_format_name(sampler
[i
].texture_state
.format
));