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/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 (void) target
; /* silence unused var warning for non-debug build */
101 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, map
,
102 target
, dvbuffer_type
,
103 DRAW_JIT_DVBUFFER_MAP
);
104 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, size
,
105 target
, dvbuffer_type
,
106 DRAW_JIT_DVBUFFER_SIZE
);
108 return dvbuffer_type
;
112 * Create LLVM type for struct draw_jit_texture
115 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
117 LLVMTargetDataRef target
= gallivm
->target
;
118 LLVMTypeRef texture_type
;
119 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
120 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
122 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
123 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
124 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
125 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
126 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
127 elem_types
[DRAW_JIT_TEXTURE_BASE
] =
128 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
129 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
130 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
131 elem_types
[DRAW_JIT_TEXTURE_MIP_OFFSETS
] =
132 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
134 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
135 Elements(elem_types
), 0);
137 (void) target
; /* silence unused var warning for non-debug build */
138 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
139 target
, texture_type
,
140 DRAW_JIT_TEXTURE_WIDTH
);
141 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
142 target
, texture_type
,
143 DRAW_JIT_TEXTURE_HEIGHT
);
144 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
145 target
, texture_type
,
146 DRAW_JIT_TEXTURE_DEPTH
);
147 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
148 target
, texture_type
,
149 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
150 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
151 target
, texture_type
,
152 DRAW_JIT_TEXTURE_LAST_LEVEL
);
153 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, base
,
154 target
, texture_type
,
155 DRAW_JIT_TEXTURE_BASE
);
156 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
157 target
, texture_type
,
158 DRAW_JIT_TEXTURE_ROW_STRIDE
);
159 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
160 target
, texture_type
,
161 DRAW_JIT_TEXTURE_IMG_STRIDE
);
162 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, mip_offsets
,
163 target
, texture_type
,
164 DRAW_JIT_TEXTURE_MIP_OFFSETS
);
166 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
173 * Create LLVM type for struct draw_jit_sampler
176 create_jit_sampler_type(struct gallivm_state
*gallivm
, const char *struct_name
)
178 LLVMTargetDataRef target
= gallivm
->target
;
179 LLVMTypeRef sampler_type
;
180 LLVMTypeRef elem_types
[DRAW_JIT_SAMPLER_NUM_FIELDS
];
182 elem_types
[DRAW_JIT_SAMPLER_MIN_LOD
] =
183 elem_types
[DRAW_JIT_SAMPLER_MAX_LOD
] =
184 elem_types
[DRAW_JIT_SAMPLER_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
185 elem_types
[DRAW_JIT_SAMPLER_BORDER_COLOR
] =
186 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
188 sampler_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
189 Elements(elem_types
), 0);
191 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, min_lod
,
192 target
, sampler_type
,
193 DRAW_JIT_SAMPLER_MIN_LOD
);
194 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, max_lod
,
195 target
, sampler_type
,
196 DRAW_JIT_SAMPLER_MAX_LOD
);
197 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, lod_bias
,
198 target
, sampler_type
,
199 DRAW_JIT_SAMPLER_LOD_BIAS
);
200 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, border_color
,
201 target
, sampler_type
,
202 DRAW_JIT_SAMPLER_BORDER_COLOR
);
204 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler
, target
, sampler_type
);
211 * Create LLVM type for struct draw_jit_context
214 create_jit_context_type(struct gallivm_state
*gallivm
,
215 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
216 const char *struct_name
)
218 LLVMTargetDataRef target
= gallivm
->target
;
219 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
220 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
221 LLVMTypeRef elem_types
[DRAW_JIT_CTX_NUM_FIELDS
];
222 LLVMTypeRef context_type
;
224 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
225 LP_MAX_TGSI_CONST_BUFFERS
);
226 elem_types
[1] = LLVMArrayType(int_type
, /* num_vs_constants */
227 LP_MAX_TGSI_CONST_BUFFERS
);
228 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
229 DRAW_TOTAL_CLIP_PLANES
), 0);
230 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewports */
231 elem_types
[4] = LLVMArrayType(texture_type
,
232 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
233 elem_types
[5] = LLVMArrayType(sampler_type
,
234 PIPE_MAX_SAMPLERS
); /* samplers */
235 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
236 Elements(elem_types
), 0);
237 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
238 target
, context_type
, DRAW_JIT_CTX_CONSTANTS
);
239 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, num_vs_constants
,
240 target
, context_type
, DRAW_JIT_CTX_NUM_CONSTANTS
);
241 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
242 target
, context_type
, DRAW_JIT_CTX_PLANES
);
243 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewports
,
244 target
, context_type
, DRAW_JIT_CTX_VIEWPORT
);
245 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
246 target
, context_type
,
247 DRAW_JIT_CTX_TEXTURES
);
248 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
249 target
, context_type
,
250 DRAW_JIT_CTX_SAMPLERS
);
251 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
252 target
, context_type
);
259 * Create LLVM type for struct draw_gs_jit_context
262 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
263 unsigned vector_length
,
264 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
265 const char *struct_name
)
267 LLVMTargetDataRef target
= gallivm
->target
;
268 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
269 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
270 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
271 LLVMTypeRef context_type
;
273 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
274 LP_MAX_TGSI_CONST_BUFFERS
);
275 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
276 LP_MAX_TGSI_CONST_BUFFERS
);
277 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
278 DRAW_TOTAL_CLIP_PLANES
), 0);
279 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewports */
281 elem_types
[4] = LLVMArrayType(texture_type
,
282 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
283 elem_types
[5] = LLVMArrayType(sampler_type
,
284 PIPE_MAX_SAMPLERS
); /* samplers */
286 elem_types
[6] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
287 elem_types
[7] = LLVMPointerType(LLVMVectorType(int_type
,
289 elem_types
[8] = LLVMPointerType(LLVMVectorType(int_type
,
292 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
293 Elements(elem_types
), 0);
295 (void) target
; /* silence unused var warning for non-debug build */
296 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
297 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
298 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, num_constants
,
299 target
, context_type
, DRAW_GS_JIT_CTX_NUM_CONSTANTS
);
300 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
301 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
302 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewports
,
303 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
304 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
305 target
, context_type
,
306 DRAW_GS_JIT_CTX_TEXTURES
);
307 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
308 target
, context_type
,
309 DRAW_GS_JIT_CTX_SAMPLERS
);
310 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
311 target
, context_type
,
312 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
313 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
314 target
, context_type
,
315 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
316 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
317 target
, context_type
,
318 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
319 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
320 target
, context_type
);
327 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
329 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
330 LLVMTypeRef input_array
;
332 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
333 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
334 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
335 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
341 * Create LLVM type for struct pipe_vertex_buffer
344 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
,
345 const char *struct_name
)
347 LLVMTargetDataRef target
= gallivm
->target
;
348 LLVMTypeRef elem_types
[4];
352 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
354 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
356 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
357 Elements(elem_types
), 0);
359 (void) target
; /* silence unused var warning for non-debug build */
360 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
362 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
365 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
372 * Create LLVM type for struct vertex_header;
375 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
377 LLVMTargetDataRef target
= gallivm
->target
;
378 LLVMTypeRef elem_types
[4];
379 LLVMTypeRef vertex_header
;
380 char struct_name
[24];
382 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
384 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
385 elem_types
[DRAW_JIT_VERTEX_CLIP
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
386 elem_types
[DRAW_JIT_VERTEX_PRE_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
387 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
389 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
390 Elements(elem_types
), 0);
392 /* these are bit-fields and we can't take address of them
393 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
394 target, vertex_header,
395 DRAW_JIT_VERTEX_CLIPMASK);
396 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
397 target, vertex_header,
398 DRAW_JIT_VERTEX_EDGEFLAG);
399 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
400 target, vertex_header,
401 DRAW_JIT_VERTEX_PAD);
402 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
403 target, vertex_header,
404 DRAW_JIT_VERTEX_VERTEX_ID);
406 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
407 target
, vertex_header
,
408 DRAW_JIT_VERTEX_CLIP
);
409 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, pre_clip_pos
,
410 target
, vertex_header
,
411 DRAW_JIT_VERTEX_PRE_CLIP_POS
);
412 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
413 target
, vertex_header
,
414 DRAW_JIT_VERTEX_DATA
);
416 assert(LLVMABISizeOfType(target
, vertex_header
) ==
417 offsetof(struct vertex_header
, data
[data_elems
]));
419 return vertex_header
;
424 * Create LLVM types for various structures.
427 create_jit_types(struct draw_llvm_variant
*variant
)
429 struct gallivm_state
*gallivm
= variant
->gallivm
;
430 LLVMTypeRef texture_type
, sampler_type
, context_type
, buffer_type
,
433 texture_type
= create_jit_texture_type(gallivm
, "texture");
434 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
436 context_type
= create_jit_context_type(gallivm
, texture_type
, sampler_type
,
438 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
440 buffer_type
= create_jit_dvbuffer_type(gallivm
, "draw_vertex_buffer");
441 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
443 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
444 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
449 get_context_ptr_type(struct draw_llvm_variant
*variant
)
451 if (!variant
->context_ptr_type
)
452 create_jit_types(variant
);
453 return variant
->context_ptr_type
;
458 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
460 if (!variant
->buffer_ptr_type
)
461 create_jit_types(variant
);
462 return variant
->buffer_ptr_type
;
467 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
469 if (!variant
->vb_ptr_type
)
470 create_jit_types(variant
);
471 return variant
->vb_ptr_type
;
475 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
477 if (!variant
->vertex_header_ptr_type
)
478 create_jit_types(variant
);
479 return variant
->vertex_header_ptr_type
;
484 * Create per-context LLVM info.
487 draw_llvm_create(struct draw_context
*draw
, LLVMContextRef context
)
489 struct draw_llvm
*llvm
;
491 if (!lp_build_init())
494 llvm
= CALLOC_STRUCT( draw_llvm
);
500 llvm
->context
= context
;
501 if (!llvm
->context
) {
502 llvm
->context
= LLVMContextCreate();
503 llvm
->context_owned
= true;
508 llvm
->nr_variants
= 0;
509 make_empty_list(&llvm
->vs_variants_list
);
511 llvm
->nr_gs_variants
= 0;
512 make_empty_list(&llvm
->gs_variants_list
);
517 draw_llvm_destroy(llvm
);
523 * Free per-context LLVM info.
526 draw_llvm_destroy(struct draw_llvm
*llvm
)
528 if (llvm
->context_owned
)
529 LLVMContextDispose(llvm
->context
);
530 llvm
->context
= NULL
;
532 /* XXX free other draw_llvm data? */
538 * Create LLVM-generated code for a vertex shader.
540 struct draw_llvm_variant
*
541 draw_llvm_create_variant(struct draw_llvm
*llvm
,
543 const struct draw_llvm_variant_key
*key
)
545 struct draw_llvm_variant
*variant
;
546 struct llvm_vertex_shader
*shader
=
547 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
548 LLVMTypeRef vertex_header
;
549 char module_name
[64];
551 variant
= MALLOC(sizeof *variant
+
552 shader
->variant_key_size
-
553 sizeof variant
->key
);
557 variant
->llvm
= llvm
;
558 variant
->shader
= shader
;
560 util_snprintf(module_name
, sizeof(module_name
), "draw_llvm_vs_variant%u",
561 variant
->shader
->variants_cached
);
563 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
565 create_jit_types(variant
);
567 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
569 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
570 tgsi_dump(llvm
->draw
->vs
.vertex_shader
->state
.tokens
, 0);
571 draw_llvm_dump_variant_key(&variant
->key
);
574 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
576 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
578 draw_llvm_generate(llvm
, variant
, FALSE
); /* linear */
579 draw_llvm_generate(llvm
, variant
, TRUE
); /* elts */
581 gallivm_compile_module(variant
->gallivm
);
583 variant
->jit_func
= (draw_jit_vert_func
)
584 gallivm_jit_function(variant
->gallivm
, variant
->function
);
586 variant
->jit_func_elts
= (draw_jit_vert_func_elts
)
587 gallivm_jit_function(variant
->gallivm
, variant
->function_elts
);
589 gallivm_free_ir(variant
->gallivm
);
591 variant
->list_item_global
.base
= variant
;
592 variant
->list_item_local
.base
= variant
;
593 /*variant->no = */shader
->variants_created
++;
594 variant
->list_item_global
.base
= variant
;
601 generate_vs(struct draw_llvm_variant
*variant
,
602 LLVMBuilderRef builder
,
603 struct lp_type vs_type
,
604 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
605 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
606 const struct lp_bld_tgsi_system_values
*system_values
,
607 LLVMValueRef context_ptr
,
608 struct lp_build_sampler_soa
*draw_sampler
,
609 boolean clamp_vertex_color
)
611 struct draw_llvm
*llvm
= variant
->llvm
;
612 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
613 LLVMValueRef consts_ptr
=
614 draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
615 LLVMValueRef num_consts_ptr
=
616 draw_jit_context_num_vs_constants(variant
->gallivm
, context_ptr
);
618 lp_build_tgsi_soa(variant
->gallivm
,
621 NULL
/*struct lp_build_mask_context *mask*/,
629 &llvm
->draw
->vs
.vertex_shader
->info
,
634 unsigned chan
, attrib
;
635 struct lp_build_context bld
;
636 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
637 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
639 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
640 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
641 if (outputs
[attrib
][chan
]) {
642 switch (info
->output_semantic_name
[attrib
]) {
643 case TGSI_SEMANTIC_COLOR
:
644 case TGSI_SEMANTIC_BCOLOR
:
645 if (clamp_vertex_color
) {
646 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
647 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
648 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
659 generate_fetch(struct gallivm_state
*gallivm
,
660 struct draw_context
*draw
,
661 LLVMValueRef vbuffers_ptr
,
663 struct pipe_vertex_element
*velem
,
666 LLVMValueRef instance_id
,
667 LLVMValueRef start_instance
)
669 const struct util_format_description
*format_desc
=
670 util_format_description(velem
->src_format
);
671 LLVMValueRef zero
= LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
));
672 LLVMBuilderRef builder
= gallivm
->builder
;
673 LLVMValueRef indices
=
674 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
675 velem
->vertex_buffer_index
, 0);
676 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
678 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
679 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
680 LLVMValueRef map_ptr
= draw_jit_dvbuffer_map(gallivm
, vbuffer_ptr
);
681 LLVMValueRef buffer_size
= draw_jit_dvbuffer_size(gallivm
, vbuffer_ptr
);
683 LLVMValueRef buffer_overflowed
;
684 LLVMValueRef needed_buffer_size
;
685 LLVMValueRef temp_ptr
=
686 lp_build_alloca(gallivm
,
687 lp_build_vec_type(gallivm
, lp_float32_vec4_type()), "");
688 LLVMValueRef ofbit
= NULL
;
689 struct lp_build_if_state if_ctx
;
691 if (velem
->instance_divisor
) {
692 /* Index is equal to the start instance plus the number of current
693 * instance divided by the divisor. In this case we compute it as:
694 * index = start_instance + (instance_id / divisor)
696 LLVMValueRef current_instance
;
697 current_instance
= LLVMBuildUDiv(builder
, instance_id
,
698 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
700 index
= lp_build_uadd_overflow(gallivm
, start_instance
,
701 current_instance
, &ofbit
);
704 stride
= lp_build_umul_overflow(gallivm
, vb_stride
, index
, &ofbit
);
705 stride
= lp_build_uadd_overflow(gallivm
, stride
, vb_buffer_offset
, &ofbit
);
706 stride
= lp_build_uadd_overflow(
708 lp_build_const_int32(gallivm
, velem
->src_offset
), &ofbit
);
709 needed_buffer_size
= lp_build_uadd_overflow(
711 lp_build_const_int32(gallivm
,
712 util_format_get_blocksize(velem
->src_format
)),
715 buffer_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
716 needed_buffer_size
, buffer_size
,
717 "buffer_overflowed");
718 buffer_overflowed
= LLVMBuildOr(builder
, buffer_overflowed
, ofbit
, "");
720 lp_build_printf(gallivm
, "vbuf index = %u, vb_stride is %u\n",
722 lp_build_printf(gallivm
, " vb_buffer_offset = %u, src_offset is %u\n",
724 lp_build_const_int32(gallivm
, velem
->src_offset
));
725 lp_build_print_value(gallivm
, " blocksize = ",
726 lp_build_const_int32(
728 util_format_get_blocksize(velem
->src_format
)));
729 lp_build_printf(gallivm
, " instance_id = %u\n", instance_id
);
730 lp_build_printf(gallivm
, " stride = %u\n", stride
);
731 lp_build_printf(gallivm
, " buffer size = %u\n", buffer_size
);
732 lp_build_printf(gallivm
, " needed_buffer_size = %u\n", needed_buffer_size
);
733 lp_build_print_value(gallivm
, " buffer overflowed = ", buffer_overflowed
);
736 lp_build_if(&if_ctx
, gallivm
, buffer_overflowed
);
739 lp_build_const_vec(gallivm
, lp_float32_vec4_type(), 0);
740 LLVMBuildStore(builder
, val
, temp_ptr
);
742 lp_build_else(&if_ctx
);
745 map_ptr
= LLVMBuildGEP(builder
, map_ptr
, &stride
, 1, "");
747 val
= lp_build_fetch_rgba_aos(gallivm
,
749 lp_float32_vec4_type(),
753 LLVMBuildStore(builder
, val
, temp_ptr
);
755 lp_build_endif(&if_ctx
);
757 *res
= LLVMBuildLoad(builder
, temp_ptr
, "aos");
761 convert_to_soa(struct gallivm_state
*gallivm
,
762 LLVMValueRef (*src_aos
)[LP_MAX_VECTOR_WIDTH
/ 32],
763 LLVMValueRef (*dst_soa
)[TGSI_NUM_CHANNELS
],
764 unsigned num_attribs
, const struct lp_type soa_type
)
767 struct lp_type aos_channel_type
= soa_type
;
769 debug_assert(TGSI_NUM_CHANNELS
== 4);
770 debug_assert((soa_type
.length
% TGSI_NUM_CHANNELS
) == 0);
772 aos_channel_type
.length
>>= 1;
774 for (i
= 0; i
< num_attribs
; ++i
) {
775 LLVMValueRef aos_channels
[TGSI_NUM_CHANNELS
];
776 unsigned pixels_per_channel
= soa_type
.length
/ TGSI_NUM_CHANNELS
;
778 for (j
= 0; j
< TGSI_NUM_CHANNELS
; ++j
) {
779 LLVMValueRef channel
[LP_MAX_VECTOR_LENGTH
] = { 0 };
781 assert(pixels_per_channel
<= LP_MAX_VECTOR_LENGTH
);
783 for (k
= 0; k
< pixels_per_channel
; ++k
) {
784 channel
[k
] = src_aos
[i
][j
+ TGSI_NUM_CHANNELS
* k
];
787 aos_channels
[j
] = lp_build_concat(gallivm
, channel
, aos_channel_type
, pixels_per_channel
);
790 lp_build_transpose_aos(gallivm
, soa_type
, aos_channels
, dst_soa
[i
]);
796 store_aos(struct gallivm_state
*gallivm
,
801 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
802 LLVMBuilderRef builder
= gallivm
->builder
;
803 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
804 LLVMValueRef indices
[3];
806 indices
[0] = lp_build_const_int32(gallivm
, 0);
808 indices
[2] = lp_build_const_int32(gallivm
, 0);
810 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
811 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
814 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
817 /* Unaligned store due to the vertex header */
818 lp_set_store_alignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
822 * Adjust the mask to architecture endianess. The mask will the store in struct:
824 * struct vertex_header {
825 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
826 * unsigned edgeflag:1;
827 * unsigned have_clipdist:1;
828 * unsigned vertex_id:16;
832 * On little-endian machine nothing needs to done, however on bit-endian machine
833 * the mask's fields need to be adjusted with the algorithm:
835 * uint32_t reverse (uint32_t x)
837 * return (x >> 16) | // vertex_id
838 * ((x & 0x3fff) << 18) | // clipmask
839 * ((x & 0x4000) << 3) | // have_clipdist
840 * ((x & 0x8000) << 1); // edgeflag
844 adjust_mask(struct gallivm_state
*gallivm
,
847 #ifdef PIPE_ARCH_BIG_ENDIAN
848 LLVMBuilderRef builder
= gallivm
->builder
;
849 LLVMValueRef vertex_id
;
850 LLVMValueRef clipmask
;
851 LLVMValueRef have_clipdist
;
852 LLVMValueRef edgeflag
;
854 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
855 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
856 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
857 have_clipdist
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
858 have_clipdist
= LLVMBuildShl(builder
, have_clipdist
, lp_build_const_int32(gallivm
, 3), "");
859 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
860 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
862 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
863 mask
= LLVMBuildOr(builder
, mask
, have_clipdist
, "");
864 mask
= LLVMBuildOr(builder
, mask
, edgeflag
, "");
870 store_aos_array(struct gallivm_state
*gallivm
,
871 struct lp_type soa_type
,
873 LLVMValueRef
*indices
,
877 LLVMValueRef clipmask
,
878 boolean have_clipdist
)
880 LLVMBuilderRef builder
= gallivm
->builder
;
881 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
882 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
883 LLVMValueRef linear_inds
[LP_MAX_VECTOR_WIDTH
/ 32];
884 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
885 int vector_length
= soa_type
.length
;
888 debug_assert(TGSI_NUM_CHANNELS
== 4);
890 for (i
= 0; i
< vector_length
; i
++) {
891 linear_inds
[i
] = lp_build_const_int32(gallivm
, i
);
893 inds
[i
] = indices
[i
];
895 inds
[i
] = linear_inds
[i
];
897 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
901 /* store vertex header for each of the n vertices */
902 LLVMValueRef val
, cliptmp
;
903 int vertex_id_pad_edgeflag
;
905 /* If this assertion fails, it means we need to update the bit twidding
906 * code here. See struct vertex_header in draw_private.h.
908 assert(DRAW_TOTAL_CLIP_PLANES
==14);
909 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
910 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
912 vertex_id_pad_edgeflag
|= 1 << (DRAW_TOTAL_CLIP_PLANES
+1);
913 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
), vertex_id_pad_edgeflag
);
914 /* OR with the clipmask */
915 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
916 for (i
= 0; i
< vector_length
; i
++) {
917 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
918 val
= LLVMBuildExtractElement(builder
, cliptmp
, linear_inds
[i
], "");
919 val
= adjust_mask(gallivm
, val
);
921 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
922 io_ptrs
[i
], inds
[i
], val
);
924 LLVMBuildStore(builder
, val
, id_ptr
);
928 /* store for each of the n vertices */
929 for (i
= 0; i
< vector_length
; i
++) {
930 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
936 convert_to_aos(struct gallivm_state
*gallivm
,
938 LLVMValueRef
*indices
,
939 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
940 LLVMValueRef clipmask
,
942 struct lp_type soa_type
,
943 boolean have_clipdist
)
945 LLVMBuilderRef builder
= gallivm
->builder
;
946 unsigned chan
, attrib
, i
;
949 lp_build_printf(gallivm
, " # storing begin\n");
951 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
952 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
953 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
954 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
955 if (outputs
[attrib
][chan
]) {
956 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
957 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
959 lp_build_printf(gallivm
, "output %d : %d ",
960 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
962 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
964 lp_build_print_value(gallivm
, "val = ", out
);
967 LLVMBuildBitCast(builder
, out
, lp_build_int_vec_type(gallivm
, soa_type
), "");
969 lp_build_print_value(gallivm
, " ival = ", iv
);
980 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
981 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
983 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
985 for (i
= 0; i
< soa_type
.length
; ++i
) {
986 aos
[i
] = lp_build_extract_range(gallivm
,
987 soa
[i
% TGSI_NUM_CHANNELS
],
988 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
993 store_aos_array(gallivm
,
999 clipmask
, have_clipdist
);
1002 lp_build_printf(gallivm
, " # storing end\n");
1008 * Stores original vertex positions in clip coordinates
1011 store_clip(struct gallivm_state
*gallivm
,
1012 const struct lp_type vs_type
,
1013 LLVMValueRef io_ptr
,
1014 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1015 boolean pre_clip_pos
, int idx
)
1017 LLVMBuilderRef builder
= gallivm
->builder
;
1018 LLVMValueRef soa
[4];
1019 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
1020 LLVMValueRef indices
[2];
1021 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1022 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1023 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1024 LLVMTypeRef clip_ptr_type
=
1025 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
),
1030 indices
[1] = lp_build_const_int32(gallivm
, 0);
1032 for (i
= 0; i
< vs_type
.length
; i
++) {
1033 inds
[i
] = lp_build_const_int32(gallivm
, i
);
1034 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1037 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
1038 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
1039 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
1040 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
1042 if (!pre_clip_pos
) {
1043 for (i
= 0; i
< vs_type
.length
; i
++) {
1044 clip_ptrs
[i
] = draw_jit_header_clip(gallivm
, io_ptrs
[i
]);
1047 for (i
= 0; i
< vs_type
.length
; i
++) {
1048 clip_ptrs
[i
] = draw_jit_header_pre_clip_pos(gallivm
, io_ptrs
[i
]);
1052 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
1053 for (i
= 0; i
< vs_type
.length
; ++i
) {
1054 aos
[i
] = lp_build_extract_range(gallivm
,
1055 soa
[i
% TGSI_NUM_CHANNELS
],
1056 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1060 for (j
= 0; j
< vs_type
.length
; j
++) {
1061 LLVMValueRef clip_ptr
;
1063 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
1064 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
1066 /* Unaligned store */
1067 lp_set_store_alignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
1073 * Transforms the outputs for viewport mapping
1076 generate_viewport(struct draw_llvm_variant
*variant
,
1077 LLVMBuilderRef builder
,
1078 struct lp_type vs_type
,
1079 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1080 LLVMValueRef context_ptr
)
1083 struct gallivm_state
*gallivm
= variant
->gallivm
;
1084 struct lp_type f32_type
= vs_type
;
1085 const unsigned pos
= variant
->llvm
->draw
->vs
.position_output
;
1086 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1087 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn*/
1088 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1089 LLVMValueRef vp_ptr
= draw_jit_context_viewports(gallivm
, context_ptr
);
1091 /* We treat pipe_viewport_state as a float array */
1092 const int scale_index_offset
= offsetof(struct pipe_viewport_state
, scale
) / sizeof(float);
1093 const int trans_index_offset
= offsetof(struct pipe_viewport_state
, translate
) / sizeof(float);
1095 /* for 1/w convention*/
1096 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1097 LLVMBuildStore(builder
, out3
, outputs
[pos
][3]);
1099 /* Viewport Mapping */
1100 for (i
=0; i
<3; i
++) {
1101 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[pos
][i
], ""); /*x0 x1 .. xn*/
1104 LLVMValueRef scale_i
;
1105 LLVMValueRef trans_i
;
1108 index
= lp_build_const_int32(gallivm
, i
+ scale_index_offset
);
1109 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1111 index
= lp_build_const_int32(gallivm
, i
+ trans_index_offset
);
1112 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1114 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1115 LLVMBuildLoad(builder
, scale_i
, "scale"));
1116 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1117 LLVMBuildLoad(builder
, trans_i
, "trans"));
1120 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1122 out
= LLVMBuildFMul(builder
, out
, scale
, "");
1123 /* add translation */
1124 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
1126 /* store transformed outputs */
1127 LLVMBuildStore(builder
, out
, outputs
[pos
][i
]);
1134 * Returns clipmask as nxi32 bitmask for the n vertices
1137 generate_clipmask(struct draw_llvm
*llvm
,
1138 struct gallivm_state
*gallivm
,
1139 struct lp_type vs_type
,
1140 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1145 unsigned ucp_enable
,
1146 LLVMValueRef context_ptr
,
1147 boolean
*have_clipdist
)
1149 LLVMBuilderRef builder
= gallivm
->builder
;
1150 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1151 LLVMValueRef test
, temp
;
1152 LLVMValueRef zero
, shift
;
1153 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1154 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1155 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1156 struct lp_type f32_type
= vs_type
;
1157 struct lp_type i32_type
= lp_int_type(vs_type
);
1158 const unsigned pos
= llvm
->draw
->vs
.position_output
;
1159 const unsigned cv
= llvm
->draw
->vs
.clipvertex_output
;
1160 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1161 bool have_cd
= false;
1164 cd
[0] = llvm
->draw
->vs
.clipdistance_output
[0];
1165 cd
[1] = llvm
->draw
->vs
.clipdistance_output
[1];
1167 if (cd
[0] != pos
|| cd
[1] != pos
)
1170 if (num_written_clipdistance
&& !clip_user
) {
1172 ucp_enable
= (1 << num_written_clipdistance
) - 1;
1175 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1176 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1177 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1178 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1181 * load clipvertex and position from correct locations.
1182 * if they are the same just load them once.
1184 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1185 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1186 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1187 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1189 if (clip_user
&& cv
!= pos
) {
1190 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1191 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1192 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1193 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1201 /* Cliptest, for hardwired planes */
1204 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1206 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1210 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1211 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1212 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1213 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1214 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1217 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1218 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1219 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1220 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1223 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1224 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1225 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1226 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1227 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1231 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1234 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1235 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1236 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1240 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1241 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1242 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1243 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1246 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1247 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1248 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1249 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1253 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1254 LLVMValueRef indices
[3];
1255 LLVMValueRef is_nan_or_inf
;
1257 /* userclip planes */
1258 while (ucp_enable
) {
1259 unsigned plane_idx
= ffs(ucp_enable
)-1;
1260 ucp_enable
&= ~(1 << plane_idx
);
1263 if (have_cd
&& num_written_clipdistance
) {
1264 LLVMValueRef clipdist
;
1268 *have_clipdist
= TRUE
;
1270 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1272 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1274 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1275 is_nan_or_inf
= lp_build_is_inf_or_nan(gallivm
, vs_type
, clipdist
);
1276 test
= LLVMBuildOr(builder
, test
, is_nan_or_inf
, "");
1277 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1LL << plane_idx
);
1278 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1279 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1281 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1282 indices
[0] = lp_build_const_int32(gallivm
, 0);
1283 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1285 indices
[2] = lp_build_const_int32(gallivm
, 0);
1286 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1287 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1288 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1289 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1291 indices
[2] = lp_build_const_int32(gallivm
, 1);
1292 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1293 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1294 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1295 test
= LLVMBuildFMul(builder
, planes
, cv_y
, "");
1296 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1298 indices
[2] = lp_build_const_int32(gallivm
, 2);
1299 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1300 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1301 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1302 test
= LLVMBuildFMul(builder
, planes
, cv_z
, "");
1303 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1305 indices
[2] = lp_build_const_int32(gallivm
, 3);
1306 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1307 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1308 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1309 test
= LLVMBuildFMul(builder
, planes
, cv_w
, "");
1310 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1312 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1313 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1LL << plane_idx
);
1314 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1315 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1324 * Returns boolean if any clipping has occurred
1325 * Used zero/non-zero i32 value to represent boolean
1328 clipmask_booli32(struct gallivm_state
*gallivm
,
1329 const struct lp_type vs_type
,
1330 LLVMValueRef clipmask_bool_ptr
)
1332 LLVMBuilderRef builder
= gallivm
->builder
;
1333 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
1334 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1335 LLVMValueRef ret
= LLVMConstNull(int32_type
);
1340 * Can do this with log2(vector length) pack instructions and one extract
1341 * (as we don't actually need a or) with sse2 which would be way better.
1343 for (i
=0; i
< vs_type
.length
; i
++) {
1344 temp
= LLVMBuildExtractElement(builder
, clipmask_bool
,
1345 lp_build_const_int32(gallivm
, i
) , "");
1346 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1352 draw_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
1353 struct lp_build_tgsi_context
* bld_base
,
1354 boolean is_vindex_indirect
,
1355 LLVMValueRef vertex_index
,
1356 boolean is_aindex_indirect
,
1357 LLVMValueRef attrib_index
,
1358 LLVMValueRef swizzle_index
)
1360 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1361 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1362 LLVMBuilderRef builder
= gallivm
->builder
;
1363 LLVMValueRef indices
[3];
1365 struct lp_type type
= bld_base
->base
.type
;
1367 if (is_vindex_indirect
|| is_aindex_indirect
) {
1369 res
= bld_base
->base
.zero
;
1370 for (i
= 0; i
< type
.length
; ++i
) {
1371 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1372 LLVMValueRef vert_chan_index
= vertex_index
;
1373 LLVMValueRef attr_chan_index
= attrib_index
;
1374 LLVMValueRef channel_vec
, value
;
1376 if (is_vindex_indirect
) {
1377 vert_chan_index
= LLVMBuildExtractElement(builder
,
1378 vertex_index
, idx
, "");
1380 if (is_aindex_indirect
) {
1381 attr_chan_index
= LLVMBuildExtractElement(builder
,
1382 attrib_index
, idx
, "");
1385 indices
[0] = vert_chan_index
;
1386 indices
[1] = attr_chan_index
;
1387 indices
[2] = swizzle_index
;
1389 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1390 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1391 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1393 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1396 indices
[0] = vertex_index
;
1397 indices
[1] = attrib_index
;
1398 indices
[2] = swizzle_index
;
1400 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1401 res
= LLVMBuildLoad(builder
, res
, "");
1408 draw_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
1409 struct lp_build_tgsi_context
* bld_base
,
1410 LLVMValueRef (*outputs
)[4],
1411 LLVMValueRef emitted_vertices_vec
)
1413 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1414 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1415 struct gallivm_state
*gallivm
= variant
->gallivm
;
1416 LLVMBuilderRef builder
= gallivm
->builder
;
1417 struct lp_type gs_type
= bld_base
->base
.type
;
1418 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1419 lp_int_type(gs_type
), 0);
1420 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1421 LLVMValueRef next_prim_offset
=
1422 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1423 LLVMValueRef io
= variant
->io_ptr
;
1425 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1427 for (i
= 0; i
< gs_type
.length
; ++i
) {
1428 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1429 LLVMValueRef currently_emitted
=
1430 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1431 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1432 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1435 convert_to_aos(gallivm
, io
, indices
,
1437 gs_info
->num_outputs
, gs_type
,
1442 draw_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
1443 struct lp_build_tgsi_context
* bld_base
,
1444 LLVMValueRef verts_per_prim_vec
,
1445 LLVMValueRef emitted_prims_vec
)
1447 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1448 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1449 struct gallivm_state
*gallivm
= variant
->gallivm
;
1450 LLVMBuilderRef builder
= gallivm
->builder
;
1451 LLVMValueRef prim_lengts_ptr
=
1452 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1455 for (i
= 0; i
< bld_base
->base
.type
.length
; ++i
) {
1456 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1457 LLVMValueRef prims_emitted
=
1458 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1459 LLVMValueRef store_ptr
;
1460 LLVMValueRef num_vertices
=
1461 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1463 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1464 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1465 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1466 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1471 draw_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
1472 struct lp_build_tgsi_context
* bld_base
,
1473 LLVMValueRef total_emitted_vertices_vec
,
1474 LLVMValueRef emitted_prims_vec
)
1476 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1477 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1478 struct gallivm_state
*gallivm
= variant
->gallivm
;
1479 LLVMBuilderRef builder
= gallivm
->builder
;
1480 LLVMValueRef emitted_verts_ptr
=
1481 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1482 LLVMValueRef emitted_prims_ptr
=
1483 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1484 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1486 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &zero
, 0, "");
1487 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &zero
, 0, "");
1489 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1490 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1494 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1497 struct gallivm_state
*gallivm
= variant
->gallivm
;
1498 LLVMContextRef context
= gallivm
->context
;
1499 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1500 LLVMTypeRef arg_types
[11];
1501 unsigned num_arg_types
=
1502 elts
? Elements(arg_types
) : Elements(arg_types
) - 1;
1503 LLVMTypeRef func_type
;
1504 LLVMValueRef context_ptr
;
1505 LLVMBasicBlockRef block
;
1506 LLVMBuilderRef builder
;
1508 struct lp_type vs_type
;
1509 LLVMValueRef end
, start
;
1510 LLVMValueRef count
, fetch_elts
, fetch_elt_max
, fetch_count
;
1511 LLVMValueRef vertex_id_offset
, start_instance
;
1512 LLVMValueRef stride
, step
, io_itr
;
1513 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1514 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1515 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1516 struct draw_context
*draw
= llvm
->draw
;
1517 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1519 struct lp_build_context bld
;
1520 struct lp_build_loop_state lp_loop
;
1521 const int vector_length
= lp_native_vector_width
/ 32;
1522 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1523 LLVMValueRef fetch_max
;
1524 struct lp_build_sampler_soa
*sampler
= 0;
1525 LLVMValueRef ret
, clipmask_bool_ptr
;
1526 struct draw_llvm_variant_key
*key
= &variant
->key
;
1527 /* If geometry shader is present we need to skip both the viewport
1528 * transformation and clipping otherwise the inputs to the geometry
1529 * shader will be incorrect.
1530 * The code can't handle vp transform when vs writes vp index neither
1531 * (though this would be fixable here, but couldn't just broadcast
1534 const boolean bypass_viewport
= key
->has_gs
|| key
->bypass_viewport
||
1535 llvm
->draw
->vs
.vertex_shader
->info
.writes_viewport_index
;
1536 const boolean enable_cliptest
= !key
->has_gs
&& (key
->clip_xy
||
1539 LLVMValueRef variant_func
;
1540 const unsigned pos
= llvm
->draw
->vs
.position_output
;
1541 const unsigned cv
= llvm
->draw
->vs
.clipvertex_output
;
1542 boolean have_clipdist
= FALSE
;
1543 struct lp_bld_tgsi_system_values system_values
;
1545 memset(&system_values
, 0, sizeof(system_values
));
1547 util_snprintf(func_name
, sizeof(func_name
), "draw_llvm_vs_variant%u_%s",
1548 variant
->shader
->variants_cached
, elts
? "elts" : "linear");
1551 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1552 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1553 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1555 arg_types
[i
++] = LLVMPointerType(int32_type
, 0);/* fetch_elts */
1556 arg_types
[i
++] = int32_type
; /* fetch_elt_max */
1558 arg_types
[i
++] = int32_type
; /* start */
1559 arg_types
[i
++] = int32_type
; /* fetch_count / count */
1560 arg_types
[i
++] = int32_type
; /* stride */
1561 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1562 arg_types
[i
++] = int32_type
; /* instance_id */
1563 arg_types
[i
++] = int32_type
; /* vertex_id_offset */
1564 arg_types
[i
++] = int32_type
; /* start_instance */
1566 func_type
= LLVMFunctionType(int32_type
, arg_types
, num_arg_types
, 0);
1568 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
1571 variant
->function_elts
= variant_func
;
1573 variant
->function
= variant_func
;
1575 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1576 for (i
= 0; i
< num_arg_types
; ++i
)
1577 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1578 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1579 LLVMNoAliasAttribute
);
1581 context_ptr
= LLVMGetParam(variant_func
, 0);
1582 io_ptr
= LLVMGetParam(variant_func
, 1);
1583 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1584 stride
= LLVMGetParam(variant_func
, 5 + (elts
? 1 : 0));
1585 vb_ptr
= LLVMGetParam(variant_func
, 6 + (elts
? 1 : 0));
1586 system_values
.instance_id
= LLVMGetParam(variant_func
, 7 + (elts
? 1 : 0));
1587 vertex_id_offset
= LLVMGetParam(variant_func
, 8 + (elts
? 1 : 0));
1588 start_instance
= LLVMGetParam(variant_func
, 9 + (elts
? 1 : 0));
1590 lp_build_name(context_ptr
, "context");
1591 lp_build_name(io_ptr
, "io");
1592 lp_build_name(vbuffers_ptr
, "vbuffers");
1593 lp_build_name(stride
, "stride");
1594 lp_build_name(vb_ptr
, "vb");
1595 lp_build_name(system_values
.instance_id
, "instance_id");
1596 lp_build_name(vertex_id_offset
, "vertex_id_offset");
1597 lp_build_name(start_instance
, "start_instance");
1600 fetch_elts
= LLVMGetParam(variant_func
, 3);
1601 fetch_elt_max
= LLVMGetParam(variant_func
, 4);
1602 fetch_count
= LLVMGetParam(variant_func
, 5);
1603 lp_build_name(fetch_elts
, "fetch_elts");
1604 lp_build_name(fetch_elt_max
, "fetch_elt_max");
1605 lp_build_name(fetch_count
, "fetch_count");
1606 start
= count
= NULL
;
1609 start
= LLVMGetParam(variant_func
, 3);
1610 count
= LLVMGetParam(variant_func
, 4);
1611 lp_build_name(start
, "start");
1612 lp_build_name(count
, "count");
1613 fetch_elts
= fetch_count
= NULL
;
1620 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1621 builder
= gallivm
->builder
;
1622 LLVMPositionBuilderAtEnd(builder
, block
);
1624 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1626 memset(&vs_type
, 0, sizeof vs_type
);
1627 vs_type
.floating
= TRUE
; /* floating point values */
1628 vs_type
.sign
= TRUE
; /* values are signed */
1629 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1630 vs_type
.width
= 32; /* 32-bit float */
1631 vs_type
.length
= vector_length
;
1633 /* hold temporary "bool" clipmask */
1634 clipmask_bool_ptr
= lp_build_alloca(gallivm
, lp_build_int_vec_type(gallivm
, vs_type
), "");
1635 LLVMBuildStore(builder
, lp_build_zero(gallivm
, lp_int_type(vs_type
)), clipmask_bool_ptr
);
1637 /* code generated texture sampling */
1638 sampler
= draw_llvm_sampler_soa_create(draw_llvm_variant_key_samplers(key
));
1643 count
= fetch_count
;
1646 end
= lp_build_add(&bld
, start
, count
);
1649 step
= lp_build_const_int32(gallivm
, vector_length
);
1651 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1653 lp_build_loop_begin(&lp_loop
, gallivm
, zero
);
1655 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1656 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][LP_MAX_VECTOR_WIDTH
/ 32] = { { 0 } };
1658 LLVMValueRef clipmask
; /* holds the clipmask value */
1659 LLVMValueRef true_index_array
= lp_build_zero(gallivm
,
1660 lp_type_uint_vec(32, 32*vector_length
));
1661 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1663 io_itr
= lp_loop
.counter
;
1665 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1667 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1668 io_itr
, io
, lp_loop
.counter
);
1670 for (i
= 0; i
< vector_length
; ++i
) {
1671 LLVMValueRef vert_index
=
1672 LLVMBuildAdd(builder
,
1674 lp_build_const_int32(gallivm
, i
), "");
1675 LLVMValueRef true_index
=
1676 LLVMBuildAdd(builder
, start
, vert_index
, "");
1678 /* make sure we're not out of bounds which can happen
1679 * if fetch_count % 4 != 0, because on the last iteration
1680 * a few of the 4 vertex fetches will be out of bounds */
1681 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1684 LLVMValueRef fetch_ptr
;
1685 LLVMValueRef index_overflowed
;
1686 LLVMValueRef index_ptr
=
1689 lp_build_vec_type(gallivm
, lp_type_int(32)), "");
1690 struct lp_build_if_state if_ctx
;
1691 index_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
1692 true_index
, fetch_elt_max
,
1693 "index_overflowed");
1695 lp_build_if(&if_ctx
, gallivm
, index_overflowed
);
1697 /* Generate maximum possible index so that
1698 * generate_fetch can treat it just like
1699 * any other overflow and return zeros.
1700 * We don't have to worry about the restart
1701 * primitive index because it has already been
1705 lp_build_const_int32(gallivm
, 0xffffffff);
1706 LLVMBuildStore(builder
, val
, index_ptr
);
1708 lp_build_else(&if_ctx
);
1711 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1712 &true_index
, 1, "");
1713 val
= LLVMBuildLoad(builder
, fetch_ptr
, "");
1714 LLVMBuildStore(builder
, val
, index_ptr
);
1716 lp_build_endif(&if_ctx
);
1717 true_index
= LLVMBuildLoad(builder
, index_ptr
, "true_index");
1719 true_index_array
= LLVMBuildInsertElement(
1720 gallivm
->builder
, true_index_array
, true_index
,
1721 lp_build_const_int32(gallivm
, i
), "");
1723 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1724 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1725 LLVMValueRef vb_index
=
1726 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1727 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1728 generate_fetch(gallivm
, draw
, vbuffers_ptr
,
1729 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1730 system_values
.instance_id
, start_instance
);
1733 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1734 draw
->pt
.nr_vertex_elements
, vs_type
);
1736 /* In the paths with elts vertex id has to be unaffected by the
1737 * index bias and because indices inside our elements array have
1738 * already had index bias applied we need to subtract it here to
1739 * get back to the original index.
1740 * in the linear paths vertex id has to be unaffected by the
1741 * original start index and because we abuse the 'start' variable
1742 * to either represent the actual start index or the index at which
1743 * the primitive was split (we split rendering into chunks of at
1744 * most 4095-vertices) we need to back out the original start
1745 * index out of our vertex id here.
1747 system_values
.basevertex
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
,
1748 lp_type_uint_vec(32, 32*vector_length
)),
1750 system_values
.vertex_id
= true_index_array
;
1751 system_values
.vertex_id_nobase
= LLVMBuildSub(builder
, true_index_array
,
1752 system_values
.basevertex
, "");
1754 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1755 generate_vs(variant
,
1763 key
->clamp_vertex_color
);
1765 if (pos
!= -1 && cv
!= -1) {
1766 /* store original positions in clip before further manipulation */
1767 store_clip(gallivm
, vs_type
, io
, outputs
, FALSE
, key
->clip_user
? cv
: pos
);
1768 store_clip(gallivm
, vs_type
, io
, outputs
, TRUE
, pos
);
1771 if (enable_cliptest
) {
1772 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1773 /* allocate clipmask, assign it integer type */
1774 clipmask
= generate_clipmask(llvm
,
1783 context_ptr
, &have_clipdist
);
1784 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1785 /* store temporary clipping boolean value */
1786 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1789 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1792 /* do viewport mapping */
1793 if (!bypass_viewport
) {
1794 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1798 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1801 /* store clipmask in vertex header,
1802 * original positions in clip
1803 * and transformed positions in data
1805 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
1806 vs_info
->num_outputs
, vs_type
,
1809 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
1811 sampler
->destroy(sampler
);
1813 /* return clipping boolean value for function */
1814 ret
= clipmask_booli32(gallivm
, vs_type
, clipmask_bool_ptr
);
1816 LLVMBuildRet(builder
, ret
);
1818 gallivm_verify_function(gallivm
, variant_func
);
1822 struct draw_llvm_variant_key
*
1823 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1826 struct draw_llvm_variant_key
*key
;
1827 struct draw_sampler_static_state
*draw_sampler
;
1829 key
= (struct draw_llvm_variant_key
*)store
;
1831 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1833 /* Presumably all variants of the shader should have the same
1834 * number of vertex elements - ie the number of shader inputs.
1835 * NOTE: we NEED to store the needed number of needed inputs
1836 * here, not the number of provided elements to match keysize
1837 * (and the offset of sampler state in the key).
1839 key
->nr_vertex_elements
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
1840 assert(key
->nr_vertex_elements
<= llvm
->draw
->pt
.nr_vertex_elements
);
1842 /* will have to rig this up properly later */
1843 key
->clip_xy
= llvm
->draw
->clip_xy
;
1844 key
->clip_z
= llvm
->draw
->clip_z
;
1845 key
->clip_user
= llvm
->draw
->clip_user
;
1846 key
->bypass_viewport
= llvm
->draw
->bypass_viewport
;
1847 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
1848 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1849 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1850 key
->has_gs
= llvm
->draw
->gs
.geometry_shader
!= NULL
;
1851 key
->num_outputs
= draw_total_vs_outputs(llvm
->draw
);
1854 /* All variants of this shader will have the same value for
1855 * nr_samplers. Not yet trying to compact away holes in the
1858 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1859 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
1860 key
->nr_sampler_views
=
1861 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
1864 key
->nr_sampler_views
= key
->nr_samplers
;
1867 draw_sampler
= draw_llvm_variant_key_samplers(key
);
1869 memcpy(key
->vertex_element
,
1870 llvm
->draw
->pt
.vertex_element
,
1871 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1873 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
1875 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1876 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
1877 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
1879 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1880 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
1881 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
1889 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
1892 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
1894 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
1895 debug_printf("clip_xy = %u\n", key
->clip_xy
);
1896 debug_printf("clip_z = %u\n", key
->clip_z
);
1897 debug_printf("clip_user = %u\n", key
->clip_user
);
1898 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
1899 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
1900 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
1901 debug_printf("has_gs = %u\n", key
->has_gs
);
1902 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
1904 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
1905 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
1906 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
1907 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
1908 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
1911 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1912 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
1918 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1919 unsigned shader_stage
,
1921 uint32_t width
, uint32_t height
, uint32_t depth
,
1922 uint32_t first_level
, uint32_t last_level
,
1923 const void *base_ptr
,
1924 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1925 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1926 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
1929 struct draw_jit_texture
*jit_tex
;
1931 assert(shader_stage
== PIPE_SHADER_VERTEX
||
1932 shader_stage
== PIPE_SHADER_GEOMETRY
);
1934 if (shader_stage
== PIPE_SHADER_VERTEX
) {
1935 assert(sview_idx
< Elements(draw
->llvm
->jit_context
.textures
));
1937 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
1938 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
1939 assert(sview_idx
< Elements(draw
->llvm
->gs_jit_context
.textures
));
1941 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
1947 jit_tex
->width
= width
;
1948 jit_tex
->height
= height
;
1949 jit_tex
->depth
= depth
;
1950 jit_tex
->first_level
= first_level
;
1951 jit_tex
->last_level
= last_level
;
1952 jit_tex
->base
= base_ptr
;
1954 for (j
= first_level
; j
<= last_level
; j
++) {
1955 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
1956 jit_tex
->row_stride
[j
] = row_stride
[j
];
1957 jit_tex
->img_stride
[j
] = img_stride
[j
];
1963 draw_llvm_set_sampler_state(struct draw_context
*draw
,
1964 unsigned shader_type
)
1968 if (shader_type
== PIPE_SHADER_VERTEX
) {
1969 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
1970 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
1972 if (draw
->samplers
[PIPE_SHADER_VERTEX
][i
]) {
1973 const struct pipe_sampler_state
*s
1974 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
1975 jit_sam
->min_lod
= s
->min_lod
;
1976 jit_sam
->max_lod
= s
->max_lod
;
1977 jit_sam
->lod_bias
= s
->lod_bias
;
1978 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1981 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
1982 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
1983 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
1985 if (draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]) {
1986 const struct pipe_sampler_state
*s
1987 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
1988 jit_sam
->min_lod
= s
->min_lod
;
1989 jit_sam
->max_lod
= s
->max_lod
;
1990 jit_sam
->lod_bias
= s
->lod_bias
;
1991 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1999 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
2001 struct draw_llvm
*llvm
= variant
->llvm
;
2003 gallivm_destroy(variant
->gallivm
);
2005 remove_from_list(&variant
->list_item_local
);
2006 variant
->shader
->variants_cached
--;
2007 remove_from_list(&variant
->list_item_global
);
2008 llvm
->nr_variants
--;
2014 * Create LLVM types for various structures.
2017 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
2019 struct gallivm_state
*gallivm
= var
->gallivm
;
2020 LLVMTypeRef texture_type
, sampler_type
, context_type
;
2022 texture_type
= create_jit_texture_type(gallivm
, "texture");
2023 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2025 context_type
= create_gs_jit_context_type(gallivm
,
2026 var
->shader
->base
.vector_length
,
2027 texture_type
, sampler_type
,
2028 "draw_gs_jit_context");
2029 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2031 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
2035 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
2037 if (!variant
->context_ptr_type
)
2038 create_gs_jit_types(variant
);
2039 return variant
->context_ptr_type
;
2043 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2044 struct lp_type gs_type
)
2046 struct gallivm_state
*gallivm
= variant
->gallivm
;
2047 LLVMBuilderRef builder
= gallivm
->builder
;
2048 struct lp_type mask_type
= lp_int_type(gs_type
);
2049 LLVMValueRef num_prims
;
2050 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
2053 num_prims
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
),
2054 variant
->num_prims
);
2055 for (i
= 0; i
< gs_type
.length
; i
++) {
2056 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
2057 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, idx
, idx
, "");
2059 mask_val
= lp_build_compare(gallivm
, mask_type
,
2060 PIPE_FUNC_GREATER
, num_prims
, mask_val
);
2066 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2067 struct draw_gs_llvm_variant
*variant
)
2069 struct gallivm_state
*gallivm
= variant
->gallivm
;
2070 LLVMContextRef context
= gallivm
->context
;
2071 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2072 LLVMTypeRef arg_types
[7];
2073 LLVMTypeRef func_type
;
2074 LLVMValueRef variant_func
;
2075 LLVMValueRef context_ptr
;
2076 LLVMValueRef prim_id_ptr
;
2077 LLVMBasicBlockRef block
;
2078 LLVMBuilderRef builder
;
2079 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2080 struct lp_build_sampler_soa
*sampler
= 0;
2081 struct lp_build_context bld
;
2082 struct lp_bld_tgsi_system_values system_values
;
2084 struct lp_type gs_type
;
2086 struct draw_gs_llvm_iface gs_iface
;
2087 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2088 LLVMValueRef consts_ptr
, num_consts_ptr
;
2089 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2090 struct lp_build_mask_context mask
;
2091 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2092 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2094 memset(&system_values
, 0, sizeof(system_values
));
2096 util_snprintf(func_name
, sizeof(func_name
), "draw_llvm_gs_variant%u",
2097 variant
->shader
->variants_cached
);
2099 assert(variant
->vertex_header_ptr_type
);
2101 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2102 arg_types
[1] = variant
->input_array_type
; /* input */
2103 arg_types
[2] = variant
->vertex_header_ptr_type
; /* vertex_header */
2104 arg_types
[3] = int32_type
; /* num_prims */
2105 arg_types
[4] = int32_type
; /* instance_id */
2106 arg_types
[5] = LLVMPointerType(
2107 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2108 arg_types
[6] = int32_type
;
2110 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
2112 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
2114 variant
->function
= variant_func
;
2116 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2118 for (i
= 0; i
< Elements(arg_types
); ++i
)
2119 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2120 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
2121 LLVMNoAliasAttribute
);
2123 context_ptr
= LLVMGetParam(variant_func
, 0);
2124 input_array
= LLVMGetParam(variant_func
, 1);
2125 io_ptr
= LLVMGetParam(variant_func
, 2);
2126 num_prims
= LLVMGetParam(variant_func
, 3);
2127 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2128 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2129 system_values
.invocation_id
= LLVMGetParam(variant_func
, 6);
2131 lp_build_name(context_ptr
, "context");
2132 lp_build_name(input_array
, "input");
2133 lp_build_name(io_ptr
, "io");
2134 lp_build_name(num_prims
, "num_prims");
2135 lp_build_name(system_values
.instance_id
, "instance_id");
2136 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2137 lp_build_name(system_values
.invocation_id
, "invocation_id");
2139 variant
->context_ptr
= context_ptr
;
2140 variant
->io_ptr
= io_ptr
;
2141 variant
->num_prims
= num_prims
;
2143 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2144 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2145 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2146 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2147 gs_iface
.input
= input_array
;
2148 gs_iface
.variant
= variant
;
2154 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2155 builder
= gallivm
->builder
;
2156 LLVMPositionBuilderAtEnd(builder
, block
);
2158 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2160 memset(&gs_type
, 0, sizeof gs_type
);
2161 gs_type
.floating
= TRUE
; /* floating point values */
2162 gs_type
.sign
= TRUE
; /* values are signed */
2163 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2164 gs_type
.width
= 32; /* 32-bit float */
2165 gs_type
.length
= vector_length
;
2167 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2169 draw_gs_jit_context_num_constants(variant
->gallivm
, context_ptr
);
2171 /* code generated texture sampling */
2172 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
);
2174 mask_val
= generate_mask_value(variant
, gs_type
);
2175 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2177 if (gs_info
->uses_primid
) {
2178 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");
2181 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2182 tgsi_dump(tokens
, 0);
2183 draw_gs_llvm_dump_variant_key(&variant
->key
);
2186 lp_build_tgsi_soa(variant
->gallivm
,
2197 &llvm
->draw
->gs
.geometry_shader
->info
,
2198 (const struct lp_build_tgsi_gs_iface
*)&gs_iface
);
2200 sampler
->destroy(sampler
);
2202 lp_build_mask_end(&mask
);
2204 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2206 gallivm_verify_function(gallivm
, variant_func
);
2210 struct draw_gs_llvm_variant
*
2211 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2212 unsigned num_outputs
,
2213 const struct draw_gs_llvm_variant_key
*key
)
2215 struct draw_gs_llvm_variant
*variant
;
2216 struct llvm_geometry_shader
*shader
=
2217 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2218 LLVMTypeRef vertex_header
;
2219 char module_name
[64];
2221 variant
= MALLOC(sizeof *variant
+
2222 shader
->variant_key_size
-
2223 sizeof variant
->key
);
2224 if (variant
== NULL
)
2227 variant
->llvm
= llvm
;
2228 variant
->shader
= shader
;
2230 util_snprintf(module_name
, sizeof(module_name
), "draw_llvm_gs_variant%u",
2231 variant
->shader
->variants_cached
);
2233 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
2235 create_gs_jit_types(variant
);
2237 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2239 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2241 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2243 draw_gs_llvm_generate(llvm
, variant
);
2245 gallivm_compile_module(variant
->gallivm
);
2247 variant
->jit_func
= (draw_gs_jit_func
)
2248 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2250 gallivm_free_ir(variant
->gallivm
);
2252 variant
->list_item_global
.base
= variant
;
2253 variant
->list_item_local
.base
= variant
;
2254 /*variant->no = */shader
->variants_created
++;
2255 variant
->list_item_global
.base
= variant
;
2261 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2263 struct draw_llvm
*llvm
= variant
->llvm
;
2265 gallivm_destroy(variant
->gallivm
);
2267 remove_from_list(&variant
->list_item_local
);
2268 variant
->shader
->variants_cached
--;
2269 remove_from_list(&variant
->list_item_global
);
2270 llvm
->nr_gs_variants
--;
2274 struct draw_gs_llvm_variant_key
*
2275 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2278 struct draw_gs_llvm_variant_key
*key
;
2279 struct draw_sampler_static_state
*draw_sampler
;
2281 key
= (struct draw_gs_llvm_variant_key
*)store
;
2283 key
->num_outputs
= draw_total_gs_outputs(llvm
->draw
);
2285 /* All variants of this shader will have the same value for
2286 * nr_samplers. Not yet trying to compact away holes in the
2289 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2290 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2291 key
->nr_sampler_views
=
2292 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2295 key
->nr_sampler_views
= key
->nr_samplers
;
2298 draw_sampler
= key
->samplers
;
2300 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2302 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2303 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2304 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2306 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2307 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2308 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2315 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2318 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2320 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2321 debug_printf("sampler[%i].src_format = %s\n", i
,
2322 util_format_name(sampler
[i
].texture_state
.format
));