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_logic.h"
36 #include "gallivm/lp_bld_const.h"
37 #include "gallivm/lp_bld_swizzle.h"
38 #include "gallivm/lp_bld_struct.h"
39 #include "gallivm/lp_bld_type.h"
40 #include "gallivm/lp_bld_flow.h"
41 #include "gallivm/lp_bld_debug.h"
42 #include "gallivm/lp_bld_tgsi.h"
43 #include "gallivm/lp_bld_printf.h"
44 #include "gallivm/lp_bld_intr.h"
45 #include "gallivm/lp_bld_init.h"
46 #include "gallivm/lp_bld_type.h"
47 #include "gallivm/lp_bld_pack.h"
48 #include "gallivm/lp_bld_format.h"
50 #include "tgsi/tgsi_exec.h"
51 #include "tgsi/tgsi_dump.h"
53 #include "util/u_math.h"
54 #include "util/u_pointer.h"
55 #include "util/u_string.h"
56 #include "util/u_simple_list.h"
63 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
,
67 struct draw_gs_llvm_iface
{
68 struct lp_build_tgsi_gs_iface base
;
70 struct draw_gs_llvm_variant
*variant
;
74 static INLINE
const struct draw_gs_llvm_iface
*
75 draw_gs_llvm_iface(const struct lp_build_tgsi_gs_iface
*iface
)
77 return (const struct draw_gs_llvm_iface
*)iface
;
81 * Create LLVM type for draw_vertex_buffer.
84 create_jit_dvbuffer_type(struct gallivm_state
*gallivm
,
85 const char *struct_name
)
87 LLVMTargetDataRef target
= gallivm
->target
;
88 LLVMTypeRef dvbuffer_type
;
89 LLVMTypeRef elem_types
[DRAW_JIT_DVBUFFER_NUM_FIELDS
];
90 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
92 elem_types
[DRAW_JIT_DVBUFFER_MAP
] =
93 LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
94 elem_types
[DRAW_JIT_DVBUFFER_SIZE
] = int32_type
;
96 dvbuffer_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
97 Elements(elem_types
), 0);
99 #if HAVE_LLVM < 0x0300
100 LLVMAddTypeName(gallivm
->module
, struct_name
, dvbuffer_type
);
102 /* Make sure the target's struct layout cache doesn't return
103 * stale/invalid data.
105 LLVMInvalidateStructLayout(gallivm
->target
, dvbuffer_type
);
108 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, map
,
109 target
, dvbuffer_type
,
110 DRAW_JIT_DVBUFFER_MAP
);
111 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, size
,
112 target
, dvbuffer_type
,
113 DRAW_JIT_DVBUFFER_SIZE
);
115 return dvbuffer_type
;
119 * Create LLVM type for struct draw_jit_texture
122 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
124 LLVMTargetDataRef target
= gallivm
->target
;
125 LLVMTypeRef texture_type
;
126 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
127 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
129 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
130 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
131 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
132 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
133 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
134 elem_types
[DRAW_JIT_TEXTURE_BASE
] =
135 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
136 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
137 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
138 elem_types
[DRAW_JIT_TEXTURE_MIP_OFFSETS
] =
139 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
141 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
142 Elements(elem_types
), 0);
144 #if HAVE_LLVM < 0x0300
145 LLVMAddTypeName(gallivm
->module
, struct_name
, texture_type
);
147 /* Make sure the target's struct layout cache doesn't return
148 * stale/invalid data.
150 LLVMInvalidateStructLayout(gallivm
->target
, texture_type
);
153 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
154 target
, texture_type
,
155 DRAW_JIT_TEXTURE_WIDTH
);
156 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
157 target
, texture_type
,
158 DRAW_JIT_TEXTURE_HEIGHT
);
159 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
160 target
, texture_type
,
161 DRAW_JIT_TEXTURE_DEPTH
);
162 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
163 target
, texture_type
,
164 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
165 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
166 target
, texture_type
,
167 DRAW_JIT_TEXTURE_LAST_LEVEL
);
168 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, base
,
169 target
, texture_type
,
170 DRAW_JIT_TEXTURE_BASE
);
171 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
172 target
, texture_type
,
173 DRAW_JIT_TEXTURE_ROW_STRIDE
);
174 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
175 target
, texture_type
,
176 DRAW_JIT_TEXTURE_IMG_STRIDE
);
177 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, mip_offsets
,
178 target
, texture_type
,
179 DRAW_JIT_TEXTURE_MIP_OFFSETS
);
181 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
188 * Create LLVM type for struct draw_jit_sampler
191 create_jit_sampler_type(struct gallivm_state
*gallivm
, const char *struct_name
)
193 LLVMTargetDataRef target
= gallivm
->target
;
194 LLVMTypeRef sampler_type
;
195 LLVMTypeRef elem_types
[DRAW_JIT_SAMPLER_NUM_FIELDS
];
197 elem_types
[DRAW_JIT_SAMPLER_MIN_LOD
] =
198 elem_types
[DRAW_JIT_SAMPLER_MAX_LOD
] =
199 elem_types
[DRAW_JIT_SAMPLER_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
200 elem_types
[DRAW_JIT_SAMPLER_BORDER_COLOR
] =
201 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
203 sampler_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
204 Elements(elem_types
), 0);
206 #if HAVE_LLVM < 0x0300
207 LLVMAddTypeName(gallivm
->module
, struct_name
, sampler_type
);
209 /* Make sure the target's struct layout cache doesn't return
210 * stale/invalid data.
212 LLVMInvalidateStructLayout(gallivm
->target
, sampler_type
);
215 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, min_lod
,
216 target
, sampler_type
,
217 DRAW_JIT_SAMPLER_MIN_LOD
);
218 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, max_lod
,
219 target
, sampler_type
,
220 DRAW_JIT_SAMPLER_MAX_LOD
);
221 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, lod_bias
,
222 target
, sampler_type
,
223 DRAW_JIT_SAMPLER_LOD_BIAS
);
224 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, border_color
,
225 target
, sampler_type
,
226 DRAW_JIT_SAMPLER_BORDER_COLOR
);
228 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler
, target
, sampler_type
);
235 * Create LLVM type for struct draw_jit_context
238 create_jit_context_type(struct gallivm_state
*gallivm
,
239 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
240 const char *struct_name
)
242 LLVMTargetDataRef target
= gallivm
->target
;
243 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
244 LLVMTypeRef elem_types
[DRAW_JIT_CTX_NUM_FIELDS
];
245 LLVMTypeRef context_type
;
247 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
248 LP_MAX_TGSI_CONST_BUFFERS
);
249 elem_types
[1] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
250 DRAW_TOTAL_CLIP_PLANES
), 0);
251 elem_types
[2] = LLVMPointerType(float_type
, 0); /* viewport */
252 elem_types
[3] = LLVMArrayType(texture_type
,
253 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
254 elem_types
[4] = LLVMArrayType(sampler_type
,
255 PIPE_MAX_SAMPLERS
); /* samplers */
256 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
257 Elements(elem_types
), 0);
258 #if HAVE_LLVM < 0x0300
259 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
261 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
264 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
265 target
, context_type
, DRAW_JIT_CTX_CONSTANTS
);
266 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
267 target
, context_type
, DRAW_JIT_CTX_PLANES
);
268 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewport
,
269 target
, context_type
, DRAW_JIT_CTX_VIEWPORT
);
270 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
271 target
, context_type
,
272 DRAW_JIT_CTX_TEXTURES
);
273 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
274 target
, context_type
,
275 DRAW_JIT_CTX_SAMPLERS
);
276 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
277 target
, context_type
);
284 * Create LLVM type for struct draw_gs_jit_context
287 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
288 unsigned vector_length
,
289 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
290 const char *struct_name
)
292 LLVMTargetDataRef target
= gallivm
->target
;
293 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
294 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
295 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
296 LLVMTypeRef context_type
;
298 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
299 LP_MAX_TGSI_CONST_BUFFERS
);
300 elem_types
[1] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
301 DRAW_TOTAL_CLIP_PLANES
), 0);
302 elem_types
[2] = LLVMPointerType(float_type
, 0); /* viewport */
304 elem_types
[3] = LLVMArrayType(texture_type
,
305 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
306 elem_types
[4] = LLVMArrayType(sampler_type
,
307 PIPE_MAX_SAMPLERS
); /* samplers */
309 elem_types
[5] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
310 elem_types
[6] = LLVMPointerType(LLVMVectorType(int_type
,
312 elem_types
[7] = LLVMPointerType(LLVMVectorType(int_type
,
315 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
316 Elements(elem_types
), 0);
317 #if HAVE_LLVM < 0x0300
318 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
320 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
323 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
324 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
325 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
326 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
327 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewport
,
328 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
329 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
330 target
, context_type
,
331 DRAW_GS_JIT_CTX_TEXTURES
);
332 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
333 target
, context_type
,
334 DRAW_GS_JIT_CTX_SAMPLERS
);
335 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
336 target
, context_type
,
337 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
338 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
339 target
, context_type
,
340 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
341 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
342 target
, context_type
,
343 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
344 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
345 target
, context_type
);
352 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
354 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
355 LLVMTypeRef input_array
;
357 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
358 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
359 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
360 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
366 * Create LLVM type for struct pipe_vertex_buffer
369 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
,
370 const char *struct_name
)
372 LLVMTargetDataRef target
= gallivm
->target
;
373 LLVMTypeRef elem_types
[4];
377 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
379 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
381 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
382 Elements(elem_types
), 0);
383 #if HAVE_LLVM < 0x0300
384 LLVMAddTypeName(gallivm
->module
, struct_name
, vb_type
);
386 LLVMInvalidateStructLayout(gallivm
->target
, vb_type
);
389 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
391 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
394 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
401 * Create LLVM type for struct vertex_header;
404 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
406 LLVMTargetDataRef target
= gallivm
->target
;
407 LLVMTypeRef elem_types
[4];
408 LLVMTypeRef vertex_header
;
409 char struct_name
[24];
411 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
413 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
414 elem_types
[DRAW_JIT_VERTEX_CLIP
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
415 elem_types
[DRAW_JIT_VERTEX_PRE_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
416 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
418 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
419 Elements(elem_types
), 0);
420 #if HAVE_LLVM < 0x0300
421 LLVMAddTypeName(gallivm
->module
, struct_name
, vertex_header
);
423 LLVMInvalidateStructLayout(gallivm
->target
, vertex_header
);
426 /* these are bit-fields and we can't take address of them
427 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
428 target, vertex_header,
429 DRAW_JIT_VERTEX_CLIPMASK);
430 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
431 target, vertex_header,
432 DRAW_JIT_VERTEX_EDGEFLAG);
433 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
434 target, vertex_header,
435 DRAW_JIT_VERTEX_PAD);
436 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
437 target, vertex_header,
438 DRAW_JIT_VERTEX_VERTEX_ID);
440 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
441 target
, vertex_header
,
442 DRAW_JIT_VERTEX_CLIP
);
443 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, pre_clip_pos
,
444 target
, vertex_header
,
445 DRAW_JIT_VERTEX_PRE_CLIP_POS
);
446 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
447 target
, vertex_header
,
448 DRAW_JIT_VERTEX_DATA
);
450 assert(LLVMABISizeOfType(target
, vertex_header
) ==
451 offsetof(struct vertex_header
, data
[data_elems
]));
453 return vertex_header
;
458 * Create LLVM types for various structures.
461 create_jit_types(struct draw_llvm_variant
*variant
)
463 struct gallivm_state
*gallivm
= variant
->gallivm
;
464 LLVMTypeRef texture_type
, sampler_type
, context_type
, buffer_type
,
467 texture_type
= create_jit_texture_type(gallivm
, "texture");
468 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
470 context_type
= create_jit_context_type(gallivm
, texture_type
, sampler_type
,
472 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
474 buffer_type
= create_jit_dvbuffer_type(gallivm
, "draw_vertex_buffer");
475 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
477 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
478 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
483 get_context_ptr_type(struct draw_llvm_variant
*variant
)
485 if (!variant
->context_ptr_type
)
486 create_jit_types(variant
);
487 return variant
->context_ptr_type
;
492 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
494 if (!variant
->buffer_ptr_type
)
495 create_jit_types(variant
);
496 return variant
->buffer_ptr_type
;
501 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
503 if (!variant
->vb_ptr_type
)
504 create_jit_types(variant
);
505 return variant
->vb_ptr_type
;
509 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
511 if (!variant
->vertex_header_ptr_type
)
512 create_jit_types(variant
);
513 return variant
->vertex_header_ptr_type
;
518 * Create per-context LLVM info.
521 draw_llvm_create(struct draw_context
*draw
)
523 struct draw_llvm
*llvm
;
525 llvm
= CALLOC_STRUCT( draw_llvm
);
533 llvm
->nr_variants
= 0;
534 make_empty_list(&llvm
->vs_variants_list
);
536 llvm
->nr_gs_variants
= 0;
537 make_empty_list(&llvm
->gs_variants_list
);
544 * Free per-context LLVM info.
547 draw_llvm_destroy(struct draw_llvm
*llvm
)
549 /* XXX free other draw_llvm data? */
555 * Create LLVM-generated code for a vertex shader.
557 struct draw_llvm_variant
*
558 draw_llvm_create_variant(struct draw_llvm
*llvm
,
560 const struct draw_llvm_variant_key
*key
)
562 struct draw_llvm_variant
*variant
;
563 struct llvm_vertex_shader
*shader
=
564 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
565 LLVMTypeRef vertex_header
;
567 variant
= MALLOC(sizeof *variant
+
568 shader
->variant_key_size
-
569 sizeof variant
->key
);
573 variant
->llvm
= llvm
;
575 variant
->gallivm
= gallivm_create();
577 create_jit_types(variant
);
579 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
581 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
583 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
585 draw_llvm_generate(llvm
, variant
, FALSE
); /* linear */
586 draw_llvm_generate(llvm
, variant
, TRUE
); /* elts */
588 gallivm_compile_module(variant
->gallivm
);
590 variant
->jit_func
= (draw_jit_vert_func
)
591 gallivm_jit_function(variant
->gallivm
, variant
->function
);
593 variant
->jit_func_elts
= (draw_jit_vert_func_elts
)
594 gallivm_jit_function(variant
->gallivm
, variant
->function_elts
);
596 variant
->shader
= shader
;
597 variant
->list_item_global
.base
= variant
;
598 variant
->list_item_local
.base
= variant
;
599 /*variant->no = */shader
->variants_created
++;
600 variant
->list_item_global
.base
= variant
;
607 generate_vs(struct draw_llvm_variant
*variant
,
608 LLVMBuilderRef builder
,
609 struct lp_type vs_type
,
610 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
611 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
612 const struct lp_bld_tgsi_system_values
*system_values
,
613 LLVMValueRef context_ptr
,
614 struct lp_build_sampler_soa
*draw_sampler
,
615 boolean clamp_vertex_color
)
617 struct draw_llvm
*llvm
= variant
->llvm
;
618 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
619 LLVMValueRef consts_ptr
= draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
620 struct lp_build_sampler_soa
*sampler
= 0;
622 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
623 tgsi_dump(tokens
, 0);
624 draw_llvm_dump_variant_key(&variant
->key
);
627 if (llvm
->draw
->num_sampler_views
&& llvm
->draw
->num_samplers
)
628 sampler
= draw_sampler
;
630 lp_build_tgsi_soa(variant
->gallivm
,
633 NULL
/*struct lp_build_mask_context *mask*/,
639 &llvm
->draw
->vs
.vertex_shader
->info
,
644 unsigned chan
, attrib
;
645 struct lp_build_context bld
;
646 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
647 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
649 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
650 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
651 if (outputs
[attrib
][chan
]) {
652 switch (info
->output_semantic_name
[attrib
]) {
653 case TGSI_SEMANTIC_COLOR
:
654 case TGSI_SEMANTIC_BCOLOR
:
655 if (clamp_vertex_color
) {
656 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
657 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
658 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
661 case TGSI_SEMANTIC_FOG
:
662 if (chan
== 1 || chan
== 2)
663 LLVMBuildStore(builder
, bld
.zero
, outputs
[attrib
][chan
]);
665 LLVMBuildStore(builder
, bld
.one
, outputs
[attrib
][chan
]);
675 generate_fetch(struct gallivm_state
*gallivm
,
676 LLVMValueRef vbuffers_ptr
,
678 struct pipe_vertex_element
*velem
,
681 LLVMValueRef instance_id
)
683 const struct util_format_description
*format_desc
=
684 util_format_description(velem
->src_format
);
685 LLVMValueRef zero
= LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
));
686 LLVMBuilderRef builder
= gallivm
->builder
;
687 LLVMValueRef indices
=
688 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
689 velem
->vertex_buffer_index
, 0);
690 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
692 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
693 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
694 LLVMValueRef map_ptr
= draw_jit_dvbuffer_map(gallivm
, vbuffer_ptr
);
695 LLVMValueRef buffer_size
= draw_jit_dvbuffer_size(gallivm
, vbuffer_ptr
);
697 LLVMValueRef buffer_overflowed
;
698 LLVMValueRef needed_buffer_size
;
699 LLVMValueRef temp_ptr
=
700 lp_build_alloca(gallivm
,
701 lp_build_vec_type(gallivm
, lp_float32_vec4_type()), "");
702 LLVMValueRef ofbit
, oresult
;
703 LLVMTypeRef oelems
[2] = {
704 LLVMInt32TypeInContext(gallivm
->context
),
705 LLVMInt1TypeInContext(gallivm
->context
)
707 LLVMTypeRef otype
= LLVMStructTypeInContext(gallivm
->context
,
709 struct lp_build_if_state if_ctx
;
711 if (velem
->instance_divisor
) {
712 /* array index = instance_id / instance_divisor */
713 index
= LLVMBuildUDiv(builder
, instance_id
,
714 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
718 oresult
= lp_build_intrinsic_binary(builder
,
719 "llvm.umul.with.overflow.i32",
720 otype
, vb_stride
, index
);
721 ofbit
= LLVMBuildExtractValue(builder
, oresult
, 1, "");
722 stride
= LLVMBuildExtractValue(builder
, oresult
, 0, "");
724 oresult
= lp_build_intrinsic_binary(builder
,
725 "llvm.uadd.with.overflow.i32",
726 otype
, stride
, vb_buffer_offset
);
729 LLVMBuildExtractValue(builder
, oresult
, 1, ""),
731 stride
= LLVMBuildExtractValue(builder
, oresult
, 0, "");
733 oresult
= lp_build_intrinsic_binary(
735 "llvm.uadd.with.overflow.i32",
737 lp_build_const_int32(gallivm
, velem
->src_offset
));
740 LLVMBuildExtractValue(builder
, oresult
, 1, ""),
742 stride
= LLVMBuildExtractValue(builder
, oresult
, 0, "");
745 oresult
= lp_build_intrinsic_binary(
747 "llvm.uadd.with.overflow.i32",
749 lp_build_const_int32(gallivm
,
750 util_format_get_blocksize(velem
->src_format
)));
753 LLVMBuildExtractValue(builder
, oresult
, 1, ""),
755 needed_buffer_size
= LLVMBuildExtractValue(builder
, oresult
, 0, "");
757 buffer_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
758 needed_buffer_size
, buffer_size
,
759 "buffer_overflowed");
760 buffer_overflowed
= LLVMBuildOr(builder
, buffer_overflowed
, ofbit
, "");
762 lp_build_printf(gallivm
, "vbuf index = %u, vb_stride is %u\n",
764 lp_build_printf(gallivm
, " vb_buffer_offset = %u, src_offset is %u\n",
766 lp_build_const_int32(gallivm
, velem
->src_offset
));
767 lp_build_print_value(gallivm
, " blocksize = ",
768 lp_build_const_int32(
770 util_format_get_blocksize(velem
->src_format
)));
771 lp_build_printf(gallivm
, " instance_id = %u\n", instance_id
);
772 lp_build_printf(gallivm
, " stride = %u\n", stride
);
773 lp_build_printf(gallivm
, " buffer size = %u\n", buffer_size
);
774 lp_build_printf(gallivm
, " needed_buffer_size = %u\n", needed_buffer_size
);
775 lp_build_print_value(gallivm
, " buffer overflowed = ", buffer_overflowed
);
778 lp_build_if(&if_ctx
, gallivm
, buffer_overflowed
);
781 lp_build_const_vec(gallivm
, lp_float32_vec4_type(), 0);
782 LLVMBuildStore(builder
, val
, temp_ptr
);
784 lp_build_else(&if_ctx
);
787 map_ptr
= LLVMBuildGEP(builder
, map_ptr
, &stride
, 1, "");
789 val
= lp_build_fetch_rgba_aos(gallivm
,
791 lp_float32_vec4_type(),
794 LLVMBuildStore(builder
, val
, temp_ptr
);
796 lp_build_endif(&if_ctx
);
798 *res
= LLVMBuildLoad(builder
, temp_ptr
, "aos");
802 convert_to_soa(struct gallivm_state
*gallivm
,
803 LLVMValueRef (*src_aos
)[LP_MAX_VECTOR_WIDTH
/ 32],
804 LLVMValueRef (*dst_soa
)[TGSI_NUM_CHANNELS
],
805 unsigned num_attribs
, const struct lp_type soa_type
)
808 struct lp_type aos_channel_type
= soa_type
;
810 debug_assert(TGSI_NUM_CHANNELS
== 4);
811 debug_assert((soa_type
.length
% TGSI_NUM_CHANNELS
) == 0);
813 aos_channel_type
.length
>>= 1;
815 for (i
= 0; i
< num_attribs
; ++i
) {
816 LLVMValueRef aos_channels
[TGSI_NUM_CHANNELS
];
817 unsigned pixels_per_channel
= soa_type
.length
/ TGSI_NUM_CHANNELS
;
819 for (j
= 0; j
< TGSI_NUM_CHANNELS
; ++j
) {
820 LLVMValueRef channel
[LP_MAX_VECTOR_LENGTH
] = { 0 };
822 assert(pixels_per_channel
<= LP_MAX_VECTOR_LENGTH
);
824 for (k
= 0; k
< pixels_per_channel
; ++k
) {
825 channel
[k
] = src_aos
[i
][j
+ TGSI_NUM_CHANNELS
* k
];
828 aos_channels
[j
] = lp_build_concat(gallivm
, channel
, aos_channel_type
, pixels_per_channel
);
831 lp_build_transpose_aos(gallivm
, soa_type
, aos_channels
, dst_soa
[i
]);
837 store_aos(struct gallivm_state
*gallivm
,
842 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
843 LLVMBuilderRef builder
= gallivm
->builder
;
844 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
845 LLVMValueRef indices
[3];
847 indices
[0] = lp_build_const_int32(gallivm
, 0);
849 indices
[2] = lp_build_const_int32(gallivm
, 0);
851 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
852 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
855 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
858 /* Unaligned store due to the vertex header */
859 lp_set_store_alignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
863 * Adjust the mask to architecture endianess. The mask will the store in struct:
865 * struct vertex_header {
866 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
867 * unsigned edgeflag:1;
868 * unsigned have_clipdist:1;
869 * unsigned vertex_id:16;
873 * On little-endian machine nothing needs to done, however on bit-endian machine
874 * the mask's fields need to be adjusted with the algorithm:
876 * uint32_t reverse (uint32_t x)
878 * return (x >> 16) | // vertex_id
879 * ((x & 0x3fff) << 18) | // clipmask
880 * ((x & 0x4000) << 3) | // have_clipdist
881 * ((x & 0x8000) << 1); // edgeflag
885 adjust_mask(struct gallivm_state
*gallivm
,
888 #ifdef PIPE_ARCH_BIG_ENDIAN
889 LLVMBuilderRef builder
= gallivm
->builder
;
890 LLVMValueRef vertex_id
;
891 LLVMValueRef clipmask
;
892 LLVMValueRef have_clipdist
;
893 LLVMValueRef edgeflag
;
895 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
896 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
897 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
898 have_clipdist
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
899 have_clipdist
= LLVMBuildShl(builder
, have_clipdist
, lp_build_const_int32(gallivm
, 3), "");
900 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
901 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
903 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
904 mask
= LLVMBuildOr(builder
, mask
, have_clipdist
, "");
905 mask
= LLVMBuildOr(builder
, mask
, edgeflag
, "");
911 store_aos_array(struct gallivm_state
*gallivm
,
912 struct lp_type soa_type
,
914 LLVMValueRef
*indices
,
918 LLVMValueRef clipmask
,
919 boolean have_clipdist
)
921 LLVMBuilderRef builder
= gallivm
->builder
;
922 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
923 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
924 LLVMValueRef linear_inds
[LP_MAX_VECTOR_WIDTH
/ 32];
925 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
926 int vector_length
= soa_type
.length
;
929 debug_assert(TGSI_NUM_CHANNELS
== 4);
931 for (i
= 0; i
< vector_length
; i
++) {
932 linear_inds
[i
] = lp_build_const_int32(gallivm
, i
);
934 inds
[i
] = indices
[i
];
936 inds
[i
] = linear_inds
[i
];
938 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
942 /* store vertex header for each of the n vertices */
943 LLVMValueRef val
, cliptmp
;
944 int vertex_id_pad_edgeflag
;
946 /* If this assertion fails, it means we need to update the bit twidding
947 * code here. See struct vertex_header in draw_private.h.
949 assert(DRAW_TOTAL_CLIP_PLANES
==14);
950 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
951 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
953 vertex_id_pad_edgeflag
|= 1 << (DRAW_TOTAL_CLIP_PLANES
+1);
954 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
), vertex_id_pad_edgeflag
);
955 /* OR with the clipmask */
956 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
957 for (i
= 0; i
< vector_length
; i
++) {
958 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
959 val
= LLVMBuildExtractElement(builder
, cliptmp
, linear_inds
[i
], "");
960 val
= adjust_mask(gallivm
, val
);
961 LLVMBuildStore(builder
, val
, id_ptr
);
963 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
964 io_ptrs
[i
], inds
[i
], val
);
969 /* store for each of the n vertices */
970 for (i
= 0; i
< vector_length
; i
++) {
971 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
977 convert_to_aos(struct gallivm_state
*gallivm
,
979 LLVMValueRef
*indices
,
980 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
981 LLVMValueRef clipmask
,
983 struct lp_type soa_type
,
984 boolean have_clipdist
)
986 LLVMBuilderRef builder
= gallivm
->builder
;
987 unsigned chan
, attrib
, i
;
990 lp_build_printf(gallivm
, " # storing begin\n");
992 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
993 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
994 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
995 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
996 if (outputs
[attrib
][chan
]) {
997 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
998 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
1000 lp_build_printf(gallivm
, "output %d : %d ",
1001 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
1003 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
1005 lp_build_print_value(gallivm
, "val = ", out
);
1015 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
1016 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
1018 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
1020 for (i
= 0; i
< soa_type
.length
; ++i
) {
1021 aos
[i
] = lp_build_extract_range(gallivm
,
1022 soa
[i
% TGSI_NUM_CHANNELS
],
1023 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1028 store_aos_array(gallivm
,
1034 clipmask
, have_clipdist
);
1037 lp_build_printf(gallivm
, " # storing end\n");
1043 * Stores original vertex positions in clip coordinates
1046 store_clip(struct gallivm_state
*gallivm
,
1047 const struct lp_type vs_type
,
1048 LLVMValueRef io_ptr
,
1049 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1050 boolean pre_clip_pos
, int idx
)
1052 LLVMBuilderRef builder
= gallivm
->builder
;
1053 LLVMValueRef soa
[4];
1054 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
1055 LLVMValueRef indices
[2];
1056 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1057 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1058 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1062 indices
[1] = lp_build_const_int32(gallivm
, 0);
1064 for (i
= 0; i
< vs_type
.length
; i
++) {
1065 inds
[i
] = lp_build_const_int32(gallivm
, i
);
1066 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1069 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
1070 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
1071 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
1072 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
1074 if (!pre_clip_pos
) {
1075 for (i
= 0; i
< vs_type
.length
; i
++) {
1076 clip_ptrs
[i
] = draw_jit_header_clip(gallivm
, io_ptrs
[i
]);
1079 for (i
= 0; i
< vs_type
.length
; i
++) {
1080 clip_ptrs
[i
] = draw_jit_header_pre_clip_pos(gallivm
, io_ptrs
[i
]);
1084 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
1085 for (i
= 0; i
< vs_type
.length
; ++i
) {
1086 aos
[i
] = lp_build_extract_range(gallivm
,
1087 soa
[i
% TGSI_NUM_CHANNELS
],
1088 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1092 for (j
= 0; j
< vs_type
.length
; j
++) {
1093 LLVMTypeRef clip_ptr_type
= LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4), 0);
1094 LLVMValueRef clip_ptr
;
1096 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
1097 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
1099 /* Unaligned store */
1100 lp_set_store_alignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
1106 * Transforms the outputs for viewport mapping
1109 generate_viewport(struct draw_llvm_variant
*variant
,
1110 LLVMBuilderRef builder
,
1111 struct lp_type vs_type
,
1112 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1113 LLVMValueRef context_ptr
)
1116 struct gallivm_state
*gallivm
= variant
->gallivm
;
1117 struct lp_type f32_type
= vs_type
;
1118 const unsigned pos
= draw_current_shader_position_output(variant
->llvm
->draw
);
1119 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1120 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn*/
1121 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1122 LLVMValueRef vp_ptr
= draw_jit_context_viewport(gallivm
, context_ptr
);
1124 /* for 1/w convention*/
1125 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1126 LLVMBuildStore(builder
, out3
, outputs
[pos
][3]);
1128 /* Viewport Mapping */
1129 for (i
=0; i
<3; i
++) {
1130 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[pos
][i
], ""); /*x0 x1 .. xn*/
1133 LLVMValueRef scale_i
;
1134 LLVMValueRef trans_i
;
1137 index
= lp_build_const_int32(gallivm
, i
);
1138 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1140 index
= lp_build_const_int32(gallivm
, i
+4);
1141 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1143 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1144 LLVMBuildLoad(builder
, scale_i
, "scale"));
1145 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1146 LLVMBuildLoad(builder
, trans_i
, "trans"));
1149 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1151 out
= LLVMBuildFMul(builder
, out
, scale
, "");
1152 /* add translation */
1153 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
1155 /* store transformed outputs */
1156 LLVMBuildStore(builder
, out
, outputs
[pos
][i
]);
1163 * Returns clipmask as nxi32 bitmask for the n vertices
1166 generate_clipmask(struct draw_llvm
*llvm
,
1167 struct gallivm_state
*gallivm
,
1168 struct lp_type vs_type
,
1169 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1174 unsigned ucp_enable
,
1175 LLVMValueRef context_ptr
,
1176 boolean
*have_clipdist
)
1178 LLVMBuilderRef builder
= gallivm
->builder
;
1179 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1180 LLVMValueRef test
, temp
;
1181 LLVMValueRef zero
, shift
;
1182 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1183 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1184 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1185 struct lp_type f32_type
= vs_type
;
1186 struct lp_type i32_type
= lp_int_type(vs_type
);
1187 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1188 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1189 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1190 bool have_cd
= false;
1193 cd
[0] = draw_current_shader_clipdistance_output(llvm
->draw
, 0);
1194 cd
[1] = draw_current_shader_clipdistance_output(llvm
->draw
, 1);
1196 if (cd
[0] != pos
|| cd
[1] != pos
)
1199 if (num_written_clipdistance
&& !clip_user
) {
1201 ucp_enable
= (1 << num_written_clipdistance
) - 1;
1204 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1205 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1206 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1207 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1210 * load clipvertex and position from correct locations.
1211 * if they are the same just load them once.
1213 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1214 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1215 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1216 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1218 if (clip_user
&& cv
!= pos
) {
1219 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1220 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1221 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1222 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1230 /* Cliptest, for hardwired planes */
1233 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1235 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1239 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1240 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1241 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1242 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1243 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1246 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1247 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1248 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1249 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1252 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1253 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1254 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1255 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1256 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1260 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1263 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1264 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1265 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1269 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1270 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1271 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1272 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1275 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1276 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1277 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1278 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1282 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1283 LLVMValueRef indices
[3];
1285 /* userclip planes */
1286 while (ucp_enable
) {
1287 unsigned plane_idx
= ffs(ucp_enable
)-1;
1288 ucp_enable
&= ~(1 << plane_idx
);
1291 if (have_cd
&& num_written_clipdistance
) {
1292 LLVMValueRef clipdist
;
1296 *have_clipdist
= TRUE
;
1298 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1300 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1302 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
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
, "");
1307 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1308 indices
[0] = lp_build_const_int32(gallivm
, 0);
1309 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1311 indices
[2] = lp_build_const_int32(gallivm
, 0);
1312 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1313 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1314 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1315 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1317 indices
[2] = lp_build_const_int32(gallivm
, 1);
1318 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1319 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1320 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1321 test
= LLVMBuildFMul(builder
, planes
, cv_y
, "");
1322 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1324 indices
[2] = lp_build_const_int32(gallivm
, 2);
1325 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1326 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1327 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1328 test
= LLVMBuildFMul(builder
, planes
, cv_z
, "");
1329 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1331 indices
[2] = lp_build_const_int32(gallivm
, 3);
1332 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1333 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1334 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1335 test
= LLVMBuildFMul(builder
, planes
, cv_w
, "");
1336 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1338 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1339 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1340 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1341 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1350 * Returns boolean if any clipping has occurred
1351 * Used zero/non-zero i32 value to represent boolean
1354 clipmask_booli32(struct gallivm_state
*gallivm
,
1355 const struct lp_type vs_type
,
1356 LLVMValueRef clipmask_bool_ptr
)
1358 LLVMBuilderRef builder
= gallivm
->builder
;
1359 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
1360 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1361 LLVMValueRef ret
= LLVMConstNull(int32_type
);
1366 * Can do this with log2(vector length) pack instructions and one extract
1367 * (as we don't actually need a or) with sse2 which would be way better.
1369 for (i
=0; i
< vs_type
.length
; i
++) {
1370 temp
= LLVMBuildExtractElement(builder
, clipmask_bool
,
1371 lp_build_const_int32(gallivm
, i
) , "");
1372 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1378 draw_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
1379 struct lp_build_tgsi_context
* bld_base
,
1380 boolean is_indirect
,
1381 LLVMValueRef vertex_index
,
1382 LLVMValueRef attrib_index
,
1383 LLVMValueRef swizzle_index
)
1385 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1386 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1387 LLVMBuilderRef builder
= gallivm
->builder
;
1388 LLVMValueRef indices
[3];
1390 struct lp_type type
= bld_base
->base
.type
;
1394 res
= bld_base
->base
.zero
;
1395 for (i
= 0; i
< type
.length
; ++i
) {
1396 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1397 LLVMValueRef vert_chan_index
= LLVMBuildExtractElement(builder
,
1398 vertex_index
, idx
, "");
1399 LLVMValueRef channel_vec
, value
;
1400 indices
[0] = vert_chan_index
;
1401 indices
[1] = attrib_index
;
1402 indices
[2] = swizzle_index
;
1404 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1405 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1406 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1408 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1411 indices
[0] = vertex_index
;
1412 indices
[1] = attrib_index
;
1413 indices
[2] = swizzle_index
;
1415 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1416 res
= LLVMBuildLoad(builder
, res
, "");
1423 draw_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
1424 struct lp_build_tgsi_context
* bld_base
,
1425 LLVMValueRef (*outputs
)[4],
1426 LLVMValueRef emitted_vertices_vec
)
1428 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1429 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1430 struct gallivm_state
*gallivm
= variant
->gallivm
;
1431 LLVMBuilderRef builder
= gallivm
->builder
;
1432 struct lp_type gs_type
= bld_base
->base
.type
;
1433 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1434 lp_int_type(gs_type
), 0);
1435 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1436 LLVMValueRef next_prim_offset
=
1437 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1438 LLVMValueRef io
= variant
->io_ptr
;
1440 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1442 for (i
= 0; i
< gs_type
.length
; ++i
) {
1443 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1444 LLVMValueRef currently_emitted
=
1445 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1446 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1447 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1450 convert_to_aos(gallivm
, io
, indices
,
1452 gs_info
->num_outputs
, gs_type
,
1457 draw_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
1458 struct lp_build_tgsi_context
* bld_base
,
1459 LLVMValueRef verts_per_prim_vec
,
1460 LLVMValueRef emitted_prims_vec
)
1462 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1463 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1464 struct gallivm_state
*gallivm
= variant
->gallivm
;
1465 LLVMBuilderRef builder
= gallivm
->builder
;
1466 LLVMValueRef prim_lengts_ptr
=
1467 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1470 for (i
= 0; i
< bld_base
->base
.type
.length
; ++i
) {
1471 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1472 LLVMValueRef prims_emitted
=
1473 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1474 LLVMValueRef store_ptr
;
1475 LLVMValueRef num_vertices
=
1476 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1478 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1479 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1480 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1481 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1486 draw_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
1487 struct lp_build_tgsi_context
* bld_base
,
1488 LLVMValueRef total_emitted_vertices_vec
,
1489 LLVMValueRef emitted_prims_vec
)
1491 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1492 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1493 struct gallivm_state
*gallivm
= variant
->gallivm
;
1494 LLVMBuilderRef builder
= gallivm
->builder
;
1495 LLVMValueRef emitted_verts_ptr
=
1496 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1497 LLVMValueRef emitted_prims_ptr
=
1498 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1499 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1501 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &zero
, 0, "");
1502 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &zero
, 0, "");
1504 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1505 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1509 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1512 struct gallivm_state
*gallivm
= variant
->gallivm
;
1513 LLVMContextRef context
= gallivm
->context
;
1514 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1515 LLVMTypeRef arg_types
[9];
1516 unsigned num_arg_types
=
1517 elts
? Elements(arg_types
) : Elements(arg_types
) - 1;
1518 LLVMTypeRef func_type
;
1519 LLVMValueRef context_ptr
;
1520 LLVMBasicBlockRef block
;
1521 LLVMBuilderRef builder
;
1522 struct lp_type vs_type
;
1523 LLVMValueRef end
, start
;
1524 LLVMValueRef count
, fetch_elts
, fetch_elt_max
, fetch_count
;
1525 LLVMValueRef stride
, step
, io_itr
;
1526 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1527 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1528 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1529 struct draw_context
*draw
= llvm
->draw
;
1530 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1532 struct lp_build_context bld
;
1533 struct lp_build_loop_state lp_loop
;
1534 const int vector_length
= lp_native_vector_width
/ 32;
1535 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1536 LLVMValueRef fetch_max
;
1537 struct lp_build_sampler_soa
*sampler
= 0;
1538 LLVMValueRef ret
, clipmask_bool_ptr
;
1539 const struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
1540 struct draw_llvm_variant_key
*key
= &variant
->key
;
1541 /* If geometry shader is present we need to skip both the viewport
1542 * transformation and clipping otherwise the inputs to the geometry
1543 * shader will be incorrect.
1545 const boolean bypass_viewport
= gs
|| key
->bypass_viewport
;
1546 const boolean enable_cliptest
= !gs
&& (key
->clip_xy
||
1549 LLVMValueRef variant_func
;
1550 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1551 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1552 boolean have_clipdist
= FALSE
;
1553 struct lp_bld_tgsi_system_values system_values
;
1555 memset(&system_values
, 0, sizeof(system_values
));
1558 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1559 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1560 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1562 arg_types
[i
++] = LLVMPointerType(int32_type
, 0);/* fetch_elts */
1563 arg_types
[i
++] = int32_type
; /* fetch_elt_max */
1565 arg_types
[i
++] = int32_type
; /* start */
1566 arg_types
[i
++] = int32_type
; /* fetch_count / count */
1567 arg_types
[i
++] = int32_type
; /* stride */
1568 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1569 arg_types
[i
++] = int32_type
; /* instance_id */
1571 func_type
= LLVMFunctionType(int32_type
, arg_types
, num_arg_types
, 0);
1573 variant_func
= LLVMAddFunction(gallivm
->module
,
1574 elts
? "draw_llvm_shader_elts" : "draw_llvm_shader",
1578 variant
->function_elts
= variant_func
;
1580 variant
->function
= variant_func
;
1582 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1583 for (i
= 0; i
< num_arg_types
; ++i
)
1584 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1585 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1586 LLVMNoAliasAttribute
);
1588 context_ptr
= LLVMGetParam(variant_func
, 0);
1589 io_ptr
= LLVMGetParam(variant_func
, 1);
1590 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1591 stride
= LLVMGetParam(variant_func
, 5 + (elts
? 1 : 0));
1592 vb_ptr
= LLVMGetParam(variant_func
, 6 + (elts
? 1 : 0));
1593 system_values
.instance_id
= LLVMGetParam(variant_func
, 7 + (elts
? 1 : 0));
1595 lp_build_name(context_ptr
, "context");
1596 lp_build_name(io_ptr
, "io");
1597 lp_build_name(vbuffers_ptr
, "vbuffers");
1598 lp_build_name(stride
, "stride");
1599 lp_build_name(vb_ptr
, "vb");
1600 lp_build_name(system_values
.instance_id
, "instance_id");
1603 fetch_elts
= LLVMGetParam(variant_func
, 3);
1604 fetch_elt_max
= LLVMGetParam(variant_func
, 4);
1605 fetch_count
= LLVMGetParam(variant_func
, 5);
1606 lp_build_name(fetch_elts
, "fetch_elts");
1607 lp_build_name(fetch_elt_max
, "fetch_elt_max");
1608 lp_build_name(fetch_count
, "fetch_count");
1609 start
= count
= NULL
;
1612 start
= LLVMGetParam(variant_func
, 3);
1613 count
= LLVMGetParam(variant_func
, 4);
1614 lp_build_name(start
, "start");
1615 lp_build_name(count
, "count");
1616 fetch_elts
= fetch_count
= NULL
;
1623 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1624 builder
= gallivm
->builder
;
1625 LLVMPositionBuilderAtEnd(builder
, block
);
1627 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1629 memset(&vs_type
, 0, sizeof vs_type
);
1630 vs_type
.floating
= TRUE
; /* floating point values */
1631 vs_type
.sign
= TRUE
; /* values are signed */
1632 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1633 vs_type
.width
= 32; /* 32-bit float */
1634 vs_type
.length
= vector_length
;
1636 /* hold temporary "bool" clipmask */
1637 clipmask_bool_ptr
= lp_build_alloca(gallivm
, lp_build_int_vec_type(gallivm
, vs_type
), "");
1638 LLVMBuildStore(builder
, lp_build_zero(gallivm
, lp_int_type(vs_type
)), clipmask_bool_ptr
);
1640 /* code generated texture sampling */
1641 sampler
= draw_llvm_sampler_soa_create(
1642 draw_llvm_variant_key_samplers(key
),
1648 count
= fetch_count
;
1651 end
= lp_build_add(&bld
, start
, count
);
1654 step
= lp_build_const_int32(gallivm
, vector_length
);
1656 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1658 lp_build_loop_begin(&lp_loop
, gallivm
, zero
);
1660 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1661 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][LP_MAX_VECTOR_WIDTH
/ 32] = { { 0 } };
1663 LLVMValueRef clipmask
; /* holds the clipmask value */
1664 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1666 io_itr
= lp_loop
.counter
;
1668 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1670 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1671 io_itr
, io
, lp_loop
.counter
);
1673 system_values
.vertex_id
= lp_build_zero(gallivm
, lp_type_uint_vec(32, 32*vector_length
));
1674 for (i
= 0; i
< vector_length
; ++i
) {
1675 LLVMValueRef true_index
=
1676 LLVMBuildAdd(builder
,
1678 lp_build_const_int32(gallivm
, i
), "");
1679 true_index
= LLVMBuildAdd(builder
, start
, true_index
, "");
1681 /* make sure we're not out of bounds which can happen
1682 * if fetch_count % 4 != 0, because on the last iteration
1683 * a few of the 4 vertex fetches will be out of bounds */
1684 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1686 system_values
.vertex_id
= LLVMBuildInsertElement(
1688 system_values
.vertex_id
, true_index
,
1689 lp_build_const_int32(gallivm
, i
), "");
1692 LLVMValueRef fetch_ptr
;
1693 LLVMValueRef index_overflowed
;
1694 LLVMValueRef index_ptr
=
1697 lp_build_vec_type(gallivm
, lp_type_int(32)), "");
1698 struct lp_build_if_state if_ctx
;
1699 index_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGE
,
1700 true_index
, fetch_elt_max
,
1701 "index_overflowed");
1703 lp_build_if(&if_ctx
, gallivm
, index_overflowed
);
1705 /* Generate maximum possible index so that
1706 * generate_fetch can treat it just like
1707 * any other overflow and return zeros.
1708 * We don't have to worry about the restart
1709 * primitive index because it has already been
1713 lp_build_const_int32(gallivm
, 0xffffffff);
1714 LLVMBuildStore(builder
, val
, index_ptr
);
1716 lp_build_else(&if_ctx
);
1719 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1720 &true_index
, 1, "");
1721 val
= LLVMBuildLoad(builder
, fetch_ptr
, "");
1722 LLVMBuildStore(builder
, val
, index_ptr
);
1724 lp_build_endif(&if_ctx
);
1725 true_index
= LLVMBuildLoad(builder
, index_ptr
, "true_index");
1728 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1729 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1730 LLVMValueRef vb_index
=
1731 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1732 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1733 generate_fetch(gallivm
, vbuffers_ptr
,
1734 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1735 system_values
.instance_id
);
1738 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1739 draw
->pt
.nr_vertex_elements
, vs_type
);
1741 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1742 generate_vs(variant
,
1750 key
->clamp_vertex_color
);
1752 if (pos
!= -1 && cv
!= -1) {
1753 /* store original positions in clip before further manipulation */
1754 store_clip(gallivm
, vs_type
, io
, outputs
, 0, cv
);
1755 store_clip(gallivm
, vs_type
, io
, outputs
, 1, pos
);
1758 if (enable_cliptest
) {
1759 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1760 /* allocate clipmask, assign it integer type */
1761 clipmask
= generate_clipmask(llvm
,
1770 context_ptr
, &have_clipdist
);
1771 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1772 /* store temporary clipping boolean value */
1773 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1776 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1779 /* do viewport mapping */
1780 if (!bypass_viewport
) {
1781 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1785 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1788 /* store clipmask in vertex header,
1789 * original positions in clip
1790 * and transformed positions in data
1792 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
1793 vs_info
->num_outputs
, vs_type
,
1796 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
1798 sampler
->destroy(sampler
);
1800 /* return clipping boolean value for function */
1801 ret
= clipmask_booli32(gallivm
, vs_type
, clipmask_bool_ptr
);
1803 LLVMBuildRet(builder
, ret
);
1805 gallivm_verify_function(gallivm
, variant_func
);
1809 struct draw_llvm_variant_key
*
1810 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1813 struct draw_llvm_variant_key
*key
;
1814 struct draw_sampler_static_state
*draw_sampler
;
1816 key
= (struct draw_llvm_variant_key
*)store
;
1818 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1820 /* Presumably all variants of the shader should have the same
1821 * number of vertex elements - ie the number of shader inputs.
1822 * NOTE: we NEED to store the needed number of needed inputs
1823 * here, not the number of provided elements to match keysize
1824 * (and the offset of sampler state in the key).
1826 key
->nr_vertex_elements
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
1827 assert(key
->nr_vertex_elements
<= llvm
->draw
->pt
.nr_vertex_elements
);
1829 /* will have to rig this up properly later */
1830 key
->clip_xy
= llvm
->draw
->clip_xy
;
1831 key
->clip_z
= llvm
->draw
->clip_z
;
1832 key
->clip_user
= llvm
->draw
->clip_user
;
1833 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1834 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
1835 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1836 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1837 key
->has_gs
= llvm
->draw
->gs
.geometry_shader
!= NULL
;
1840 /* All variants of this shader will have the same value for
1841 * nr_samplers. Not yet trying to compact away holes in the
1844 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1845 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
1846 key
->nr_sampler_views
=
1847 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
1850 key
->nr_sampler_views
= key
->nr_samplers
;
1853 draw_sampler
= draw_llvm_variant_key_samplers(key
);
1855 memcpy(key
->vertex_element
,
1856 llvm
->draw
->pt
.vertex_element
,
1857 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1859 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
1861 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1862 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
1863 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
1865 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1866 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
1867 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
1875 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
1878 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
1880 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
1881 debug_printf("clip_xy = %u\n", key
->clip_xy
);
1882 debug_printf("clip_z = %u\n", key
->clip_z
);
1883 debug_printf("clip_user = %u\n", key
->clip_user
);
1884 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
1885 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
1886 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
1887 debug_printf("has_gs = %u\n", key
->has_gs
);
1888 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
1890 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
1891 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
1892 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
1893 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
1894 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
1897 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1898 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
1904 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1905 unsigned shader_stage
,
1907 uint32_t width
, uint32_t height
, uint32_t depth
,
1908 uint32_t first_level
, uint32_t last_level
,
1909 const void *base_ptr
,
1910 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1911 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1912 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
1915 struct draw_jit_texture
*jit_tex
;
1917 assert(shader_stage
== PIPE_SHADER_VERTEX
||
1918 shader_stage
== PIPE_SHADER_GEOMETRY
);
1920 if (shader_stage
== PIPE_SHADER_VERTEX
) {
1921 assert(sview_idx
< Elements(draw
->llvm
->jit_context
.textures
));
1923 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
1924 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
1925 assert(sview_idx
< Elements(draw
->llvm
->gs_jit_context
.textures
));
1927 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
1933 jit_tex
->width
= width
;
1934 jit_tex
->height
= height
;
1935 jit_tex
->depth
= depth
;
1936 jit_tex
->first_level
= first_level
;
1937 jit_tex
->last_level
= last_level
;
1938 jit_tex
->base
= base_ptr
;
1940 for (j
= first_level
; j
<= last_level
; j
++) {
1941 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
1942 jit_tex
->row_stride
[j
] = row_stride
[j
];
1943 jit_tex
->img_stride
[j
] = img_stride
[j
];
1949 draw_llvm_set_sampler_state(struct draw_context
*draw
,
1950 unsigned shader_type
)
1954 if (shader_type
== PIPE_SHADER_VERTEX
) {
1955 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
1956 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
1958 if (draw
->samplers
[i
]) {
1959 const struct pipe_sampler_state
*s
1960 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
1961 jit_sam
->min_lod
= s
->min_lod
;
1962 jit_sam
->max_lod
= s
->max_lod
;
1963 jit_sam
->lod_bias
= s
->lod_bias
;
1964 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1967 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
1968 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
1969 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
1971 if (draw
->samplers
[i
]) {
1972 const struct pipe_sampler_state
*s
1973 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
1974 jit_sam
->min_lod
= s
->min_lod
;
1975 jit_sam
->max_lod
= s
->max_lod
;
1976 jit_sam
->lod_bias
= s
->lod_bias
;
1977 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1985 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
1987 struct draw_llvm
*llvm
= variant
->llvm
;
1989 if (variant
->function_elts
) {
1990 gallivm_free_function(variant
->gallivm
,
1991 variant
->function_elts
, variant
->jit_func_elts
);
1994 if (variant
->function
) {
1995 gallivm_free_function(variant
->gallivm
,
1996 variant
->function
, variant
->jit_func
);
1999 gallivm_destroy(variant
->gallivm
);
2001 remove_from_list(&variant
->list_item_local
);
2002 variant
->shader
->variants_cached
--;
2003 remove_from_list(&variant
->list_item_global
);
2004 llvm
->nr_variants
--;
2010 * Create LLVM types for various structures.
2013 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
2015 struct gallivm_state
*gallivm
= var
->gallivm
;
2016 LLVMTypeRef texture_type
, sampler_type
, context_type
;
2018 texture_type
= create_jit_texture_type(gallivm
, "texture");
2019 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2021 context_type
= create_gs_jit_context_type(gallivm
,
2022 var
->shader
->base
.vector_length
,
2023 texture_type
, sampler_type
,
2024 "draw_gs_jit_context");
2025 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2027 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
2031 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
2033 if (!variant
->context_ptr_type
)
2034 create_gs_jit_types(variant
);
2035 return variant
->context_ptr_type
;
2039 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2040 struct lp_type gs_type
)
2042 struct gallivm_state
*gallivm
= variant
->gallivm
;
2043 LLVMBuilderRef builder
= gallivm
->builder
;
2044 LLVMValueRef bits
[16];
2045 struct lp_type mask_type
= lp_int_type(gs_type
);
2046 struct lp_type mask_elem_type
= lp_elem_type(mask_type
);
2047 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
,
2052 assert(gs_type
.length
<= Elements(bits
));
2054 for (i
= gs_type
.length
; i
>= 1; --i
) {
2056 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
2057 bits
[idx
] = lp_build_compare(gallivm
,
2058 mask_elem_type
, PIPE_FUNC_GEQUAL
,
2059 variant
->num_prims
, ind
);
2061 for (i
= 0; i
< gs_type
.length
; ++i
) {
2062 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
2063 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, bits
[i
], ind
, "");
2065 mask_val
= lp_build_compare(gallivm
,
2066 mask_type
, PIPE_FUNC_NOTEQUAL
,
2068 lp_build_const_int_vec(gallivm
, mask_type
, 0));
2074 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2075 struct draw_gs_llvm_variant
*variant
)
2077 struct gallivm_state
*gallivm
= variant
->gallivm
;
2078 LLVMContextRef context
= gallivm
->context
;
2079 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2080 LLVMTypeRef arg_types
[6];
2081 LLVMTypeRef func_type
;
2082 LLVMValueRef variant_func
;
2083 LLVMValueRef context_ptr
;
2084 LLVMValueRef prim_id_ptr
;
2085 LLVMBasicBlockRef block
;
2086 LLVMBuilderRef builder
;
2087 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2088 struct lp_build_sampler_soa
*sampler
= 0;
2089 struct lp_build_context bld
;
2090 struct lp_bld_tgsi_system_values system_values
;
2091 struct lp_type gs_type
;
2093 struct draw_gs_llvm_iface gs_iface
;
2094 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2095 LLVMValueRef consts_ptr
;
2096 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2097 struct lp_build_mask_context mask
;
2098 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2099 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2101 memset(&system_values
, 0, sizeof(system_values
));
2103 assert(variant
->vertex_header_ptr_type
);
2105 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2106 arg_types
[1] = variant
->input_array_type
; /* input */
2107 arg_types
[2] = variant
->vertex_header_ptr_type
; /* vertex_header */
2108 arg_types
[3] = int32_type
; /* num_prims */
2109 arg_types
[4] = int32_type
; /* instance_id */
2110 arg_types
[5] = LLVMPointerType(
2111 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2113 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
2115 variant_func
= LLVMAddFunction(gallivm
->module
, "draw_geometry_shader",
2117 variant
->function
= variant_func
;
2119 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2121 for (i
= 0; i
< Elements(arg_types
); ++i
)
2122 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2123 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
2124 LLVMNoAliasAttribute
);
2126 context_ptr
= LLVMGetParam(variant_func
, 0);
2127 input_array
= LLVMGetParam(variant_func
, 1);
2128 io_ptr
= LLVMGetParam(variant_func
, 2);
2129 num_prims
= LLVMGetParam(variant_func
, 3);
2130 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2131 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2133 lp_build_name(context_ptr
, "context");
2134 lp_build_name(input_array
, "input");
2135 lp_build_name(io_ptr
, "io");
2136 lp_build_name(num_prims
, "num_prims");
2137 lp_build_name(system_values
.instance_id
, "instance_id");
2138 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2140 variant
->context_ptr
= context_ptr
;
2141 variant
->io_ptr
= io_ptr
;
2142 variant
->num_prims
= num_prims
;
2144 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2145 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2146 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2147 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2148 gs_iface
.input
= input_array
;
2149 gs_iface
.variant
= variant
;
2155 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2156 builder
= gallivm
->builder
;
2157 LLVMPositionBuilderAtEnd(builder
, block
);
2159 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2161 memset(&gs_type
, 0, sizeof gs_type
);
2162 gs_type
.floating
= TRUE
; /* floating point values */
2163 gs_type
.sign
= TRUE
; /* values are signed */
2164 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2165 gs_type
.width
= 32; /* 32-bit float */
2166 gs_type
.length
= vector_length
;
2168 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2170 /* code generated texture sampling */
2171 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
,
2174 mask_val
= generate_mask_value(variant
, gs_type
);
2175 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2177 if (gs_info
->uses_primid
) {
2178 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");;
2181 lp_build_tgsi_soa(variant
->gallivm
,
2190 &llvm
->draw
->gs
.geometry_shader
->info
,
2191 (const struct lp_build_tgsi_gs_iface
*)&gs_iface
);
2193 sampler
->destroy(sampler
);
2195 lp_build_mask_end(&mask
);
2197 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2199 gallivm_verify_function(gallivm
, variant_func
);
2203 struct draw_gs_llvm_variant
*
2204 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2205 unsigned num_outputs
,
2206 const struct draw_gs_llvm_variant_key
*key
)
2208 struct draw_gs_llvm_variant
*variant
;
2209 struct llvm_geometry_shader
*shader
=
2210 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2211 LLVMTypeRef vertex_header
;
2213 variant
= MALLOC(sizeof *variant
+
2214 shader
->variant_key_size
-
2215 sizeof variant
->key
);
2216 if (variant
== NULL
)
2219 variant
->llvm
= llvm
;
2220 variant
->shader
= shader
;
2222 variant
->gallivm
= gallivm_create();
2224 create_gs_jit_types(variant
);
2226 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2228 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2230 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2232 draw_gs_llvm_generate(llvm
, variant
);
2234 gallivm_compile_module(variant
->gallivm
);
2236 variant
->jit_func
= (draw_gs_jit_func
)
2237 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2239 variant
->list_item_global
.base
= variant
;
2240 variant
->list_item_local
.base
= variant
;
2241 /*variant->no = */shader
->variants_created
++;
2242 variant
->list_item_global
.base
= variant
;
2248 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2250 struct draw_llvm
*llvm
= variant
->llvm
;
2252 if (variant
->function
) {
2253 gallivm_free_function(variant
->gallivm
,
2254 variant
->function
, variant
->jit_func
);
2257 gallivm_destroy(variant
->gallivm
);
2259 remove_from_list(&variant
->list_item_local
);
2260 variant
->shader
->variants_cached
--;
2261 remove_from_list(&variant
->list_item_global
);
2262 llvm
->nr_gs_variants
--;
2266 struct draw_gs_llvm_variant_key
*
2267 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2270 struct draw_gs_llvm_variant_key
*key
;
2271 struct draw_sampler_static_state
*draw_sampler
;
2273 key
= (struct draw_gs_llvm_variant_key
*)store
;
2275 /* All variants of this shader will have the same value for
2276 * nr_samplers. Not yet trying to compact away holes in the
2279 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2280 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2281 key
->nr_sampler_views
=
2282 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2285 key
->nr_sampler_views
= key
->nr_samplers
;
2288 draw_sampler
= key
->samplers
;
2290 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2292 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2293 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2294 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2296 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2297 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2298 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2305 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2308 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2310 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2311 debug_printf("sampler[%i].src_format = %s\n", i
,
2312 util_format_name(sampler
[i
].texture_state
.format
));