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_bitarit.h"
37 #include "gallivm/lp_bld_gather.h"
38 #include "gallivm/lp_bld_logic.h"
39 #include "gallivm/lp_bld_const.h"
40 #include "gallivm/lp_bld_swizzle.h"
41 #include "gallivm/lp_bld_struct.h"
42 #include "gallivm/lp_bld_type.h"
43 #include "gallivm/lp_bld_flow.h"
44 #include "gallivm/lp_bld_debug.h"
45 #include "gallivm/lp_bld_tgsi.h"
46 #include "gallivm/lp_bld_printf.h"
47 #include "gallivm/lp_bld_intr.h"
48 #include "gallivm/lp_bld_init.h"
49 #include "gallivm/lp_bld_type.h"
50 #include "gallivm/lp_bld_pack.h"
51 #include "gallivm/lp_bld_format.h"
53 #include "tgsi/tgsi_exec.h"
54 #include "tgsi/tgsi_dump.h"
56 #include "util/u_math.h"
57 #include "util/u_pointer.h"
58 #include "util/u_string.h"
59 #include "util/simple_list.h"
66 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
);
69 struct draw_gs_llvm_iface
{
70 struct lp_build_tgsi_gs_iface base
;
72 struct draw_gs_llvm_variant
*variant
;
76 static inline const struct draw_gs_llvm_iface
*
77 draw_gs_llvm_iface(const struct lp_build_tgsi_gs_iface
*iface
)
79 return (const struct draw_gs_llvm_iface
*)iface
;
83 * Create LLVM type for draw_vertex_buffer.
86 create_jit_dvbuffer_type(struct gallivm_state
*gallivm
,
87 const char *struct_name
)
89 LLVMTargetDataRef target
= gallivm
->target
;
90 LLVMTypeRef dvbuffer_type
;
91 LLVMTypeRef elem_types
[DRAW_JIT_DVBUFFER_NUM_FIELDS
];
92 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
94 elem_types
[DRAW_JIT_DVBUFFER_MAP
] =
95 LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
96 elem_types
[DRAW_JIT_DVBUFFER_SIZE
] = int32_type
;
98 dvbuffer_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
99 ARRAY_SIZE(elem_types
), 0);
101 (void) target
; /* silence unused var warning for non-debug build */
102 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, map
,
103 target
, dvbuffer_type
,
104 DRAW_JIT_DVBUFFER_MAP
);
105 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, size
,
106 target
, dvbuffer_type
,
107 DRAW_JIT_DVBUFFER_SIZE
);
109 return dvbuffer_type
;
113 * Create LLVM type for struct draw_jit_texture
116 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
118 LLVMTargetDataRef target
= gallivm
->target
;
119 LLVMTypeRef texture_type
;
120 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
121 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
123 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
124 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
125 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
126 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
127 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
128 elem_types
[DRAW_JIT_TEXTURE_BASE
] =
129 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
130 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
131 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
132 elem_types
[DRAW_JIT_TEXTURE_MIP_OFFSETS
] =
133 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
135 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
136 ARRAY_SIZE(elem_types
), 0);
138 (void) target
; /* silence unused var warning for non-debug build */
139 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
140 target
, texture_type
,
141 DRAW_JIT_TEXTURE_WIDTH
);
142 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
143 target
, texture_type
,
144 DRAW_JIT_TEXTURE_HEIGHT
);
145 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
146 target
, texture_type
,
147 DRAW_JIT_TEXTURE_DEPTH
);
148 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
149 target
, texture_type
,
150 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
151 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
152 target
, texture_type
,
153 DRAW_JIT_TEXTURE_LAST_LEVEL
);
154 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, base
,
155 target
, texture_type
,
156 DRAW_JIT_TEXTURE_BASE
);
157 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
158 target
, texture_type
,
159 DRAW_JIT_TEXTURE_ROW_STRIDE
);
160 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
161 target
, texture_type
,
162 DRAW_JIT_TEXTURE_IMG_STRIDE
);
163 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, mip_offsets
,
164 target
, texture_type
,
165 DRAW_JIT_TEXTURE_MIP_OFFSETS
);
167 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
174 * Create LLVM type for struct draw_jit_sampler
177 create_jit_sampler_type(struct gallivm_state
*gallivm
, const char *struct_name
)
179 LLVMTargetDataRef target
= gallivm
->target
;
180 LLVMTypeRef sampler_type
;
181 LLVMTypeRef elem_types
[DRAW_JIT_SAMPLER_NUM_FIELDS
];
183 elem_types
[DRAW_JIT_SAMPLER_MIN_LOD
] =
184 elem_types
[DRAW_JIT_SAMPLER_MAX_LOD
] =
185 elem_types
[DRAW_JIT_SAMPLER_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
186 elem_types
[DRAW_JIT_SAMPLER_BORDER_COLOR
] =
187 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
189 sampler_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
190 ARRAY_SIZE(elem_types
), 0);
192 (void) target
; /* silence unused var warning for non-debug build */
193 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, min_lod
,
194 target
, sampler_type
,
195 DRAW_JIT_SAMPLER_MIN_LOD
);
196 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, max_lod
,
197 target
, sampler_type
,
198 DRAW_JIT_SAMPLER_MAX_LOD
);
199 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, lod_bias
,
200 target
, sampler_type
,
201 DRAW_JIT_SAMPLER_LOD_BIAS
);
202 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, border_color
,
203 target
, sampler_type
,
204 DRAW_JIT_SAMPLER_BORDER_COLOR
);
206 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler
, target
, sampler_type
);
213 * Create LLVM type for struct draw_jit_context
216 create_jit_context_type(struct gallivm_state
*gallivm
,
217 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
218 const char *struct_name
)
220 LLVMTargetDataRef target
= gallivm
->target
;
221 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
222 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
223 LLVMTypeRef elem_types
[DRAW_JIT_CTX_NUM_FIELDS
];
224 LLVMTypeRef context_type
;
226 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
227 LP_MAX_TGSI_CONST_BUFFERS
);
228 elem_types
[1] = LLVMArrayType(int_type
, /* num_vs_constants */
229 LP_MAX_TGSI_CONST_BUFFERS
);
230 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
231 DRAW_TOTAL_CLIP_PLANES
), 0);
232 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewports */
233 elem_types
[4] = LLVMArrayType(texture_type
,
234 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
235 elem_types
[5] = LLVMArrayType(sampler_type
,
236 PIPE_MAX_SAMPLERS
); /* samplers */
237 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
238 ARRAY_SIZE(elem_types
), 0);
240 (void) target
; /* silence unused var warning for non-debug build */
241 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
242 target
, context_type
, DRAW_JIT_CTX_CONSTANTS
);
243 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, num_vs_constants
,
244 target
, context_type
, DRAW_JIT_CTX_NUM_CONSTANTS
);
245 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
246 target
, context_type
, DRAW_JIT_CTX_PLANES
);
247 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewports
,
248 target
, context_type
, DRAW_JIT_CTX_VIEWPORT
);
249 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
250 target
, context_type
,
251 DRAW_JIT_CTX_TEXTURES
);
252 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
253 target
, context_type
,
254 DRAW_JIT_CTX_SAMPLERS
);
255 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
256 target
, context_type
);
263 * Create LLVM type for struct draw_gs_jit_context
266 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
267 unsigned vector_length
,
268 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
269 const char *struct_name
)
271 LLVMTargetDataRef target
= gallivm
->target
;
272 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
273 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
274 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
275 LLVMTypeRef context_type
;
277 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
278 LP_MAX_TGSI_CONST_BUFFERS
);
279 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
280 LP_MAX_TGSI_CONST_BUFFERS
);
281 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
282 DRAW_TOTAL_CLIP_PLANES
), 0);
283 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewports */
285 elem_types
[4] = LLVMArrayType(texture_type
,
286 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
287 elem_types
[5] = LLVMArrayType(sampler_type
,
288 PIPE_MAX_SAMPLERS
); /* samplers */
290 elem_types
[6] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
291 elem_types
[7] = LLVMPointerType(LLVMVectorType(int_type
,
293 elem_types
[8] = LLVMPointerType(LLVMVectorType(int_type
,
296 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
297 ARRAY_SIZE(elem_types
), 0);
299 (void) target
; /* silence unused var warning for non-debug build */
300 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
301 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
302 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, num_constants
,
303 target
, context_type
, DRAW_GS_JIT_CTX_NUM_CONSTANTS
);
304 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
305 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
306 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewports
,
307 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
308 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
309 target
, context_type
,
310 DRAW_GS_JIT_CTX_TEXTURES
);
311 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
312 target
, context_type
,
313 DRAW_GS_JIT_CTX_SAMPLERS
);
314 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
315 target
, context_type
,
316 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
317 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
318 target
, context_type
,
319 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
320 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
321 target
, context_type
,
322 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
323 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
324 target
, context_type
);
331 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
333 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
334 LLVMTypeRef input_array
;
336 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
337 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
338 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
339 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
345 * Create LLVM type for struct pipe_vertex_buffer
348 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
,
349 const char *struct_name
)
351 LLVMTargetDataRef target
= gallivm
->target
;
352 LLVMTypeRef elem_types
[4];
355 elem_types
[0] = LLVMInt16TypeInContext(gallivm
->context
);
356 elem_types
[1] = LLVMInt8TypeInContext(gallivm
->context
);
357 elem_types
[2] = LLVMInt32TypeInContext(gallivm
->context
);
358 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
360 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
361 ARRAY_SIZE(elem_types
), 0);
363 (void) target
; /* silence unused var warning for non-debug build */
364 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
366 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, is_user_buffer
,
368 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
370 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer
.resource
,
373 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
380 * Create LLVM type for struct vertex_header;
383 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
385 LLVMTargetDataRef target
= gallivm
->target
;
386 LLVMTypeRef elem_types
[3];
387 LLVMTypeRef vertex_header
;
388 char struct_name
[24];
390 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
392 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
393 elem_types
[DRAW_JIT_VERTEX_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
394 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
396 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
397 ARRAY_SIZE(elem_types
), 0);
399 /* these are bit-fields and we can't take address of them
400 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
401 target, vertex_header,
402 DRAW_JIT_VERTEX_CLIPMASK);
403 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
404 target, vertex_header,
405 DRAW_JIT_VERTEX_EDGEFLAG);
406 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
407 target, vertex_header,
408 DRAW_JIT_VERTEX_PAD);
409 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
410 target, vertex_header,
411 DRAW_JIT_VERTEX_VERTEX_ID);
413 (void) target
; /* silence unused var warning for non-debug build */
414 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip_pos
,
415 target
, vertex_header
,
416 DRAW_JIT_VERTEX_CLIP_POS
);
417 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
418 target
, vertex_header
,
419 DRAW_JIT_VERTEX_DATA
);
421 assert(LLVMABISizeOfType(target
, vertex_header
) ==
422 offsetof(struct vertex_header
, data
[data_elems
]));
424 return vertex_header
;
429 * Create LLVM types for various structures.
432 create_jit_types(struct draw_llvm_variant
*variant
)
434 struct gallivm_state
*gallivm
= variant
->gallivm
;
435 LLVMTypeRef texture_type
, sampler_type
, context_type
, buffer_type
,
438 texture_type
= create_jit_texture_type(gallivm
, "texture");
439 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
441 context_type
= create_jit_context_type(gallivm
, texture_type
, sampler_type
,
443 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
445 buffer_type
= create_jit_dvbuffer_type(gallivm
, "draw_vertex_buffer");
446 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
448 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
449 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
454 get_context_ptr_type(struct draw_llvm_variant
*variant
)
456 if (!variant
->context_ptr_type
)
457 create_jit_types(variant
);
458 return variant
->context_ptr_type
;
463 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
465 if (!variant
->buffer_ptr_type
)
466 create_jit_types(variant
);
467 return variant
->buffer_ptr_type
;
472 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
474 if (!variant
->vb_ptr_type
)
475 create_jit_types(variant
);
476 return variant
->vb_ptr_type
;
480 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
482 if (!variant
->vertex_header_ptr_type
)
483 create_jit_types(variant
);
484 return variant
->vertex_header_ptr_type
;
489 * Create per-context LLVM info.
492 draw_llvm_create(struct draw_context
*draw
, LLVMContextRef context
)
494 struct draw_llvm
*llvm
;
496 if (!lp_build_init())
499 llvm
= CALLOC_STRUCT( draw_llvm
);
505 llvm
->context
= context
;
506 if (!llvm
->context
) {
507 llvm
->context
= LLVMContextCreate();
508 llvm
->context_owned
= true;
513 llvm
->nr_variants
= 0;
514 make_empty_list(&llvm
->vs_variants_list
);
516 llvm
->nr_gs_variants
= 0;
517 make_empty_list(&llvm
->gs_variants_list
);
522 draw_llvm_destroy(llvm
);
528 * Free per-context LLVM info.
531 draw_llvm_destroy(struct draw_llvm
*llvm
)
533 if (llvm
->context_owned
)
534 LLVMContextDispose(llvm
->context
);
535 llvm
->context
= NULL
;
537 /* XXX free other draw_llvm data? */
543 * Create LLVM-generated code for a vertex shader.
545 struct draw_llvm_variant
*
546 draw_llvm_create_variant(struct draw_llvm
*llvm
,
548 const struct draw_llvm_variant_key
*key
)
550 struct draw_llvm_variant
*variant
;
551 struct llvm_vertex_shader
*shader
=
552 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
553 LLVMTypeRef vertex_header
;
554 char module_name
[64];
556 variant
= MALLOC(sizeof *variant
+
557 shader
->variant_key_size
-
558 sizeof variant
->key
);
562 variant
->llvm
= llvm
;
563 variant
->shader
= shader
;
565 util_snprintf(module_name
, sizeof(module_name
), "draw_llvm_vs_variant%u",
566 variant
->shader
->variants_cached
);
568 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
570 create_jit_types(variant
);
572 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
574 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
575 tgsi_dump(llvm
->draw
->vs
.vertex_shader
->state
.tokens
, 0);
576 draw_llvm_dump_variant_key(&variant
->key
);
579 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
581 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
583 draw_llvm_generate(llvm
, variant
);
585 gallivm_compile_module(variant
->gallivm
);
587 variant
->jit_func
= (draw_jit_vert_func
)
588 gallivm_jit_function(variant
->gallivm
, variant
->function
);
590 gallivm_free_ir(variant
->gallivm
);
592 variant
->list_item_global
.base
= variant
;
593 variant
->list_item_local
.base
= variant
;
594 /*variant->no = */shader
->variants_created
++;
595 variant
->list_item_global
.base
= variant
;
602 generate_vs(struct draw_llvm_variant
*variant
,
603 LLVMBuilderRef builder
,
604 struct lp_type vs_type
,
605 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
606 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
607 const struct lp_bld_tgsi_system_values
*system_values
,
608 LLVMValueRef context_ptr
,
609 struct lp_build_sampler_soa
*draw_sampler
,
610 boolean clamp_vertex_color
)
612 struct draw_llvm
*llvm
= variant
->llvm
;
613 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
614 LLVMValueRef consts_ptr
=
615 draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
616 LLVMValueRef num_consts_ptr
=
617 draw_jit_context_num_vs_constants(variant
->gallivm
, context_ptr
);
619 lp_build_tgsi_soa(variant
->gallivm
,
622 NULL
/*struct lp_build_mask_context *mask*/,
631 &llvm
->draw
->vs
.vertex_shader
->info
,
636 unsigned chan
, attrib
;
637 struct lp_build_context bld
;
638 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
639 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
641 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
642 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
643 if (outputs
[attrib
][chan
]) {
644 switch (info
->output_semantic_name
[attrib
]) {
645 case TGSI_SEMANTIC_COLOR
:
646 case TGSI_SEMANTIC_BCOLOR
:
647 if (clamp_vertex_color
) {
648 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
649 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
650 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
662 fetch_instanced(struct gallivm_state
*gallivm
,
663 const struct util_format_description
*format_desc
,
664 struct lp_type vs_type
,
665 LLVMValueRef vb_stride
,
666 LLVMValueRef map_ptr
,
667 LLVMValueRef buffer_size_adj
,
668 LLVMValueRef
*inputs
,
671 LLVMTypeRef i32_t
= LLVMInt32TypeInContext(gallivm
->context
);
672 LLVMTypeRef aosf_t
, aosi_t
;
673 LLVMValueRef zero
= LLVMConstNull(i32_t
);
674 LLVMBuilderRef builder
= gallivm
->builder
;
675 LLVMValueRef stride
, buffer_overflowed
, aos
, index_valid
;
678 aosf_t
= lp_build_vec_type(gallivm
, lp_float32_vec4_type());
679 aosi_t
= lp_build_vec_type(gallivm
, lp_int32_vec4_type());
681 /* This mul can overflow. Wraparound is ok. */
682 stride
= LLVMBuildMul(builder
, vb_stride
, index
, "");
684 buffer_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGE
,
685 stride
, buffer_size_adj
,
686 "buffer_overflowed");
689 lp_build_print_value(gallivm
, " instance index = ", index
);
690 lp_build_print_value(gallivm
, " buffer overflowed = ", buffer_overflowed
);
693 index_valid
= LLVMBuildNot(builder
, buffer_overflowed
, "");
694 index_valid
= LLVMBuildSExt(builder
, index_valid
, i32_t
, "");
695 stride
= LLVMBuildAnd(builder
, stride
, index_valid
, "");
697 aos
= lp_build_fetch_rgba_aos(gallivm
,
699 lp_float32_vec4_type(),
705 index_valid
= lp_build_broadcast(gallivm
, aosi_t
, index_valid
);
706 aos
= LLVMBuildBitCast(builder
, aos
, aosi_t
, "");
707 aos
= LLVMBuildAnd(builder
, aos
, index_valid
, "");
708 aos
= LLVMBuildBitCast(builder
, aos
, aosf_t
, "");
710 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
711 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
712 inputs
[i
] = lp_build_extract_broadcast(gallivm
,
713 lp_float32_vec4_type(),
714 vs_type
, aos
, index
);
720 fetch_vector(struct gallivm_state
*gallivm
,
721 const struct util_format_description
*format_desc
,
722 struct lp_type vs_type
,
723 LLVMValueRef vb_stride
,
724 LLVMValueRef map_ptr
,
725 LLVMValueRef buffer_size_adj
,
726 LLVMValueRef
*inputs
,
727 LLVMValueRef indices
)
729 LLVMBuilderRef builder
= gallivm
->builder
;
730 struct lp_build_context blduivec
;
731 struct lp_type fetch_type
= vs_type
;
732 LLVMValueRef offset
, valid_mask
;
735 lp_build_context_init(&blduivec
, gallivm
, lp_uint_type(vs_type
));
737 vb_stride
= lp_build_broadcast_scalar(&blduivec
, vb_stride
);
738 buffer_size_adj
= lp_build_broadcast_scalar(&blduivec
, buffer_size_adj
);
740 /* This mul can overflow. Wraparound is ok. */
741 offset
= lp_build_mul(&blduivec
, vb_stride
, indices
);
743 valid_mask
= lp_build_compare(gallivm
, blduivec
.type
,
744 PIPE_FUNC_LESS
, offset
, buffer_size_adj
);
746 /* not valid elements use offset 0 */
747 offset
= LLVMBuildAnd(builder
, offset
, valid_mask
, "");
750 lp_build_print_value(gallivm
, " indices = ", indices
);
751 lp_build_print_value(gallivm
, " offsets = ", offset
);
752 lp_build_print_value(gallivm
, " valid_mask = ", valid_mask
);
756 * Unlike fetch_instanced, use SoA fetch instead of multiple AoS fetches.
757 * This should always produce better code.
760 /* The type handling is annoying here... */
761 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
&&
762 format_desc
->channel
[0].pure_integer
) {
763 if (format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_SIGNED
) {
764 fetch_type
= lp_type_int_vec(vs_type
.width
, vs_type
.width
* vs_type
.length
);
766 else if (format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
767 fetch_type
= lp_type_uint_vec(vs_type
.width
, vs_type
.width
* vs_type
.length
);
771 lp_build_fetch_rgba_soa(gallivm
, format_desc
,
772 fetch_type
, FALSE
, map_ptr
, offset
,
773 blduivec
.zero
, blduivec
.zero
,
776 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
777 inputs
[i
] = LLVMBuildBitCast(builder
, inputs
[i
],
778 lp_build_vec_type(gallivm
, vs_type
), "");
781 /* out-of-bound fetches return all zeros */
782 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
783 inputs
[i
] = LLVMBuildBitCast(builder
, inputs
[i
], blduivec
.vec_type
, "");
784 inputs
[i
] = LLVMBuildAnd(builder
, inputs
[i
], valid_mask
, "");
785 inputs
[i
] = LLVMBuildBitCast(builder
, inputs
[i
],
786 lp_build_vec_type(gallivm
, vs_type
), "");
792 store_aos(struct gallivm_state
*gallivm
,
797 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
798 LLVMBuilderRef builder
= gallivm
->builder
;
799 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
800 LLVMValueRef indices
[3];
802 indices
[0] = lp_build_const_int32(gallivm
, 0);
804 indices
[2] = lp_build_const_int32(gallivm
, 0);
806 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
807 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
810 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
813 /* Unaligned store due to the vertex header */
814 LLVMSetAlignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
818 * Adjust the mask to architecture endianess. The mask will the store in struct:
820 * struct vertex_header {
821 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
822 * unsigned edgeflag:1;
824 * unsigned vertex_id:16;
828 * On little-endian machine nothing needs to done, however on bit-endian machine
829 * the mask's fields need to be adjusted with the algorithm:
831 * uint32_t reverse (uint32_t x)
833 * return (x >> 16) | // vertex_id
834 * ((x & 0x3fff) << 18) | // clipmask
835 * ((x & 0x4000) << 3) | // pad
836 * ((x & 0x8000) << 1); // edgeflag
840 adjust_mask(struct gallivm_state
*gallivm
,
843 #ifdef PIPE_ARCH_BIG_ENDIAN
844 LLVMBuilderRef builder
= gallivm
->builder
;
845 LLVMValueRef vertex_id
;
846 LLVMValueRef clipmask
;
848 LLVMValueRef edgeflag
;
850 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
851 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
852 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
854 pad
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
855 pad
= LLVMBuildShl(builder
, pad
, lp_build_const_int32(gallivm
, 3), "");
857 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
858 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
860 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
862 mask
= LLVMBuildOr(builder
, mask
, pad
, "");
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 need_edgeflag
)
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, pad:1 = 0, edgeflag:1 = 1 */
910 if (!need_edgeflag
) {
911 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
914 vertex_id_pad_edgeflag
= (0xffff << 16);
916 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
),
917 vertex_id_pad_edgeflag
);
918 /* OR with the clipmask */
919 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
920 for (i
= 0; i
< vector_length
; i
++) {
921 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
922 val
= LLVMBuildExtractElement(builder
, cliptmp
, linear_inds
[i
], "");
923 val
= adjust_mask(gallivm
, val
);
925 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
926 io_ptrs
[i
], inds
[i
], val
);
928 LLVMBuildStore(builder
, val
, id_ptr
);
932 /* store for each of the n vertices */
933 for (i
= 0; i
< vector_length
; i
++) {
934 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
940 convert_to_aos(struct gallivm_state
*gallivm
,
942 LLVMValueRef
*indices
,
943 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
944 LLVMValueRef clipmask
,
946 struct lp_type soa_type
,
947 boolean need_edgeflag
)
949 LLVMBuilderRef builder
= gallivm
->builder
;
950 unsigned chan
, attrib
, i
;
953 lp_build_printf(gallivm
, " # storing begin\n");
955 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
956 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
957 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
958 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
959 if (outputs
[attrib
][chan
]) {
960 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
961 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
963 lp_build_printf(gallivm
, "output %d : %d ",
964 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
966 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
968 lp_build_print_value(gallivm
, "val = ", out
);
971 LLVMBuildBitCast(builder
, out
, lp_build_int_vec_type(gallivm
, soa_type
), "");
973 lp_build_print_value(gallivm
, " ival = ", iv
);
984 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
985 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
987 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
989 for (i
= 0; i
< soa_type
.length
; ++i
) {
990 aos
[i
] = lp_build_extract_range(gallivm
,
991 soa
[i
% TGSI_NUM_CHANNELS
],
992 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
997 store_aos_array(gallivm
,
1007 lp_build_printf(gallivm
, " # storing end\n");
1013 * Stores original vertex positions in clip coordinates
1016 store_clip(struct gallivm_state
*gallivm
,
1017 const struct lp_type vs_type
,
1018 LLVMValueRef io_ptr
,
1019 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1022 LLVMBuilderRef builder
= gallivm
->builder
;
1023 LLVMValueRef soa
[4];
1024 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
1025 LLVMValueRef indices
[2];
1026 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1027 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1028 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1029 LLVMTypeRef clip_ptr_type
=
1030 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
),
1035 indices
[1] = lp_build_const_int32(gallivm
, 0);
1037 for (i
= 0; i
< vs_type
.length
; i
++) {
1038 inds
[i
] = lp_build_const_int32(gallivm
, i
);
1039 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1042 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
1043 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
1044 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
1045 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
1047 for (i
= 0; i
< vs_type
.length
; i
++) {
1048 clip_ptrs
[i
] = draw_jit_header_clip_pos(gallivm
, io_ptrs
[i
]);
1051 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
1052 for (i
= 0; i
< vs_type
.length
; ++i
) {
1053 aos
[i
] = lp_build_extract_range(gallivm
,
1054 soa
[i
% TGSI_NUM_CHANNELS
],
1055 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1059 for (j
= 0; j
< vs_type
.length
; j
++) {
1060 LLVMValueRef clip_ptr
;
1062 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
1063 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
1065 /* Unaligned store */
1066 LLVMSetAlignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
1072 * Transforms the outputs for viewport mapping
1075 generate_viewport(struct draw_llvm_variant
*variant
,
1076 LLVMBuilderRef builder
,
1077 struct lp_type vs_type
,
1078 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1079 LLVMValueRef context_ptr
)
1082 struct gallivm_state
*gallivm
= variant
->gallivm
;
1083 struct lp_type f32_type
= vs_type
;
1084 const unsigned pos
= variant
->llvm
->draw
->vs
.position_output
;
1085 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1086 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn*/
1087 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1088 LLVMValueRef vp_ptr
= draw_jit_context_viewports(gallivm
, context_ptr
);
1090 /* We treat pipe_viewport_state as a float array */
1091 const int scale_index_offset
= offsetof(struct pipe_viewport_state
, scale
) / sizeof(float);
1092 const int trans_index_offset
= offsetof(struct pipe_viewport_state
, translate
) / sizeof(float);
1094 /* for 1/w convention*/
1095 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1096 LLVMBuildStore(builder
, out3
, outputs
[pos
][3]);
1098 /* Viewport Mapping */
1099 for (i
=0; i
<3; i
++) {
1100 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[pos
][i
], ""); /*x0 x1 .. xn*/
1103 LLVMValueRef scale_i
;
1104 LLVMValueRef trans_i
;
1107 index
= lp_build_const_int32(gallivm
, i
+ scale_index_offset
);
1108 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1110 index
= lp_build_const_int32(gallivm
, i
+ trans_index_offset
);
1111 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1113 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1114 LLVMBuildLoad(builder
, scale_i
, "scale"));
1115 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1116 LLVMBuildLoad(builder
, trans_i
, "trans"));
1119 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1120 /* mult by scale, add translation */
1121 out
= lp_build_fmuladd(builder
, out
, scale
, trans
);
1123 /* store transformed outputs */
1124 LLVMBuildStore(builder
, out
, outputs
[pos
][i
]);
1131 * Returns clipmask as nxi32 bitmask for the n vertices
1134 generate_clipmask(struct draw_llvm
*llvm
,
1135 struct gallivm_state
*gallivm
,
1136 struct lp_type vs_type
,
1137 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1138 struct draw_llvm_variant_key
*key
,
1139 LLVMValueRef context_ptr
,
1140 boolean
*have_clipdist
)
1142 LLVMBuilderRef builder
= gallivm
->builder
;
1143 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1144 LLVMValueRef test
, temp
;
1145 LLVMValueRef zero
, shift
;
1146 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1147 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1148 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1149 struct lp_type f32_type
= vs_type
;
1150 struct lp_type i32_type
= lp_int_type(vs_type
);
1151 const unsigned pos
= llvm
->draw
->vs
.position_output
;
1152 const unsigned cv
= llvm
->draw
->vs
.clipvertex_output
;
1153 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1154 boolean have_cd
= false;
1155 boolean clip_user
= key
->clip_user
;
1156 unsigned ucp_enable
= key
->ucp_enable
;
1159 cd
[0] = llvm
->draw
->vs
.ccdistance_output
[0];
1160 cd
[1] = llvm
->draw
->vs
.ccdistance_output
[1];
1162 if (cd
[0] != pos
|| cd
[1] != pos
)
1165 if (num_written_clipdistance
&& !clip_user
) {
1167 ucp_enable
= (1 << num_written_clipdistance
) - 1;
1170 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1171 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1172 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1173 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1176 * load clipvertex and position from correct locations.
1177 * if they are the same just load them once.
1179 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1180 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1181 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1182 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1184 if (clip_user
&& cv
!= pos
) {
1185 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1186 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1187 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1188 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1197 * Be careful with the comparisons and NaNs (using llvm's unordered
1198 * comparisons here).
1200 /* Cliptest, for hardwired planes */
1202 * XXX should take guardband into account (currently not in key).
1203 * Otherwise might run the draw pipeline stages for nothing.
1207 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1209 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1213 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1214 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1215 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1216 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1217 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1220 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1221 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1222 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1223 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1226 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1227 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1228 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1229 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1230 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1234 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1235 if (key
->clip_halfz
) {
1237 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1238 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1239 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1243 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1244 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1245 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1246 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1249 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1250 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1251 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1252 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1256 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1257 LLVMValueRef indices
[3];
1258 LLVMValueRef is_nan_or_inf
;
1260 /* userclip planes */
1261 while (ucp_enable
) {
1262 unsigned plane_idx
= ffs(ucp_enable
)-1;
1263 ucp_enable
&= ~(1 << plane_idx
);
1266 if (have_cd
&& num_written_clipdistance
) {
1267 LLVMValueRef clipdist
;
1271 *have_clipdist
= TRUE
;
1273 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1275 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1277 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1278 is_nan_or_inf
= lp_build_is_inf_or_nan(gallivm
, vs_type
, clipdist
);
1279 test
= LLVMBuildOr(builder
, test
, is_nan_or_inf
, "");
1280 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1LL << plane_idx
);
1281 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1282 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1284 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1285 indices
[0] = lp_build_const_int32(gallivm
, 0);
1286 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1288 indices
[2] = lp_build_const_int32(gallivm
, 0);
1289 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1290 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1291 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1292 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1294 indices
[2] = lp_build_const_int32(gallivm
, 1);
1295 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1296 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1297 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1298 sum
= lp_build_fmuladd(builder
, planes
, cv_y
, sum
);
1300 indices
[2] = lp_build_const_int32(gallivm
, 2);
1301 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1302 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1303 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1304 sum
= lp_build_fmuladd(builder
, planes
, cv_z
, sum
);
1306 indices
[2] = lp_build_const_int32(gallivm
, 3);
1307 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1308 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1309 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1310 sum
= lp_build_fmuladd(builder
, planes
, cv_w
, sum
);
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
, "");
1319 if (key
->need_edgeflags
) {
1321 * This isn't really part of clipmask but stored the same in vertex
1322 * header later, so do it here.
1324 unsigned edge_attr
= llvm
->draw
->vs
.edgeflag_output
;
1325 LLVMValueRef one
= lp_build_const_vec(gallivm
, f32_type
, 1.0);
1326 LLVMValueRef edgeflag
= LLVMBuildLoad(builder
, outputs
[edge_attr
][0], "");
1327 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_EQUAL
, one
, edgeflag
);
1328 temp
= lp_build_const_int_vec(gallivm
, i32_type
,
1329 1LL << DRAW_TOTAL_CLIP_PLANES
);
1330 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1331 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1338 * Returns boolean if any clipping has occurred
1339 * Used zero/one i8 value to represent boolean
1342 clipmask_booli8(struct gallivm_state
*gallivm
,
1343 const struct lp_type vs_type
,
1344 LLVMValueRef clipmask_bool_ptr
,
1345 boolean edgeflag_in_clipmask
)
1347 LLVMBuilderRef builder
= gallivm
->builder
;
1348 LLVMTypeRef int8_type
= LLVMInt8TypeInContext(gallivm
->context
);
1349 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1351 struct lp_build_context bldivec
;
1353 lp_build_context_init(&bldivec
, gallivm
, lp_int_type(vs_type
));
1356 * We need to invert the edgeflag bit from the clipmask here
1357 * (because the result is really if we want to run the pipeline or not
1358 * and we (may) need it if edgeflag was 0).
1360 if (edgeflag_in_clipmask
) {
1361 LLVMValueRef edge
= lp_build_const_int_vec(gallivm
, bldivec
.type
,
1362 1LL << DRAW_TOTAL_CLIP_PLANES
);
1363 clipmask_bool
= LLVMBuildXor(builder
, clipmask_bool
, edge
, "");
1367 * XXX: probably should mask off bits from the mask which come from
1368 * vertices which were beyond the count (i.e. indices_valid for
1369 * linear fetches, for elts ones we don't have the correct mask
1370 * right now). Otherwise might run the pipeline for nothing,
1371 * though everything should still work.
1373 ret
= lp_build_any_true_range(&bldivec
, vs_type
.length
, clipmask_bool
);
1374 ret
= LLVMBuildZExt(builder
, ret
, int8_type
, "");
1379 draw_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
1380 struct lp_build_tgsi_context
* bld_base
,
1381 boolean is_vindex_indirect
,
1382 LLVMValueRef vertex_index
,
1383 boolean is_aindex_indirect
,
1384 LLVMValueRef attrib_index
,
1385 LLVMValueRef swizzle_index
)
1387 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1388 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1389 LLVMBuilderRef builder
= gallivm
->builder
;
1390 LLVMValueRef indices
[3];
1392 struct lp_type type
= bld_base
->base
.type
;
1394 if (is_vindex_indirect
|| is_aindex_indirect
) {
1396 res
= bld_base
->base
.zero
;
1397 for (i
= 0; i
< type
.length
; ++i
) {
1398 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1399 LLVMValueRef vert_chan_index
= vertex_index
;
1400 LLVMValueRef attr_chan_index
= attrib_index
;
1401 LLVMValueRef channel_vec
, value
;
1403 if (is_vindex_indirect
) {
1404 vert_chan_index
= LLVMBuildExtractElement(builder
,
1405 vertex_index
, idx
, "");
1407 if (is_aindex_indirect
) {
1408 attr_chan_index
= LLVMBuildExtractElement(builder
,
1409 attrib_index
, idx
, "");
1412 indices
[0] = vert_chan_index
;
1413 indices
[1] = attr_chan_index
;
1414 indices
[2] = swizzle_index
;
1416 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1417 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1418 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1420 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1423 indices
[0] = vertex_index
;
1424 indices
[1] = attrib_index
;
1425 indices
[2] = swizzle_index
;
1427 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1428 res
= LLVMBuildLoad(builder
, res
, "");
1435 draw_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
1436 struct lp_build_tgsi_context
* bld_base
,
1437 LLVMValueRef (*outputs
)[4],
1438 LLVMValueRef emitted_vertices_vec
)
1440 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1441 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1442 struct gallivm_state
*gallivm
= variant
->gallivm
;
1443 LLVMBuilderRef builder
= gallivm
->builder
;
1444 struct lp_type gs_type
= bld_base
->base
.type
;
1445 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1446 lp_int_type(gs_type
), 0);
1447 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1448 LLVMValueRef next_prim_offset
=
1449 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1450 LLVMValueRef io
= variant
->io_ptr
;
1452 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1454 for (i
= 0; i
< gs_type
.length
; ++i
) {
1455 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1456 LLVMValueRef currently_emitted
=
1457 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1458 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1459 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1462 convert_to_aos(gallivm
, io
, indices
,
1464 gs_info
->num_outputs
, gs_type
,
1469 draw_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
1470 struct lp_build_tgsi_context
* bld_base
,
1471 LLVMValueRef verts_per_prim_vec
,
1472 LLVMValueRef emitted_prims_vec
)
1474 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1475 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1476 struct gallivm_state
*gallivm
= variant
->gallivm
;
1477 LLVMBuilderRef builder
= gallivm
->builder
;
1478 LLVMValueRef prim_lengts_ptr
=
1479 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1482 for (i
= 0; i
< bld_base
->base
.type
.length
; ++i
) {
1483 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1484 LLVMValueRef prims_emitted
=
1485 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1486 LLVMValueRef store_ptr
;
1487 LLVMValueRef num_vertices
=
1488 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1490 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1491 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1492 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1493 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1498 draw_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
1499 struct lp_build_tgsi_context
* bld_base
,
1500 LLVMValueRef total_emitted_vertices_vec
,
1501 LLVMValueRef emitted_prims_vec
)
1503 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1504 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1505 struct gallivm_state
*gallivm
= variant
->gallivm
;
1506 LLVMBuilderRef builder
= gallivm
->builder
;
1507 LLVMValueRef emitted_verts_ptr
=
1508 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1509 LLVMValueRef emitted_prims_ptr
=
1510 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1511 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1513 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &zero
, 0, "");
1514 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &zero
, 0, "");
1516 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1517 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1521 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
)
1523 struct gallivm_state
*gallivm
= variant
->gallivm
;
1524 LLVMContextRef context
= gallivm
->context
;
1525 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1526 LLVMTypeRef arg_types
[11];
1527 unsigned num_arg_types
= ARRAY_SIZE(arg_types
);
1528 LLVMTypeRef func_type
;
1529 LLVMValueRef context_ptr
;
1530 LLVMBasicBlockRef block
;
1531 LLVMBuilderRef builder
;
1533 struct lp_type vs_type
;
1534 LLVMValueRef count
, fetch_elts
, start_or_maxelt
;
1535 LLVMValueRef vertex_id_offset
, start_instance
;
1536 LLVMValueRef stride
, step
, io_itr
;
1537 LLVMValueRef ind_vec
, start_vec
, have_elts
, fetch_max
, tmp
;
1538 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1539 LLVMValueRef vb_stride
[PIPE_MAX_ATTRIBS
];
1540 LLVMValueRef map_ptr
[PIPE_MAX_ATTRIBS
];
1541 LLVMValueRef buffer_size_adj
[PIPE_MAX_ATTRIBS
];
1542 LLVMValueRef instance_index
[PIPE_MAX_ATTRIBS
];
1543 LLVMValueRef fake_buf_ptr
, fake_buf
;
1545 struct draw_context
*draw
= llvm
->draw
;
1546 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1548 struct lp_build_context bld
, blduivec
;
1549 struct lp_build_loop_state lp_loop
;
1550 struct lp_build_if_state if_ctx
;
1551 const int vector_length
= lp_native_vector_width
/ 32;
1552 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1553 struct lp_build_sampler_soa
*sampler
= 0;
1554 LLVMValueRef ret
, clipmask_bool_ptr
;
1555 struct draw_llvm_variant_key
*key
= &variant
->key
;
1556 /* If geometry shader is present we need to skip both the viewport
1557 * transformation and clipping otherwise the inputs to the geometry
1558 * shader will be incorrect.
1559 * The code can't handle vp transform when vs writes vp index neither
1560 * (though this would be fixable here, but couldn't just broadcast
1563 const boolean bypass_viewport
= key
->has_gs
|| key
->bypass_viewport
||
1564 vs_info
->writes_viewport_index
;
1565 const boolean enable_cliptest
= !key
->has_gs
&& (key
->clip_xy
||
1568 key
->need_edgeflags
);
1569 LLVMValueRef variant_func
;
1570 const unsigned pos
= draw
->vs
.position_output
;
1571 const unsigned cv
= draw
->vs
.clipvertex_output
;
1572 boolean have_clipdist
= FALSE
;
1573 struct lp_bld_tgsi_system_values system_values
;
1575 memset(&system_values
, 0, sizeof(system_values
));
1577 util_snprintf(func_name
, sizeof(func_name
), "draw_llvm_vs_variant%u",
1578 variant
->shader
->variants_cached
);
1581 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1582 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1583 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1584 arg_types
[i
++] = int32_type
; /* count */
1585 arg_types
[i
++] = int32_type
; /* start/fetch_elt_max */
1586 arg_types
[i
++] = int32_type
; /* stride */
1587 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1588 arg_types
[i
++] = int32_type
; /* instance_id */
1589 arg_types
[i
++] = int32_type
; /* vertex_id_offset */
1590 arg_types
[i
++] = int32_type
; /* start_instance */
1591 arg_types
[i
++] = LLVMPointerType(int32_type
, 0); /* fetch_elts */
1593 func_type
= LLVMFunctionType(LLVMInt8TypeInContext(context
),
1594 arg_types
, num_arg_types
, 0);
1596 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
1597 variant
->function
= variant_func
;
1599 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1600 for (i
= 0; i
< num_arg_types
; ++i
)
1601 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1602 lp_add_function_attr(variant_func
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
1604 context_ptr
= LLVMGetParam(variant_func
, 0);
1605 io_ptr
= LLVMGetParam(variant_func
, 1);
1606 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1607 count
= LLVMGetParam(variant_func
, 3);
1609 * XXX: the maxelt part is unused. Not really useful, since we cannot
1610 * get index buffer overflows due to vsplit (which provides its own
1611 * elts buffer, with a different size than what's passed in here).
1613 start_or_maxelt
= LLVMGetParam(variant_func
, 4);
1615 * XXX: stride is actually unused. The stride we use is strictly calculated
1616 * from the number of outputs (including the draw_extra outputs).
1617 * Should probably fix some day (we need a new vs just because of extra
1618 * outputs which the generated vs won't touch).
1620 stride
= LLVMGetParam(variant_func
, 5);
1621 vb_ptr
= LLVMGetParam(variant_func
, 6);
1622 system_values
.instance_id
= LLVMGetParam(variant_func
, 7);
1623 vertex_id_offset
= LLVMGetParam(variant_func
, 8);
1624 start_instance
= LLVMGetParam(variant_func
, 9);
1625 fetch_elts
= LLVMGetParam(variant_func
, 10);
1627 lp_build_name(context_ptr
, "context");
1628 lp_build_name(io_ptr
, "io");
1629 lp_build_name(vbuffers_ptr
, "vbuffers");
1630 lp_build_name(count
, "count");
1631 lp_build_name(start_or_maxelt
, "start_or_maxelt");
1632 lp_build_name(stride
, "stride");
1633 lp_build_name(vb_ptr
, "vb");
1634 lp_build_name(system_values
.instance_id
, "instance_id");
1635 lp_build_name(vertex_id_offset
, "vertex_id_offset");
1636 lp_build_name(start_instance
, "start_instance");
1637 lp_build_name(fetch_elts
, "fetch_elts");
1643 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1644 builder
= gallivm
->builder
;
1645 LLVMPositionBuilderAtEnd(builder
, block
);
1647 memset(&vs_type
, 0, sizeof vs_type
);
1648 vs_type
.floating
= TRUE
; /* floating point values */
1649 vs_type
.sign
= TRUE
; /* values are signed */
1650 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1651 vs_type
.width
= 32; /* 32-bit float */
1652 vs_type
.length
= vector_length
;
1654 lp_build_context_init(&bld
, gallivm
, lp_type_uint(32));
1655 lp_build_context_init(&blduivec
, gallivm
, lp_uint_type(vs_type
));
1657 /* hold temporary "bool" clipmask */
1658 clipmask_bool_ptr
= lp_build_alloca(gallivm
, blduivec
.vec_type
, "");
1660 fake_buf
= lp_build_alloca_undef(gallivm
,
1661 LLVMVectorType(LLVMInt64TypeInContext(context
), 4), "");
1662 fake_buf
= LLVMBuildBitCast(builder
, fake_buf
,
1663 LLVMPointerType(LLVMInt8TypeInContext(context
), 0), "");
1664 fake_buf_ptr
= LLVMBuildGEP(builder
, fake_buf
, &bld
.zero
, 1, "");
1666 /* code generated texture sampling */
1667 sampler
= draw_llvm_sampler_soa_create(draw_llvm_variant_key_samplers(key
));
1669 step
= lp_build_const_int32(gallivm
, vector_length
);
1671 ind_vec
= blduivec
.undef
;
1672 for (i
= 0; i
< vs_type
.length
; i
++) {
1673 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
1674 ind_vec
= LLVMBuildInsertElement(builder
, ind_vec
, index
, index
, "");
1677 have_elts
= LLVMBuildICmp(builder
, LLVMIntNE
,
1678 LLVMConstPointerNull(arg_types
[10]), fetch_elts
, "");
1680 fetch_max
= LLVMBuildSub(builder
, count
, bld
.one
, "fetch_max");
1681 fetch_max
= lp_build_broadcast_scalar(&blduivec
, fetch_max
);
1683 * Only needed for non-indexed path.
1685 start_vec
= lp_build_broadcast_scalar(&blduivec
, start_or_maxelt
);
1688 * Pre-calculate everything which is constant per shader invocation.
1690 for (j
= 0; j
< key
->nr_vertex_elements
; ++j
) {
1691 LLVMValueRef vb_buffer_offset
, buffer_size
, temp_ptr
;
1692 LLVMValueRef vb_info
, vbuffer_ptr
, buf_offset
, ofbit
;
1693 struct pipe_vertex_element
*velem
= &key
->vertex_element
[j
];
1694 LLVMValueRef vb_index
=
1695 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1696 LLVMValueRef bsize
= lp_build_const_int32(gallivm
,
1697 util_format_get_blocksize(velem
->src_format
));
1698 LLVMValueRef src_offset
= lp_build_const_int32(gallivm
,
1700 struct lp_build_if_state if_ctx
;
1702 if (velem
->src_format
!= PIPE_FORMAT_NONE
) {
1703 vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
, &vb_index
, 1, "");
1704 vb_info
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1705 vb_stride
[j
] = draw_jit_vbuffer_stride(gallivm
, vb_info
);
1706 vb_stride
[j
] = LLVMBuildZExt(gallivm
->builder
, vb_stride
[j
],
1707 LLVMInt32TypeInContext(context
), "");
1708 vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vb_info
);
1709 map_ptr
[j
] = draw_jit_dvbuffer_map(gallivm
, vbuffer_ptr
);
1710 buffer_size
= draw_jit_dvbuffer_size(gallivm
, vbuffer_ptr
);
1714 * We'll set buffer_size_adj to zero if we have of, so it will
1715 * always overflow later automatically without having to keep ofbit.
1716 * Overflows (with normal wraparound) doing the actual offset
1717 * calculation should be ok, just not for the buffer size calc.
1718 * It would also be possible to detect such overflows and return
1719 * zeros if that happens, but this would be more complex.
1721 buf_offset
= lp_build_add(&bld
, vb_buffer_offset
, src_offset
);
1722 tmp
= lp_build_sub(&bld
, bsize
, bld
.one
);
1723 buffer_size_adj
[j
] = lp_build_usub_overflow(gallivm
, buffer_size
, tmp
,
1725 buffer_size_adj
[j
] = lp_build_usub_overflow(gallivm
, buffer_size_adj
[j
],
1726 buf_offset
, &ofbit
);
1729 * We can't easily set fake vertex buffers outside the generated code.
1730 * Hence, set fake vertex buffers here instead basically, so fetch
1731 * code can always fetch using offset 0, eliminating all control flow
1732 * inside the main loop.
1733 * (Alternatively, could have control flow per vector skipping fetch
1734 * if ofbit is true.)
1736 if (velem
->instance_divisor
) {
1738 * Index is equal to the start instance plus the number of current
1739 * instance divided by the divisor. In this case we compute it as:
1740 * index = start_instance + (instance_id / divisor).
1741 * Note we could actually do the fetch here, outside the loop -
1742 * it's all constant, hopefully llvm recognizes this.
1744 LLVMValueRef current_instance
;
1745 current_instance
= LLVMBuildUDiv(builder
, system_values
.instance_id
,
1746 lp_build_const_int32(gallivm
,
1747 velem
->instance_divisor
),
1748 "instance_divisor");
1749 instance_index
[j
] = lp_build_uadd_overflow(gallivm
, start_instance
,
1750 current_instance
, &ofbit
);
1753 buffer_size_adj
[j
] = LLVMBuildSelect(builder
, ofbit
, bld
.zero
,
1754 buffer_size_adj
[j
], "");
1756 temp_ptr
= lp_build_alloca_undef(gallivm
,
1757 LLVMPointerType(LLVMInt8TypeInContext(context
), 0), "");
1759 lp_build_if(&if_ctx
, gallivm
, ofbit
);
1761 LLVMBuildStore(builder
, fake_buf_ptr
, temp_ptr
);
1763 lp_build_else(&if_ctx
);
1765 map_ptr
[j
] = LLVMBuildGEP(builder
, map_ptr
[j
], &buf_offset
, 1, "");
1766 LLVMBuildStore(builder
, map_ptr
[j
], temp_ptr
);
1768 lp_build_endif(&if_ctx
);
1769 map_ptr
[j
] = LLVMBuildLoad(builder
, temp_ptr
, "map_ptr");
1772 lp_build_printf(gallivm
, "velem %d, vbuf index = %u, vb_stride = %u\n",
1773 lp_build_const_int32(gallivm
, j
),
1774 vb_index
, vb_stride
[j
]);
1775 lp_build_printf(gallivm
,
1776 " vb_buffer_offset = %u, src_offset = %u, buf_offset = %u\n",
1777 vb_buffer_offset
, src_offset
, buf_offset
);
1778 lp_build_printf(gallivm
, " buffer size = %u, blocksize = %u\n",
1779 buffer_size
, bsize
);
1780 lp_build_printf(gallivm
, " instance_id = %u\n", system_values
.instance_id
);
1785 lp_build_loop_begin(&lp_loop
, gallivm
, bld
.zero
);
1787 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1789 LLVMValueRef clipmask
; /* holds the clipmask value */
1790 LLVMValueRef true_index_array
, index_store
;
1791 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1793 io_itr
= lp_loop
.counter
;
1795 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1797 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1798 io_itr
, io
, lp_loop
.counter
);
1801 true_index_array
= lp_build_broadcast_scalar(&blduivec
, lp_loop
.counter
);
1802 true_index_array
= LLVMBuildAdd(builder
, true_index_array
, ind_vec
, "");
1805 * Limit indices to fetch_max, otherwise might try to access indices
1806 * beyond index buffer (or rather vsplit elt buffer) size.
1807 * Could probably safely (?) skip this for non-indexed draws and
1808 * simplify things minimally (by removing it could combine the ind_vec
1809 * and start_vec adds). I think the only effect for non-indexed draws will
1810 * be that for the invalid elements they will be all fetched from the
1811 * same location as the last valid one, but noone should really care.
1813 true_index_array
= lp_build_min(&blduivec
, true_index_array
, fetch_max
);
1815 index_store
= lp_build_alloca_undef(gallivm
, blduivec
.vec_type
, "index_store");
1817 lp_build_if(&if_ctx
, gallivm
, have_elts
);
1820 * Note: you'd expect some comparison/clamp against fetch_elt_max
1822 * There used to be one here but it was incorrect: overflow was
1823 * detected if index > fetch_elt_max - but the correct condition
1824 * would be index >= fetch_elt_max (since this is just size of elts
1825 * buffer / element size).
1826 * Using the correct condition however will cause failures - due to
1827 * vsplit/vcache code which rebases indices. So, as an example, if
1828 * fetch_elt_max is just 1 and fetch_count 2, vsplit cache will
1829 * replace all invalid indices with 0 - which in case of elt_bias
1830 * not being zero will get a different fetch index than the valid
1831 * index 0. So, just rely on vsplit code preventing out-of-bounds
1832 * fetches. This is also why it's safe to do elts fetch even if there
1833 * was no index buffer bound - the real buffer is never seen here, at
1834 * least not if there are index buffer overflows...
1838 * XXX should not have to do this, as scale can be handled
1839 * natively by loads (hits asserts though).
1841 tmp
= lp_build_shl_imm(&blduivec
, true_index_array
, 2);
1842 fetch_elts
= LLVMBuildBitCast(builder
, fetch_elts
,
1843 LLVMPointerType(LLVMInt8TypeInContext(context
),
1845 tmp
= lp_build_gather(gallivm
, vs_type
.length
,
1847 fetch_elts
, tmp
, FALSE
);
1848 LLVMBuildStore(builder
, tmp
, index_store
);
1850 lp_build_else(&if_ctx
);
1852 tmp
= LLVMBuildAdd(builder
, true_index_array
, start_vec
, "");
1853 LLVMBuildStore(builder
, tmp
, index_store
);
1855 lp_build_endif(&if_ctx
);
1857 true_index_array
= LLVMBuildLoad(builder
, index_store
, "");
1859 for (j
= 0; j
< key
->nr_vertex_elements
; ++j
) {
1860 struct pipe_vertex_element
*velem
= &key
->vertex_element
[j
];
1861 const struct util_format_description
*format_desc
=
1862 util_format_description(velem
->src_format
);
1864 if (format_desc
->format
== PIPE_FORMAT_NONE
) {
1865 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1866 inputs
[j
][i
] = lp_build_zero(gallivm
, vs_type
);
1869 else if (velem
->instance_divisor
) {
1870 fetch_instanced(gallivm
, format_desc
, vs_type
,
1871 vb_stride
[j
], map_ptr
[j
],
1873 inputs
[j
], instance_index
[j
]);
1876 fetch_vector(gallivm
, format_desc
, vs_type
,
1877 vb_stride
[j
], map_ptr
[j
],
1879 inputs
[j
], true_index_array
);
1883 /* In the paths with elts vertex id has to be unaffected by the
1884 * index bias and because indices inside our elements array have
1885 * already had index bias applied we need to subtract it here to
1886 * get back to the original index.
1887 * in the linear paths vertex id has to be unaffected by the
1888 * original start index and because we abuse the 'start' variable
1889 * to either represent the actual start index or the index at which
1890 * the primitive was split (we split rendering into chunks of at
1891 * most 4095-vertices) we need to back out the original start
1892 * index out of our vertex id here.
1894 system_values
.basevertex
= lp_build_broadcast_scalar(&blduivec
,
1896 system_values
.vertex_id
= true_index_array
;
1897 system_values
.vertex_id_nobase
= LLVMBuildSub(builder
, true_index_array
,
1898 system_values
.basevertex
, "");
1900 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1901 generate_vs(variant
,
1909 key
->clamp_vertex_color
);
1911 if (pos
!= -1 && cv
!= -1) {
1912 /* store original positions in clip before further manipulation */
1913 store_clip(gallivm
, vs_type
, io
, outputs
, pos
);
1916 if (enable_cliptest
) {
1917 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1918 /* allocate clipmask, assign it integer type */
1919 clipmask
= generate_clipmask(llvm
,
1924 context_ptr
, &have_clipdist
);
1925 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1926 /* store temporary clipping boolean value */
1927 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1930 clipmask
= blduivec
.zero
;
1933 /* do viewport mapping */
1934 if (!bypass_viewport
) {
1935 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1939 clipmask
= blduivec
.zero
;
1942 /* store clipmask in vertex header,
1943 * original positions in clip
1944 * and transformed positions in data
1946 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
1947 vs_info
->num_outputs
, vs_type
,
1948 enable_cliptest
&& key
->need_edgeflags
);
1950 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
1952 sampler
->destroy(sampler
);
1954 /* return clipping boolean value for function */
1955 ret
= clipmask_booli8(gallivm
, vs_type
, clipmask_bool_ptr
,
1956 enable_cliptest
&& key
->need_edgeflags
);
1958 LLVMBuildRet(builder
, ret
);
1960 gallivm_verify_function(gallivm
, variant_func
);
1964 struct draw_llvm_variant_key
*
1965 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1968 struct draw_llvm_variant_key
*key
;
1969 struct draw_sampler_static_state
*draw_sampler
;
1971 key
= (struct draw_llvm_variant_key
*)store
;
1973 memset(key
, 0, offsetof(struct draw_llvm_variant_key
, vertex_element
[0]));
1975 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1977 /* will have to rig this up properly later */
1978 key
->clip_xy
= llvm
->draw
->clip_xy
;
1979 key
->clip_z
= llvm
->draw
->clip_z
;
1980 key
->clip_user
= llvm
->draw
->clip_user
;
1981 key
->bypass_viewport
= llvm
->draw
->bypass_viewport
;
1982 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
1983 /* XXX assumes edgeflag output not at 0 */
1984 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1985 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1986 key
->has_gs
= llvm
->draw
->gs
.geometry_shader
!= NULL
;
1987 key
->num_outputs
= draw_total_vs_outputs(llvm
->draw
);
1989 /* All variants of this shader will have the same value for
1990 * nr_samplers. Not yet trying to compact away holes in the
1993 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1994 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
1995 key
->nr_sampler_views
=
1996 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
1999 key
->nr_sampler_views
= key
->nr_samplers
;
2002 /* Presumably all variants of the shader should have the same
2003 * number of vertex elements - ie the number of shader inputs.
2004 * NOTE: we NEED to store the needed number of needed inputs
2005 * here, not the number of provided elements to match keysize
2006 * (and the offset of sampler state in the key).
2007 * If we have excess number of vertex elements, this is valid,
2008 * but the excess ones don't matter.
2009 * If we don't have enough vertex elements (which looks not really
2010 * valid but we'll handle it gracefully) fill out missing ones with
2011 * zero (we'll recognize these later by PIPE_FORMAT_NONE).
2013 key
->nr_vertex_elements
=
2014 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
2016 if (llvm
->draw
->pt
.nr_vertex_elements
< key
->nr_vertex_elements
) {
2017 debug_printf("draw: vs with %d inputs but only have %d vertex elements\n",
2018 key
->nr_vertex_elements
, llvm
->draw
->pt
.nr_vertex_elements
);
2019 memset(key
->vertex_element
, 0,
2020 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
2022 memcpy(key
->vertex_element
,
2023 llvm
->draw
->pt
.vertex_element
,
2024 sizeof(struct pipe_vertex_element
) *
2025 MIN2(key
->nr_vertex_elements
, llvm
->draw
->pt
.nr_vertex_elements
));
2027 draw_sampler
= draw_llvm_variant_key_samplers(key
);
2028 memset(draw_sampler
, 0,
2029 MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2031 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2032 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2033 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
2035 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2036 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2037 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
2045 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
2048 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
2050 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
2051 debug_printf("clip_xy = %u\n", key
->clip_xy
);
2052 debug_printf("clip_z = %u\n", key
->clip_z
);
2053 debug_printf("clip_user = %u\n", key
->clip_user
);
2054 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
2055 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
2056 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
2057 debug_printf("has_gs = %u\n", key
->has_gs
);
2058 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
2060 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
2061 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
2062 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
2063 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
2064 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
2067 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2068 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
2074 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
2075 enum pipe_shader_type shader_stage
,
2077 uint32_t width
, uint32_t height
, uint32_t depth
,
2078 uint32_t first_level
, uint32_t last_level
,
2079 const void *base_ptr
,
2080 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
2081 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
2082 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
2085 struct draw_jit_texture
*jit_tex
;
2087 assert(shader_stage
== PIPE_SHADER_VERTEX
||
2088 shader_stage
== PIPE_SHADER_GEOMETRY
);
2090 if (shader_stage
== PIPE_SHADER_VERTEX
) {
2091 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->jit_context
.textures
));
2093 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
2094 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
2095 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->gs_jit_context
.textures
));
2097 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
2103 jit_tex
->width
= width
;
2104 jit_tex
->height
= height
;
2105 jit_tex
->depth
= depth
;
2106 jit_tex
->first_level
= first_level
;
2107 jit_tex
->last_level
= last_level
;
2108 jit_tex
->base
= base_ptr
;
2110 for (j
= first_level
; j
<= last_level
; j
++) {
2111 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
2112 jit_tex
->row_stride
[j
] = row_stride
[j
];
2113 jit_tex
->img_stride
[j
] = img_stride
[j
];
2119 draw_llvm_set_sampler_state(struct draw_context
*draw
,
2120 enum pipe_shader_type shader_type
)
2124 if (shader_type
== PIPE_SHADER_VERTEX
) {
2125 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
2126 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
2128 if (draw
->samplers
[PIPE_SHADER_VERTEX
][i
]) {
2129 const struct pipe_sampler_state
*s
2130 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
2131 jit_sam
->min_lod
= s
->min_lod
;
2132 jit_sam
->max_lod
= s
->max_lod
;
2133 jit_sam
->lod_bias
= s
->lod_bias
;
2134 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2137 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
2138 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
2139 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
2141 if (draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]) {
2142 const struct pipe_sampler_state
*s
2143 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
2144 jit_sam
->min_lod
= s
->min_lod
;
2145 jit_sam
->max_lod
= s
->max_lod
;
2146 jit_sam
->lod_bias
= s
->lod_bias
;
2147 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2155 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
2157 struct draw_llvm
*llvm
= variant
->llvm
;
2159 gallivm_destroy(variant
->gallivm
);
2161 remove_from_list(&variant
->list_item_local
);
2162 variant
->shader
->variants_cached
--;
2163 remove_from_list(&variant
->list_item_global
);
2164 llvm
->nr_variants
--;
2170 * Create LLVM types for various structures.
2173 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
2175 struct gallivm_state
*gallivm
= var
->gallivm
;
2176 LLVMTypeRef texture_type
, sampler_type
, context_type
;
2178 texture_type
= create_jit_texture_type(gallivm
, "texture");
2179 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2181 context_type
= create_gs_jit_context_type(gallivm
,
2182 var
->shader
->base
.vector_length
,
2183 texture_type
, sampler_type
,
2184 "draw_gs_jit_context");
2185 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2187 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
2191 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
2193 if (!variant
->context_ptr_type
)
2194 create_gs_jit_types(variant
);
2195 return variant
->context_ptr_type
;
2199 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2200 struct lp_type gs_type
)
2202 struct gallivm_state
*gallivm
= variant
->gallivm
;
2203 LLVMBuilderRef builder
= gallivm
->builder
;
2204 struct lp_type mask_type
= lp_int_type(gs_type
);
2205 LLVMValueRef num_prims
;
2206 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
2209 num_prims
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
),
2210 variant
->num_prims
);
2211 for (i
= 0; i
< gs_type
.length
; i
++) {
2212 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
2213 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, idx
, idx
, "");
2215 mask_val
= lp_build_compare(gallivm
, mask_type
,
2216 PIPE_FUNC_GREATER
, num_prims
, mask_val
);
2222 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2223 struct draw_gs_llvm_variant
*variant
)
2225 struct gallivm_state
*gallivm
= variant
->gallivm
;
2226 LLVMContextRef context
= gallivm
->context
;
2227 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2228 LLVMTypeRef arg_types
[7];
2229 LLVMTypeRef func_type
;
2230 LLVMValueRef variant_func
;
2231 LLVMValueRef context_ptr
;
2232 LLVMValueRef prim_id_ptr
;
2233 LLVMBasicBlockRef block
;
2234 LLVMBuilderRef builder
;
2235 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2236 struct lp_build_sampler_soa
*sampler
= 0;
2237 struct lp_build_context bld
;
2238 struct lp_bld_tgsi_system_values system_values
;
2240 struct lp_type gs_type
;
2242 struct draw_gs_llvm_iface gs_iface
;
2243 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2244 LLVMValueRef consts_ptr
, num_consts_ptr
;
2245 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2246 struct lp_build_mask_context mask
;
2247 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2248 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2250 memset(&system_values
, 0, sizeof(system_values
));
2252 util_snprintf(func_name
, sizeof(func_name
), "draw_llvm_gs_variant%u",
2253 variant
->shader
->variants_cached
);
2255 assert(variant
->vertex_header_ptr_type
);
2257 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2258 arg_types
[1] = variant
->input_array_type
; /* input */
2259 arg_types
[2] = variant
->vertex_header_ptr_type
; /* vertex_header */
2260 arg_types
[3] = int32_type
; /* num_prims */
2261 arg_types
[4] = int32_type
; /* instance_id */
2262 arg_types
[5] = LLVMPointerType(
2263 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2264 arg_types
[6] = int32_type
;
2266 func_type
= LLVMFunctionType(int32_type
, arg_types
, ARRAY_SIZE(arg_types
), 0);
2268 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
2270 variant
->function
= variant_func
;
2272 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2274 for (i
= 0; i
< ARRAY_SIZE(arg_types
); ++i
)
2275 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2276 lp_add_function_attr(variant_func
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
2278 context_ptr
= LLVMGetParam(variant_func
, 0);
2279 input_array
= LLVMGetParam(variant_func
, 1);
2280 io_ptr
= LLVMGetParam(variant_func
, 2);
2281 num_prims
= LLVMGetParam(variant_func
, 3);
2282 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2283 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2284 system_values
.invocation_id
= LLVMGetParam(variant_func
, 6);
2286 lp_build_name(context_ptr
, "context");
2287 lp_build_name(input_array
, "input");
2288 lp_build_name(io_ptr
, "io");
2289 lp_build_name(num_prims
, "num_prims");
2290 lp_build_name(system_values
.instance_id
, "instance_id");
2291 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2292 lp_build_name(system_values
.invocation_id
, "invocation_id");
2294 variant
->context_ptr
= context_ptr
;
2295 variant
->io_ptr
= io_ptr
;
2296 variant
->num_prims
= num_prims
;
2298 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2299 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2300 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2301 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2302 gs_iface
.input
= input_array
;
2303 gs_iface
.variant
= variant
;
2309 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2310 builder
= gallivm
->builder
;
2311 LLVMPositionBuilderAtEnd(builder
, block
);
2313 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2315 memset(&gs_type
, 0, sizeof gs_type
);
2316 gs_type
.floating
= TRUE
; /* floating point values */
2317 gs_type
.sign
= TRUE
; /* values are signed */
2318 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2319 gs_type
.width
= 32; /* 32-bit float */
2320 gs_type
.length
= vector_length
;
2322 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2324 draw_gs_jit_context_num_constants(variant
->gallivm
, context_ptr
);
2326 /* code generated texture sampling */
2327 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
);
2329 mask_val
= generate_mask_value(variant
, gs_type
);
2330 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2332 if (gs_info
->uses_primid
) {
2333 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");
2336 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2337 tgsi_dump(tokens
, 0);
2338 draw_gs_llvm_dump_variant_key(&variant
->key
);
2341 lp_build_tgsi_soa(variant
->gallivm
,
2353 &llvm
->draw
->gs
.geometry_shader
->info
,
2354 (const struct lp_build_tgsi_gs_iface
*)&gs_iface
);
2356 sampler
->destroy(sampler
);
2358 lp_build_mask_end(&mask
);
2360 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2362 gallivm_verify_function(gallivm
, variant_func
);
2366 struct draw_gs_llvm_variant
*
2367 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2368 unsigned num_outputs
,
2369 const struct draw_gs_llvm_variant_key
*key
)
2371 struct draw_gs_llvm_variant
*variant
;
2372 struct llvm_geometry_shader
*shader
=
2373 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2374 LLVMTypeRef vertex_header
;
2375 char module_name
[64];
2377 variant
= MALLOC(sizeof *variant
+
2378 shader
->variant_key_size
-
2379 sizeof variant
->key
);
2383 variant
->llvm
= llvm
;
2384 variant
->shader
= shader
;
2386 util_snprintf(module_name
, sizeof(module_name
), "draw_llvm_gs_variant%u",
2387 variant
->shader
->variants_cached
);
2389 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
2391 create_gs_jit_types(variant
);
2393 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2395 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2397 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2399 draw_gs_llvm_generate(llvm
, variant
);
2401 gallivm_compile_module(variant
->gallivm
);
2403 variant
->jit_func
= (draw_gs_jit_func
)
2404 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2406 gallivm_free_ir(variant
->gallivm
);
2408 variant
->list_item_global
.base
= variant
;
2409 variant
->list_item_local
.base
= variant
;
2410 /*variant->no = */shader
->variants_created
++;
2411 variant
->list_item_global
.base
= variant
;
2417 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2419 struct draw_llvm
*llvm
= variant
->llvm
;
2421 gallivm_destroy(variant
->gallivm
);
2423 remove_from_list(&variant
->list_item_local
);
2424 variant
->shader
->variants_cached
--;
2425 remove_from_list(&variant
->list_item_global
);
2426 llvm
->nr_gs_variants
--;
2430 struct draw_gs_llvm_variant_key
*
2431 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2434 struct draw_gs_llvm_variant_key
*key
;
2435 struct draw_sampler_static_state
*draw_sampler
;
2437 key
= (struct draw_gs_llvm_variant_key
*)store
;
2439 memset(key
, 0, offsetof(struct draw_gs_llvm_variant_key
, samplers
[0]));
2441 key
->num_outputs
= draw_total_gs_outputs(llvm
->draw
);
2443 /* All variants of this shader will have the same value for
2444 * nr_samplers. Not yet trying to compact away holes in the
2447 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2448 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2449 key
->nr_sampler_views
=
2450 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2453 key
->nr_sampler_views
= key
->nr_samplers
;
2456 draw_sampler
= key
->samplers
;
2458 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2460 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2461 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2462 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2464 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2465 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2466 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2473 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2476 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2478 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2479 debug_printf("sampler[%i].src_format = %s\n", i
,
2480 util_format_name(sampler
[i
].texture_state
.format
));