1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "draw_llvm.h"
30 #include "draw_context.h"
34 #include "gallivm/lp_bld_arit.h"
35 #include "gallivm/lp_bld_arit_overflow.h"
36 #include "gallivm/lp_bld_logic.h"
37 #include "gallivm/lp_bld_const.h"
38 #include "gallivm/lp_bld_swizzle.h"
39 #include "gallivm/lp_bld_struct.h"
40 #include "gallivm/lp_bld_type.h"
41 #include "gallivm/lp_bld_flow.h"
42 #include "gallivm/lp_bld_debug.h"
43 #include "gallivm/lp_bld_tgsi.h"
44 #include "gallivm/lp_bld_printf.h"
45 #include "gallivm/lp_bld_intr.h"
46 #include "gallivm/lp_bld_init.h"
47 #include "gallivm/lp_bld_type.h"
48 #include "gallivm/lp_bld_pack.h"
49 #include "gallivm/lp_bld_format.h"
51 #include "tgsi/tgsi_exec.h"
52 #include "tgsi/tgsi_dump.h"
54 #include "util/u_math.h"
55 #include "util/u_pointer.h"
56 #include "util/u_string.h"
57 #include "util/u_simple_list.h"
64 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
,
68 struct draw_gs_llvm_iface
{
69 struct lp_build_tgsi_gs_iface base
;
71 struct draw_gs_llvm_variant
*variant
;
75 static INLINE
const struct draw_gs_llvm_iface
*
76 draw_gs_llvm_iface(const struct lp_build_tgsi_gs_iface
*iface
)
78 return (const struct draw_gs_llvm_iface
*)iface
;
82 * Create LLVM type for draw_vertex_buffer.
85 create_jit_dvbuffer_type(struct gallivm_state
*gallivm
,
86 const char *struct_name
)
88 LLVMTargetDataRef target
= gallivm
->target
;
89 LLVMTypeRef dvbuffer_type
;
90 LLVMTypeRef elem_types
[DRAW_JIT_DVBUFFER_NUM_FIELDS
];
91 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
93 elem_types
[DRAW_JIT_DVBUFFER_MAP
] =
94 LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
95 elem_types
[DRAW_JIT_DVBUFFER_SIZE
] = int32_type
;
97 dvbuffer_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
98 Elements(elem_types
), 0);
100 #if HAVE_LLVM < 0x0300
101 LLVMAddTypeName(gallivm
->module
, struct_name
, dvbuffer_type
);
103 /* Make sure the target's struct layout cache doesn't return
104 * stale/invalid data.
106 LLVMInvalidateStructLayout(gallivm
->target
, dvbuffer_type
);
109 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, map
,
110 target
, dvbuffer_type
,
111 DRAW_JIT_DVBUFFER_MAP
);
112 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, size
,
113 target
, dvbuffer_type
,
114 DRAW_JIT_DVBUFFER_SIZE
);
116 return dvbuffer_type
;
120 * Create LLVM type for struct draw_jit_texture
123 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
125 LLVMTargetDataRef target
= gallivm
->target
;
126 LLVMTypeRef texture_type
;
127 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
128 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
130 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
131 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
132 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
133 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
134 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
135 elem_types
[DRAW_JIT_TEXTURE_BASE
] =
136 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
137 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
138 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
139 elem_types
[DRAW_JIT_TEXTURE_MIP_OFFSETS
] =
140 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
142 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
143 Elements(elem_types
), 0);
145 #if HAVE_LLVM < 0x0300
146 LLVMAddTypeName(gallivm
->module
, struct_name
, texture_type
);
148 /* Make sure the target's struct layout cache doesn't return
149 * stale/invalid data.
151 LLVMInvalidateStructLayout(gallivm
->target
, texture_type
);
154 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
155 target
, texture_type
,
156 DRAW_JIT_TEXTURE_WIDTH
);
157 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
158 target
, texture_type
,
159 DRAW_JIT_TEXTURE_HEIGHT
);
160 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
161 target
, texture_type
,
162 DRAW_JIT_TEXTURE_DEPTH
);
163 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
164 target
, texture_type
,
165 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
166 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
167 target
, texture_type
,
168 DRAW_JIT_TEXTURE_LAST_LEVEL
);
169 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, base
,
170 target
, texture_type
,
171 DRAW_JIT_TEXTURE_BASE
);
172 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
173 target
, texture_type
,
174 DRAW_JIT_TEXTURE_ROW_STRIDE
);
175 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
176 target
, texture_type
,
177 DRAW_JIT_TEXTURE_IMG_STRIDE
);
178 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, mip_offsets
,
179 target
, texture_type
,
180 DRAW_JIT_TEXTURE_MIP_OFFSETS
);
182 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
189 * Create LLVM type for struct draw_jit_sampler
192 create_jit_sampler_type(struct gallivm_state
*gallivm
, const char *struct_name
)
194 LLVMTargetDataRef target
= gallivm
->target
;
195 LLVMTypeRef sampler_type
;
196 LLVMTypeRef elem_types
[DRAW_JIT_SAMPLER_NUM_FIELDS
];
198 elem_types
[DRAW_JIT_SAMPLER_MIN_LOD
] =
199 elem_types
[DRAW_JIT_SAMPLER_MAX_LOD
] =
200 elem_types
[DRAW_JIT_SAMPLER_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
201 elem_types
[DRAW_JIT_SAMPLER_BORDER_COLOR
] =
202 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
204 sampler_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
205 Elements(elem_types
), 0);
207 #if HAVE_LLVM < 0x0300
208 LLVMAddTypeName(gallivm
->module
, struct_name
, sampler_type
);
210 /* Make sure the target's struct layout cache doesn't return
211 * stale/invalid data.
213 LLVMInvalidateStructLayout(gallivm
->target
, sampler_type
);
216 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, min_lod
,
217 target
, sampler_type
,
218 DRAW_JIT_SAMPLER_MIN_LOD
);
219 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, max_lod
,
220 target
, sampler_type
,
221 DRAW_JIT_SAMPLER_MAX_LOD
);
222 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, lod_bias
,
223 target
, sampler_type
,
224 DRAW_JIT_SAMPLER_LOD_BIAS
);
225 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, border_color
,
226 target
, sampler_type
,
227 DRAW_JIT_SAMPLER_BORDER_COLOR
);
229 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler
, target
, sampler_type
);
236 * Create LLVM type for struct draw_jit_context
239 create_jit_context_type(struct gallivm_state
*gallivm
,
240 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
241 const char *struct_name
)
243 LLVMTargetDataRef target
= gallivm
->target
;
244 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
245 LLVMTypeRef elem_types
[DRAW_JIT_CTX_NUM_FIELDS
];
246 LLVMTypeRef context_type
;
248 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
249 LP_MAX_TGSI_CONST_BUFFERS
);
250 elem_types
[1] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
251 DRAW_TOTAL_CLIP_PLANES
), 0);
252 elem_types
[2] = LLVMPointerType(float_type
, 0); /* viewport */
253 elem_types
[3] = LLVMArrayType(texture_type
,
254 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
255 elem_types
[4] = LLVMArrayType(sampler_type
,
256 PIPE_MAX_SAMPLERS
); /* samplers */
257 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
258 Elements(elem_types
), 0);
259 #if HAVE_LLVM < 0x0300
260 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
262 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
265 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
266 target
, context_type
, DRAW_JIT_CTX_CONSTANTS
);
267 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
268 target
, context_type
, DRAW_JIT_CTX_PLANES
);
269 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewport
,
270 target
, context_type
, DRAW_JIT_CTX_VIEWPORT
);
271 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
272 target
, context_type
,
273 DRAW_JIT_CTX_TEXTURES
);
274 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
275 target
, context_type
,
276 DRAW_JIT_CTX_SAMPLERS
);
277 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
278 target
, context_type
);
285 * Create LLVM type for struct draw_gs_jit_context
288 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
289 unsigned vector_length
,
290 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
291 const char *struct_name
)
293 LLVMTargetDataRef target
= gallivm
->target
;
294 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
295 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
296 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
297 LLVMTypeRef context_type
;
299 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
300 LP_MAX_TGSI_CONST_BUFFERS
);
301 elem_types
[1] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
302 DRAW_TOTAL_CLIP_PLANES
), 0);
303 elem_types
[2] = LLVMPointerType(float_type
, 0); /* viewport */
305 elem_types
[3] = LLVMArrayType(texture_type
,
306 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
307 elem_types
[4] = LLVMArrayType(sampler_type
,
308 PIPE_MAX_SAMPLERS
); /* samplers */
310 elem_types
[5] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
311 elem_types
[6] = LLVMPointerType(LLVMVectorType(int_type
,
313 elem_types
[7] = LLVMPointerType(LLVMVectorType(int_type
,
316 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
317 Elements(elem_types
), 0);
318 #if HAVE_LLVM < 0x0300
319 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
321 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
324 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
325 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
326 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
327 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
328 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewport
,
329 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
330 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
331 target
, context_type
,
332 DRAW_GS_JIT_CTX_TEXTURES
);
333 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
334 target
, context_type
,
335 DRAW_GS_JIT_CTX_SAMPLERS
);
336 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
337 target
, context_type
,
338 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
339 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
340 target
, context_type
,
341 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
342 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
343 target
, context_type
,
344 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
345 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
346 target
, context_type
);
353 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
355 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
356 LLVMTypeRef input_array
;
358 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
359 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
360 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
361 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
367 * Create LLVM type for struct pipe_vertex_buffer
370 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
,
371 const char *struct_name
)
373 LLVMTargetDataRef target
= gallivm
->target
;
374 LLVMTypeRef elem_types
[4];
378 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
380 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
382 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
383 Elements(elem_types
), 0);
384 #if HAVE_LLVM < 0x0300
385 LLVMAddTypeName(gallivm
->module
, struct_name
, vb_type
);
387 LLVMInvalidateStructLayout(gallivm
->target
, vb_type
);
390 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
392 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
395 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
402 * Create LLVM type for struct vertex_header;
405 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
407 LLVMTargetDataRef target
= gallivm
->target
;
408 LLVMTypeRef elem_types
[4];
409 LLVMTypeRef vertex_header
;
410 char struct_name
[24];
412 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
414 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
415 elem_types
[DRAW_JIT_VERTEX_CLIP
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
416 elem_types
[DRAW_JIT_VERTEX_PRE_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
417 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
419 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
420 Elements(elem_types
), 0);
421 #if HAVE_LLVM < 0x0300
422 LLVMAddTypeName(gallivm
->module
, struct_name
, vertex_header
);
424 LLVMInvalidateStructLayout(gallivm
->target
, vertex_header
);
427 /* these are bit-fields and we can't take address of them
428 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
429 target, vertex_header,
430 DRAW_JIT_VERTEX_CLIPMASK);
431 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
432 target, vertex_header,
433 DRAW_JIT_VERTEX_EDGEFLAG);
434 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
435 target, vertex_header,
436 DRAW_JIT_VERTEX_PAD);
437 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
438 target, vertex_header,
439 DRAW_JIT_VERTEX_VERTEX_ID);
441 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
442 target
, vertex_header
,
443 DRAW_JIT_VERTEX_CLIP
);
444 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, pre_clip_pos
,
445 target
, vertex_header
,
446 DRAW_JIT_VERTEX_PRE_CLIP_POS
);
447 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
448 target
, vertex_header
,
449 DRAW_JIT_VERTEX_DATA
);
451 assert(LLVMABISizeOfType(target
, vertex_header
) ==
452 offsetof(struct vertex_header
, data
[data_elems
]));
454 return vertex_header
;
459 * Create LLVM types for various structures.
462 create_jit_types(struct draw_llvm_variant
*variant
)
464 struct gallivm_state
*gallivm
= variant
->gallivm
;
465 LLVMTypeRef texture_type
, sampler_type
, context_type
, buffer_type
,
468 texture_type
= create_jit_texture_type(gallivm
, "texture");
469 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
471 context_type
= create_jit_context_type(gallivm
, texture_type
, sampler_type
,
473 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
475 buffer_type
= create_jit_dvbuffer_type(gallivm
, "draw_vertex_buffer");
476 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
478 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
479 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
484 get_context_ptr_type(struct draw_llvm_variant
*variant
)
486 if (!variant
->context_ptr_type
)
487 create_jit_types(variant
);
488 return variant
->context_ptr_type
;
493 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
495 if (!variant
->buffer_ptr_type
)
496 create_jit_types(variant
);
497 return variant
->buffer_ptr_type
;
502 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
504 if (!variant
->vb_ptr_type
)
505 create_jit_types(variant
);
506 return variant
->vb_ptr_type
;
510 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
512 if (!variant
->vertex_header_ptr_type
)
513 create_jit_types(variant
);
514 return variant
->vertex_header_ptr_type
;
519 * Create per-context LLVM info.
522 draw_llvm_create(struct draw_context
*draw
)
524 struct draw_llvm
*llvm
;
526 llvm
= CALLOC_STRUCT( draw_llvm
);
534 llvm
->nr_variants
= 0;
535 make_empty_list(&llvm
->vs_variants_list
);
537 llvm
->nr_gs_variants
= 0;
538 make_empty_list(&llvm
->gs_variants_list
);
545 * Free per-context LLVM info.
548 draw_llvm_destroy(struct draw_llvm
*llvm
)
550 /* XXX free other draw_llvm data? */
556 * Create LLVM-generated code for a vertex shader.
558 struct draw_llvm_variant
*
559 draw_llvm_create_variant(struct draw_llvm
*llvm
,
561 const struct draw_llvm_variant_key
*key
)
563 struct draw_llvm_variant
*variant
;
564 struct llvm_vertex_shader
*shader
=
565 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
566 LLVMTypeRef vertex_header
;
568 variant
= MALLOC(sizeof *variant
+
569 shader
->variant_key_size
-
570 sizeof variant
->key
);
574 variant
->llvm
= llvm
;
576 variant
->gallivm
= gallivm_create();
578 create_jit_types(variant
);
580 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
582 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
584 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
586 draw_llvm_generate(llvm
, variant
, FALSE
); /* linear */
587 draw_llvm_generate(llvm
, variant
, TRUE
); /* elts */
589 gallivm_compile_module(variant
->gallivm
);
591 variant
->jit_func
= (draw_jit_vert_func
)
592 gallivm_jit_function(variant
->gallivm
, variant
->function
);
594 variant
->jit_func_elts
= (draw_jit_vert_func_elts
)
595 gallivm_jit_function(variant
->gallivm
, variant
->function_elts
);
597 variant
->shader
= shader
;
598 variant
->list_item_global
.base
= variant
;
599 variant
->list_item_local
.base
= variant
;
600 /*variant->no = */shader
->variants_created
++;
601 variant
->list_item_global
.base
= variant
;
608 generate_vs(struct draw_llvm_variant
*variant
,
609 LLVMBuilderRef builder
,
610 struct lp_type vs_type
,
611 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
612 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
613 const struct lp_bld_tgsi_system_values
*system_values
,
614 LLVMValueRef context_ptr
,
615 struct lp_build_sampler_soa
*draw_sampler
,
616 boolean clamp_vertex_color
)
618 struct draw_llvm
*llvm
= variant
->llvm
;
619 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
620 LLVMValueRef consts_ptr
= draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
621 struct lp_build_sampler_soa
*sampler
= 0;
623 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
624 tgsi_dump(tokens
, 0);
625 draw_llvm_dump_variant_key(&variant
->key
);
628 if (llvm
->draw
->num_sampler_views
&& llvm
->draw
->num_samplers
)
629 sampler
= draw_sampler
;
631 lp_build_tgsi_soa(variant
->gallivm
,
634 NULL
/*struct lp_build_mask_context *mask*/,
640 &llvm
->draw
->vs
.vertex_shader
->info
,
645 unsigned chan
, attrib
;
646 struct lp_build_context bld
;
647 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
648 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
650 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
651 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
652 if (outputs
[attrib
][chan
]) {
653 switch (info
->output_semantic_name
[attrib
]) {
654 case TGSI_SEMANTIC_COLOR
:
655 case TGSI_SEMANTIC_BCOLOR
:
656 if (clamp_vertex_color
) {
657 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
658 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
659 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
662 case TGSI_SEMANTIC_FOG
:
663 if (chan
== 1 || chan
== 2)
664 LLVMBuildStore(builder
, bld
.zero
, outputs
[attrib
][chan
]);
666 LLVMBuildStore(builder
, bld
.one
, outputs
[attrib
][chan
]);
676 generate_fetch(struct gallivm_state
*gallivm
,
677 struct draw_context
*draw
,
678 LLVMValueRef vbuffers_ptr
,
680 struct pipe_vertex_element
*velem
,
683 LLVMValueRef instance_id
)
685 const struct util_format_description
*format_desc
=
686 util_format_description(velem
->src_format
);
687 LLVMValueRef zero
= LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
));
688 LLVMBuilderRef builder
= gallivm
->builder
;
689 LLVMValueRef indices
=
690 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
691 velem
->vertex_buffer_index
, 0);
692 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
694 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
695 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
696 LLVMValueRef map_ptr
= draw_jit_dvbuffer_map(gallivm
, vbuffer_ptr
);
697 LLVMValueRef buffer_size
= draw_jit_dvbuffer_size(gallivm
, vbuffer_ptr
);
699 LLVMValueRef buffer_overflowed
;
700 LLVMValueRef needed_buffer_size
;
701 LLVMValueRef temp_ptr
=
702 lp_build_alloca(gallivm
,
703 lp_build_vec_type(gallivm
, lp_float32_vec4_type()), "");
704 LLVMValueRef ofbit
= NULL
;
705 struct lp_build_if_state if_ctx
;
707 if (velem
->instance_divisor
) {
708 /* Index is equal to the start instance plus the number of current
709 * instance divided by the divisor. In this case we compute it as:
710 * index = start_instance + (instance_id / divisor)
712 LLVMValueRef current_instance
;
713 index
= lp_build_const_int32(gallivm
, draw
->start_instance
);
714 current_instance
= LLVMBuildUDiv(builder
, instance_id
,
715 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
717 index
= lp_build_uadd_overflow(gallivm
, index
, current_instance
, &ofbit
);
720 stride
= lp_build_umul_overflow(gallivm
, vb_stride
, index
, &ofbit
);
721 stride
= lp_build_uadd_overflow(gallivm
, stride
, vb_buffer_offset
, &ofbit
);
722 stride
= lp_build_uadd_overflow(
724 lp_build_const_int32(gallivm
, velem
->src_offset
), &ofbit
);
725 needed_buffer_size
= lp_build_uadd_overflow(
727 lp_build_const_int32(gallivm
,
728 util_format_get_blocksize(velem
->src_format
)),
731 buffer_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
732 needed_buffer_size
, buffer_size
,
733 "buffer_overflowed");
734 buffer_overflowed
= LLVMBuildOr(builder
, buffer_overflowed
, ofbit
, "");
736 lp_build_printf(gallivm
, "vbuf index = %u, vb_stride is %u\n",
738 lp_build_printf(gallivm
, " vb_buffer_offset = %u, src_offset is %u\n",
740 lp_build_const_int32(gallivm
, velem
->src_offset
));
741 lp_build_print_value(gallivm
, " blocksize = ",
742 lp_build_const_int32(
744 util_format_get_blocksize(velem
->src_format
)));
745 lp_build_printf(gallivm
, " instance_id = %u\n", instance_id
);
746 lp_build_printf(gallivm
, " stride = %u\n", stride
);
747 lp_build_printf(gallivm
, " buffer size = %u\n", buffer_size
);
748 lp_build_printf(gallivm
, " needed_buffer_size = %u\n", needed_buffer_size
);
749 lp_build_print_value(gallivm
, " buffer overflowed = ", buffer_overflowed
);
752 lp_build_if(&if_ctx
, gallivm
, buffer_overflowed
);
755 lp_build_const_vec(gallivm
, lp_float32_vec4_type(), 0);
756 LLVMBuildStore(builder
, val
, temp_ptr
);
758 lp_build_else(&if_ctx
);
761 map_ptr
= LLVMBuildGEP(builder
, map_ptr
, &stride
, 1, "");
763 val
= lp_build_fetch_rgba_aos(gallivm
,
765 lp_float32_vec4_type(),
768 LLVMBuildStore(builder
, val
, temp_ptr
);
770 lp_build_endif(&if_ctx
);
772 *res
= LLVMBuildLoad(builder
, temp_ptr
, "aos");
776 convert_to_soa(struct gallivm_state
*gallivm
,
777 LLVMValueRef (*src_aos
)[LP_MAX_VECTOR_WIDTH
/ 32],
778 LLVMValueRef (*dst_soa
)[TGSI_NUM_CHANNELS
],
779 unsigned num_attribs
, const struct lp_type soa_type
)
782 struct lp_type aos_channel_type
= soa_type
;
784 debug_assert(TGSI_NUM_CHANNELS
== 4);
785 debug_assert((soa_type
.length
% TGSI_NUM_CHANNELS
) == 0);
787 aos_channel_type
.length
>>= 1;
789 for (i
= 0; i
< num_attribs
; ++i
) {
790 LLVMValueRef aos_channels
[TGSI_NUM_CHANNELS
];
791 unsigned pixels_per_channel
= soa_type
.length
/ TGSI_NUM_CHANNELS
;
793 for (j
= 0; j
< TGSI_NUM_CHANNELS
; ++j
) {
794 LLVMValueRef channel
[LP_MAX_VECTOR_LENGTH
] = { 0 };
796 assert(pixels_per_channel
<= LP_MAX_VECTOR_LENGTH
);
798 for (k
= 0; k
< pixels_per_channel
; ++k
) {
799 channel
[k
] = src_aos
[i
][j
+ TGSI_NUM_CHANNELS
* k
];
802 aos_channels
[j
] = lp_build_concat(gallivm
, channel
, aos_channel_type
, pixels_per_channel
);
805 lp_build_transpose_aos(gallivm
, soa_type
, aos_channels
, dst_soa
[i
]);
811 store_aos(struct gallivm_state
*gallivm
,
816 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
817 LLVMBuilderRef builder
= gallivm
->builder
;
818 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
819 LLVMValueRef indices
[3];
821 indices
[0] = lp_build_const_int32(gallivm
, 0);
823 indices
[2] = lp_build_const_int32(gallivm
, 0);
825 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
826 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
829 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
832 /* Unaligned store due to the vertex header */
833 lp_set_store_alignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
837 * Adjust the mask to architecture endianess. The mask will the store in struct:
839 * struct vertex_header {
840 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
841 * unsigned edgeflag:1;
842 * unsigned have_clipdist:1;
843 * unsigned vertex_id:16;
847 * On little-endian machine nothing needs to done, however on bit-endian machine
848 * the mask's fields need to be adjusted with the algorithm:
850 * uint32_t reverse (uint32_t x)
852 * return (x >> 16) | // vertex_id
853 * ((x & 0x3fff) << 18) | // clipmask
854 * ((x & 0x4000) << 3) | // have_clipdist
855 * ((x & 0x8000) << 1); // edgeflag
859 adjust_mask(struct gallivm_state
*gallivm
,
862 #ifdef PIPE_ARCH_BIG_ENDIAN
863 LLVMBuilderRef builder
= gallivm
->builder
;
864 LLVMValueRef vertex_id
;
865 LLVMValueRef clipmask
;
866 LLVMValueRef have_clipdist
;
867 LLVMValueRef edgeflag
;
869 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
870 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
871 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
872 have_clipdist
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
873 have_clipdist
= LLVMBuildShl(builder
, have_clipdist
, lp_build_const_int32(gallivm
, 3), "");
874 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
875 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
877 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
878 mask
= LLVMBuildOr(builder
, mask
, have_clipdist
, "");
879 mask
= LLVMBuildOr(builder
, mask
, edgeflag
, "");
885 store_aos_array(struct gallivm_state
*gallivm
,
886 struct lp_type soa_type
,
888 LLVMValueRef
*indices
,
892 LLVMValueRef clipmask
,
893 boolean have_clipdist
)
895 LLVMBuilderRef builder
= gallivm
->builder
;
896 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
897 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
898 LLVMValueRef linear_inds
[LP_MAX_VECTOR_WIDTH
/ 32];
899 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
900 int vector_length
= soa_type
.length
;
903 debug_assert(TGSI_NUM_CHANNELS
== 4);
905 for (i
= 0; i
< vector_length
; i
++) {
906 linear_inds
[i
] = lp_build_const_int32(gallivm
, i
);
908 inds
[i
] = indices
[i
];
910 inds
[i
] = linear_inds
[i
];
912 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
916 /* store vertex header for each of the n vertices */
917 LLVMValueRef val
, cliptmp
;
918 int vertex_id_pad_edgeflag
;
920 /* If this assertion fails, it means we need to update the bit twidding
921 * code here. See struct vertex_header in draw_private.h.
923 assert(DRAW_TOTAL_CLIP_PLANES
==14);
924 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
925 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
927 vertex_id_pad_edgeflag
|= 1 << (DRAW_TOTAL_CLIP_PLANES
+1);
928 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
), vertex_id_pad_edgeflag
);
929 /* OR with the clipmask */
930 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
931 for (i
= 0; i
< vector_length
; i
++) {
932 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
933 val
= LLVMBuildExtractElement(builder
, cliptmp
, linear_inds
[i
], "");
934 val
= adjust_mask(gallivm
, val
);
935 LLVMBuildStore(builder
, val
, id_ptr
);
937 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
938 io_ptrs
[i
], inds
[i
], val
);
943 /* store for each of the n vertices */
944 for (i
= 0; i
< vector_length
; i
++) {
945 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
951 convert_to_aos(struct gallivm_state
*gallivm
,
953 LLVMValueRef
*indices
,
954 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
955 LLVMValueRef clipmask
,
957 struct lp_type soa_type
,
958 boolean have_clipdist
)
960 LLVMBuilderRef builder
= gallivm
->builder
;
961 unsigned chan
, attrib
, i
;
964 lp_build_printf(gallivm
, " # storing begin\n");
966 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
967 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
968 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
969 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
970 if (outputs
[attrib
][chan
]) {
971 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
972 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
974 lp_build_printf(gallivm
, "output %d : %d ",
975 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
977 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
979 lp_build_print_value(gallivm
, "val = ", out
);
989 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
990 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
992 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
994 for (i
= 0; i
< soa_type
.length
; ++i
) {
995 aos
[i
] = lp_build_extract_range(gallivm
,
996 soa
[i
% TGSI_NUM_CHANNELS
],
997 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1002 store_aos_array(gallivm
,
1008 clipmask
, have_clipdist
);
1011 lp_build_printf(gallivm
, " # storing end\n");
1017 * Stores original vertex positions in clip coordinates
1020 store_clip(struct gallivm_state
*gallivm
,
1021 const struct lp_type vs_type
,
1022 LLVMValueRef io_ptr
,
1023 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1024 boolean pre_clip_pos
, int idx
)
1026 LLVMBuilderRef builder
= gallivm
->builder
;
1027 LLVMValueRef soa
[4];
1028 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
1029 LLVMValueRef indices
[2];
1030 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1031 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1032 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1036 indices
[1] = lp_build_const_int32(gallivm
, 0);
1038 for (i
= 0; i
< vs_type
.length
; i
++) {
1039 inds
[i
] = lp_build_const_int32(gallivm
, i
);
1040 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1043 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
1044 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
1045 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
1046 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
1048 if (!pre_clip_pos
) {
1049 for (i
= 0; i
< vs_type
.length
; i
++) {
1050 clip_ptrs
[i
] = draw_jit_header_clip(gallivm
, io_ptrs
[i
]);
1053 for (i
= 0; i
< vs_type
.length
; i
++) {
1054 clip_ptrs
[i
] = draw_jit_header_pre_clip_pos(gallivm
, io_ptrs
[i
]);
1058 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
1059 for (i
= 0; i
< vs_type
.length
; ++i
) {
1060 aos
[i
] = lp_build_extract_range(gallivm
,
1061 soa
[i
% TGSI_NUM_CHANNELS
],
1062 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1066 for (j
= 0; j
< vs_type
.length
; j
++) {
1067 LLVMTypeRef clip_ptr_type
= LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4), 0);
1068 LLVMValueRef clip_ptr
;
1070 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
1071 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
1073 /* Unaligned store */
1074 lp_set_store_alignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
1080 * Transforms the outputs for viewport mapping
1083 generate_viewport(struct draw_llvm_variant
*variant
,
1084 LLVMBuilderRef builder
,
1085 struct lp_type vs_type
,
1086 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1087 LLVMValueRef context_ptr
)
1090 struct gallivm_state
*gallivm
= variant
->gallivm
;
1091 struct lp_type f32_type
= vs_type
;
1092 const unsigned pos
= draw_current_shader_position_output(variant
->llvm
->draw
);
1093 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1094 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn*/
1095 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1096 LLVMValueRef vp_ptr
= draw_jit_context_viewport(gallivm
, context_ptr
);
1098 /* for 1/w convention*/
1099 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1100 LLVMBuildStore(builder
, out3
, outputs
[pos
][3]);
1102 /* Viewport Mapping */
1103 for (i
=0; i
<3; i
++) {
1104 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[pos
][i
], ""); /*x0 x1 .. xn*/
1107 LLVMValueRef scale_i
;
1108 LLVMValueRef trans_i
;
1111 index
= lp_build_const_int32(gallivm
, i
);
1112 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1114 index
= lp_build_const_int32(gallivm
, i
+4);
1115 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1117 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1118 LLVMBuildLoad(builder
, scale_i
, "scale"));
1119 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1120 LLVMBuildLoad(builder
, trans_i
, "trans"));
1123 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1125 out
= LLVMBuildFMul(builder
, out
, scale
, "");
1126 /* add translation */
1127 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
1129 /* store transformed outputs */
1130 LLVMBuildStore(builder
, out
, outputs
[pos
][i
]);
1137 * Returns clipmask as nxi32 bitmask for the n vertices
1140 generate_clipmask(struct draw_llvm
*llvm
,
1141 struct gallivm_state
*gallivm
,
1142 struct lp_type vs_type
,
1143 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1148 unsigned ucp_enable
,
1149 LLVMValueRef context_ptr
,
1150 boolean
*have_clipdist
)
1152 LLVMBuilderRef builder
= gallivm
->builder
;
1153 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1154 LLVMValueRef test
, temp
;
1155 LLVMValueRef zero
, shift
;
1156 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1157 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1158 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1159 struct lp_type f32_type
= vs_type
;
1160 struct lp_type i32_type
= lp_int_type(vs_type
);
1161 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1162 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1163 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1164 bool have_cd
= false;
1167 cd
[0] = draw_current_shader_clipdistance_output(llvm
->draw
, 0);
1168 cd
[1] = draw_current_shader_clipdistance_output(llvm
->draw
, 1);
1170 if (cd
[0] != pos
|| cd
[1] != pos
)
1173 if (num_written_clipdistance
&& !clip_user
) {
1175 ucp_enable
= (1 << num_written_clipdistance
) - 1;
1178 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1179 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1180 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1181 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1184 * load clipvertex and position from correct locations.
1185 * if they are the same just load them once.
1187 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1188 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1189 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1190 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1192 if (clip_user
&& cv
!= pos
) {
1193 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1194 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1195 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1196 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1204 /* Cliptest, for hardwired planes */
1207 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1209 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1213 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1214 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1215 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1216 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1217 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1220 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1221 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1222 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1223 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1226 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1227 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1228 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1229 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1230 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1234 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1237 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1238 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1239 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1243 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1244 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1245 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1246 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1249 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1250 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1251 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1252 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1256 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1257 LLVMValueRef indices
[3];
1259 /* userclip planes */
1260 while (ucp_enable
) {
1261 unsigned plane_idx
= ffs(ucp_enable
)-1;
1262 ucp_enable
&= ~(1 << plane_idx
);
1265 if (have_cd
&& num_written_clipdistance
) {
1266 LLVMValueRef clipdist
;
1270 *have_clipdist
= TRUE
;
1272 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1274 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1276 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1277 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1278 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1279 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1281 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1282 indices
[0] = lp_build_const_int32(gallivm
, 0);
1283 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1285 indices
[2] = lp_build_const_int32(gallivm
, 0);
1286 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1287 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1288 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1289 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1291 indices
[2] = lp_build_const_int32(gallivm
, 1);
1292 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1293 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1294 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1295 test
= LLVMBuildFMul(builder
, planes
, cv_y
, "");
1296 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1298 indices
[2] = lp_build_const_int32(gallivm
, 2);
1299 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1300 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1301 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1302 test
= LLVMBuildFMul(builder
, planes
, cv_z
, "");
1303 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1305 indices
[2] = lp_build_const_int32(gallivm
, 3);
1306 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1307 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1308 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1309 test
= LLVMBuildFMul(builder
, planes
, cv_w
, "");
1310 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1312 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1313 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1314 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1315 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1324 * Returns boolean if any clipping has occurred
1325 * Used zero/non-zero i32 value to represent boolean
1328 clipmask_booli32(struct gallivm_state
*gallivm
,
1329 const struct lp_type vs_type
,
1330 LLVMValueRef clipmask_bool_ptr
)
1332 LLVMBuilderRef builder
= gallivm
->builder
;
1333 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
1334 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1335 LLVMValueRef ret
= LLVMConstNull(int32_type
);
1340 * Can do this with log2(vector length) pack instructions and one extract
1341 * (as we don't actually need a or) with sse2 which would be way better.
1343 for (i
=0; i
< vs_type
.length
; i
++) {
1344 temp
= LLVMBuildExtractElement(builder
, clipmask_bool
,
1345 lp_build_const_int32(gallivm
, i
) , "");
1346 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1352 draw_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
1353 struct lp_build_tgsi_context
* bld_base
,
1354 boolean is_indirect
,
1355 LLVMValueRef vertex_index
,
1356 LLVMValueRef attrib_index
,
1357 LLVMValueRef swizzle_index
)
1359 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1360 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1361 LLVMBuilderRef builder
= gallivm
->builder
;
1362 LLVMValueRef indices
[3];
1364 struct lp_type type
= bld_base
->base
.type
;
1368 res
= bld_base
->base
.zero
;
1369 for (i
= 0; i
< type
.length
; ++i
) {
1370 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1371 LLVMValueRef vert_chan_index
= LLVMBuildExtractElement(builder
,
1372 vertex_index
, idx
, "");
1373 LLVMValueRef channel_vec
, value
;
1374 indices
[0] = vert_chan_index
;
1375 indices
[1] = attrib_index
;
1376 indices
[2] = swizzle_index
;
1378 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1379 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1380 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1382 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1385 indices
[0] = vertex_index
;
1386 indices
[1] = attrib_index
;
1387 indices
[2] = swizzle_index
;
1389 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1390 res
= LLVMBuildLoad(builder
, res
, "");
1397 draw_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
1398 struct lp_build_tgsi_context
* bld_base
,
1399 LLVMValueRef (*outputs
)[4],
1400 LLVMValueRef emitted_vertices_vec
)
1402 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1403 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1404 struct gallivm_state
*gallivm
= variant
->gallivm
;
1405 LLVMBuilderRef builder
= gallivm
->builder
;
1406 struct lp_type gs_type
= bld_base
->base
.type
;
1407 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1408 lp_int_type(gs_type
), 0);
1409 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1410 LLVMValueRef next_prim_offset
=
1411 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1412 LLVMValueRef io
= variant
->io_ptr
;
1414 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1416 for (i
= 0; i
< gs_type
.length
; ++i
) {
1417 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1418 LLVMValueRef currently_emitted
=
1419 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1420 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1421 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1424 convert_to_aos(gallivm
, io
, indices
,
1426 gs_info
->num_outputs
, gs_type
,
1431 draw_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
1432 struct lp_build_tgsi_context
* bld_base
,
1433 LLVMValueRef verts_per_prim_vec
,
1434 LLVMValueRef emitted_prims_vec
)
1436 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1437 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1438 struct gallivm_state
*gallivm
= variant
->gallivm
;
1439 LLVMBuilderRef builder
= gallivm
->builder
;
1440 LLVMValueRef prim_lengts_ptr
=
1441 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1444 for (i
= 0; i
< bld_base
->base
.type
.length
; ++i
) {
1445 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1446 LLVMValueRef prims_emitted
=
1447 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1448 LLVMValueRef store_ptr
;
1449 LLVMValueRef num_vertices
=
1450 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1452 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1453 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1454 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1455 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1460 draw_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
1461 struct lp_build_tgsi_context
* bld_base
,
1462 LLVMValueRef total_emitted_vertices_vec
,
1463 LLVMValueRef emitted_prims_vec
)
1465 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1466 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1467 struct gallivm_state
*gallivm
= variant
->gallivm
;
1468 LLVMBuilderRef builder
= gallivm
->builder
;
1469 LLVMValueRef emitted_verts_ptr
=
1470 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1471 LLVMValueRef emitted_prims_ptr
=
1472 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1473 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1475 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &zero
, 0, "");
1476 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &zero
, 0, "");
1478 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1479 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1483 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1486 struct gallivm_state
*gallivm
= variant
->gallivm
;
1487 LLVMContextRef context
= gallivm
->context
;
1488 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1489 LLVMTypeRef arg_types
[9];
1490 unsigned num_arg_types
=
1491 elts
? Elements(arg_types
) : Elements(arg_types
) - 1;
1492 LLVMTypeRef func_type
;
1493 LLVMValueRef context_ptr
;
1494 LLVMBasicBlockRef block
;
1495 LLVMBuilderRef builder
;
1496 struct lp_type vs_type
;
1497 LLVMValueRef end
, start
;
1498 LLVMValueRef count
, fetch_elts
, fetch_elt_max
, fetch_count
;
1499 LLVMValueRef stride
, step
, io_itr
;
1500 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1501 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1502 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1503 struct draw_context
*draw
= llvm
->draw
;
1504 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1506 struct lp_build_context bld
;
1507 struct lp_build_loop_state lp_loop
;
1508 const int vector_length
= lp_native_vector_width
/ 32;
1509 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1510 LLVMValueRef fetch_max
;
1511 struct lp_build_sampler_soa
*sampler
= 0;
1512 LLVMValueRef ret
, clipmask_bool_ptr
;
1513 const struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
1514 struct draw_llvm_variant_key
*key
= &variant
->key
;
1515 /* If geometry shader is present we need to skip both the viewport
1516 * transformation and clipping otherwise the inputs to the geometry
1517 * shader will be incorrect.
1519 const boolean bypass_viewport
= gs
|| key
->bypass_viewport
;
1520 const boolean enable_cliptest
= !gs
&& (key
->clip_xy
||
1523 LLVMValueRef variant_func
;
1524 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1525 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1526 boolean have_clipdist
= FALSE
;
1527 struct lp_bld_tgsi_system_values system_values
;
1529 memset(&system_values
, 0, sizeof(system_values
));
1532 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1533 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1534 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1536 arg_types
[i
++] = LLVMPointerType(int32_type
, 0);/* fetch_elts */
1537 arg_types
[i
++] = int32_type
; /* fetch_elt_max */
1539 arg_types
[i
++] = int32_type
; /* start */
1540 arg_types
[i
++] = int32_type
; /* fetch_count / count */
1541 arg_types
[i
++] = int32_type
; /* stride */
1542 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1543 arg_types
[i
++] = int32_type
; /* instance_id */
1545 func_type
= LLVMFunctionType(int32_type
, arg_types
, num_arg_types
, 0);
1547 variant_func
= LLVMAddFunction(gallivm
->module
,
1548 elts
? "draw_llvm_shader_elts" : "draw_llvm_shader",
1552 variant
->function_elts
= variant_func
;
1554 variant
->function
= variant_func
;
1556 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1557 for (i
= 0; i
< num_arg_types
; ++i
)
1558 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1559 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1560 LLVMNoAliasAttribute
);
1562 context_ptr
= LLVMGetParam(variant_func
, 0);
1563 io_ptr
= LLVMGetParam(variant_func
, 1);
1564 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1565 stride
= LLVMGetParam(variant_func
, 5 + (elts
? 1 : 0));
1566 vb_ptr
= LLVMGetParam(variant_func
, 6 + (elts
? 1 : 0));
1567 system_values
.instance_id
= LLVMGetParam(variant_func
, 7 + (elts
? 1 : 0));
1569 lp_build_name(context_ptr
, "context");
1570 lp_build_name(io_ptr
, "io");
1571 lp_build_name(vbuffers_ptr
, "vbuffers");
1572 lp_build_name(stride
, "stride");
1573 lp_build_name(vb_ptr
, "vb");
1574 lp_build_name(system_values
.instance_id
, "instance_id");
1577 fetch_elts
= LLVMGetParam(variant_func
, 3);
1578 fetch_elt_max
= LLVMGetParam(variant_func
, 4);
1579 fetch_count
= LLVMGetParam(variant_func
, 5);
1580 lp_build_name(fetch_elts
, "fetch_elts");
1581 lp_build_name(fetch_elt_max
, "fetch_elt_max");
1582 lp_build_name(fetch_count
, "fetch_count");
1583 start
= count
= NULL
;
1586 start
= LLVMGetParam(variant_func
, 3);
1587 count
= LLVMGetParam(variant_func
, 4);
1588 lp_build_name(start
, "start");
1589 lp_build_name(count
, "count");
1590 fetch_elts
= fetch_count
= NULL
;
1597 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1598 builder
= gallivm
->builder
;
1599 LLVMPositionBuilderAtEnd(builder
, block
);
1601 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1603 memset(&vs_type
, 0, sizeof vs_type
);
1604 vs_type
.floating
= TRUE
; /* floating point values */
1605 vs_type
.sign
= TRUE
; /* values are signed */
1606 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1607 vs_type
.width
= 32; /* 32-bit float */
1608 vs_type
.length
= vector_length
;
1610 /* hold temporary "bool" clipmask */
1611 clipmask_bool_ptr
= lp_build_alloca(gallivm
, lp_build_int_vec_type(gallivm
, vs_type
), "");
1612 LLVMBuildStore(builder
, lp_build_zero(gallivm
, lp_int_type(vs_type
)), clipmask_bool_ptr
);
1614 /* code generated texture sampling */
1615 sampler
= draw_llvm_sampler_soa_create(
1616 draw_llvm_variant_key_samplers(key
),
1622 count
= fetch_count
;
1625 end
= lp_build_add(&bld
, start
, count
);
1628 step
= lp_build_const_int32(gallivm
, vector_length
);
1630 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1632 lp_build_loop_begin(&lp_loop
, gallivm
, zero
);
1634 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1635 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][LP_MAX_VECTOR_WIDTH
/ 32] = { { 0 } };
1637 LLVMValueRef clipmask
; /* holds the clipmask value */
1638 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1640 io_itr
= lp_loop
.counter
;
1642 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1644 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1645 io_itr
, io
, lp_loop
.counter
);
1647 system_values
.vertex_id
= lp_build_zero(gallivm
, lp_type_uint_vec(32, 32*vector_length
));
1648 for (i
= 0; i
< vector_length
; ++i
) {
1649 LLVMValueRef true_index
=
1650 LLVMBuildAdd(builder
,
1652 lp_build_const_int32(gallivm
, i
), "");
1653 true_index
= LLVMBuildAdd(builder
, start
, true_index
, "");
1655 /* make sure we're not out of bounds which can happen
1656 * if fetch_count % 4 != 0, because on the last iteration
1657 * a few of the 4 vertex fetches will be out of bounds */
1658 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1660 system_values
.vertex_id
= LLVMBuildInsertElement(
1662 system_values
.vertex_id
, true_index
,
1663 lp_build_const_int32(gallivm
, i
), "");
1666 LLVMValueRef fetch_ptr
;
1667 LLVMValueRef index_overflowed
;
1668 LLVMValueRef index_ptr
=
1671 lp_build_vec_type(gallivm
, lp_type_int(32)), "");
1672 struct lp_build_if_state if_ctx
;
1673 index_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
1674 true_index
, fetch_elt_max
,
1675 "index_overflowed");
1677 lp_build_if(&if_ctx
, gallivm
, index_overflowed
);
1679 /* Generate maximum possible index so that
1680 * generate_fetch can treat it just like
1681 * any other overflow and return zeros.
1682 * We don't have to worry about the restart
1683 * primitive index because it has already been
1687 lp_build_const_int32(gallivm
, 0xffffffff);
1688 LLVMBuildStore(builder
, val
, index_ptr
);
1690 lp_build_else(&if_ctx
);
1693 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1694 &true_index
, 1, "");
1695 val
= LLVMBuildLoad(builder
, fetch_ptr
, "");
1696 LLVMBuildStore(builder
, val
, index_ptr
);
1698 lp_build_endif(&if_ctx
);
1699 true_index
= LLVMBuildLoad(builder
, index_ptr
, "true_index");
1702 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1703 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1704 LLVMValueRef vb_index
=
1705 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1706 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1707 generate_fetch(gallivm
, draw
, vbuffers_ptr
,
1708 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1709 system_values
.instance_id
);
1712 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1713 draw
->pt
.nr_vertex_elements
, vs_type
);
1715 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1716 generate_vs(variant
,
1724 key
->clamp_vertex_color
);
1726 if (pos
!= -1 && cv
!= -1) {
1727 /* store original positions in clip before further manipulation */
1728 store_clip(gallivm
, vs_type
, io
, outputs
, 0, cv
);
1729 store_clip(gallivm
, vs_type
, io
, outputs
, 1, pos
);
1732 if (enable_cliptest
) {
1733 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1734 /* allocate clipmask, assign it integer type */
1735 clipmask
= generate_clipmask(llvm
,
1744 context_ptr
, &have_clipdist
);
1745 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1746 /* store temporary clipping boolean value */
1747 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1750 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1753 /* do viewport mapping */
1754 if (!bypass_viewport
) {
1755 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1759 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1762 /* store clipmask in vertex header,
1763 * original positions in clip
1764 * and transformed positions in data
1766 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
1767 vs_info
->num_outputs
, vs_type
,
1770 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
1772 sampler
->destroy(sampler
);
1774 /* return clipping boolean value for function */
1775 ret
= clipmask_booli32(gallivm
, vs_type
, clipmask_bool_ptr
);
1777 LLVMBuildRet(builder
, ret
);
1779 gallivm_verify_function(gallivm
, variant_func
);
1783 struct draw_llvm_variant_key
*
1784 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1787 struct draw_llvm_variant_key
*key
;
1788 struct draw_sampler_static_state
*draw_sampler
;
1790 key
= (struct draw_llvm_variant_key
*)store
;
1792 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1794 /* Presumably all variants of the shader should have the same
1795 * number of vertex elements - ie the number of shader inputs.
1796 * NOTE: we NEED to store the needed number of needed inputs
1797 * here, not the number of provided elements to match keysize
1798 * (and the offset of sampler state in the key).
1800 key
->nr_vertex_elements
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
1801 assert(key
->nr_vertex_elements
<= llvm
->draw
->pt
.nr_vertex_elements
);
1803 /* will have to rig this up properly later */
1804 key
->clip_xy
= llvm
->draw
->clip_xy
;
1805 key
->clip_z
= llvm
->draw
->clip_z
;
1806 key
->clip_user
= llvm
->draw
->clip_user
;
1807 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1808 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
1809 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1810 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1811 key
->has_gs
= llvm
->draw
->gs
.geometry_shader
!= NULL
;
1814 /* All variants of this shader will have the same value for
1815 * nr_samplers. Not yet trying to compact away holes in the
1818 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1819 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
1820 key
->nr_sampler_views
=
1821 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
1824 key
->nr_sampler_views
= key
->nr_samplers
;
1827 draw_sampler
= draw_llvm_variant_key_samplers(key
);
1829 memcpy(key
->vertex_element
,
1830 llvm
->draw
->pt
.vertex_element
,
1831 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1833 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
1835 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1836 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
1837 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
1839 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1840 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
1841 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
1849 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
1852 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
1854 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
1855 debug_printf("clip_xy = %u\n", key
->clip_xy
);
1856 debug_printf("clip_z = %u\n", key
->clip_z
);
1857 debug_printf("clip_user = %u\n", key
->clip_user
);
1858 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
1859 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
1860 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
1861 debug_printf("has_gs = %u\n", key
->has_gs
);
1862 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
1864 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
1865 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
1866 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
1867 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
1868 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
1871 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1872 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
1878 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1879 unsigned shader_stage
,
1881 uint32_t width
, uint32_t height
, uint32_t depth
,
1882 uint32_t first_level
, uint32_t last_level
,
1883 const void *base_ptr
,
1884 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1885 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1886 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
1889 struct draw_jit_texture
*jit_tex
;
1891 assert(shader_stage
== PIPE_SHADER_VERTEX
||
1892 shader_stage
== PIPE_SHADER_GEOMETRY
);
1894 if (shader_stage
== PIPE_SHADER_VERTEX
) {
1895 assert(sview_idx
< Elements(draw
->llvm
->jit_context
.textures
));
1897 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
1898 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
1899 assert(sview_idx
< Elements(draw
->llvm
->gs_jit_context
.textures
));
1901 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
1907 jit_tex
->width
= width
;
1908 jit_tex
->height
= height
;
1909 jit_tex
->depth
= depth
;
1910 jit_tex
->first_level
= first_level
;
1911 jit_tex
->last_level
= last_level
;
1912 jit_tex
->base
= base_ptr
;
1914 for (j
= first_level
; j
<= last_level
; j
++) {
1915 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
1916 jit_tex
->row_stride
[j
] = row_stride
[j
];
1917 jit_tex
->img_stride
[j
] = img_stride
[j
];
1923 draw_llvm_set_sampler_state(struct draw_context
*draw
,
1924 unsigned shader_type
)
1928 if (shader_type
== PIPE_SHADER_VERTEX
) {
1929 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
1930 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
1932 if (draw
->samplers
[i
]) {
1933 const struct pipe_sampler_state
*s
1934 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
1935 jit_sam
->min_lod
= s
->min_lod
;
1936 jit_sam
->max_lod
= s
->max_lod
;
1937 jit_sam
->lod_bias
= s
->lod_bias
;
1938 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1941 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
1942 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
1943 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
1945 if (draw
->samplers
[i
]) {
1946 const struct pipe_sampler_state
*s
1947 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
1948 jit_sam
->min_lod
= s
->min_lod
;
1949 jit_sam
->max_lod
= s
->max_lod
;
1950 jit_sam
->lod_bias
= s
->lod_bias
;
1951 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1959 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
1961 struct draw_llvm
*llvm
= variant
->llvm
;
1963 if (variant
->function_elts
) {
1964 gallivm_free_function(variant
->gallivm
,
1965 variant
->function_elts
, variant
->jit_func_elts
);
1968 if (variant
->function
) {
1969 gallivm_free_function(variant
->gallivm
,
1970 variant
->function
, variant
->jit_func
);
1973 gallivm_destroy(variant
->gallivm
);
1975 remove_from_list(&variant
->list_item_local
);
1976 variant
->shader
->variants_cached
--;
1977 remove_from_list(&variant
->list_item_global
);
1978 llvm
->nr_variants
--;
1984 * Create LLVM types for various structures.
1987 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
1989 struct gallivm_state
*gallivm
= var
->gallivm
;
1990 LLVMTypeRef texture_type
, sampler_type
, context_type
;
1992 texture_type
= create_jit_texture_type(gallivm
, "texture");
1993 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
1995 context_type
= create_gs_jit_context_type(gallivm
,
1996 var
->shader
->base
.vector_length
,
1997 texture_type
, sampler_type
,
1998 "draw_gs_jit_context");
1999 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2001 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
2005 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
2007 if (!variant
->context_ptr_type
)
2008 create_gs_jit_types(variant
);
2009 return variant
->context_ptr_type
;
2013 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2014 struct lp_type gs_type
)
2016 struct gallivm_state
*gallivm
= variant
->gallivm
;
2017 LLVMBuilderRef builder
= gallivm
->builder
;
2018 LLVMValueRef bits
[16];
2019 struct lp_type mask_type
= lp_int_type(gs_type
);
2020 struct lp_type mask_elem_type
= lp_elem_type(mask_type
);
2021 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
,
2026 assert(gs_type
.length
<= Elements(bits
));
2028 for (i
= gs_type
.length
; i
>= 1; --i
) {
2030 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
2031 bits
[idx
] = lp_build_compare(gallivm
,
2032 mask_elem_type
, PIPE_FUNC_GEQUAL
,
2033 variant
->num_prims
, ind
);
2035 for (i
= 0; i
< gs_type
.length
; ++i
) {
2036 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
2037 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, bits
[i
], ind
, "");
2039 mask_val
= lp_build_compare(gallivm
,
2040 mask_type
, PIPE_FUNC_NOTEQUAL
,
2042 lp_build_const_int_vec(gallivm
, mask_type
, 0));
2048 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2049 struct draw_gs_llvm_variant
*variant
)
2051 struct gallivm_state
*gallivm
= variant
->gallivm
;
2052 LLVMContextRef context
= gallivm
->context
;
2053 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2054 LLVMTypeRef arg_types
[6];
2055 LLVMTypeRef func_type
;
2056 LLVMValueRef variant_func
;
2057 LLVMValueRef context_ptr
;
2058 LLVMValueRef prim_id_ptr
;
2059 LLVMBasicBlockRef block
;
2060 LLVMBuilderRef builder
;
2061 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2062 struct lp_build_sampler_soa
*sampler
= 0;
2063 struct lp_build_context bld
;
2064 struct lp_bld_tgsi_system_values system_values
;
2065 struct lp_type gs_type
;
2067 struct draw_gs_llvm_iface gs_iface
;
2068 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2069 LLVMValueRef consts_ptr
;
2070 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2071 struct lp_build_mask_context mask
;
2072 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2073 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2075 memset(&system_values
, 0, sizeof(system_values
));
2077 assert(variant
->vertex_header_ptr_type
);
2079 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2080 arg_types
[1] = variant
->input_array_type
; /* input */
2081 arg_types
[2] = variant
->vertex_header_ptr_type
; /* vertex_header */
2082 arg_types
[3] = int32_type
; /* num_prims */
2083 arg_types
[4] = int32_type
; /* instance_id */
2084 arg_types
[5] = LLVMPointerType(
2085 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2087 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
2089 variant_func
= LLVMAddFunction(gallivm
->module
, "draw_geometry_shader",
2091 variant
->function
= variant_func
;
2093 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2095 for (i
= 0; i
< Elements(arg_types
); ++i
)
2096 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2097 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
2098 LLVMNoAliasAttribute
);
2100 context_ptr
= LLVMGetParam(variant_func
, 0);
2101 input_array
= LLVMGetParam(variant_func
, 1);
2102 io_ptr
= LLVMGetParam(variant_func
, 2);
2103 num_prims
= LLVMGetParam(variant_func
, 3);
2104 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2105 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2107 lp_build_name(context_ptr
, "context");
2108 lp_build_name(input_array
, "input");
2109 lp_build_name(io_ptr
, "io");
2110 lp_build_name(num_prims
, "num_prims");
2111 lp_build_name(system_values
.instance_id
, "instance_id");
2112 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2114 variant
->context_ptr
= context_ptr
;
2115 variant
->io_ptr
= io_ptr
;
2116 variant
->num_prims
= num_prims
;
2118 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2119 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2120 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2121 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2122 gs_iface
.input
= input_array
;
2123 gs_iface
.variant
= variant
;
2129 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2130 builder
= gallivm
->builder
;
2131 LLVMPositionBuilderAtEnd(builder
, block
);
2133 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2135 memset(&gs_type
, 0, sizeof gs_type
);
2136 gs_type
.floating
= TRUE
; /* floating point values */
2137 gs_type
.sign
= TRUE
; /* values are signed */
2138 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2139 gs_type
.width
= 32; /* 32-bit float */
2140 gs_type
.length
= vector_length
;
2142 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2144 /* code generated texture sampling */
2145 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
,
2148 mask_val
= generate_mask_value(variant
, gs_type
);
2149 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2151 if (gs_info
->uses_primid
) {
2152 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");;
2155 lp_build_tgsi_soa(variant
->gallivm
,
2164 &llvm
->draw
->gs
.geometry_shader
->info
,
2165 (const struct lp_build_tgsi_gs_iface
*)&gs_iface
);
2167 sampler
->destroy(sampler
);
2169 lp_build_mask_end(&mask
);
2171 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2173 gallivm_verify_function(gallivm
, variant_func
);
2177 struct draw_gs_llvm_variant
*
2178 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2179 unsigned num_outputs
,
2180 const struct draw_gs_llvm_variant_key
*key
)
2182 struct draw_gs_llvm_variant
*variant
;
2183 struct llvm_geometry_shader
*shader
=
2184 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2185 LLVMTypeRef vertex_header
;
2187 variant
= MALLOC(sizeof *variant
+
2188 shader
->variant_key_size
-
2189 sizeof variant
->key
);
2190 if (variant
== NULL
)
2193 variant
->llvm
= llvm
;
2194 variant
->shader
= shader
;
2196 variant
->gallivm
= gallivm_create();
2198 create_gs_jit_types(variant
);
2200 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2202 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2204 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2206 draw_gs_llvm_generate(llvm
, variant
);
2208 gallivm_compile_module(variant
->gallivm
);
2210 variant
->jit_func
= (draw_gs_jit_func
)
2211 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2213 variant
->list_item_global
.base
= variant
;
2214 variant
->list_item_local
.base
= variant
;
2215 /*variant->no = */shader
->variants_created
++;
2216 variant
->list_item_global
.base
= variant
;
2222 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2224 struct draw_llvm
*llvm
= variant
->llvm
;
2226 if (variant
->function
) {
2227 gallivm_free_function(variant
->gallivm
,
2228 variant
->function
, variant
->jit_func
);
2231 gallivm_destroy(variant
->gallivm
);
2233 remove_from_list(&variant
->list_item_local
);
2234 variant
->shader
->variants_cached
--;
2235 remove_from_list(&variant
->list_item_global
);
2236 llvm
->nr_gs_variants
--;
2240 struct draw_gs_llvm_variant_key
*
2241 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2244 struct draw_gs_llvm_variant_key
*key
;
2245 struct draw_sampler_static_state
*draw_sampler
;
2247 key
= (struct draw_gs_llvm_variant_key
*)store
;
2249 /* All variants of this shader will have the same value for
2250 * nr_samplers. Not yet trying to compact away holes in the
2253 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2254 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2255 key
->nr_sampler_views
=
2256 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2259 key
->nr_sampler_views
= key
->nr_samplers
;
2262 draw_sampler
= key
->samplers
;
2264 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2266 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2267 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2268 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2270 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2271 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2272 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2279 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2282 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2284 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2285 debug_printf("sampler[%i].src_format = %s\n", i
,
2286 util_format_name(sampler
[i
].texture_state
.format
));