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 ARRAY_SIZE(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 ARRAY_SIZE(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 ARRAY_SIZE(elem_types
), 0);
191 (void) target
; /* silence unused var warning for non-debug build */
192 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, min_lod
,
193 target
, sampler_type
,
194 DRAW_JIT_SAMPLER_MIN_LOD
);
195 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, max_lod
,
196 target
, sampler_type
,
197 DRAW_JIT_SAMPLER_MAX_LOD
);
198 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, lod_bias
,
199 target
, sampler_type
,
200 DRAW_JIT_SAMPLER_LOD_BIAS
);
201 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, border_color
,
202 target
, sampler_type
,
203 DRAW_JIT_SAMPLER_BORDER_COLOR
);
205 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler
, target
, sampler_type
);
212 * Create LLVM type for struct draw_jit_context
215 create_jit_context_type(struct gallivm_state
*gallivm
,
216 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
217 const char *struct_name
)
219 LLVMTargetDataRef target
= gallivm
->target
;
220 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
221 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
222 LLVMTypeRef elem_types
[DRAW_JIT_CTX_NUM_FIELDS
];
223 LLVMTypeRef context_type
;
225 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
226 LP_MAX_TGSI_CONST_BUFFERS
);
227 elem_types
[1] = LLVMArrayType(int_type
, /* num_vs_constants */
228 LP_MAX_TGSI_CONST_BUFFERS
);
229 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
230 DRAW_TOTAL_CLIP_PLANES
), 0);
231 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewports */
232 elem_types
[4] = LLVMArrayType(texture_type
,
233 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
234 elem_types
[5] = LLVMArrayType(sampler_type
,
235 PIPE_MAX_SAMPLERS
); /* samplers */
236 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
237 ARRAY_SIZE(elem_types
), 0);
239 (void) target
; /* silence unused var warning for non-debug build */
240 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
241 target
, context_type
, DRAW_JIT_CTX_CONSTANTS
);
242 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, num_vs_constants
,
243 target
, context_type
, DRAW_JIT_CTX_NUM_CONSTANTS
);
244 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
245 target
, context_type
, DRAW_JIT_CTX_PLANES
);
246 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewports
,
247 target
, context_type
, DRAW_JIT_CTX_VIEWPORT
);
248 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
249 target
, context_type
,
250 DRAW_JIT_CTX_TEXTURES
);
251 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
252 target
, context_type
,
253 DRAW_JIT_CTX_SAMPLERS
);
254 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
255 target
, context_type
);
262 * Create LLVM type for struct draw_gs_jit_context
265 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
266 unsigned vector_length
,
267 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
268 const char *struct_name
)
270 LLVMTargetDataRef target
= gallivm
->target
;
271 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
272 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
273 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
274 LLVMTypeRef context_type
;
276 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
277 LP_MAX_TGSI_CONST_BUFFERS
);
278 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
279 LP_MAX_TGSI_CONST_BUFFERS
);
280 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
281 DRAW_TOTAL_CLIP_PLANES
), 0);
282 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewports */
284 elem_types
[4] = LLVMArrayType(texture_type
,
285 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
286 elem_types
[5] = LLVMArrayType(sampler_type
,
287 PIPE_MAX_SAMPLERS
); /* samplers */
289 elem_types
[6] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
290 elem_types
[7] = LLVMPointerType(LLVMVectorType(int_type
,
292 elem_types
[8] = LLVMPointerType(LLVMVectorType(int_type
,
295 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
296 ARRAY_SIZE(elem_types
), 0);
298 (void) target
; /* silence unused var warning for non-debug build */
299 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
300 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
301 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, num_constants
,
302 target
, context_type
, DRAW_GS_JIT_CTX_NUM_CONSTANTS
);
303 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
304 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
305 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewports
,
306 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
307 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
308 target
, context_type
,
309 DRAW_GS_JIT_CTX_TEXTURES
);
310 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
311 target
, context_type
,
312 DRAW_GS_JIT_CTX_SAMPLERS
);
313 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
314 target
, context_type
,
315 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
316 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
317 target
, context_type
,
318 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
319 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
320 target
, context_type
,
321 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
322 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
323 target
, context_type
);
330 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
332 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
333 LLVMTypeRef input_array
;
335 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
336 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
337 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
338 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
344 * Create LLVM type for struct pipe_vertex_buffer
347 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
,
348 const char *struct_name
)
350 LLVMTargetDataRef target
= gallivm
->target
;
351 LLVMTypeRef elem_types
[4];
355 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
357 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
359 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
360 ARRAY_SIZE(elem_types
), 0);
362 (void) target
; /* silence unused var warning for non-debug build */
363 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
365 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
368 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
375 * Create LLVM type for struct vertex_header;
378 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
380 LLVMTargetDataRef target
= gallivm
->target
;
381 LLVMTypeRef elem_types
[3];
382 LLVMTypeRef vertex_header
;
383 char struct_name
[24];
385 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
387 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
388 elem_types
[DRAW_JIT_VERTEX_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
389 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
391 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
392 ARRAY_SIZE(elem_types
), 0);
394 /* these are bit-fields and we can't take address of them
395 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
396 target, vertex_header,
397 DRAW_JIT_VERTEX_CLIPMASK);
398 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
399 target, vertex_header,
400 DRAW_JIT_VERTEX_EDGEFLAG);
401 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
402 target, vertex_header,
403 DRAW_JIT_VERTEX_PAD);
404 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
405 target, vertex_header,
406 DRAW_JIT_VERTEX_VERTEX_ID);
408 (void) target
; /* silence unused var warning for non-debug build */
409 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip_pos
,
410 target
, vertex_header
,
411 DRAW_JIT_VERTEX_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*/,
630 &llvm
->draw
->vs
.vertex_shader
->info
,
635 unsigned chan
, attrib
;
636 struct lp_build_context bld
;
637 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
638 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
640 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
641 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
642 if (outputs
[attrib
][chan
]) {
643 switch (info
->output_semantic_name
[attrib
]) {
644 case TGSI_SEMANTIC_COLOR
:
645 case TGSI_SEMANTIC_BCOLOR
:
646 if (clamp_vertex_color
) {
647 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
648 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
649 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
660 generate_fetch(struct gallivm_state
*gallivm
,
661 const struct util_format_description
*format_desc
,
662 LLVMValueRef vb_stride
,
663 LLVMValueRef stride_fixed
,
664 LLVMValueRef map_ptr
,
665 LLVMValueRef buffer_size_adj
,
670 LLVMValueRef zero
= LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
));
671 LLVMBuilderRef builder
= gallivm
->builder
;
673 LLVMValueRef buffer_overflowed
;
674 LLVMValueRef temp_ptr
=
675 lp_build_alloca(gallivm
,
676 lp_build_vec_type(gallivm
, lp_float32_vec4_type()), "");
677 struct lp_build_if_state if_ctx
;
679 stride
= lp_build_umul_overflow(gallivm
, vb_stride
, index
, &ofbit
);
680 stride
= lp_build_uadd_overflow(gallivm
, stride
, stride_fixed
, &ofbit
);
682 buffer_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
683 stride
, buffer_size_adj
,
684 "buffer_overflowed");
685 buffer_overflowed
= LLVMBuildOr(builder
, buffer_overflowed
, ofbit
, "");
688 lp_build_printf(gallivm
, " stride = %u\n", stride
);
689 lp_build_printf(gallivm
, " buffer size adj = %u\n", buffer_size_adj
);
690 lp_build_print_value(gallivm
, " buffer overflowed = ", buffer_overflowed
);
693 lp_build_if(&if_ctx
, gallivm
, buffer_overflowed
);
696 lp_build_const_vec(gallivm
, lp_float32_vec4_type(), 0);
697 LLVMBuildStore(builder
, val
, temp_ptr
);
699 lp_build_else(&if_ctx
);
702 map_ptr
= LLVMBuildGEP(builder
, map_ptr
, &stride
, 1, "");
704 val
= lp_build_fetch_rgba_aos(gallivm
,
706 lp_float32_vec4_type(),
711 LLVMBuildStore(builder
, val
, temp_ptr
);
713 lp_build_endif(&if_ctx
);
715 *res
= LLVMBuildLoad(builder
, temp_ptr
, "aos");
719 convert_to_soa(struct gallivm_state
*gallivm
,
720 LLVMValueRef src_aos
[LP_MAX_VECTOR_WIDTH
/ 32],
721 LLVMValueRef (*dst_soa
)[TGSI_NUM_CHANNELS
],
722 unsigned attrib
, const struct lp_type soa_type
)
725 struct lp_type aos_channel_type
= soa_type
;
727 LLVMValueRef aos_channels
[TGSI_NUM_CHANNELS
];
728 unsigned pixels_per_channel
= soa_type
.length
/ TGSI_NUM_CHANNELS
;
730 debug_assert(TGSI_NUM_CHANNELS
== 4);
731 debug_assert((soa_type
.length
% TGSI_NUM_CHANNELS
) == 0);
733 aos_channel_type
.length
>>= 1;
735 for (j
= 0; j
< TGSI_NUM_CHANNELS
; ++j
) {
736 LLVMValueRef channel
[LP_MAX_VECTOR_LENGTH
] = { 0 };
738 assert(pixels_per_channel
<= LP_MAX_VECTOR_LENGTH
);
740 for (k
= 0; k
< pixels_per_channel
; ++k
) {
741 channel
[k
] = src_aos
[j
+ TGSI_NUM_CHANNELS
* k
];
744 aos_channels
[j
] = lp_build_concat(gallivm
, channel
, aos_channel_type
, pixels_per_channel
);
747 lp_build_transpose_aos(gallivm
, soa_type
, aos_channels
, dst_soa
[attrib
]);
752 store_aos(struct gallivm_state
*gallivm
,
757 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
758 LLVMBuilderRef builder
= gallivm
->builder
;
759 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
760 LLVMValueRef indices
[3];
762 indices
[0] = lp_build_const_int32(gallivm
, 0);
764 indices
[2] = lp_build_const_int32(gallivm
, 0);
766 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
767 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
770 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
773 /* Unaligned store due to the vertex header */
774 LLVMSetAlignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
778 * Adjust the mask to architecture endianess. The mask will the store in struct:
780 * struct vertex_header {
781 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
782 * unsigned edgeflag:1;
784 * unsigned vertex_id:16;
788 * On little-endian machine nothing needs to done, however on bit-endian machine
789 * the mask's fields need to be adjusted with the algorithm:
791 * uint32_t reverse (uint32_t x)
793 * return (x >> 16) | // vertex_id
794 * ((x & 0x3fff) << 18) | // clipmask
795 * ((x & 0x4000) << 3) | // pad
796 * ((x & 0x8000) << 1); // edgeflag
800 adjust_mask(struct gallivm_state
*gallivm
,
803 #ifdef PIPE_ARCH_BIG_ENDIAN
804 LLVMBuilderRef builder
= gallivm
->builder
;
805 LLVMValueRef vertex_id
;
806 LLVMValueRef clipmask
;
808 LLVMValueRef edgeflag
;
810 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
811 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
812 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
814 pad
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
815 pad
= LLVMBuildShl(builder
, pad
, lp_build_const_int32(gallivm
, 3), "");
817 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
818 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
820 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
822 mask
= LLVMBuildOr(builder
, mask
, pad
, "");
824 mask
= LLVMBuildOr(builder
, mask
, edgeflag
, "");
830 store_aos_array(struct gallivm_state
*gallivm
,
831 struct lp_type soa_type
,
833 LLVMValueRef
*indices
,
837 LLVMValueRef clipmask
,
838 boolean need_edgeflag
)
840 LLVMBuilderRef builder
= gallivm
->builder
;
841 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
842 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
843 LLVMValueRef linear_inds
[LP_MAX_VECTOR_WIDTH
/ 32];
844 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
845 int vector_length
= soa_type
.length
;
848 debug_assert(TGSI_NUM_CHANNELS
== 4);
850 for (i
= 0; i
< vector_length
; i
++) {
851 linear_inds
[i
] = lp_build_const_int32(gallivm
, i
);
853 inds
[i
] = indices
[i
];
855 inds
[i
] = linear_inds
[i
];
857 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
861 /* store vertex header for each of the n vertices */
862 LLVMValueRef val
, cliptmp
;
863 int vertex_id_pad_edgeflag
;
865 /* If this assertion fails, it means we need to update the bit twidding
866 * code here. See struct vertex_header in draw_private.h.
868 assert(DRAW_TOTAL_CLIP_PLANES
==14);
869 /* initialize vertex id:16 = 0xffff, pad:1 = 0, edgeflag:1 = 1 */
870 if (!need_edgeflag
) {
871 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
874 vertex_id_pad_edgeflag
= (0xffff << 16);
876 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
),
877 vertex_id_pad_edgeflag
);
878 /* OR with the clipmask */
879 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
880 for (i
= 0; i
< vector_length
; i
++) {
881 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
882 val
= LLVMBuildExtractElement(builder
, cliptmp
, linear_inds
[i
], "");
883 val
= adjust_mask(gallivm
, val
);
885 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
886 io_ptrs
[i
], inds
[i
], val
);
888 LLVMBuildStore(builder
, val
, id_ptr
);
892 /* store for each of the n vertices */
893 for (i
= 0; i
< vector_length
; i
++) {
894 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
900 convert_to_aos(struct gallivm_state
*gallivm
,
902 LLVMValueRef
*indices
,
903 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
904 LLVMValueRef clipmask
,
906 struct lp_type soa_type
,
907 boolean need_edgeflag
)
909 LLVMBuilderRef builder
= gallivm
->builder
;
910 unsigned chan
, attrib
, i
;
913 lp_build_printf(gallivm
, " # storing begin\n");
915 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
916 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
917 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
918 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
919 if (outputs
[attrib
][chan
]) {
920 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
921 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
923 lp_build_printf(gallivm
, "output %d : %d ",
924 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
926 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
928 lp_build_print_value(gallivm
, "val = ", out
);
931 LLVMBuildBitCast(builder
, out
, lp_build_int_vec_type(gallivm
, soa_type
), "");
933 lp_build_print_value(gallivm
, " ival = ", iv
);
944 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
945 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
947 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
949 for (i
= 0; i
< soa_type
.length
; ++i
) {
950 aos
[i
] = lp_build_extract_range(gallivm
,
951 soa
[i
% TGSI_NUM_CHANNELS
],
952 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
957 store_aos_array(gallivm
,
967 lp_build_printf(gallivm
, " # storing end\n");
973 * Stores original vertex positions in clip coordinates
976 store_clip(struct gallivm_state
*gallivm
,
977 const struct lp_type vs_type
,
979 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
982 LLVMBuilderRef builder
= gallivm
->builder
;
984 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
985 LLVMValueRef indices
[2];
986 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
987 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
988 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
989 LLVMTypeRef clip_ptr_type
=
990 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
),
995 indices
[1] = lp_build_const_int32(gallivm
, 0);
997 for (i
= 0; i
< vs_type
.length
; i
++) {
998 inds
[i
] = lp_build_const_int32(gallivm
, i
);
999 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1002 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
1003 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
1004 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
1005 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
1007 for (i
= 0; i
< vs_type
.length
; i
++) {
1008 clip_ptrs
[i
] = draw_jit_header_clip_pos(gallivm
, io_ptrs
[i
]);
1011 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
1012 for (i
= 0; i
< vs_type
.length
; ++i
) {
1013 aos
[i
] = lp_build_extract_range(gallivm
,
1014 soa
[i
% TGSI_NUM_CHANNELS
],
1015 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1019 for (j
= 0; j
< vs_type
.length
; j
++) {
1020 LLVMValueRef clip_ptr
;
1022 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
1023 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
1025 /* Unaligned store */
1026 LLVMSetAlignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
1032 * Transforms the outputs for viewport mapping
1035 generate_viewport(struct draw_llvm_variant
*variant
,
1036 LLVMBuilderRef builder
,
1037 struct lp_type vs_type
,
1038 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1039 LLVMValueRef context_ptr
)
1042 struct gallivm_state
*gallivm
= variant
->gallivm
;
1043 struct lp_type f32_type
= vs_type
;
1044 const unsigned pos
= variant
->llvm
->draw
->vs
.position_output
;
1045 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1046 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn*/
1047 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1048 LLVMValueRef vp_ptr
= draw_jit_context_viewports(gallivm
, context_ptr
);
1050 /* We treat pipe_viewport_state as a float array */
1051 const int scale_index_offset
= offsetof(struct pipe_viewport_state
, scale
) / sizeof(float);
1052 const int trans_index_offset
= offsetof(struct pipe_viewport_state
, translate
) / sizeof(float);
1054 /* for 1/w convention*/
1055 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1056 LLVMBuildStore(builder
, out3
, outputs
[pos
][3]);
1058 /* Viewport Mapping */
1059 for (i
=0; i
<3; i
++) {
1060 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[pos
][i
], ""); /*x0 x1 .. xn*/
1063 LLVMValueRef scale_i
;
1064 LLVMValueRef trans_i
;
1067 index
= lp_build_const_int32(gallivm
, i
+ scale_index_offset
);
1068 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1070 index
= lp_build_const_int32(gallivm
, i
+ trans_index_offset
);
1071 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1073 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1074 LLVMBuildLoad(builder
, scale_i
, "scale"));
1075 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1076 LLVMBuildLoad(builder
, trans_i
, "trans"));
1079 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1080 /* mult by scale, add translation */
1081 out
= lp_build_fmuladd(builder
, out
, scale
, trans
);
1083 /* store transformed outputs */
1084 LLVMBuildStore(builder
, out
, outputs
[pos
][i
]);
1091 * Returns clipmask as nxi32 bitmask for the n vertices
1094 generate_clipmask(struct draw_llvm
*llvm
,
1095 struct gallivm_state
*gallivm
,
1096 struct lp_type vs_type
,
1097 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1098 struct draw_llvm_variant_key
*key
,
1099 LLVMValueRef context_ptr
,
1100 boolean
*have_clipdist
)
1102 LLVMBuilderRef builder
= gallivm
->builder
;
1103 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1104 LLVMValueRef test
, temp
;
1105 LLVMValueRef zero
, shift
;
1106 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1107 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1108 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1109 struct lp_type f32_type
= vs_type
;
1110 struct lp_type i32_type
= lp_int_type(vs_type
);
1111 const unsigned pos
= llvm
->draw
->vs
.position_output
;
1112 const unsigned cv
= llvm
->draw
->vs
.clipvertex_output
;
1113 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1114 boolean have_cd
= false;
1115 boolean clip_user
= key
->clip_user
;
1116 unsigned ucp_enable
= key
->ucp_enable
;
1119 cd
[0] = llvm
->draw
->vs
.ccdistance_output
[0];
1120 cd
[1] = llvm
->draw
->vs
.ccdistance_output
[1];
1122 if (cd
[0] != pos
|| cd
[1] != pos
)
1125 if (num_written_clipdistance
&& !clip_user
) {
1127 ucp_enable
= (1 << num_written_clipdistance
) - 1;
1130 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1131 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1132 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1133 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1136 * load clipvertex and position from correct locations.
1137 * if they are the same just load them once.
1139 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1140 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1141 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1142 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1144 if (clip_user
&& cv
!= pos
) {
1145 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1146 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1147 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1148 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1157 * Be careful with the comparisons and NaNs (using llvm's unordered
1158 * comparisons here).
1160 /* Cliptest, for hardwired planes */
1162 * XXX should take guardband into account (currently not in key).
1163 * Otherwise might run the draw pipeline stages for nothing.
1167 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1169 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1173 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1174 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1175 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1176 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1177 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1180 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1181 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1182 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1183 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1186 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1187 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1188 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1189 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1190 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1194 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1195 if (key
->clip_halfz
) {
1197 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1198 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1199 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1203 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1204 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1205 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1206 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1209 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1210 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1211 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1212 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1216 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1217 LLVMValueRef indices
[3];
1218 LLVMValueRef is_nan_or_inf
;
1220 /* userclip planes */
1221 while (ucp_enable
) {
1222 unsigned plane_idx
= ffs(ucp_enable
)-1;
1223 ucp_enable
&= ~(1 << plane_idx
);
1226 if (have_cd
&& num_written_clipdistance
) {
1227 LLVMValueRef clipdist
;
1231 *have_clipdist
= TRUE
;
1233 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1235 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1237 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1238 is_nan_or_inf
= lp_build_is_inf_or_nan(gallivm
, vs_type
, clipdist
);
1239 test
= LLVMBuildOr(builder
, test
, is_nan_or_inf
, "");
1240 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1LL << plane_idx
);
1241 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1242 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1244 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1245 indices
[0] = lp_build_const_int32(gallivm
, 0);
1246 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1248 indices
[2] = lp_build_const_int32(gallivm
, 0);
1249 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1250 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1251 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1252 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1254 indices
[2] = lp_build_const_int32(gallivm
, 1);
1255 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1256 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1257 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1258 sum
= lp_build_fmuladd(builder
, planes
, cv_y
, sum
);
1260 indices
[2] = lp_build_const_int32(gallivm
, 2);
1261 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1262 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1263 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1264 sum
= lp_build_fmuladd(builder
, planes
, cv_z
, sum
);
1266 indices
[2] = lp_build_const_int32(gallivm
, 3);
1267 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1268 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1269 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1270 sum
= lp_build_fmuladd(builder
, planes
, cv_w
, sum
);
1272 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1273 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1LL << plane_idx
);
1274 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1275 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1279 if (key
->need_edgeflags
) {
1281 * This isn't really part of clipmask but stored the same in vertex
1282 * header later, so do it here.
1284 unsigned edge_attr
= llvm
->draw
->vs
.edgeflag_output
;
1285 LLVMValueRef one
= lp_build_const_vec(gallivm
, f32_type
, 1.0);
1286 LLVMValueRef edgeflag
= LLVMBuildLoad(builder
, outputs
[edge_attr
][0], "");
1287 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_EQUAL
, one
, edgeflag
);
1288 temp
= lp_build_const_int_vec(gallivm
, i32_type
,
1289 1LL << DRAW_TOTAL_CLIP_PLANES
);
1290 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1291 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1298 * Returns boolean if any clipping has occurred
1299 * Used zero/non-zero i32 value to represent boolean
1302 clipmask_booli32(struct gallivm_state
*gallivm
,
1303 const struct lp_type vs_type
,
1304 LLVMValueRef clipmask_bool_ptr
,
1305 boolean edgeflag_in_clipmask
)
1307 LLVMBuilderRef builder
= gallivm
->builder
;
1308 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
1309 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1310 LLVMValueRef ret
= LLVMConstNull(int32_type
);
1315 * We need to invert the edgeflag bit from the clipmask here
1316 * (because the result is really if we want to run the pipeline or not
1317 * and we (may) need it if edgeflag was 0).
1319 if (edgeflag_in_clipmask
) {
1320 struct lp_type i32_type
= lp_int_type(vs_type
);
1321 LLVMValueRef edge
= lp_build_const_int_vec(gallivm
, i32_type
,
1322 1LL << DRAW_TOTAL_CLIP_PLANES
);
1323 clipmask_bool
= LLVMBuildXor(builder
, clipmask_bool
, edge
, "");
1326 * Could do much better with just cmp/movmskps.
1328 for (i
=0; i
< vs_type
.length
; i
++) {
1329 temp
= LLVMBuildExtractElement(builder
, clipmask_bool
,
1330 lp_build_const_int32(gallivm
, i
) , "");
1331 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1337 draw_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
1338 struct lp_build_tgsi_context
* bld_base
,
1339 boolean is_vindex_indirect
,
1340 LLVMValueRef vertex_index
,
1341 boolean is_aindex_indirect
,
1342 LLVMValueRef attrib_index
,
1343 LLVMValueRef swizzle_index
)
1345 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1346 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1347 LLVMBuilderRef builder
= gallivm
->builder
;
1348 LLVMValueRef indices
[3];
1350 struct lp_type type
= bld_base
->base
.type
;
1352 if (is_vindex_indirect
|| is_aindex_indirect
) {
1354 res
= bld_base
->base
.zero
;
1355 for (i
= 0; i
< type
.length
; ++i
) {
1356 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1357 LLVMValueRef vert_chan_index
= vertex_index
;
1358 LLVMValueRef attr_chan_index
= attrib_index
;
1359 LLVMValueRef channel_vec
, value
;
1361 if (is_vindex_indirect
) {
1362 vert_chan_index
= LLVMBuildExtractElement(builder
,
1363 vertex_index
, idx
, "");
1365 if (is_aindex_indirect
) {
1366 attr_chan_index
= LLVMBuildExtractElement(builder
,
1367 attrib_index
, idx
, "");
1370 indices
[0] = vert_chan_index
;
1371 indices
[1] = attr_chan_index
;
1372 indices
[2] = swizzle_index
;
1374 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1375 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1376 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1378 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1381 indices
[0] = vertex_index
;
1382 indices
[1] = attrib_index
;
1383 indices
[2] = swizzle_index
;
1385 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1386 res
= LLVMBuildLoad(builder
, res
, "");
1393 draw_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
1394 struct lp_build_tgsi_context
* bld_base
,
1395 LLVMValueRef (*outputs
)[4],
1396 LLVMValueRef emitted_vertices_vec
)
1398 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1399 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1400 struct gallivm_state
*gallivm
= variant
->gallivm
;
1401 LLVMBuilderRef builder
= gallivm
->builder
;
1402 struct lp_type gs_type
= bld_base
->base
.type
;
1403 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1404 lp_int_type(gs_type
), 0);
1405 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1406 LLVMValueRef next_prim_offset
=
1407 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1408 LLVMValueRef io
= variant
->io_ptr
;
1410 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1412 for (i
= 0; i
< gs_type
.length
; ++i
) {
1413 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1414 LLVMValueRef currently_emitted
=
1415 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1416 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1417 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1420 convert_to_aos(gallivm
, io
, indices
,
1422 gs_info
->num_outputs
, gs_type
,
1427 draw_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
1428 struct lp_build_tgsi_context
* bld_base
,
1429 LLVMValueRef verts_per_prim_vec
,
1430 LLVMValueRef emitted_prims_vec
)
1432 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1433 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1434 struct gallivm_state
*gallivm
= variant
->gallivm
;
1435 LLVMBuilderRef builder
= gallivm
->builder
;
1436 LLVMValueRef prim_lengts_ptr
=
1437 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1440 for (i
= 0; i
< bld_base
->base
.type
.length
; ++i
) {
1441 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1442 LLVMValueRef prims_emitted
=
1443 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1444 LLVMValueRef store_ptr
;
1445 LLVMValueRef num_vertices
=
1446 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1448 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1449 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1450 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1451 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1456 draw_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
1457 struct lp_build_tgsi_context
* bld_base
,
1458 LLVMValueRef total_emitted_vertices_vec
,
1459 LLVMValueRef emitted_prims_vec
)
1461 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1462 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1463 struct gallivm_state
*gallivm
= variant
->gallivm
;
1464 LLVMBuilderRef builder
= gallivm
->builder
;
1465 LLVMValueRef emitted_verts_ptr
=
1466 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1467 LLVMValueRef emitted_prims_ptr
=
1468 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1469 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1471 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &zero
, 0, "");
1472 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &zero
, 0, "");
1474 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1475 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1479 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1482 struct gallivm_state
*gallivm
= variant
->gallivm
;
1483 LLVMContextRef context
= gallivm
->context
;
1484 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1485 LLVMTypeRef arg_types
[11];
1486 unsigned num_arg_types
=
1487 elts
? ARRAY_SIZE(arg_types
) : ARRAY_SIZE(arg_types
) - 1;
1488 LLVMTypeRef func_type
;
1489 LLVMValueRef context_ptr
;
1490 LLVMBasicBlockRef block
;
1491 LLVMBuilderRef builder
;
1493 struct lp_type vs_type
;
1494 LLVMValueRef end
, start
;
1495 LLVMValueRef count
, fetch_elts
, fetch_elt_max
, fetch_count
;
1496 LLVMValueRef vertex_id_offset
, start_instance
;
1497 LLVMValueRef stride
, step
, io_itr
;
1498 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1499 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1500 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1501 LLVMValueRef vb_stride
[PIPE_MAX_SHADER_INPUTS
];
1502 LLVMValueRef map_ptr
[PIPE_MAX_SHADER_INPUTS
];
1503 LLVMValueRef buffer_size_adj
[PIPE_MAX_SHADER_INPUTS
];
1504 LLVMValueRef stride_fixed
[PIPE_MAX_SHADER_INPUTS
];
1505 LLVMValueRef ofbit
[PIPE_MAX_SHADER_INPUTS
];
1506 LLVMValueRef instance_index
[PIPE_MAX_SHADER_INPUTS
];
1508 struct draw_context
*draw
= llvm
->draw
;
1509 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1511 struct lp_build_context bld
;
1512 struct lp_build_loop_state lp_loop
;
1513 const int vector_length
= lp_native_vector_width
/ 32;
1514 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1515 LLVMValueRef fetch_max
;
1516 struct lp_build_sampler_soa
*sampler
= 0;
1517 LLVMValueRef ret
, clipmask_bool_ptr
;
1518 struct draw_llvm_variant_key
*key
= &variant
->key
;
1519 /* If geometry shader is present we need to skip both the viewport
1520 * transformation and clipping otherwise the inputs to the geometry
1521 * shader will be incorrect.
1522 * The code can't handle vp transform when vs writes vp index neither
1523 * (though this would be fixable here, but couldn't just broadcast
1526 const boolean bypass_viewport
= key
->has_gs
|| key
->bypass_viewport
||
1527 vs_info
->writes_viewport_index
;
1528 const boolean enable_cliptest
= !key
->has_gs
&& (key
->clip_xy
||
1531 key
->need_edgeflags
);
1532 LLVMValueRef variant_func
;
1533 const unsigned pos
= draw
->vs
.position_output
;
1534 const unsigned cv
= draw
->vs
.clipvertex_output
;
1535 boolean have_clipdist
= FALSE
;
1536 struct lp_bld_tgsi_system_values system_values
;
1538 memset(&system_values
, 0, sizeof(system_values
));
1540 util_snprintf(func_name
, sizeof(func_name
), "draw_llvm_vs_variant%u_%s",
1541 variant
->shader
->variants_cached
, elts
? "elts" : "linear");
1544 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1545 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1546 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1548 arg_types
[i
++] = LLVMPointerType(int32_type
, 0);/* fetch_elts */
1549 arg_types
[i
++] = int32_type
; /* fetch_elt_max */
1551 arg_types
[i
++] = int32_type
; /* start */
1552 arg_types
[i
++] = int32_type
; /* fetch_count / count */
1553 arg_types
[i
++] = int32_type
; /* stride */
1554 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1555 arg_types
[i
++] = int32_type
; /* instance_id */
1556 arg_types
[i
++] = int32_type
; /* vertex_id_offset */
1557 arg_types
[i
++] = int32_type
; /* start_instance */
1559 func_type
= LLVMFunctionType(int32_type
, arg_types
, num_arg_types
, 0);
1561 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
1564 variant
->function_elts
= variant_func
;
1566 variant
->function
= variant_func
;
1568 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1569 for (i
= 0; i
< num_arg_types
; ++i
)
1570 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1571 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1572 LLVMNoAliasAttribute
);
1574 context_ptr
= LLVMGetParam(variant_func
, 0);
1575 io_ptr
= LLVMGetParam(variant_func
, 1);
1576 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1578 * XXX: stride is actually unused. The stride we use is strictly calculated
1579 * from the number of outputs (including the draw_extra outputs).
1580 * Should probably fix some day (we need a new vs just because of extra
1581 * outputs which the generated vs won't touch).
1583 stride
= LLVMGetParam(variant_func
, 5 + (elts
? 1 : 0));
1584 vb_ptr
= LLVMGetParam(variant_func
, 6 + (elts
? 1 : 0));
1585 system_values
.instance_id
= LLVMGetParam(variant_func
, 7 + (elts
? 1 : 0));
1586 vertex_id_offset
= LLVMGetParam(variant_func
, 8 + (elts
? 1 : 0));
1587 start_instance
= LLVMGetParam(variant_func
, 9 + (elts
? 1 : 0));
1589 lp_build_name(context_ptr
, "context");
1590 lp_build_name(io_ptr
, "io");
1591 lp_build_name(vbuffers_ptr
, "vbuffers");
1592 lp_build_name(stride
, "stride");
1593 lp_build_name(vb_ptr
, "vb");
1594 lp_build_name(system_values
.instance_id
, "instance_id");
1595 lp_build_name(vertex_id_offset
, "vertex_id_offset");
1596 lp_build_name(start_instance
, "start_instance");
1599 fetch_elts
= LLVMGetParam(variant_func
, 3);
1600 fetch_elt_max
= LLVMGetParam(variant_func
, 4);
1601 fetch_count
= LLVMGetParam(variant_func
, 5);
1602 lp_build_name(fetch_elts
, "fetch_elts");
1603 lp_build_name(fetch_elt_max
, "fetch_elt_max");
1604 lp_build_name(fetch_count
, "fetch_count");
1605 start
= count
= NULL
;
1608 start
= LLVMGetParam(variant_func
, 3);
1609 count
= LLVMGetParam(variant_func
, 4);
1610 lp_build_name(start
, "start");
1611 lp_build_name(count
, "count");
1612 fetch_elts
= fetch_count
= NULL
;
1619 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1620 builder
= gallivm
->builder
;
1621 LLVMPositionBuilderAtEnd(builder
, block
);
1623 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1625 memset(&vs_type
, 0, sizeof vs_type
);
1626 vs_type
.floating
= TRUE
; /* floating point values */
1627 vs_type
.sign
= TRUE
; /* values are signed */
1628 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1629 vs_type
.width
= 32; /* 32-bit float */
1630 vs_type
.length
= vector_length
;
1632 /* hold temporary "bool" clipmask */
1633 clipmask_bool_ptr
= lp_build_alloca(gallivm
, lp_build_int_vec_type(gallivm
, vs_type
), "");
1634 LLVMBuildStore(builder
, lp_build_zero(gallivm
, lp_int_type(vs_type
)), clipmask_bool_ptr
);
1636 /* code generated texture sampling */
1637 sampler
= draw_llvm_sampler_soa_create(draw_llvm_variant_key_samplers(key
));
1642 count
= fetch_count
;
1645 end
= lp_build_add(&bld
, start
, count
);
1648 step
= lp_build_const_int32(gallivm
, vector_length
);
1650 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1653 * Pre-calculate everything which is constant per shader invocation.
1655 for (j
= 0; j
< key
->nr_vertex_elements
; ++j
) {
1656 LLVMValueRef vb_buffer_offset
, buffer_size
;
1657 LLVMValueRef vb_info
, vbuffer_ptr
;
1658 struct pipe_vertex_element
*velem
= &key
->vertex_element
[j
];
1659 LLVMValueRef vb_index
=
1660 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1661 LLVMValueRef bsize
= lp_build_const_int32(gallivm
,
1662 util_format_get_blocksize(velem
->src_format
));
1663 LLVMValueRef src_offset
= lp_build_const_int32(gallivm
,
1666 if (velem
->src_format
!= PIPE_FORMAT_NONE
) {
1667 vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
, &vb_index
, 1, "");
1668 vb_info
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1669 vb_stride
[j
] = draw_jit_vbuffer_stride(gallivm
, vb_info
);
1670 vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vb_info
);
1671 map_ptr
[j
] = draw_jit_dvbuffer_map(gallivm
, vbuffer_ptr
);
1672 buffer_size
= draw_jit_dvbuffer_size(gallivm
, vbuffer_ptr
);
1675 stride_fixed
[j
] = lp_build_uadd_overflow(gallivm
, vb_buffer_offset
,
1676 src_offset
, &ofbit
[j
]);
1677 buffer_size_adj
[j
] = lp_build_usub_overflow(gallivm
, buffer_size
, bsize
,
1680 if (velem
->instance_divisor
) {
1681 /* Index is equal to the start instance plus the number of current
1682 * instance divided by the divisor. In this case we compute it as:
1683 * index = start_instance + (instance_id / divisor)
1685 LLVMValueRef current_instance
;
1686 current_instance
= LLVMBuildUDiv(builder
, system_values
.instance_id
,
1687 lp_build_const_int32(gallivm
,
1688 velem
->instance_divisor
),
1689 "instance_divisor");
1690 instance_index
[j
] = lp_build_uadd_overflow(gallivm
, start_instance
,
1691 current_instance
, &ofbit
[j
]);
1695 lp_build_printf(gallivm
, "vbuf index = %u, vb_stride is %u\n",
1696 vb_index
, vb_stride
[j
]);
1697 lp_build_printf(gallivm
, " vb_buffer_offset = %u, src_offset is %u\n",
1698 vb_buffer_offset
, src_offset
);
1699 lp_build_print_value(gallivm
, " blocksize = ", bsize
);
1700 lp_build_printf(gallivm
, " instance_id = %u\n",
1701 system_values
.instance_id
);
1702 lp_build_printf(gallivm
, " buffer size = %u\n", buffer_size
);
1707 lp_build_loop_begin(&lp_loop
, gallivm
, zero
);
1709 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1710 LLVMValueRef aos_attribs
[LP_MAX_VECTOR_WIDTH
/ 32] = { 0 };
1712 LLVMValueRef clipmask
; /* holds the clipmask value */
1713 LLVMValueRef true_index_array
= lp_build_zero(gallivm
,
1714 lp_type_uint_vec(32, 32*vector_length
));
1715 LLVMValueRef true_indices
[LP_MAX_VECTOR_WIDTH
/ 32];
1716 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1718 io_itr
= lp_loop
.counter
;
1720 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1722 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1723 io_itr
, io
, lp_loop
.counter
);
1726 for (i
= 0; i
< vector_length
; ++i
) {
1727 LLVMValueRef vert_index
=
1728 LLVMBuildAdd(builder
,
1730 lp_build_const_int32(gallivm
, i
), "");
1731 LLVMValueRef true_index
=
1732 LLVMBuildAdd(builder
, start
, vert_index
, "");
1734 /* make sure we're not out of bounds which can happen
1735 * if fetch_count % 4 != 0, because on the last iteration
1736 * a few of the 4 vertex fetches will be out of bounds */
1737 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1740 LLVMValueRef fetch_ptr
;
1741 LLVMValueRef index_overflowed
;
1742 LLVMValueRef index_ptr
=
1745 lp_build_vec_type(gallivm
, lp_type_int(32)), "");
1746 struct lp_build_if_state if_ctx
;
1747 index_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
1748 true_index
, fetch_elt_max
,
1749 "index_overflowed");
1751 lp_build_if(&if_ctx
, gallivm
, index_overflowed
);
1753 /* Generate maximum possible index so that
1754 * generate_fetch can treat it just like
1755 * any other overflow and return zeros.
1756 * We don't have to worry about the restart
1757 * primitive index because it has already been
1761 lp_build_const_int32(gallivm
, 0xffffffff);
1762 LLVMBuildStore(builder
, val
, index_ptr
);
1764 lp_build_else(&if_ctx
);
1767 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1768 &true_index
, 1, "");
1769 val
= LLVMBuildLoad(builder
, fetch_ptr
, "");
1770 LLVMBuildStore(builder
, val
, index_ptr
);
1772 lp_build_endif(&if_ctx
);
1773 true_index
= LLVMBuildLoad(builder
, index_ptr
, "true_index");
1775 true_indices
[i
] = true_index
;
1776 true_index_array
= LLVMBuildInsertElement(
1777 gallivm
->builder
, true_index_array
, true_index
,
1778 lp_build_const_int32(gallivm
, i
), "");
1781 for (j
= 0; j
< key
->nr_vertex_elements
; ++j
) {
1782 struct pipe_vertex_element
*velem
= &key
->vertex_element
[j
];
1783 const struct util_format_description
*format_desc
=
1784 util_format_description(velem
->src_format
);
1786 for (i
= 0; i
< vector_length
; ++i
) {
1787 if (format_desc
->format
== PIPE_FORMAT_NONE
) {
1788 aos_attribs
[i
] = lp_build_const_vec(gallivm
,
1789 lp_float32_vec4_type(), 0);
1792 generate_fetch(gallivm
, format_desc
,
1793 vb_stride
[j
], stride_fixed
[j
], map_ptr
[j
],
1794 buffer_size_adj
[j
], ofbit
[j
], &aos_attribs
[i
],
1795 velem
->instance_divisor
?
1796 instance_index
[j
] : true_indices
[i
]);
1799 convert_to_soa(gallivm
, aos_attribs
, inputs
, j
, vs_type
);
1802 /* In the paths with elts vertex id has to be unaffected by the
1803 * index bias and because indices inside our elements array have
1804 * already had index bias applied we need to subtract it here to
1805 * get back to the original index.
1806 * in the linear paths vertex id has to be unaffected by the
1807 * original start index and because we abuse the 'start' variable
1808 * to either represent the actual start index or the index at which
1809 * the primitive was split (we split rendering into chunks of at
1810 * most 4095-vertices) we need to back out the original start
1811 * index out of our vertex id here.
1813 system_values
.basevertex
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
,
1814 lp_type_uint_vec(32, 32*vector_length
)),
1816 system_values
.vertex_id
= true_index_array
;
1817 system_values
.vertex_id_nobase
= LLVMBuildSub(builder
, true_index_array
,
1818 system_values
.basevertex
, "");
1820 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1821 generate_vs(variant
,
1829 key
->clamp_vertex_color
);
1831 if (pos
!= -1 && cv
!= -1) {
1832 /* store original positions in clip before further manipulation */
1833 store_clip(gallivm
, vs_type
, io
, outputs
, pos
);
1836 if (enable_cliptest
) {
1837 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1838 /* allocate clipmask, assign it integer type */
1839 clipmask
= generate_clipmask(llvm
,
1844 context_ptr
, &have_clipdist
);
1845 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1846 /* store temporary clipping boolean value */
1847 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1850 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1853 /* do viewport mapping */
1854 if (!bypass_viewport
) {
1855 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1859 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1862 /* store clipmask in vertex header,
1863 * original positions in clip
1864 * and transformed positions in data
1866 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
1867 vs_info
->num_outputs
, vs_type
,
1868 enable_cliptest
&& key
->need_edgeflags
);
1870 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
1872 sampler
->destroy(sampler
);
1874 /* return clipping boolean value for function */
1875 ret
= clipmask_booli32(gallivm
, vs_type
, clipmask_bool_ptr
,
1876 enable_cliptest
&& key
->need_edgeflags
);
1878 LLVMBuildRet(builder
, ret
);
1880 gallivm_verify_function(gallivm
, variant_func
);
1884 struct draw_llvm_variant_key
*
1885 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1888 struct draw_llvm_variant_key
*key
;
1889 struct draw_sampler_static_state
*draw_sampler
;
1891 key
= (struct draw_llvm_variant_key
*)store
;
1893 memset(key
, 0, offsetof(struct draw_llvm_variant_key
, vertex_element
[0]));
1895 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1897 /* will have to rig this up properly later */
1898 key
->clip_xy
= llvm
->draw
->clip_xy
;
1899 key
->clip_z
= llvm
->draw
->clip_z
;
1900 key
->clip_user
= llvm
->draw
->clip_user
;
1901 key
->bypass_viewport
= llvm
->draw
->bypass_viewport
;
1902 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
1903 /* XXX assumes edgeflag output not at 0 */
1904 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1905 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1906 key
->has_gs
= llvm
->draw
->gs
.geometry_shader
!= NULL
;
1907 key
->num_outputs
= draw_total_vs_outputs(llvm
->draw
);
1909 /* All variants of this shader will have the same value for
1910 * nr_samplers. Not yet trying to compact away holes in the
1913 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1914 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
1915 key
->nr_sampler_views
=
1916 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
1919 key
->nr_sampler_views
= key
->nr_samplers
;
1922 /* Presumably all variants of the shader should have the same
1923 * number of vertex elements - ie the number of shader inputs.
1924 * NOTE: we NEED to store the needed number of needed inputs
1925 * here, not the number of provided elements to match keysize
1926 * (and the offset of sampler state in the key).
1927 * If we have excess number of vertex elements, this is valid,
1928 * but the excess ones don't matter.
1929 * If we don't have enough vertex elements (which looks not really
1930 * valid but we'll handle it gracefully) fill out missing ones with
1931 * zero (we'll recognize these later by PIPE_FORMAT_NONE).
1933 key
->nr_vertex_elements
=
1934 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
1936 if (llvm
->draw
->pt
.nr_vertex_elements
< key
->nr_vertex_elements
) {
1937 debug_printf("draw: vs with %d inputs but only have %d vertex elements\n",
1938 key
->nr_vertex_elements
, llvm
->draw
->pt
.nr_vertex_elements
);
1939 memset(key
->vertex_element
, 0,
1940 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1942 memcpy(key
->vertex_element
,
1943 llvm
->draw
->pt
.vertex_element
,
1944 sizeof(struct pipe_vertex_element
) *
1945 MIN2(key
->nr_vertex_elements
, llvm
->draw
->pt
.nr_vertex_elements
));
1947 draw_sampler
= draw_llvm_variant_key_samplers(key
);
1948 memset(draw_sampler
, 0,
1949 MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
1951 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1952 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
1953 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
1955 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1956 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
1957 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
1965 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
1968 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
1970 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
1971 debug_printf("clip_xy = %u\n", key
->clip_xy
);
1972 debug_printf("clip_z = %u\n", key
->clip_z
);
1973 debug_printf("clip_user = %u\n", key
->clip_user
);
1974 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
1975 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
1976 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
1977 debug_printf("has_gs = %u\n", key
->has_gs
);
1978 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
1980 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
1981 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
1982 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
1983 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
1984 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
1987 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1988 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
1994 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1995 unsigned shader_stage
,
1997 uint32_t width
, uint32_t height
, uint32_t depth
,
1998 uint32_t first_level
, uint32_t last_level
,
1999 const void *base_ptr
,
2000 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
2001 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
2002 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
2005 struct draw_jit_texture
*jit_tex
;
2007 assert(shader_stage
== PIPE_SHADER_VERTEX
||
2008 shader_stage
== PIPE_SHADER_GEOMETRY
);
2010 if (shader_stage
== PIPE_SHADER_VERTEX
) {
2011 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->jit_context
.textures
));
2013 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
2014 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
2015 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->gs_jit_context
.textures
));
2017 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
2023 jit_tex
->width
= width
;
2024 jit_tex
->height
= height
;
2025 jit_tex
->depth
= depth
;
2026 jit_tex
->first_level
= first_level
;
2027 jit_tex
->last_level
= last_level
;
2028 jit_tex
->base
= base_ptr
;
2030 for (j
= first_level
; j
<= last_level
; j
++) {
2031 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
2032 jit_tex
->row_stride
[j
] = row_stride
[j
];
2033 jit_tex
->img_stride
[j
] = img_stride
[j
];
2039 draw_llvm_set_sampler_state(struct draw_context
*draw
,
2040 unsigned shader_type
)
2044 if (shader_type
== PIPE_SHADER_VERTEX
) {
2045 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
2046 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
2048 if (draw
->samplers
[PIPE_SHADER_VERTEX
][i
]) {
2049 const struct pipe_sampler_state
*s
2050 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
2051 jit_sam
->min_lod
= s
->min_lod
;
2052 jit_sam
->max_lod
= s
->max_lod
;
2053 jit_sam
->lod_bias
= s
->lod_bias
;
2054 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2057 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
2058 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
2059 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
2061 if (draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]) {
2062 const struct pipe_sampler_state
*s
2063 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
2064 jit_sam
->min_lod
= s
->min_lod
;
2065 jit_sam
->max_lod
= s
->max_lod
;
2066 jit_sam
->lod_bias
= s
->lod_bias
;
2067 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2075 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
2077 struct draw_llvm
*llvm
= variant
->llvm
;
2079 gallivm_destroy(variant
->gallivm
);
2081 remove_from_list(&variant
->list_item_local
);
2082 variant
->shader
->variants_cached
--;
2083 remove_from_list(&variant
->list_item_global
);
2084 llvm
->nr_variants
--;
2090 * Create LLVM types for various structures.
2093 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
2095 struct gallivm_state
*gallivm
= var
->gallivm
;
2096 LLVMTypeRef texture_type
, sampler_type
, context_type
;
2098 texture_type
= create_jit_texture_type(gallivm
, "texture");
2099 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2101 context_type
= create_gs_jit_context_type(gallivm
,
2102 var
->shader
->base
.vector_length
,
2103 texture_type
, sampler_type
,
2104 "draw_gs_jit_context");
2105 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2107 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
2111 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
2113 if (!variant
->context_ptr_type
)
2114 create_gs_jit_types(variant
);
2115 return variant
->context_ptr_type
;
2119 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2120 struct lp_type gs_type
)
2122 struct gallivm_state
*gallivm
= variant
->gallivm
;
2123 LLVMBuilderRef builder
= gallivm
->builder
;
2124 struct lp_type mask_type
= lp_int_type(gs_type
);
2125 LLVMValueRef num_prims
;
2126 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
2129 num_prims
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
),
2130 variant
->num_prims
);
2131 for (i
= 0; i
< gs_type
.length
; i
++) {
2132 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
2133 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, idx
, idx
, "");
2135 mask_val
= lp_build_compare(gallivm
, mask_type
,
2136 PIPE_FUNC_GREATER
, num_prims
, mask_val
);
2142 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2143 struct draw_gs_llvm_variant
*variant
)
2145 struct gallivm_state
*gallivm
= variant
->gallivm
;
2146 LLVMContextRef context
= gallivm
->context
;
2147 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2148 LLVMTypeRef arg_types
[7];
2149 LLVMTypeRef func_type
;
2150 LLVMValueRef variant_func
;
2151 LLVMValueRef context_ptr
;
2152 LLVMValueRef prim_id_ptr
;
2153 LLVMBasicBlockRef block
;
2154 LLVMBuilderRef builder
;
2155 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2156 struct lp_build_sampler_soa
*sampler
= 0;
2157 struct lp_build_context bld
;
2158 struct lp_bld_tgsi_system_values system_values
;
2160 struct lp_type gs_type
;
2162 struct draw_gs_llvm_iface gs_iface
;
2163 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2164 LLVMValueRef consts_ptr
, num_consts_ptr
;
2165 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2166 struct lp_build_mask_context mask
;
2167 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2168 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2170 memset(&system_values
, 0, sizeof(system_values
));
2172 util_snprintf(func_name
, sizeof(func_name
), "draw_llvm_gs_variant%u",
2173 variant
->shader
->variants_cached
);
2175 assert(variant
->vertex_header_ptr_type
);
2177 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2178 arg_types
[1] = variant
->input_array_type
; /* input */
2179 arg_types
[2] = variant
->vertex_header_ptr_type
; /* vertex_header */
2180 arg_types
[3] = int32_type
; /* num_prims */
2181 arg_types
[4] = int32_type
; /* instance_id */
2182 arg_types
[5] = LLVMPointerType(
2183 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2184 arg_types
[6] = int32_type
;
2186 func_type
= LLVMFunctionType(int32_type
, arg_types
, ARRAY_SIZE(arg_types
), 0);
2188 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
2190 variant
->function
= variant_func
;
2192 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2194 for (i
= 0; i
< ARRAY_SIZE(arg_types
); ++i
)
2195 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2196 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
2197 LLVMNoAliasAttribute
);
2199 context_ptr
= LLVMGetParam(variant_func
, 0);
2200 input_array
= LLVMGetParam(variant_func
, 1);
2201 io_ptr
= LLVMGetParam(variant_func
, 2);
2202 num_prims
= LLVMGetParam(variant_func
, 3);
2203 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2204 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2205 system_values
.invocation_id
= LLVMGetParam(variant_func
, 6);
2207 lp_build_name(context_ptr
, "context");
2208 lp_build_name(input_array
, "input");
2209 lp_build_name(io_ptr
, "io");
2210 lp_build_name(num_prims
, "num_prims");
2211 lp_build_name(system_values
.instance_id
, "instance_id");
2212 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2213 lp_build_name(system_values
.invocation_id
, "invocation_id");
2215 variant
->context_ptr
= context_ptr
;
2216 variant
->io_ptr
= io_ptr
;
2217 variant
->num_prims
= num_prims
;
2219 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2220 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2221 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2222 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2223 gs_iface
.input
= input_array
;
2224 gs_iface
.variant
= variant
;
2230 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2231 builder
= gallivm
->builder
;
2232 LLVMPositionBuilderAtEnd(builder
, block
);
2234 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2236 memset(&gs_type
, 0, sizeof gs_type
);
2237 gs_type
.floating
= TRUE
; /* floating point values */
2238 gs_type
.sign
= TRUE
; /* values are signed */
2239 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2240 gs_type
.width
= 32; /* 32-bit float */
2241 gs_type
.length
= vector_length
;
2243 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2245 draw_gs_jit_context_num_constants(variant
->gallivm
, context_ptr
);
2247 /* code generated texture sampling */
2248 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
);
2250 mask_val
= generate_mask_value(variant
, gs_type
);
2251 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2253 if (gs_info
->uses_primid
) {
2254 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");
2257 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2258 tgsi_dump(tokens
, 0);
2259 draw_gs_llvm_dump_variant_key(&variant
->key
);
2262 lp_build_tgsi_soa(variant
->gallivm
,
2274 &llvm
->draw
->gs
.geometry_shader
->info
,
2275 (const struct lp_build_tgsi_gs_iface
*)&gs_iface
);
2277 sampler
->destroy(sampler
);
2279 lp_build_mask_end(&mask
);
2281 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2283 gallivm_verify_function(gallivm
, variant_func
);
2287 struct draw_gs_llvm_variant
*
2288 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2289 unsigned num_outputs
,
2290 const struct draw_gs_llvm_variant_key
*key
)
2292 struct draw_gs_llvm_variant
*variant
;
2293 struct llvm_geometry_shader
*shader
=
2294 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2295 LLVMTypeRef vertex_header
;
2296 char module_name
[64];
2298 variant
= MALLOC(sizeof *variant
+
2299 shader
->variant_key_size
-
2300 sizeof variant
->key
);
2304 variant
->llvm
= llvm
;
2305 variant
->shader
= shader
;
2307 util_snprintf(module_name
, sizeof(module_name
), "draw_llvm_gs_variant%u",
2308 variant
->shader
->variants_cached
);
2310 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
2312 create_gs_jit_types(variant
);
2314 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2316 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2318 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2320 draw_gs_llvm_generate(llvm
, variant
);
2322 gallivm_compile_module(variant
->gallivm
);
2324 variant
->jit_func
= (draw_gs_jit_func
)
2325 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2327 gallivm_free_ir(variant
->gallivm
);
2329 variant
->list_item_global
.base
= variant
;
2330 variant
->list_item_local
.base
= variant
;
2331 /*variant->no = */shader
->variants_created
++;
2332 variant
->list_item_global
.base
= variant
;
2338 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2340 struct draw_llvm
*llvm
= variant
->llvm
;
2342 gallivm_destroy(variant
->gallivm
);
2344 remove_from_list(&variant
->list_item_local
);
2345 variant
->shader
->variants_cached
--;
2346 remove_from_list(&variant
->list_item_global
);
2347 llvm
->nr_gs_variants
--;
2351 struct draw_gs_llvm_variant_key
*
2352 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2355 struct draw_gs_llvm_variant_key
*key
;
2356 struct draw_sampler_static_state
*draw_sampler
;
2358 key
= (struct draw_gs_llvm_variant_key
*)store
;
2360 memset(key
, 0, offsetof(struct draw_gs_llvm_variant_key
, samplers
[0]));
2362 key
->num_outputs
= draw_total_gs_outputs(llvm
->draw
);
2364 /* All variants of this shader will have the same value for
2365 * nr_samplers. Not yet trying to compact away holes in the
2368 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2369 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2370 key
->nr_sampler_views
=
2371 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2374 key
->nr_sampler_views
= key
->nr_samplers
;
2377 draw_sampler
= key
->samplers
;
2379 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2381 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2382 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2383 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2385 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2386 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2387 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2394 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2397 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2399 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2400 debug_printf("sampler[%i].src_format = %s\n", i
,
2401 util_format_name(sampler
[i
].texture_state
.format
));