1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "draw_llvm.h"
30 #include "draw_context.h"
34 #include "gallivm/lp_bld_arit.h"
35 #include "gallivm/lp_bld_arit_overflow.h"
36 #include "gallivm/lp_bld_logic.h"
37 #include "gallivm/lp_bld_const.h"
38 #include "gallivm/lp_bld_swizzle.h"
39 #include "gallivm/lp_bld_struct.h"
40 #include "gallivm/lp_bld_type.h"
41 #include "gallivm/lp_bld_flow.h"
42 #include "gallivm/lp_bld_debug.h"
43 #include "gallivm/lp_bld_tgsi.h"
44 #include "gallivm/lp_bld_printf.h"
45 #include "gallivm/lp_bld_intr.h"
46 #include "gallivm/lp_bld_init.h"
47 #include "gallivm/lp_bld_type.h"
48 #include "gallivm/lp_bld_pack.h"
49 #include "gallivm/lp_bld_format.h"
51 #include "tgsi/tgsi_exec.h"
52 #include "tgsi/tgsi_dump.h"
54 #include "util/u_math.h"
55 #include "util/u_pointer.h"
56 #include "util/u_string.h"
57 #include "util/u_simple_list.h"
64 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
,
68 struct draw_gs_llvm_iface
{
69 struct lp_build_tgsi_gs_iface base
;
71 struct draw_gs_llvm_variant
*variant
;
75 static INLINE
const struct draw_gs_llvm_iface
*
76 draw_gs_llvm_iface(const struct lp_build_tgsi_gs_iface
*iface
)
78 return (const struct draw_gs_llvm_iface
*)iface
;
82 * Create LLVM type for draw_vertex_buffer.
85 create_jit_dvbuffer_type(struct gallivm_state
*gallivm
,
86 const char *struct_name
)
88 LLVMTargetDataRef target
= gallivm
->target
;
89 LLVMTypeRef dvbuffer_type
;
90 LLVMTypeRef elem_types
[DRAW_JIT_DVBUFFER_NUM_FIELDS
];
91 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
93 elem_types
[DRAW_JIT_DVBUFFER_MAP
] =
94 LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
95 elem_types
[DRAW_JIT_DVBUFFER_SIZE
] = int32_type
;
97 dvbuffer_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
98 Elements(elem_types
), 0);
100 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, map
,
101 target
, dvbuffer_type
,
102 DRAW_JIT_DVBUFFER_MAP
);
103 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, size
,
104 target
, dvbuffer_type
,
105 DRAW_JIT_DVBUFFER_SIZE
);
107 return dvbuffer_type
;
111 * Create LLVM type for struct draw_jit_texture
114 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
116 LLVMTargetDataRef target
= gallivm
->target
;
117 LLVMTypeRef texture_type
;
118 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
119 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
121 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
122 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
123 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
124 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
125 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
126 elem_types
[DRAW_JIT_TEXTURE_BASE
] =
127 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
128 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
129 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
130 elem_types
[DRAW_JIT_TEXTURE_MIP_OFFSETS
] =
131 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
133 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
134 Elements(elem_types
), 0);
136 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
137 target
, texture_type
,
138 DRAW_JIT_TEXTURE_WIDTH
);
139 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
140 target
, texture_type
,
141 DRAW_JIT_TEXTURE_HEIGHT
);
142 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
143 target
, texture_type
,
144 DRAW_JIT_TEXTURE_DEPTH
);
145 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
146 target
, texture_type
,
147 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
148 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
149 target
, texture_type
,
150 DRAW_JIT_TEXTURE_LAST_LEVEL
);
151 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, base
,
152 target
, texture_type
,
153 DRAW_JIT_TEXTURE_BASE
);
154 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
155 target
, texture_type
,
156 DRAW_JIT_TEXTURE_ROW_STRIDE
);
157 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
158 target
, texture_type
,
159 DRAW_JIT_TEXTURE_IMG_STRIDE
);
160 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, mip_offsets
,
161 target
, texture_type
,
162 DRAW_JIT_TEXTURE_MIP_OFFSETS
);
164 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
171 * Create LLVM type for struct draw_jit_sampler
174 create_jit_sampler_type(struct gallivm_state
*gallivm
, const char *struct_name
)
176 LLVMTargetDataRef target
= gallivm
->target
;
177 LLVMTypeRef sampler_type
;
178 LLVMTypeRef elem_types
[DRAW_JIT_SAMPLER_NUM_FIELDS
];
180 elem_types
[DRAW_JIT_SAMPLER_MIN_LOD
] =
181 elem_types
[DRAW_JIT_SAMPLER_MAX_LOD
] =
182 elem_types
[DRAW_JIT_SAMPLER_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
183 elem_types
[DRAW_JIT_SAMPLER_BORDER_COLOR
] =
184 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
186 sampler_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
187 Elements(elem_types
), 0);
189 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, min_lod
,
190 target
, sampler_type
,
191 DRAW_JIT_SAMPLER_MIN_LOD
);
192 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, max_lod
,
193 target
, sampler_type
,
194 DRAW_JIT_SAMPLER_MAX_LOD
);
195 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, lod_bias
,
196 target
, sampler_type
,
197 DRAW_JIT_SAMPLER_LOD_BIAS
);
198 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, border_color
,
199 target
, sampler_type
,
200 DRAW_JIT_SAMPLER_BORDER_COLOR
);
202 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler
, target
, sampler_type
);
209 * Create LLVM type for struct draw_jit_context
212 create_jit_context_type(struct gallivm_state
*gallivm
,
213 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
214 const char *struct_name
)
216 LLVMTargetDataRef target
= gallivm
->target
;
217 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
218 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
219 LLVMTypeRef elem_types
[DRAW_JIT_CTX_NUM_FIELDS
];
220 LLVMTypeRef context_type
;
222 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
223 LP_MAX_TGSI_CONST_BUFFERS
);
224 elem_types
[1] = LLVMArrayType(int_type
, /* num_vs_constants */
225 LP_MAX_TGSI_CONST_BUFFERS
);
226 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
227 DRAW_TOTAL_CLIP_PLANES
), 0);
228 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewport */
229 elem_types
[4] = LLVMArrayType(texture_type
,
230 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
231 elem_types
[5] = LLVMArrayType(sampler_type
,
232 PIPE_MAX_SAMPLERS
); /* samplers */
233 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
234 Elements(elem_types
), 0);
235 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
236 target
, context_type
, DRAW_JIT_CTX_CONSTANTS
);
237 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, num_vs_constants
,
238 target
, context_type
, DRAW_JIT_CTX_NUM_CONSTANTS
);
239 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
240 target
, context_type
, DRAW_JIT_CTX_PLANES
);
241 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewport
,
242 target
, context_type
, DRAW_JIT_CTX_VIEWPORT
);
243 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
244 target
, context_type
,
245 DRAW_JIT_CTX_TEXTURES
);
246 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
247 target
, context_type
,
248 DRAW_JIT_CTX_SAMPLERS
);
249 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
250 target
, context_type
);
257 * Create LLVM type for struct draw_gs_jit_context
260 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
261 unsigned vector_length
,
262 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
263 const char *struct_name
)
265 LLVMTargetDataRef target
= gallivm
->target
;
266 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
267 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
268 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
269 LLVMTypeRef context_type
;
271 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
272 LP_MAX_TGSI_CONST_BUFFERS
);
273 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
274 LP_MAX_TGSI_CONST_BUFFERS
);
275 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
276 DRAW_TOTAL_CLIP_PLANES
), 0);
277 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewport */
279 elem_types
[4] = LLVMArrayType(texture_type
,
280 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
281 elem_types
[5] = LLVMArrayType(sampler_type
,
282 PIPE_MAX_SAMPLERS
); /* samplers */
284 elem_types
[6] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
285 elem_types
[7] = LLVMPointerType(LLVMVectorType(int_type
,
287 elem_types
[8] = LLVMPointerType(LLVMVectorType(int_type
,
290 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
291 Elements(elem_types
), 0);
293 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
294 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
295 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, num_constants
,
296 target
, context_type
, DRAW_GS_JIT_CTX_NUM_CONSTANTS
);
297 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
298 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
299 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewport
,
300 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
301 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
302 target
, context_type
,
303 DRAW_GS_JIT_CTX_TEXTURES
);
304 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
305 target
, context_type
,
306 DRAW_GS_JIT_CTX_SAMPLERS
);
307 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
308 target
, context_type
,
309 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
310 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
311 target
, context_type
,
312 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
313 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
314 target
, context_type
,
315 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
316 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
317 target
, context_type
);
324 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
326 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
327 LLVMTypeRef input_array
;
329 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
330 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
331 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
332 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
338 * Create LLVM type for struct pipe_vertex_buffer
341 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
,
342 const char *struct_name
)
344 LLVMTargetDataRef target
= gallivm
->target
;
345 LLVMTypeRef elem_types
[4];
349 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
351 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
353 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
354 Elements(elem_types
), 0);
356 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
358 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
361 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
368 * Create LLVM type for struct vertex_header;
371 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
373 LLVMTargetDataRef target
= gallivm
->target
;
374 LLVMTypeRef elem_types
[4];
375 LLVMTypeRef vertex_header
;
376 char struct_name
[24];
378 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
380 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
381 elem_types
[DRAW_JIT_VERTEX_CLIP
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
382 elem_types
[DRAW_JIT_VERTEX_PRE_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
383 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
385 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
386 Elements(elem_types
), 0);
388 /* these are bit-fields and we can't take address of them
389 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
390 target, vertex_header,
391 DRAW_JIT_VERTEX_CLIPMASK);
392 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
393 target, vertex_header,
394 DRAW_JIT_VERTEX_EDGEFLAG);
395 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
396 target, vertex_header,
397 DRAW_JIT_VERTEX_PAD);
398 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
399 target, vertex_header,
400 DRAW_JIT_VERTEX_VERTEX_ID);
402 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
403 target
, vertex_header
,
404 DRAW_JIT_VERTEX_CLIP
);
405 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, pre_clip_pos
,
406 target
, vertex_header
,
407 DRAW_JIT_VERTEX_PRE_CLIP_POS
);
408 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
409 target
, vertex_header
,
410 DRAW_JIT_VERTEX_DATA
);
412 assert(LLVMABISizeOfType(target
, vertex_header
) ==
413 offsetof(struct vertex_header
, data
[data_elems
]));
415 return vertex_header
;
420 * Create LLVM types for various structures.
423 create_jit_types(struct draw_llvm_variant
*variant
)
425 struct gallivm_state
*gallivm
= variant
->gallivm
;
426 LLVMTypeRef texture_type
, sampler_type
, context_type
, buffer_type
,
429 texture_type
= create_jit_texture_type(gallivm
, "texture");
430 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
432 context_type
= create_jit_context_type(gallivm
, texture_type
, sampler_type
,
434 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
436 buffer_type
= create_jit_dvbuffer_type(gallivm
, "draw_vertex_buffer");
437 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
439 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
440 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
445 get_context_ptr_type(struct draw_llvm_variant
*variant
)
447 if (!variant
->context_ptr_type
)
448 create_jit_types(variant
);
449 return variant
->context_ptr_type
;
454 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
456 if (!variant
->buffer_ptr_type
)
457 create_jit_types(variant
);
458 return variant
->buffer_ptr_type
;
463 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
465 if (!variant
->vb_ptr_type
)
466 create_jit_types(variant
);
467 return variant
->vb_ptr_type
;
471 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
473 if (!variant
->vertex_header_ptr_type
)
474 create_jit_types(variant
);
475 return variant
->vertex_header_ptr_type
;
480 * Create per-context LLVM info.
483 draw_llvm_create(struct draw_context
*draw
, LLVMContextRef context
)
485 struct draw_llvm
*llvm
;
487 if (!lp_build_init())
490 llvm
= CALLOC_STRUCT( draw_llvm
);
496 llvm
->context
= context
;
497 if (!llvm
->context
) {
498 llvm
->context
= LLVMContextCreate();
499 llvm
->context_owned
= true;
504 llvm
->nr_variants
= 0;
505 make_empty_list(&llvm
->vs_variants_list
);
507 llvm
->nr_gs_variants
= 0;
508 make_empty_list(&llvm
->gs_variants_list
);
513 draw_llvm_destroy(llvm
);
519 * Free per-context LLVM info.
522 draw_llvm_destroy(struct draw_llvm
*llvm
)
524 if (llvm
->context_owned
)
525 LLVMContextDispose(llvm
->context
);
526 llvm
->context
= NULL
;
528 /* XXX free other draw_llvm data? */
534 * Create LLVM-generated code for a vertex shader.
536 struct draw_llvm_variant
*
537 draw_llvm_create_variant(struct draw_llvm
*llvm
,
539 const struct draw_llvm_variant_key
*key
)
541 struct draw_llvm_variant
*variant
;
542 struct llvm_vertex_shader
*shader
=
543 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
544 LLVMTypeRef vertex_header
;
545 char module_name
[64];
547 variant
= MALLOC(sizeof *variant
+
548 shader
->variant_key_size
-
549 sizeof variant
->key
);
553 variant
->llvm
= llvm
;
554 variant
->shader
= shader
;
556 util_snprintf(module_name
, sizeof(module_name
), "draw_llvm_vs_variant%u",
557 variant
->shader
->variants_cached
);
559 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
561 create_jit_types(variant
);
563 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
565 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
567 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
569 draw_llvm_generate(llvm
, variant
, FALSE
); /* linear */
570 draw_llvm_generate(llvm
, variant
, TRUE
); /* elts */
572 gallivm_compile_module(variant
->gallivm
);
574 variant
->jit_func
= (draw_jit_vert_func
)
575 gallivm_jit_function(variant
->gallivm
, variant
->function
);
577 variant
->jit_func_elts
= (draw_jit_vert_func_elts
)
578 gallivm_jit_function(variant
->gallivm
, variant
->function_elts
);
580 gallivm_free_ir(variant
->gallivm
);
582 variant
->list_item_global
.base
= variant
;
583 variant
->list_item_local
.base
= variant
;
584 /*variant->no = */shader
->variants_created
++;
585 variant
->list_item_global
.base
= variant
;
592 generate_vs(struct draw_llvm_variant
*variant
,
593 LLVMBuilderRef builder
,
594 struct lp_type vs_type
,
595 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
596 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
597 const struct lp_bld_tgsi_system_values
*system_values
,
598 LLVMValueRef context_ptr
,
599 struct lp_build_sampler_soa
*draw_sampler
,
600 boolean clamp_vertex_color
)
602 struct draw_llvm
*llvm
= variant
->llvm
;
603 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
604 LLVMValueRef consts_ptr
=
605 draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
606 LLVMValueRef num_consts_ptr
=
607 draw_jit_context_num_vs_constants(variant
->gallivm
, context_ptr
);
609 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
610 tgsi_dump(tokens
, 0);
611 draw_llvm_dump_variant_key(&variant
->key
);
614 lp_build_tgsi_soa(variant
->gallivm
,
617 NULL
/*struct lp_build_mask_context *mask*/,
624 &llvm
->draw
->vs
.vertex_shader
->info
,
629 unsigned chan
, attrib
;
630 struct lp_build_context bld
;
631 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
632 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
634 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
635 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
636 if (outputs
[attrib
][chan
]) {
637 switch (info
->output_semantic_name
[attrib
]) {
638 case TGSI_SEMANTIC_COLOR
:
639 case TGSI_SEMANTIC_BCOLOR
:
640 if (clamp_vertex_color
) {
641 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
642 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
643 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
654 generate_fetch(struct gallivm_state
*gallivm
,
655 struct draw_context
*draw
,
656 LLVMValueRef vbuffers_ptr
,
658 struct pipe_vertex_element
*velem
,
661 LLVMValueRef instance_id
,
662 LLVMValueRef start_instance
)
664 const struct util_format_description
*format_desc
=
665 util_format_description(velem
->src_format
);
666 LLVMValueRef zero
= LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
));
667 LLVMBuilderRef builder
= gallivm
->builder
;
668 LLVMValueRef indices
=
669 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
670 velem
->vertex_buffer_index
, 0);
671 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
673 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
674 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
675 LLVMValueRef map_ptr
= draw_jit_dvbuffer_map(gallivm
, vbuffer_ptr
);
676 LLVMValueRef buffer_size
= draw_jit_dvbuffer_size(gallivm
, vbuffer_ptr
);
678 LLVMValueRef buffer_overflowed
;
679 LLVMValueRef needed_buffer_size
;
680 LLVMValueRef temp_ptr
=
681 lp_build_alloca(gallivm
,
682 lp_build_vec_type(gallivm
, lp_float32_vec4_type()), "");
683 LLVMValueRef ofbit
= NULL
;
684 struct lp_build_if_state if_ctx
;
686 if (velem
->instance_divisor
) {
687 /* Index is equal to the start instance plus the number of current
688 * instance divided by the divisor. In this case we compute it as:
689 * index = start_instance + (instance_id / divisor)
691 LLVMValueRef current_instance
;
692 current_instance
= LLVMBuildUDiv(builder
, instance_id
,
693 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
695 index
= lp_build_uadd_overflow(gallivm
, start_instance
,
696 current_instance
, &ofbit
);
699 stride
= lp_build_umul_overflow(gallivm
, vb_stride
, index
, &ofbit
);
700 stride
= lp_build_uadd_overflow(gallivm
, stride
, vb_buffer_offset
, &ofbit
);
701 stride
= lp_build_uadd_overflow(
703 lp_build_const_int32(gallivm
, velem
->src_offset
), &ofbit
);
704 needed_buffer_size
= lp_build_uadd_overflow(
706 lp_build_const_int32(gallivm
,
707 util_format_get_blocksize(velem
->src_format
)),
710 buffer_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
711 needed_buffer_size
, buffer_size
,
712 "buffer_overflowed");
713 buffer_overflowed
= LLVMBuildOr(builder
, buffer_overflowed
, ofbit
, "");
715 lp_build_printf(gallivm
, "vbuf index = %u, vb_stride is %u\n",
717 lp_build_printf(gallivm
, " vb_buffer_offset = %u, src_offset is %u\n",
719 lp_build_const_int32(gallivm
, velem
->src_offset
));
720 lp_build_print_value(gallivm
, " blocksize = ",
721 lp_build_const_int32(
723 util_format_get_blocksize(velem
->src_format
)));
724 lp_build_printf(gallivm
, " instance_id = %u\n", instance_id
);
725 lp_build_printf(gallivm
, " stride = %u\n", stride
);
726 lp_build_printf(gallivm
, " buffer size = %u\n", buffer_size
);
727 lp_build_printf(gallivm
, " needed_buffer_size = %u\n", needed_buffer_size
);
728 lp_build_print_value(gallivm
, " buffer overflowed = ", buffer_overflowed
);
731 lp_build_if(&if_ctx
, gallivm
, buffer_overflowed
);
734 lp_build_const_vec(gallivm
, lp_float32_vec4_type(), 0);
735 LLVMBuildStore(builder
, val
, temp_ptr
);
737 lp_build_else(&if_ctx
);
740 map_ptr
= LLVMBuildGEP(builder
, map_ptr
, &stride
, 1, "");
742 val
= lp_build_fetch_rgba_aos(gallivm
,
744 lp_float32_vec4_type(),
747 LLVMBuildStore(builder
, val
, temp_ptr
);
749 lp_build_endif(&if_ctx
);
751 *res
= LLVMBuildLoad(builder
, temp_ptr
, "aos");
755 convert_to_soa(struct gallivm_state
*gallivm
,
756 LLVMValueRef (*src_aos
)[LP_MAX_VECTOR_WIDTH
/ 32],
757 LLVMValueRef (*dst_soa
)[TGSI_NUM_CHANNELS
],
758 unsigned num_attribs
, const struct lp_type soa_type
)
761 struct lp_type aos_channel_type
= soa_type
;
763 debug_assert(TGSI_NUM_CHANNELS
== 4);
764 debug_assert((soa_type
.length
% TGSI_NUM_CHANNELS
) == 0);
766 aos_channel_type
.length
>>= 1;
768 for (i
= 0; i
< num_attribs
; ++i
) {
769 LLVMValueRef aos_channels
[TGSI_NUM_CHANNELS
];
770 unsigned pixels_per_channel
= soa_type
.length
/ TGSI_NUM_CHANNELS
;
772 for (j
= 0; j
< TGSI_NUM_CHANNELS
; ++j
) {
773 LLVMValueRef channel
[LP_MAX_VECTOR_LENGTH
] = { 0 };
775 assert(pixels_per_channel
<= LP_MAX_VECTOR_LENGTH
);
777 for (k
= 0; k
< pixels_per_channel
; ++k
) {
778 channel
[k
] = src_aos
[i
][j
+ TGSI_NUM_CHANNELS
* k
];
781 aos_channels
[j
] = lp_build_concat(gallivm
, channel
, aos_channel_type
, pixels_per_channel
);
784 lp_build_transpose_aos(gallivm
, soa_type
, aos_channels
, dst_soa
[i
]);
790 store_aos(struct gallivm_state
*gallivm
,
795 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
796 LLVMBuilderRef builder
= gallivm
->builder
;
797 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
798 LLVMValueRef indices
[3];
800 indices
[0] = lp_build_const_int32(gallivm
, 0);
802 indices
[2] = lp_build_const_int32(gallivm
, 0);
804 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
805 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
808 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
811 /* Unaligned store due to the vertex header */
812 lp_set_store_alignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
816 * Adjust the mask to architecture endianess. The mask will the store in struct:
818 * struct vertex_header {
819 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
820 * unsigned edgeflag:1;
821 * unsigned have_clipdist:1;
822 * unsigned vertex_id:16;
826 * On little-endian machine nothing needs to done, however on bit-endian machine
827 * the mask's fields need to be adjusted with the algorithm:
829 * uint32_t reverse (uint32_t x)
831 * return (x >> 16) | // vertex_id
832 * ((x & 0x3fff) << 18) | // clipmask
833 * ((x & 0x4000) << 3) | // have_clipdist
834 * ((x & 0x8000) << 1); // edgeflag
838 adjust_mask(struct gallivm_state
*gallivm
,
841 #ifdef PIPE_ARCH_BIG_ENDIAN
842 LLVMBuilderRef builder
= gallivm
->builder
;
843 LLVMValueRef vertex_id
;
844 LLVMValueRef clipmask
;
845 LLVMValueRef have_clipdist
;
846 LLVMValueRef edgeflag
;
848 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
849 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
850 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
851 have_clipdist
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
852 have_clipdist
= LLVMBuildShl(builder
, have_clipdist
, lp_build_const_int32(gallivm
, 3), "");
853 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
854 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
856 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
857 mask
= LLVMBuildOr(builder
, mask
, have_clipdist
, "");
858 mask
= LLVMBuildOr(builder
, mask
, edgeflag
, "");
864 store_aos_array(struct gallivm_state
*gallivm
,
865 struct lp_type soa_type
,
867 LLVMValueRef
*indices
,
871 LLVMValueRef clipmask
,
872 boolean have_clipdist
)
874 LLVMBuilderRef builder
= gallivm
->builder
;
875 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
876 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
877 LLVMValueRef linear_inds
[LP_MAX_VECTOR_WIDTH
/ 32];
878 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
879 int vector_length
= soa_type
.length
;
882 debug_assert(TGSI_NUM_CHANNELS
== 4);
884 for (i
= 0; i
< vector_length
; i
++) {
885 linear_inds
[i
] = lp_build_const_int32(gallivm
, i
);
887 inds
[i
] = indices
[i
];
889 inds
[i
] = linear_inds
[i
];
891 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
895 /* store vertex header for each of the n vertices */
896 LLVMValueRef val
, cliptmp
;
897 int vertex_id_pad_edgeflag
;
899 /* If this assertion fails, it means we need to update the bit twidding
900 * code here. See struct vertex_header in draw_private.h.
902 assert(DRAW_TOTAL_CLIP_PLANES
==14);
903 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
904 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
906 vertex_id_pad_edgeflag
|= 1 << (DRAW_TOTAL_CLIP_PLANES
+1);
907 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
), vertex_id_pad_edgeflag
);
908 /* OR with the clipmask */
909 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
910 for (i
= 0; i
< vector_length
; i
++) {
911 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
912 val
= LLVMBuildExtractElement(builder
, cliptmp
, linear_inds
[i
], "");
913 val
= adjust_mask(gallivm
, val
);
915 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
916 io_ptrs
[i
], inds
[i
], val
);
918 LLVMBuildStore(builder
, val
, id_ptr
);
922 /* store for each of the n vertices */
923 for (i
= 0; i
< vector_length
; i
++) {
924 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
930 convert_to_aos(struct gallivm_state
*gallivm
,
932 LLVMValueRef
*indices
,
933 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
934 LLVMValueRef clipmask
,
936 struct lp_type soa_type
,
937 boolean have_clipdist
)
939 LLVMBuilderRef builder
= gallivm
->builder
;
940 unsigned chan
, attrib
, i
;
943 lp_build_printf(gallivm
, " # storing begin\n");
945 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
946 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
947 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
948 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
949 if (outputs
[attrib
][chan
]) {
950 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
951 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
953 lp_build_printf(gallivm
, "output %d : %d ",
954 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
956 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
958 lp_build_print_value(gallivm
, "val = ", out
);
961 LLVMBuildBitCast(builder
, out
, lp_build_int_vec_type(gallivm
, soa_type
), "");
963 lp_build_print_value(gallivm
, " ival = ", iv
);
974 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
975 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
977 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
979 for (i
= 0; i
< soa_type
.length
; ++i
) {
980 aos
[i
] = lp_build_extract_range(gallivm
,
981 soa
[i
% TGSI_NUM_CHANNELS
],
982 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
987 store_aos_array(gallivm
,
993 clipmask
, have_clipdist
);
996 lp_build_printf(gallivm
, " # storing end\n");
1002 * Stores original vertex positions in clip coordinates
1005 store_clip(struct gallivm_state
*gallivm
,
1006 const struct lp_type vs_type
,
1007 LLVMValueRef io_ptr
,
1008 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1009 boolean pre_clip_pos
, int idx
)
1011 LLVMBuilderRef builder
= gallivm
->builder
;
1012 LLVMValueRef soa
[4];
1013 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
1014 LLVMValueRef indices
[2];
1015 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1016 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1017 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1018 LLVMTypeRef clip_ptr_type
=
1019 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
),
1024 indices
[1] = lp_build_const_int32(gallivm
, 0);
1026 for (i
= 0; i
< vs_type
.length
; i
++) {
1027 inds
[i
] = lp_build_const_int32(gallivm
, i
);
1028 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1031 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
1032 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
1033 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
1034 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
1036 if (!pre_clip_pos
) {
1037 for (i
= 0; i
< vs_type
.length
; i
++) {
1038 clip_ptrs
[i
] = draw_jit_header_clip(gallivm
, io_ptrs
[i
]);
1041 for (i
= 0; i
< vs_type
.length
; i
++) {
1042 clip_ptrs
[i
] = draw_jit_header_pre_clip_pos(gallivm
, io_ptrs
[i
]);
1046 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
1047 for (i
= 0; i
< vs_type
.length
; ++i
) {
1048 aos
[i
] = lp_build_extract_range(gallivm
,
1049 soa
[i
% TGSI_NUM_CHANNELS
],
1050 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1054 for (j
= 0; j
< vs_type
.length
; j
++) {
1055 LLVMValueRef clip_ptr
;
1057 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
1058 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
1060 /* Unaligned store */
1061 lp_set_store_alignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
1067 * Transforms the outputs for viewport mapping
1070 generate_viewport(struct draw_llvm_variant
*variant
,
1071 LLVMBuilderRef builder
,
1072 struct lp_type vs_type
,
1073 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1074 LLVMValueRef context_ptr
)
1077 struct gallivm_state
*gallivm
= variant
->gallivm
;
1078 struct lp_type f32_type
= vs_type
;
1079 const unsigned pos
= variant
->llvm
->draw
->vs
.position_output
;
1080 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1081 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn*/
1082 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1083 LLVMValueRef vp_ptr
= draw_jit_context_viewport(gallivm
, context_ptr
);
1085 /* for 1/w convention*/
1086 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1087 LLVMBuildStore(builder
, out3
, outputs
[pos
][3]);
1089 /* Viewport Mapping */
1090 for (i
=0; i
<3; i
++) {
1091 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[pos
][i
], ""); /*x0 x1 .. xn*/
1094 LLVMValueRef scale_i
;
1095 LLVMValueRef trans_i
;
1098 index
= lp_build_const_int32(gallivm
, i
);
1099 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1101 index
= lp_build_const_int32(gallivm
, i
+4);
1102 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1104 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1105 LLVMBuildLoad(builder
, scale_i
, "scale"));
1106 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1107 LLVMBuildLoad(builder
, trans_i
, "trans"));
1110 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1112 out
= LLVMBuildFMul(builder
, out
, scale
, "");
1113 /* add translation */
1114 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
1116 /* store transformed outputs */
1117 LLVMBuildStore(builder
, out
, outputs
[pos
][i
]);
1124 * Returns clipmask as nxi32 bitmask for the n vertices
1127 generate_clipmask(struct draw_llvm
*llvm
,
1128 struct gallivm_state
*gallivm
,
1129 struct lp_type vs_type
,
1130 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1135 unsigned ucp_enable
,
1136 LLVMValueRef context_ptr
,
1137 boolean
*have_clipdist
)
1139 LLVMBuilderRef builder
= gallivm
->builder
;
1140 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1141 LLVMValueRef test
, temp
;
1142 LLVMValueRef zero
, shift
;
1143 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1144 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1145 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1146 struct lp_type f32_type
= vs_type
;
1147 struct lp_type i32_type
= lp_int_type(vs_type
);
1148 const unsigned pos
= llvm
->draw
->vs
.position_output
;
1149 const unsigned cv
= llvm
->draw
->vs
.clipvertex_output
;
1150 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1151 bool have_cd
= false;
1154 cd
[0] = llvm
->draw
->vs
.clipdistance_output
[0];
1155 cd
[1] = llvm
->draw
->vs
.clipdistance_output
[1];
1157 if (cd
[0] != pos
|| cd
[1] != pos
)
1160 if (num_written_clipdistance
&& !clip_user
) {
1162 ucp_enable
= (1 << num_written_clipdistance
) - 1;
1165 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1166 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1167 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1168 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1171 * load clipvertex and position from correct locations.
1172 * if they are the same just load them once.
1174 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1175 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1176 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1177 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1179 if (clip_user
&& cv
!= pos
) {
1180 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1181 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1182 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1183 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1191 /* Cliptest, for hardwired planes */
1194 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1196 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1200 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1201 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1202 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1203 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1204 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1207 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1208 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1209 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1210 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1213 test
= LLVMBuildFAdd(builder
, pos_y
, 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
, "");
1221 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1224 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1225 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1226 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1230 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1231 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1232 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1233 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1236 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1237 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1238 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1239 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1243 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1244 LLVMValueRef indices
[3];
1245 LLVMValueRef is_nan_or_inf
;
1247 /* userclip planes */
1248 while (ucp_enable
) {
1249 unsigned plane_idx
= ffs(ucp_enable
)-1;
1250 ucp_enable
&= ~(1 << plane_idx
);
1253 if (have_cd
&& num_written_clipdistance
) {
1254 LLVMValueRef clipdist
;
1258 *have_clipdist
= TRUE
;
1260 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1262 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1264 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1265 is_nan_or_inf
= lp_build_is_inf_or_nan(gallivm
, vs_type
, clipdist
);
1266 test
= LLVMBuildOr(builder
, test
, is_nan_or_inf
, "");
1267 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1268 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1269 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1271 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1272 indices
[0] = lp_build_const_int32(gallivm
, 0);
1273 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1275 indices
[2] = lp_build_const_int32(gallivm
, 0);
1276 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1277 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1278 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1279 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1281 indices
[2] = lp_build_const_int32(gallivm
, 1);
1282 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1283 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1284 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1285 test
= LLVMBuildFMul(builder
, planes
, cv_y
, "");
1286 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1288 indices
[2] = lp_build_const_int32(gallivm
, 2);
1289 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1290 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1291 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1292 test
= LLVMBuildFMul(builder
, planes
, cv_z
, "");
1293 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1295 indices
[2] = lp_build_const_int32(gallivm
, 3);
1296 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1297 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1298 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1299 test
= LLVMBuildFMul(builder
, planes
, cv_w
, "");
1300 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1302 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1303 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1304 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1305 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1314 * Returns boolean if any clipping has occurred
1315 * Used zero/non-zero i32 value to represent boolean
1318 clipmask_booli32(struct gallivm_state
*gallivm
,
1319 const struct lp_type vs_type
,
1320 LLVMValueRef clipmask_bool_ptr
)
1322 LLVMBuilderRef builder
= gallivm
->builder
;
1323 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
1324 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1325 LLVMValueRef ret
= LLVMConstNull(int32_type
);
1330 * Can do this with log2(vector length) pack instructions and one extract
1331 * (as we don't actually need a or) with sse2 which would be way better.
1333 for (i
=0; i
< vs_type
.length
; i
++) {
1334 temp
= LLVMBuildExtractElement(builder
, clipmask_bool
,
1335 lp_build_const_int32(gallivm
, i
) , "");
1336 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1342 draw_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
1343 struct lp_build_tgsi_context
* bld_base
,
1344 boolean is_vindex_indirect
,
1345 LLVMValueRef vertex_index
,
1346 boolean is_aindex_indirect
,
1347 LLVMValueRef attrib_index
,
1348 LLVMValueRef swizzle_index
)
1350 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1351 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1352 LLVMBuilderRef builder
= gallivm
->builder
;
1353 LLVMValueRef indices
[3];
1355 struct lp_type type
= bld_base
->base
.type
;
1357 if (is_vindex_indirect
|| is_aindex_indirect
) {
1359 res
= bld_base
->base
.zero
;
1360 for (i
= 0; i
< type
.length
; ++i
) {
1361 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1362 LLVMValueRef vert_chan_index
= vertex_index
;
1363 LLVMValueRef attr_chan_index
= attrib_index
;
1364 LLVMValueRef channel_vec
, value
;
1366 if (is_vindex_indirect
) {
1367 vert_chan_index
= LLVMBuildExtractElement(builder
,
1368 vertex_index
, idx
, "");
1370 if (is_aindex_indirect
) {
1371 attr_chan_index
= LLVMBuildExtractElement(builder
,
1372 attrib_index
, idx
, "");
1375 indices
[0] = vert_chan_index
;
1376 indices
[1] = attr_chan_index
;
1377 indices
[2] = swizzle_index
;
1379 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1380 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1381 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1383 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1386 indices
[0] = vertex_index
;
1387 indices
[1] = attrib_index
;
1388 indices
[2] = swizzle_index
;
1390 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1391 res
= LLVMBuildLoad(builder
, res
, "");
1398 draw_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
1399 struct lp_build_tgsi_context
* bld_base
,
1400 LLVMValueRef (*outputs
)[4],
1401 LLVMValueRef emitted_vertices_vec
)
1403 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1404 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1405 struct gallivm_state
*gallivm
= variant
->gallivm
;
1406 LLVMBuilderRef builder
= gallivm
->builder
;
1407 struct lp_type gs_type
= bld_base
->base
.type
;
1408 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1409 lp_int_type(gs_type
), 0);
1410 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1411 LLVMValueRef next_prim_offset
=
1412 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1413 LLVMValueRef io
= variant
->io_ptr
;
1415 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1417 for (i
= 0; i
< gs_type
.length
; ++i
) {
1418 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1419 LLVMValueRef currently_emitted
=
1420 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1421 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1422 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1425 convert_to_aos(gallivm
, io
, indices
,
1427 gs_info
->num_outputs
, gs_type
,
1432 draw_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
1433 struct lp_build_tgsi_context
* bld_base
,
1434 LLVMValueRef verts_per_prim_vec
,
1435 LLVMValueRef emitted_prims_vec
)
1437 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1438 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1439 struct gallivm_state
*gallivm
= variant
->gallivm
;
1440 LLVMBuilderRef builder
= gallivm
->builder
;
1441 LLVMValueRef prim_lengts_ptr
=
1442 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1445 for (i
= 0; i
< bld_base
->base
.type
.length
; ++i
) {
1446 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1447 LLVMValueRef prims_emitted
=
1448 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1449 LLVMValueRef store_ptr
;
1450 LLVMValueRef num_vertices
=
1451 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1453 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1454 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1455 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1456 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1461 draw_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
1462 struct lp_build_tgsi_context
* bld_base
,
1463 LLVMValueRef total_emitted_vertices_vec
,
1464 LLVMValueRef emitted_prims_vec
)
1466 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1467 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1468 struct gallivm_state
*gallivm
= variant
->gallivm
;
1469 LLVMBuilderRef builder
= gallivm
->builder
;
1470 LLVMValueRef emitted_verts_ptr
=
1471 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1472 LLVMValueRef emitted_prims_ptr
=
1473 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1474 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1476 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &zero
, 0, "");
1477 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &zero
, 0, "");
1479 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1480 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1484 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1487 struct gallivm_state
*gallivm
= variant
->gallivm
;
1488 LLVMContextRef context
= gallivm
->context
;
1489 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1490 LLVMTypeRef arg_types
[11];
1491 unsigned num_arg_types
=
1492 elts
? Elements(arg_types
) : Elements(arg_types
) - 1;
1493 LLVMTypeRef func_type
;
1494 LLVMValueRef context_ptr
;
1495 LLVMBasicBlockRef block
;
1496 LLVMBuilderRef builder
;
1498 struct lp_type vs_type
;
1499 LLVMValueRef end
, start
;
1500 LLVMValueRef count
, fetch_elts
, fetch_elt_max
, fetch_count
;
1501 LLVMValueRef vertex_id_offset
, start_instance
;
1502 LLVMValueRef stride
, step
, io_itr
;
1503 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1504 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1505 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1506 struct draw_context
*draw
= llvm
->draw
;
1507 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1509 struct lp_build_context bld
;
1510 struct lp_build_loop_state lp_loop
;
1511 const int vector_length
= lp_native_vector_width
/ 32;
1512 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1513 LLVMValueRef fetch_max
;
1514 struct lp_build_sampler_soa
*sampler
= 0;
1515 LLVMValueRef ret
, clipmask_bool_ptr
;
1516 struct draw_llvm_variant_key
*key
= &variant
->key
;
1517 /* If geometry shader is present we need to skip both the viewport
1518 * transformation and clipping otherwise the inputs to the geometry
1519 * shader will be incorrect.
1521 const boolean bypass_viewport
= key
->has_gs
|| key
->bypass_viewport
;
1522 const boolean enable_cliptest
= !key
->has_gs
&& (key
->clip_xy
||
1525 LLVMValueRef variant_func
;
1526 const unsigned pos
= llvm
->draw
->vs
.position_output
;
1527 const unsigned cv
= llvm
->draw
->vs
.clipvertex_output
;
1528 boolean have_clipdist
= FALSE
;
1529 struct lp_bld_tgsi_system_values system_values
;
1531 memset(&system_values
, 0, sizeof(system_values
));
1533 util_snprintf(func_name
, sizeof(func_name
), "draw_llvm_vs_variant%u_%s",
1534 variant
->shader
->variants_cached
, elts
? "elts" : "linear");
1537 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1538 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1539 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1541 arg_types
[i
++] = LLVMPointerType(int32_type
, 0);/* fetch_elts */
1542 arg_types
[i
++] = int32_type
; /* fetch_elt_max */
1544 arg_types
[i
++] = int32_type
; /* start */
1545 arg_types
[i
++] = int32_type
; /* fetch_count / count */
1546 arg_types
[i
++] = int32_type
; /* stride */
1547 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1548 arg_types
[i
++] = int32_type
; /* instance_id */
1549 arg_types
[i
++] = int32_type
; /* vertex_id_offset */
1550 arg_types
[i
++] = int32_type
; /* start_instance */
1552 func_type
= LLVMFunctionType(int32_type
, arg_types
, num_arg_types
, 0);
1554 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
1557 variant
->function_elts
= variant_func
;
1559 variant
->function
= variant_func
;
1561 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1562 for (i
= 0; i
< num_arg_types
; ++i
)
1563 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1564 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1565 LLVMNoAliasAttribute
);
1567 context_ptr
= LLVMGetParam(variant_func
, 0);
1568 io_ptr
= LLVMGetParam(variant_func
, 1);
1569 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1570 stride
= LLVMGetParam(variant_func
, 5 + (elts
? 1 : 0));
1571 vb_ptr
= LLVMGetParam(variant_func
, 6 + (elts
? 1 : 0));
1572 system_values
.instance_id
= LLVMGetParam(variant_func
, 7 + (elts
? 1 : 0));
1573 vertex_id_offset
= LLVMGetParam(variant_func
, 8 + (elts
? 1 : 0));
1574 start_instance
= LLVMGetParam(variant_func
, 9 + (elts
? 1 : 0));
1576 lp_build_name(context_ptr
, "context");
1577 lp_build_name(io_ptr
, "io");
1578 lp_build_name(vbuffers_ptr
, "vbuffers");
1579 lp_build_name(stride
, "stride");
1580 lp_build_name(vb_ptr
, "vb");
1581 lp_build_name(system_values
.instance_id
, "instance_id");
1582 lp_build_name(vertex_id_offset
, "vertex_id_offset");
1583 lp_build_name(start_instance
, "start_instance");
1586 fetch_elts
= LLVMGetParam(variant_func
, 3);
1587 fetch_elt_max
= LLVMGetParam(variant_func
, 4);
1588 fetch_count
= LLVMGetParam(variant_func
, 5);
1589 lp_build_name(fetch_elts
, "fetch_elts");
1590 lp_build_name(fetch_elt_max
, "fetch_elt_max");
1591 lp_build_name(fetch_count
, "fetch_count");
1592 start
= count
= NULL
;
1595 start
= LLVMGetParam(variant_func
, 3);
1596 count
= LLVMGetParam(variant_func
, 4);
1597 lp_build_name(start
, "start");
1598 lp_build_name(count
, "count");
1599 fetch_elts
= fetch_count
= NULL
;
1606 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1607 builder
= gallivm
->builder
;
1608 LLVMPositionBuilderAtEnd(builder
, block
);
1610 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1612 memset(&vs_type
, 0, sizeof vs_type
);
1613 vs_type
.floating
= TRUE
; /* floating point values */
1614 vs_type
.sign
= TRUE
; /* values are signed */
1615 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1616 vs_type
.width
= 32; /* 32-bit float */
1617 vs_type
.length
= vector_length
;
1619 /* hold temporary "bool" clipmask */
1620 clipmask_bool_ptr
= lp_build_alloca(gallivm
, lp_build_int_vec_type(gallivm
, vs_type
), "");
1621 LLVMBuildStore(builder
, lp_build_zero(gallivm
, lp_int_type(vs_type
)), clipmask_bool_ptr
);
1623 /* code generated texture sampling */
1624 sampler
= draw_llvm_sampler_soa_create(
1625 draw_llvm_variant_key_samplers(key
),
1631 count
= fetch_count
;
1634 end
= lp_build_add(&bld
, start
, count
);
1637 step
= lp_build_const_int32(gallivm
, vector_length
);
1639 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1641 lp_build_loop_begin(&lp_loop
, gallivm
, zero
);
1643 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1644 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][LP_MAX_VECTOR_WIDTH
/ 32] = { { 0 } };
1646 LLVMValueRef clipmask
; /* holds the clipmask value */
1647 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1649 io_itr
= lp_loop
.counter
;
1651 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1653 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1654 io_itr
, io
, lp_loop
.counter
);
1656 system_values
.vertex_id
= lp_build_zero(gallivm
, lp_type_uint_vec(32, 32*vector_length
));
1657 for (i
= 0; i
< vector_length
; ++i
) {
1658 LLVMValueRef vert_index
=
1659 LLVMBuildAdd(builder
,
1661 lp_build_const_int32(gallivm
, i
), "");
1662 LLVMValueRef true_index
=
1663 LLVMBuildAdd(builder
, start
, vert_index
, "");
1664 LLVMValueRef vertex_id
;
1666 /* make sure we're not out of bounds which can happen
1667 * if fetch_count % 4 != 0, because on the last iteration
1668 * a few of the 4 vertex fetches will be out of bounds */
1669 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1672 LLVMValueRef fetch_ptr
;
1673 LLVMValueRef index_overflowed
;
1674 LLVMValueRef index_ptr
=
1677 lp_build_vec_type(gallivm
, lp_type_int(32)), "");
1678 struct lp_build_if_state if_ctx
;
1679 index_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
1680 true_index
, fetch_elt_max
,
1681 "index_overflowed");
1683 lp_build_if(&if_ctx
, gallivm
, index_overflowed
);
1685 /* Generate maximum possible index so that
1686 * generate_fetch can treat it just like
1687 * any other overflow and return zeros.
1688 * We don't have to worry about the restart
1689 * primitive index because it has already been
1693 lp_build_const_int32(gallivm
, 0xffffffff);
1694 LLVMBuildStore(builder
, val
, index_ptr
);
1696 lp_build_else(&if_ctx
);
1699 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1700 &true_index
, 1, "");
1701 val
= LLVMBuildLoad(builder
, fetch_ptr
, "");
1702 LLVMBuildStore(builder
, val
, index_ptr
);
1704 lp_build_endif(&if_ctx
);
1705 true_index
= LLVMBuildLoad(builder
, index_ptr
, "true_index");
1707 /* in the paths with elts vertex id has to be unaffected by the
1708 * index bias and because indices inside our elements array have
1709 * already had index bias applied we need to subtract it here to
1710 * get back to the original index.
1711 * in the linear paths vertex id has to be unaffected by the
1712 * original start index and because we abuse the 'start' variable
1713 * to either represent the actual start index or the index at which
1714 * the primitive was split (we split rendering into chunks of at
1715 * most 4095-vertices) we need to back out the original start
1716 * index out of our vertex id here.
1718 vertex_id
= LLVMBuildSub(builder
, true_index
, vertex_id_offset
, "");
1720 system_values
.vertex_id
= LLVMBuildInsertElement(
1722 system_values
.vertex_id
, vertex_id
,
1723 lp_build_const_int32(gallivm
, i
), "");
1725 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1726 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1727 LLVMValueRef vb_index
=
1728 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1729 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1730 generate_fetch(gallivm
, draw
, vbuffers_ptr
,
1731 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1732 system_values
.instance_id
, start_instance
);
1735 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1736 draw
->pt
.nr_vertex_elements
, vs_type
);
1738 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1739 generate_vs(variant
,
1747 key
->clamp_vertex_color
);
1749 if (pos
!= -1 && cv
!= -1) {
1750 /* store original positions in clip before further manipulation */
1751 store_clip(gallivm
, vs_type
, io
, outputs
, FALSE
, key
->clip_user
? cv
: pos
);
1752 store_clip(gallivm
, vs_type
, io
, outputs
, TRUE
, pos
);
1755 if (enable_cliptest
) {
1756 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1757 /* allocate clipmask, assign it integer type */
1758 clipmask
= generate_clipmask(llvm
,
1767 context_ptr
, &have_clipdist
);
1768 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1769 /* store temporary clipping boolean value */
1770 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1773 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1776 /* do viewport mapping */
1777 if (!bypass_viewport
) {
1778 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1782 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1785 /* store clipmask in vertex header,
1786 * original positions in clip
1787 * and transformed positions in data
1789 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
1790 vs_info
->num_outputs
, vs_type
,
1793 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
1795 sampler
->destroy(sampler
);
1797 /* return clipping boolean value for function */
1798 ret
= clipmask_booli32(gallivm
, vs_type
, clipmask_bool_ptr
);
1800 LLVMBuildRet(builder
, ret
);
1802 gallivm_verify_function(gallivm
, variant_func
);
1806 struct draw_llvm_variant_key
*
1807 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1810 struct draw_llvm_variant_key
*key
;
1811 struct draw_sampler_static_state
*draw_sampler
;
1813 key
= (struct draw_llvm_variant_key
*)store
;
1815 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1817 /* Presumably all variants of the shader should have the same
1818 * number of vertex elements - ie the number of shader inputs.
1819 * NOTE: we NEED to store the needed number of needed inputs
1820 * here, not the number of provided elements to match keysize
1821 * (and the offset of sampler state in the key).
1823 key
->nr_vertex_elements
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
1824 assert(key
->nr_vertex_elements
<= llvm
->draw
->pt
.nr_vertex_elements
);
1826 /* will have to rig this up properly later */
1827 key
->clip_xy
= llvm
->draw
->clip_xy
;
1828 key
->clip_z
= llvm
->draw
->clip_z
;
1829 key
->clip_user
= llvm
->draw
->clip_user
;
1830 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1831 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
1832 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1833 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1834 key
->has_gs
= llvm
->draw
->gs
.geometry_shader
!= NULL
;
1835 key
->num_outputs
= draw_total_vs_outputs(llvm
->draw
);
1838 /* All variants of this shader will have the same value for
1839 * nr_samplers. Not yet trying to compact away holes in the
1842 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1843 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
1844 key
->nr_sampler_views
=
1845 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
1848 key
->nr_sampler_views
= key
->nr_samplers
;
1851 draw_sampler
= draw_llvm_variant_key_samplers(key
);
1853 memcpy(key
->vertex_element
,
1854 llvm
->draw
->pt
.vertex_element
,
1855 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1857 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
1859 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1860 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
1861 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
1863 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1864 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
1865 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
1873 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
1876 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
1878 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
1879 debug_printf("clip_xy = %u\n", key
->clip_xy
);
1880 debug_printf("clip_z = %u\n", key
->clip_z
);
1881 debug_printf("clip_user = %u\n", key
->clip_user
);
1882 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
1883 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
1884 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
1885 debug_printf("has_gs = %u\n", key
->has_gs
);
1886 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
1888 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
1889 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
1890 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
1891 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
1892 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
1895 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1896 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
1902 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1903 unsigned shader_stage
,
1905 uint32_t width
, uint32_t height
, uint32_t depth
,
1906 uint32_t first_level
, uint32_t last_level
,
1907 const void *base_ptr
,
1908 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1909 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1910 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
1913 struct draw_jit_texture
*jit_tex
;
1915 assert(shader_stage
== PIPE_SHADER_VERTEX
||
1916 shader_stage
== PIPE_SHADER_GEOMETRY
);
1918 if (shader_stage
== PIPE_SHADER_VERTEX
) {
1919 assert(sview_idx
< Elements(draw
->llvm
->jit_context
.textures
));
1921 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
1922 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
1923 assert(sview_idx
< Elements(draw
->llvm
->gs_jit_context
.textures
));
1925 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
1931 jit_tex
->width
= width
;
1932 jit_tex
->height
= height
;
1933 jit_tex
->depth
= depth
;
1934 jit_tex
->first_level
= first_level
;
1935 jit_tex
->last_level
= last_level
;
1936 jit_tex
->base
= base_ptr
;
1938 for (j
= first_level
; j
<= last_level
; j
++) {
1939 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
1940 jit_tex
->row_stride
[j
] = row_stride
[j
];
1941 jit_tex
->img_stride
[j
] = img_stride
[j
];
1947 draw_llvm_set_sampler_state(struct draw_context
*draw
,
1948 unsigned shader_type
)
1952 if (shader_type
== PIPE_SHADER_VERTEX
) {
1953 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
1954 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
1956 if (draw
->samplers
[i
]) {
1957 const struct pipe_sampler_state
*s
1958 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
1959 jit_sam
->min_lod
= s
->min_lod
;
1960 jit_sam
->max_lod
= s
->max_lod
;
1961 jit_sam
->lod_bias
= s
->lod_bias
;
1962 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1965 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
1966 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
1967 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
1969 if (draw
->samplers
[i
]) {
1970 const struct pipe_sampler_state
*s
1971 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
1972 jit_sam
->min_lod
= s
->min_lod
;
1973 jit_sam
->max_lod
= s
->max_lod
;
1974 jit_sam
->lod_bias
= s
->lod_bias
;
1975 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1983 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
1985 struct draw_llvm
*llvm
= variant
->llvm
;
1987 gallivm_destroy(variant
->gallivm
);
1989 remove_from_list(&variant
->list_item_local
);
1990 variant
->shader
->variants_cached
--;
1991 remove_from_list(&variant
->list_item_global
);
1992 llvm
->nr_variants
--;
1998 * Create LLVM types for various structures.
2001 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
2003 struct gallivm_state
*gallivm
= var
->gallivm
;
2004 LLVMTypeRef texture_type
, sampler_type
, context_type
;
2006 texture_type
= create_jit_texture_type(gallivm
, "texture");
2007 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2009 context_type
= create_gs_jit_context_type(gallivm
,
2010 var
->shader
->base
.vector_length
,
2011 texture_type
, sampler_type
,
2012 "draw_gs_jit_context");
2013 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2015 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
2019 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
2021 if (!variant
->context_ptr_type
)
2022 create_gs_jit_types(variant
);
2023 return variant
->context_ptr_type
;
2027 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2028 struct lp_type gs_type
)
2030 struct gallivm_state
*gallivm
= variant
->gallivm
;
2031 LLVMBuilderRef builder
= gallivm
->builder
;
2032 struct lp_type mask_type
= lp_int_type(gs_type
);
2033 LLVMValueRef num_prims
;
2034 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
2037 num_prims
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
),
2038 variant
->num_prims
);
2039 for (i
= 0; i
<= gs_type
.length
; i
++) {
2040 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
2041 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, idx
, idx
, "");
2043 mask_val
= lp_build_compare(gallivm
, mask_type
,
2044 PIPE_FUNC_GREATER
, num_prims
, mask_val
);
2050 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2051 struct draw_gs_llvm_variant
*variant
)
2053 struct gallivm_state
*gallivm
= variant
->gallivm
;
2054 LLVMContextRef context
= gallivm
->context
;
2055 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2056 LLVMTypeRef arg_types
[6];
2057 LLVMTypeRef func_type
;
2058 LLVMValueRef variant_func
;
2059 LLVMValueRef context_ptr
;
2060 LLVMValueRef prim_id_ptr
;
2061 LLVMBasicBlockRef block
;
2062 LLVMBuilderRef builder
;
2063 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2064 struct lp_build_sampler_soa
*sampler
= 0;
2065 struct lp_build_context bld
;
2066 struct lp_bld_tgsi_system_values system_values
;
2068 struct lp_type gs_type
;
2070 struct draw_gs_llvm_iface gs_iface
;
2071 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2072 LLVMValueRef consts_ptr
, num_consts_ptr
;
2073 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2074 struct lp_build_mask_context mask
;
2075 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2076 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2078 memset(&system_values
, 0, sizeof(system_values
));
2080 util_snprintf(func_name
, sizeof(func_name
), "draw_llvm_gs_variant%u",
2081 variant
->shader
->variants_cached
);
2083 assert(variant
->vertex_header_ptr_type
);
2085 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2086 arg_types
[1] = variant
->input_array_type
; /* input */
2087 arg_types
[2] = variant
->vertex_header_ptr_type
; /* vertex_header */
2088 arg_types
[3] = int32_type
; /* num_prims */
2089 arg_types
[4] = int32_type
; /* instance_id */
2090 arg_types
[5] = LLVMPointerType(
2091 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2093 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
2095 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
2097 variant
->function
= variant_func
;
2099 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2101 for (i
= 0; i
< Elements(arg_types
); ++i
)
2102 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2103 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
2104 LLVMNoAliasAttribute
);
2106 context_ptr
= LLVMGetParam(variant_func
, 0);
2107 input_array
= LLVMGetParam(variant_func
, 1);
2108 io_ptr
= LLVMGetParam(variant_func
, 2);
2109 num_prims
= LLVMGetParam(variant_func
, 3);
2110 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2111 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2113 lp_build_name(context_ptr
, "context");
2114 lp_build_name(input_array
, "input");
2115 lp_build_name(io_ptr
, "io");
2116 lp_build_name(num_prims
, "num_prims");
2117 lp_build_name(system_values
.instance_id
, "instance_id");
2118 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2120 variant
->context_ptr
= context_ptr
;
2121 variant
->io_ptr
= io_ptr
;
2122 variant
->num_prims
= num_prims
;
2124 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2125 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2126 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2127 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2128 gs_iface
.input
= input_array
;
2129 gs_iface
.variant
= variant
;
2135 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2136 builder
= gallivm
->builder
;
2137 LLVMPositionBuilderAtEnd(builder
, block
);
2139 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2141 memset(&gs_type
, 0, sizeof gs_type
);
2142 gs_type
.floating
= TRUE
; /* floating point values */
2143 gs_type
.sign
= TRUE
; /* values are signed */
2144 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2145 gs_type
.width
= 32; /* 32-bit float */
2146 gs_type
.length
= vector_length
;
2148 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2150 draw_gs_jit_context_num_constants(variant
->gallivm
, context_ptr
);
2152 /* code generated texture sampling */
2153 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
,
2156 mask_val
= generate_mask_value(variant
, gs_type
);
2157 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2159 if (gs_info
->uses_primid
) {
2160 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");
2163 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2164 tgsi_dump(tokens
, 0);
2165 draw_gs_llvm_dump_variant_key(&variant
->key
);
2168 lp_build_tgsi_soa(variant
->gallivm
,
2178 &llvm
->draw
->gs
.geometry_shader
->info
,
2179 (const struct lp_build_tgsi_gs_iface
*)&gs_iface
);
2181 sampler
->destroy(sampler
);
2183 lp_build_mask_end(&mask
);
2185 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2187 gallivm_verify_function(gallivm
, variant_func
);
2191 struct draw_gs_llvm_variant
*
2192 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2193 unsigned num_outputs
,
2194 const struct draw_gs_llvm_variant_key
*key
)
2196 struct draw_gs_llvm_variant
*variant
;
2197 struct llvm_geometry_shader
*shader
=
2198 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2199 LLVMTypeRef vertex_header
;
2200 char module_name
[64];
2202 variant
= MALLOC(sizeof *variant
+
2203 shader
->variant_key_size
-
2204 sizeof variant
->key
);
2205 if (variant
== NULL
)
2208 variant
->llvm
= llvm
;
2209 variant
->shader
= shader
;
2211 util_snprintf(module_name
, sizeof(module_name
), "draw_llvm_gs_variant%u",
2212 variant
->shader
->variants_cached
);
2214 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
2216 create_gs_jit_types(variant
);
2218 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2220 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2222 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2224 draw_gs_llvm_generate(llvm
, variant
);
2226 gallivm_compile_module(variant
->gallivm
);
2228 variant
->jit_func
= (draw_gs_jit_func
)
2229 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2231 gallivm_free_ir(variant
->gallivm
);
2233 variant
->list_item_global
.base
= variant
;
2234 variant
->list_item_local
.base
= variant
;
2235 /*variant->no = */shader
->variants_created
++;
2236 variant
->list_item_global
.base
= variant
;
2242 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2244 struct draw_llvm
*llvm
= variant
->llvm
;
2246 gallivm_destroy(variant
->gallivm
);
2248 remove_from_list(&variant
->list_item_local
);
2249 variant
->shader
->variants_cached
--;
2250 remove_from_list(&variant
->list_item_global
);
2251 llvm
->nr_gs_variants
--;
2255 struct draw_gs_llvm_variant_key
*
2256 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2259 struct draw_gs_llvm_variant_key
*key
;
2260 struct draw_sampler_static_state
*draw_sampler
;
2262 key
= (struct draw_gs_llvm_variant_key
*)store
;
2264 key
->num_outputs
= draw_total_gs_outputs(llvm
->draw
);
2266 /* All variants of this shader will have the same value for
2267 * nr_samplers. Not yet trying to compact away holes in the
2270 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2271 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2272 key
->nr_sampler_views
=
2273 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2276 key
->nr_sampler_views
= key
->nr_samplers
;
2279 draw_sampler
= key
->samplers
;
2281 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2283 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2284 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2285 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2287 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2288 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2289 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2296 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2299 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2301 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2302 debug_printf("sampler[%i].src_format = %s\n", i
,
2303 util_format_name(sampler
[i
].texture_state
.format
));