1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "draw_llvm.h"
30 #include "draw_context.h"
34 #include "gallivm/lp_bld_arit.h"
35 #include "gallivm/lp_bld_arit_overflow.h"
36 #include "gallivm/lp_bld_logic.h"
37 #include "gallivm/lp_bld_const.h"
38 #include "gallivm/lp_bld_swizzle.h"
39 #include "gallivm/lp_bld_struct.h"
40 #include "gallivm/lp_bld_type.h"
41 #include "gallivm/lp_bld_flow.h"
42 #include "gallivm/lp_bld_debug.h"
43 #include "gallivm/lp_bld_tgsi.h"
44 #include "gallivm/lp_bld_printf.h"
45 #include "gallivm/lp_bld_intr.h"
46 #include "gallivm/lp_bld_init.h"
47 #include "gallivm/lp_bld_type.h"
48 #include "gallivm/lp_bld_pack.h"
49 #include "gallivm/lp_bld_format.h"
51 #include "tgsi/tgsi_exec.h"
52 #include "tgsi/tgsi_dump.h"
54 #include "util/u_math.h"
55 #include "util/u_pointer.h"
56 #include "util/u_string.h"
57 #include "util/u_simple_list.h"
64 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
,
68 struct draw_gs_llvm_iface
{
69 struct lp_build_tgsi_gs_iface base
;
71 struct draw_gs_llvm_variant
*variant
;
75 static INLINE
const struct draw_gs_llvm_iface
*
76 draw_gs_llvm_iface(const struct lp_build_tgsi_gs_iface
*iface
)
78 return (const struct draw_gs_llvm_iface
*)iface
;
82 * Create LLVM type for draw_vertex_buffer.
85 create_jit_dvbuffer_type(struct gallivm_state
*gallivm
,
86 const char *struct_name
)
88 LLVMTargetDataRef target
= gallivm
->target
;
89 LLVMTypeRef dvbuffer_type
;
90 LLVMTypeRef elem_types
[DRAW_JIT_DVBUFFER_NUM_FIELDS
];
91 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
93 elem_types
[DRAW_JIT_DVBUFFER_MAP
] =
94 LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
95 elem_types
[DRAW_JIT_DVBUFFER_SIZE
] = int32_type
;
97 dvbuffer_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
98 Elements(elem_types
), 0);
100 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, map
,
101 target
, dvbuffer_type
,
102 DRAW_JIT_DVBUFFER_MAP
);
103 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, size
,
104 target
, dvbuffer_type
,
105 DRAW_JIT_DVBUFFER_SIZE
);
107 return dvbuffer_type
;
111 * Create LLVM type for struct draw_jit_texture
114 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
116 LLVMTargetDataRef target
= gallivm
->target
;
117 LLVMTypeRef texture_type
;
118 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
119 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
121 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
122 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
123 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
124 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
125 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
126 elem_types
[DRAW_JIT_TEXTURE_BASE
] =
127 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
128 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
129 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
130 elem_types
[DRAW_JIT_TEXTURE_MIP_OFFSETS
] =
131 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
133 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
134 Elements(elem_types
), 0);
136 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
137 target
, texture_type
,
138 DRAW_JIT_TEXTURE_WIDTH
);
139 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
140 target
, texture_type
,
141 DRAW_JIT_TEXTURE_HEIGHT
);
142 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
143 target
, texture_type
,
144 DRAW_JIT_TEXTURE_DEPTH
);
145 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
146 target
, texture_type
,
147 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
148 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
149 target
, texture_type
,
150 DRAW_JIT_TEXTURE_LAST_LEVEL
);
151 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, base
,
152 target
, texture_type
,
153 DRAW_JIT_TEXTURE_BASE
);
154 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
155 target
, texture_type
,
156 DRAW_JIT_TEXTURE_ROW_STRIDE
);
157 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
158 target
, texture_type
,
159 DRAW_JIT_TEXTURE_IMG_STRIDE
);
160 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, mip_offsets
,
161 target
, texture_type
,
162 DRAW_JIT_TEXTURE_MIP_OFFSETS
);
164 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
171 * Create LLVM type for struct draw_jit_sampler
174 create_jit_sampler_type(struct gallivm_state
*gallivm
, const char *struct_name
)
176 LLVMTargetDataRef target
= gallivm
->target
;
177 LLVMTypeRef sampler_type
;
178 LLVMTypeRef elem_types
[DRAW_JIT_SAMPLER_NUM_FIELDS
];
180 elem_types
[DRAW_JIT_SAMPLER_MIN_LOD
] =
181 elem_types
[DRAW_JIT_SAMPLER_MAX_LOD
] =
182 elem_types
[DRAW_JIT_SAMPLER_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
183 elem_types
[DRAW_JIT_SAMPLER_BORDER_COLOR
] =
184 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
186 sampler_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
187 Elements(elem_types
), 0);
189 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, min_lod
,
190 target
, sampler_type
,
191 DRAW_JIT_SAMPLER_MIN_LOD
);
192 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, max_lod
,
193 target
, sampler_type
,
194 DRAW_JIT_SAMPLER_MAX_LOD
);
195 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, lod_bias
,
196 target
, sampler_type
,
197 DRAW_JIT_SAMPLER_LOD_BIAS
);
198 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, border_color
,
199 target
, sampler_type
,
200 DRAW_JIT_SAMPLER_BORDER_COLOR
);
202 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler
, target
, sampler_type
);
209 * Create LLVM type for struct draw_jit_context
212 create_jit_context_type(struct gallivm_state
*gallivm
,
213 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
214 const char *struct_name
)
216 LLVMTargetDataRef target
= gallivm
->target
;
217 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
218 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
219 LLVMTypeRef elem_types
[DRAW_JIT_CTX_NUM_FIELDS
];
220 LLVMTypeRef context_type
;
222 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
223 LP_MAX_TGSI_CONST_BUFFERS
);
224 elem_types
[1] = LLVMArrayType(int_type
, /* num_vs_constants */
225 LP_MAX_TGSI_CONST_BUFFERS
);
226 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
227 DRAW_TOTAL_CLIP_PLANES
), 0);
228 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewport */
229 elem_types
[4] = LLVMArrayType(texture_type
,
230 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
231 elem_types
[5] = LLVMArrayType(sampler_type
,
232 PIPE_MAX_SAMPLERS
); /* samplers */
233 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
234 Elements(elem_types
), 0);
235 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
236 target
, context_type
, DRAW_JIT_CTX_CONSTANTS
);
237 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, num_vs_constants
,
238 target
, context_type
, DRAW_JIT_CTX_NUM_CONSTANTS
);
239 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
240 target
, context_type
, DRAW_JIT_CTX_PLANES
);
241 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewport
,
242 target
, context_type
, DRAW_JIT_CTX_VIEWPORT
);
243 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
244 target
, context_type
,
245 DRAW_JIT_CTX_TEXTURES
);
246 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
247 target
, context_type
,
248 DRAW_JIT_CTX_SAMPLERS
);
249 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
250 target
, context_type
);
257 * Create LLVM type for struct draw_gs_jit_context
260 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
261 unsigned vector_length
,
262 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
263 const char *struct_name
)
265 LLVMTargetDataRef target
= gallivm
->target
;
266 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
267 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
268 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
269 LLVMTypeRef context_type
;
271 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
272 LP_MAX_TGSI_CONST_BUFFERS
);
273 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
274 LP_MAX_TGSI_CONST_BUFFERS
);
275 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
276 DRAW_TOTAL_CLIP_PLANES
), 0);
277 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewport */
279 elem_types
[4] = LLVMArrayType(texture_type
,
280 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
281 elem_types
[5] = LLVMArrayType(sampler_type
,
282 PIPE_MAX_SAMPLERS
); /* samplers */
284 elem_types
[6] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
285 elem_types
[7] = LLVMPointerType(LLVMVectorType(int_type
,
287 elem_types
[8] = LLVMPointerType(LLVMVectorType(int_type
,
290 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
291 Elements(elem_types
), 0);
293 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
294 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
295 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, num_constants
,
296 target
, context_type
, DRAW_GS_JIT_CTX_NUM_CONSTANTS
);
297 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
298 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
299 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewport
,
300 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
301 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
302 target
, context_type
,
303 DRAW_GS_JIT_CTX_TEXTURES
);
304 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
305 target
, context_type
,
306 DRAW_GS_JIT_CTX_SAMPLERS
);
307 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
308 target
, context_type
,
309 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
310 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
311 target
, context_type
,
312 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
313 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
314 target
, context_type
,
315 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
316 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
317 target
, context_type
);
324 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
326 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
327 LLVMTypeRef input_array
;
329 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
330 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
331 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
332 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
338 * Create LLVM type for struct pipe_vertex_buffer
341 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
,
342 const char *struct_name
)
344 LLVMTargetDataRef target
= gallivm
->target
;
345 LLVMTypeRef elem_types
[4];
349 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
351 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
353 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
354 Elements(elem_types
), 0);
356 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
358 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
361 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
368 * Create LLVM type for struct vertex_header;
371 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
373 LLVMTargetDataRef target
= gallivm
->target
;
374 LLVMTypeRef elem_types
[4];
375 LLVMTypeRef vertex_header
;
376 char struct_name
[24];
378 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
380 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
381 elem_types
[DRAW_JIT_VERTEX_CLIP
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
382 elem_types
[DRAW_JIT_VERTEX_PRE_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
383 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
385 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
386 Elements(elem_types
), 0);
388 /* these are bit-fields and we can't take address of them
389 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
390 target, vertex_header,
391 DRAW_JIT_VERTEX_CLIPMASK);
392 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
393 target, vertex_header,
394 DRAW_JIT_VERTEX_EDGEFLAG);
395 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
396 target, vertex_header,
397 DRAW_JIT_VERTEX_PAD);
398 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
399 target, vertex_header,
400 DRAW_JIT_VERTEX_VERTEX_ID);
402 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
403 target
, vertex_header
,
404 DRAW_JIT_VERTEX_CLIP
);
405 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, pre_clip_pos
,
406 target
, vertex_header
,
407 DRAW_JIT_VERTEX_PRE_CLIP_POS
);
408 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
409 target
, vertex_header
,
410 DRAW_JIT_VERTEX_DATA
);
412 assert(LLVMABISizeOfType(target
, vertex_header
) ==
413 offsetof(struct vertex_header
, data
[data_elems
]));
415 return vertex_header
;
420 * Create LLVM types for various structures.
423 create_jit_types(struct draw_llvm_variant
*variant
)
425 struct gallivm_state
*gallivm
= variant
->gallivm
;
426 LLVMTypeRef texture_type
, sampler_type
, context_type
, buffer_type
,
429 texture_type
= create_jit_texture_type(gallivm
, "texture");
430 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
432 context_type
= create_jit_context_type(gallivm
, texture_type
, sampler_type
,
434 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
436 buffer_type
= create_jit_dvbuffer_type(gallivm
, "draw_vertex_buffer");
437 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
439 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
440 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
445 get_context_ptr_type(struct draw_llvm_variant
*variant
)
447 if (!variant
->context_ptr_type
)
448 create_jit_types(variant
);
449 return variant
->context_ptr_type
;
454 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
456 if (!variant
->buffer_ptr_type
)
457 create_jit_types(variant
);
458 return variant
->buffer_ptr_type
;
463 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
465 if (!variant
->vb_ptr_type
)
466 create_jit_types(variant
);
467 return variant
->vb_ptr_type
;
471 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
473 if (!variant
->vertex_header_ptr_type
)
474 create_jit_types(variant
);
475 return variant
->vertex_header_ptr_type
;
480 * Create per-context LLVM info.
483 draw_llvm_create(struct draw_context
*draw
)
485 struct draw_llvm
*llvm
;
487 llvm
= CALLOC_STRUCT( draw_llvm
);
495 llvm
->nr_variants
= 0;
496 make_empty_list(&llvm
->vs_variants_list
);
498 llvm
->nr_gs_variants
= 0;
499 make_empty_list(&llvm
->gs_variants_list
);
506 * Free per-context LLVM info.
509 draw_llvm_destroy(struct draw_llvm
*llvm
)
511 /* XXX free other draw_llvm data? */
517 * Create LLVM-generated code for a vertex shader.
519 struct draw_llvm_variant
*
520 draw_llvm_create_variant(struct draw_llvm
*llvm
,
522 const struct draw_llvm_variant_key
*key
)
524 struct draw_llvm_variant
*variant
;
525 struct llvm_vertex_shader
*shader
=
526 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
527 LLVMTypeRef vertex_header
;
529 variant
= MALLOC(sizeof *variant
+
530 shader
->variant_key_size
-
531 sizeof variant
->key
);
535 variant
->llvm
= llvm
;
537 variant
->gallivm
= gallivm_create();
539 create_jit_types(variant
);
541 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
543 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
545 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
547 draw_llvm_generate(llvm
, variant
, FALSE
); /* linear */
548 draw_llvm_generate(llvm
, variant
, TRUE
); /* elts */
550 gallivm_compile_module(variant
->gallivm
);
552 variant
->jit_func
= (draw_jit_vert_func
)
553 gallivm_jit_function(variant
->gallivm
, variant
->function
);
555 variant
->jit_func_elts
= (draw_jit_vert_func_elts
)
556 gallivm_jit_function(variant
->gallivm
, variant
->function_elts
);
558 gallivm_free_ir(variant
->gallivm
);
560 variant
->shader
= shader
;
561 variant
->list_item_global
.base
= variant
;
562 variant
->list_item_local
.base
= variant
;
563 /*variant->no = */shader
->variants_created
++;
564 variant
->list_item_global
.base
= variant
;
571 generate_vs(struct draw_llvm_variant
*variant
,
572 LLVMBuilderRef builder
,
573 struct lp_type vs_type
,
574 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
575 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
576 const struct lp_bld_tgsi_system_values
*system_values
,
577 LLVMValueRef context_ptr
,
578 struct lp_build_sampler_soa
*draw_sampler
,
579 boolean clamp_vertex_color
)
581 struct draw_llvm
*llvm
= variant
->llvm
;
582 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
583 LLVMValueRef consts_ptr
=
584 draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
585 LLVMValueRef num_consts_ptr
=
586 draw_jit_context_num_vs_constants(variant
->gallivm
, context_ptr
);
587 struct lp_build_sampler_soa
*sampler
= 0;
589 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
590 tgsi_dump(tokens
, 0);
591 draw_llvm_dump_variant_key(&variant
->key
);
594 if (llvm
->draw
->num_sampler_views
&& llvm
->draw
->num_samplers
)
595 sampler
= draw_sampler
;
597 lp_build_tgsi_soa(variant
->gallivm
,
600 NULL
/*struct lp_build_mask_context *mask*/,
607 &llvm
->draw
->vs
.vertex_shader
->info
,
612 unsigned chan
, attrib
;
613 struct lp_build_context bld
;
614 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
615 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
617 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
618 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
619 if (outputs
[attrib
][chan
]) {
620 switch (info
->output_semantic_name
[attrib
]) {
621 case TGSI_SEMANTIC_COLOR
:
622 case TGSI_SEMANTIC_BCOLOR
:
623 if (clamp_vertex_color
) {
624 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
625 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
626 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
637 generate_fetch(struct gallivm_state
*gallivm
,
638 struct draw_context
*draw
,
639 LLVMValueRef vbuffers_ptr
,
641 struct pipe_vertex_element
*velem
,
644 LLVMValueRef instance_id
)
646 const struct util_format_description
*format_desc
=
647 util_format_description(velem
->src_format
);
648 LLVMValueRef zero
= LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
));
649 LLVMBuilderRef builder
= gallivm
->builder
;
650 LLVMValueRef indices
=
651 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
652 velem
->vertex_buffer_index
, 0);
653 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
655 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
656 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
657 LLVMValueRef map_ptr
= draw_jit_dvbuffer_map(gallivm
, vbuffer_ptr
);
658 LLVMValueRef buffer_size
= draw_jit_dvbuffer_size(gallivm
, vbuffer_ptr
);
660 LLVMValueRef buffer_overflowed
;
661 LLVMValueRef needed_buffer_size
;
662 LLVMValueRef temp_ptr
=
663 lp_build_alloca(gallivm
,
664 lp_build_vec_type(gallivm
, lp_float32_vec4_type()), "");
665 LLVMValueRef ofbit
= NULL
;
666 struct lp_build_if_state if_ctx
;
668 if (velem
->instance_divisor
) {
669 /* Index is equal to the start instance plus the number of current
670 * instance divided by the divisor. In this case we compute it as:
671 * index = start_instance + (instance_id / divisor)
673 LLVMValueRef current_instance
;
674 index
= lp_build_const_int32(gallivm
, draw
->start_instance
);
675 current_instance
= LLVMBuildUDiv(builder
, instance_id
,
676 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
678 index
= lp_build_uadd_overflow(gallivm
, index
, current_instance
, &ofbit
);
681 stride
= lp_build_umul_overflow(gallivm
, vb_stride
, index
, &ofbit
);
682 stride
= lp_build_uadd_overflow(gallivm
, stride
, vb_buffer_offset
, &ofbit
);
683 stride
= lp_build_uadd_overflow(
685 lp_build_const_int32(gallivm
, velem
->src_offset
), &ofbit
);
686 needed_buffer_size
= lp_build_uadd_overflow(
688 lp_build_const_int32(gallivm
,
689 util_format_get_blocksize(velem
->src_format
)),
692 buffer_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
693 needed_buffer_size
, buffer_size
,
694 "buffer_overflowed");
695 buffer_overflowed
= LLVMBuildOr(builder
, buffer_overflowed
, ofbit
, "");
697 lp_build_printf(gallivm
, "vbuf index = %u, vb_stride is %u\n",
699 lp_build_printf(gallivm
, " vb_buffer_offset = %u, src_offset is %u\n",
701 lp_build_const_int32(gallivm
, velem
->src_offset
));
702 lp_build_print_value(gallivm
, " blocksize = ",
703 lp_build_const_int32(
705 util_format_get_blocksize(velem
->src_format
)));
706 lp_build_printf(gallivm
, " instance_id = %u\n", instance_id
);
707 lp_build_printf(gallivm
, " stride = %u\n", stride
);
708 lp_build_printf(gallivm
, " buffer size = %u\n", buffer_size
);
709 lp_build_printf(gallivm
, " needed_buffer_size = %u\n", needed_buffer_size
);
710 lp_build_print_value(gallivm
, " buffer overflowed = ", buffer_overflowed
);
713 lp_build_if(&if_ctx
, gallivm
, buffer_overflowed
);
716 lp_build_const_vec(gallivm
, lp_float32_vec4_type(), 0);
717 LLVMBuildStore(builder
, val
, temp_ptr
);
719 lp_build_else(&if_ctx
);
722 map_ptr
= LLVMBuildGEP(builder
, map_ptr
, &stride
, 1, "");
724 val
= lp_build_fetch_rgba_aos(gallivm
,
726 lp_float32_vec4_type(),
729 LLVMBuildStore(builder
, val
, temp_ptr
);
731 lp_build_endif(&if_ctx
);
733 *res
= LLVMBuildLoad(builder
, temp_ptr
, "aos");
737 convert_to_soa(struct gallivm_state
*gallivm
,
738 LLVMValueRef (*src_aos
)[LP_MAX_VECTOR_WIDTH
/ 32],
739 LLVMValueRef (*dst_soa
)[TGSI_NUM_CHANNELS
],
740 unsigned num_attribs
, const struct lp_type soa_type
)
743 struct lp_type aos_channel_type
= soa_type
;
745 debug_assert(TGSI_NUM_CHANNELS
== 4);
746 debug_assert((soa_type
.length
% TGSI_NUM_CHANNELS
) == 0);
748 aos_channel_type
.length
>>= 1;
750 for (i
= 0; i
< num_attribs
; ++i
) {
751 LLVMValueRef aos_channels
[TGSI_NUM_CHANNELS
];
752 unsigned pixels_per_channel
= soa_type
.length
/ TGSI_NUM_CHANNELS
;
754 for (j
= 0; j
< TGSI_NUM_CHANNELS
; ++j
) {
755 LLVMValueRef channel
[LP_MAX_VECTOR_LENGTH
] = { 0 };
757 assert(pixels_per_channel
<= LP_MAX_VECTOR_LENGTH
);
759 for (k
= 0; k
< pixels_per_channel
; ++k
) {
760 channel
[k
] = src_aos
[i
][j
+ TGSI_NUM_CHANNELS
* k
];
763 aos_channels
[j
] = lp_build_concat(gallivm
, channel
, aos_channel_type
, pixels_per_channel
);
766 lp_build_transpose_aos(gallivm
, soa_type
, aos_channels
, dst_soa
[i
]);
772 store_aos(struct gallivm_state
*gallivm
,
777 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
778 LLVMBuilderRef builder
= gallivm
->builder
;
779 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
780 LLVMValueRef indices
[3];
782 indices
[0] = lp_build_const_int32(gallivm
, 0);
784 indices
[2] = lp_build_const_int32(gallivm
, 0);
786 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
787 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
790 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
793 /* Unaligned store due to the vertex header */
794 lp_set_store_alignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
798 * Adjust the mask to architecture endianess. The mask will the store in struct:
800 * struct vertex_header {
801 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
802 * unsigned edgeflag:1;
803 * unsigned have_clipdist:1;
804 * unsigned vertex_id:16;
808 * On little-endian machine nothing needs to done, however on bit-endian machine
809 * the mask's fields need to be adjusted with the algorithm:
811 * uint32_t reverse (uint32_t x)
813 * return (x >> 16) | // vertex_id
814 * ((x & 0x3fff) << 18) | // clipmask
815 * ((x & 0x4000) << 3) | // have_clipdist
816 * ((x & 0x8000) << 1); // edgeflag
820 adjust_mask(struct gallivm_state
*gallivm
,
823 #ifdef PIPE_ARCH_BIG_ENDIAN
824 LLVMBuilderRef builder
= gallivm
->builder
;
825 LLVMValueRef vertex_id
;
826 LLVMValueRef clipmask
;
827 LLVMValueRef have_clipdist
;
828 LLVMValueRef edgeflag
;
830 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
831 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
832 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
833 have_clipdist
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
834 have_clipdist
= LLVMBuildShl(builder
, have_clipdist
, lp_build_const_int32(gallivm
, 3), "");
835 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
836 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
838 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
839 mask
= LLVMBuildOr(builder
, mask
, have_clipdist
, "");
840 mask
= LLVMBuildOr(builder
, mask
, edgeflag
, "");
846 store_aos_array(struct gallivm_state
*gallivm
,
847 struct lp_type soa_type
,
849 LLVMValueRef
*indices
,
853 LLVMValueRef clipmask
,
854 boolean have_clipdist
)
856 LLVMBuilderRef builder
= gallivm
->builder
;
857 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
858 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
859 LLVMValueRef linear_inds
[LP_MAX_VECTOR_WIDTH
/ 32];
860 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
861 int vector_length
= soa_type
.length
;
864 debug_assert(TGSI_NUM_CHANNELS
== 4);
866 for (i
= 0; i
< vector_length
; i
++) {
867 linear_inds
[i
] = lp_build_const_int32(gallivm
, i
);
869 inds
[i
] = indices
[i
];
871 inds
[i
] = linear_inds
[i
];
873 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
877 /* store vertex header for each of the n vertices */
878 LLVMValueRef val
, cliptmp
;
879 int vertex_id_pad_edgeflag
;
881 /* If this assertion fails, it means we need to update the bit twidding
882 * code here. See struct vertex_header in draw_private.h.
884 assert(DRAW_TOTAL_CLIP_PLANES
==14);
885 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
886 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
888 vertex_id_pad_edgeflag
|= 1 << (DRAW_TOTAL_CLIP_PLANES
+1);
889 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
), vertex_id_pad_edgeflag
);
890 /* OR with the clipmask */
891 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
892 for (i
= 0; i
< vector_length
; i
++) {
893 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
894 val
= LLVMBuildExtractElement(builder
, cliptmp
, linear_inds
[i
], "");
895 val
= adjust_mask(gallivm
, val
);
897 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
898 io_ptrs
[i
], inds
[i
], val
);
900 LLVMBuildStore(builder
, val
, id_ptr
);
904 /* store for each of the n vertices */
905 for (i
= 0; i
< vector_length
; i
++) {
906 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
912 convert_to_aos(struct gallivm_state
*gallivm
,
914 LLVMValueRef
*indices
,
915 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
916 LLVMValueRef clipmask
,
918 struct lp_type soa_type
,
919 boolean have_clipdist
)
921 LLVMBuilderRef builder
= gallivm
->builder
;
922 unsigned chan
, attrib
, i
;
925 lp_build_printf(gallivm
, " # storing begin\n");
927 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
928 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
929 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
930 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
931 if (outputs
[attrib
][chan
]) {
932 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
933 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
935 lp_build_printf(gallivm
, "output %d : %d ",
936 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
938 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
940 lp_build_print_value(gallivm
, "val = ", out
);
943 LLVMBuildBitCast(builder
, out
, lp_build_int_vec_type(gallivm
, soa_type
), "");
945 lp_build_print_value(gallivm
, " ival = ", iv
);
956 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
957 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
959 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
961 for (i
= 0; i
< soa_type
.length
; ++i
) {
962 aos
[i
] = lp_build_extract_range(gallivm
,
963 soa
[i
% TGSI_NUM_CHANNELS
],
964 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
969 store_aos_array(gallivm
,
975 clipmask
, have_clipdist
);
978 lp_build_printf(gallivm
, " # storing end\n");
984 * Stores original vertex positions in clip coordinates
987 store_clip(struct gallivm_state
*gallivm
,
988 const struct lp_type vs_type
,
990 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
991 boolean pre_clip_pos
, int idx
)
993 LLVMBuilderRef builder
= gallivm
->builder
;
995 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
996 LLVMValueRef indices
[2];
997 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
998 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
999 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1000 LLVMTypeRef clip_ptr_type
=
1001 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
),
1006 indices
[1] = lp_build_const_int32(gallivm
, 0);
1008 for (i
= 0; i
< vs_type
.length
; i
++) {
1009 inds
[i
] = lp_build_const_int32(gallivm
, i
);
1010 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1013 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
1014 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
1015 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
1016 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
1018 if (!pre_clip_pos
) {
1019 for (i
= 0; i
< vs_type
.length
; i
++) {
1020 clip_ptrs
[i
] = draw_jit_header_clip(gallivm
, io_ptrs
[i
]);
1023 for (i
= 0; i
< vs_type
.length
; i
++) {
1024 clip_ptrs
[i
] = draw_jit_header_pre_clip_pos(gallivm
, io_ptrs
[i
]);
1028 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
1029 for (i
= 0; i
< vs_type
.length
; ++i
) {
1030 aos
[i
] = lp_build_extract_range(gallivm
,
1031 soa
[i
% TGSI_NUM_CHANNELS
],
1032 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1036 for (j
= 0; j
< vs_type
.length
; j
++) {
1037 LLVMValueRef clip_ptr
;
1039 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
1040 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
1042 /* Unaligned store */
1043 lp_set_store_alignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
1049 * Transforms the outputs for viewport mapping
1052 generate_viewport(struct draw_llvm_variant
*variant
,
1053 LLVMBuilderRef builder
,
1054 struct lp_type vs_type
,
1055 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1056 LLVMValueRef context_ptr
)
1059 struct gallivm_state
*gallivm
= variant
->gallivm
;
1060 struct lp_type f32_type
= vs_type
;
1061 const unsigned pos
= variant
->llvm
->draw
->vs
.position_output
;
1062 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1063 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn*/
1064 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1065 LLVMValueRef vp_ptr
= draw_jit_context_viewport(gallivm
, context_ptr
);
1067 /* for 1/w convention*/
1068 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1069 LLVMBuildStore(builder
, out3
, outputs
[pos
][3]);
1071 /* Viewport Mapping */
1072 for (i
=0; i
<3; i
++) {
1073 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[pos
][i
], ""); /*x0 x1 .. xn*/
1076 LLVMValueRef scale_i
;
1077 LLVMValueRef trans_i
;
1080 index
= lp_build_const_int32(gallivm
, i
);
1081 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1083 index
= lp_build_const_int32(gallivm
, i
+4);
1084 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1086 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1087 LLVMBuildLoad(builder
, scale_i
, "scale"));
1088 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1089 LLVMBuildLoad(builder
, trans_i
, "trans"));
1092 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1094 out
= LLVMBuildFMul(builder
, out
, scale
, "");
1095 /* add translation */
1096 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
1098 /* store transformed outputs */
1099 LLVMBuildStore(builder
, out
, outputs
[pos
][i
]);
1106 * Returns clipmask as nxi32 bitmask for the n vertices
1109 generate_clipmask(struct draw_llvm
*llvm
,
1110 struct gallivm_state
*gallivm
,
1111 struct lp_type vs_type
,
1112 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1117 unsigned ucp_enable
,
1118 LLVMValueRef context_ptr
,
1119 boolean
*have_clipdist
)
1121 LLVMBuilderRef builder
= gallivm
->builder
;
1122 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1123 LLVMValueRef test
, temp
;
1124 LLVMValueRef zero
, shift
;
1125 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1126 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1127 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1128 struct lp_type f32_type
= vs_type
;
1129 struct lp_type i32_type
= lp_int_type(vs_type
);
1130 const unsigned pos
= llvm
->draw
->vs
.position_output
;
1131 const unsigned cv
= llvm
->draw
->vs
.clipvertex_output
;
1132 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1133 bool have_cd
= false;
1136 cd
[0] = llvm
->draw
->vs
.clipdistance_output
[0];
1137 cd
[1] = llvm
->draw
->vs
.clipdistance_output
[1];
1139 if (cd
[0] != pos
|| cd
[1] != pos
)
1142 if (num_written_clipdistance
&& !clip_user
) {
1144 ucp_enable
= (1 << num_written_clipdistance
) - 1;
1147 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1148 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1149 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1150 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1153 * load clipvertex and position from correct locations.
1154 * if they are the same just load them once.
1156 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1157 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1158 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1159 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1161 if (clip_user
&& cv
!= pos
) {
1162 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1163 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1164 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1165 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1173 /* Cliptest, for hardwired planes */
1176 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1178 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1182 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1183 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1184 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1185 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1186 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1189 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1190 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1191 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1192 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1195 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1196 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1197 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1198 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1199 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1203 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1206 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1207 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1208 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1212 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1213 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1214 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1215 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1218 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1219 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1220 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1221 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1225 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1226 LLVMValueRef indices
[3];
1227 LLVMValueRef is_nan_or_inf
;
1229 /* userclip planes */
1230 while (ucp_enable
) {
1231 unsigned plane_idx
= ffs(ucp_enable
)-1;
1232 ucp_enable
&= ~(1 << plane_idx
);
1235 if (have_cd
&& num_written_clipdistance
) {
1236 LLVMValueRef clipdist
;
1240 *have_clipdist
= TRUE
;
1242 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1244 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1246 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1247 is_nan_or_inf
= lp_build_is_inf_or_nan(gallivm
, vs_type
, clipdist
);
1248 test
= LLVMBuildOr(builder
, test
, is_nan_or_inf
, "");
1249 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1250 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1251 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1253 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1254 indices
[0] = lp_build_const_int32(gallivm
, 0);
1255 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1257 indices
[2] = lp_build_const_int32(gallivm
, 0);
1258 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1259 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1260 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1261 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1263 indices
[2] = lp_build_const_int32(gallivm
, 1);
1264 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1265 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1266 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1267 test
= LLVMBuildFMul(builder
, planes
, cv_y
, "");
1268 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1270 indices
[2] = lp_build_const_int32(gallivm
, 2);
1271 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1272 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1273 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1274 test
= LLVMBuildFMul(builder
, planes
, cv_z
, "");
1275 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1277 indices
[2] = lp_build_const_int32(gallivm
, 3);
1278 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1279 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1280 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1281 test
= LLVMBuildFMul(builder
, planes
, cv_w
, "");
1282 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1284 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1285 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1286 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1287 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1296 * Returns boolean if any clipping has occurred
1297 * Used zero/non-zero i32 value to represent boolean
1300 clipmask_booli32(struct gallivm_state
*gallivm
,
1301 const struct lp_type vs_type
,
1302 LLVMValueRef clipmask_bool_ptr
)
1304 LLVMBuilderRef builder
= gallivm
->builder
;
1305 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
1306 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1307 LLVMValueRef ret
= LLVMConstNull(int32_type
);
1312 * Can do this with log2(vector length) pack instructions and one extract
1313 * (as we don't actually need a or) with sse2 which would be way better.
1315 for (i
=0; i
< vs_type
.length
; i
++) {
1316 temp
= LLVMBuildExtractElement(builder
, clipmask_bool
,
1317 lp_build_const_int32(gallivm
, i
) , "");
1318 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1324 draw_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
1325 struct lp_build_tgsi_context
* bld_base
,
1326 boolean is_vindex_indirect
,
1327 LLVMValueRef vertex_index
,
1328 boolean is_aindex_indirect
,
1329 LLVMValueRef attrib_index
,
1330 LLVMValueRef swizzle_index
)
1332 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1333 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1334 LLVMBuilderRef builder
= gallivm
->builder
;
1335 LLVMValueRef indices
[3];
1337 struct lp_type type
= bld_base
->base
.type
;
1339 if (is_vindex_indirect
|| is_aindex_indirect
) {
1341 res
= bld_base
->base
.zero
;
1342 for (i
= 0; i
< type
.length
; ++i
) {
1343 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1344 LLVMValueRef vert_chan_index
= vertex_index
;
1345 LLVMValueRef attr_chan_index
= attrib_index
;
1346 LLVMValueRef channel_vec
, value
;
1348 if (is_vindex_indirect
) {
1349 vert_chan_index
= LLVMBuildExtractElement(builder
,
1350 vertex_index
, idx
, "");
1352 if (is_aindex_indirect
) {
1353 attr_chan_index
= LLVMBuildExtractElement(builder
,
1354 attrib_index
, idx
, "");
1357 indices
[0] = vert_chan_index
;
1358 indices
[1] = attr_chan_index
;
1359 indices
[2] = swizzle_index
;
1361 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1362 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1363 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1365 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1368 indices
[0] = vertex_index
;
1369 indices
[1] = attrib_index
;
1370 indices
[2] = swizzle_index
;
1372 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1373 res
= LLVMBuildLoad(builder
, res
, "");
1380 draw_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
1381 struct lp_build_tgsi_context
* bld_base
,
1382 LLVMValueRef (*outputs
)[4],
1383 LLVMValueRef emitted_vertices_vec
)
1385 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1386 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1387 struct gallivm_state
*gallivm
= variant
->gallivm
;
1388 LLVMBuilderRef builder
= gallivm
->builder
;
1389 struct lp_type gs_type
= bld_base
->base
.type
;
1390 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1391 lp_int_type(gs_type
), 0);
1392 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1393 LLVMValueRef next_prim_offset
=
1394 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1395 LLVMValueRef io
= variant
->io_ptr
;
1397 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1399 for (i
= 0; i
< gs_type
.length
; ++i
) {
1400 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1401 LLVMValueRef currently_emitted
=
1402 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1403 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1404 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1407 convert_to_aos(gallivm
, io
, indices
,
1409 gs_info
->num_outputs
, gs_type
,
1414 draw_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
1415 struct lp_build_tgsi_context
* bld_base
,
1416 LLVMValueRef verts_per_prim_vec
,
1417 LLVMValueRef emitted_prims_vec
)
1419 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1420 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1421 struct gallivm_state
*gallivm
= variant
->gallivm
;
1422 LLVMBuilderRef builder
= gallivm
->builder
;
1423 LLVMValueRef prim_lengts_ptr
=
1424 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1427 for (i
= 0; i
< bld_base
->base
.type
.length
; ++i
) {
1428 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1429 LLVMValueRef prims_emitted
=
1430 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1431 LLVMValueRef store_ptr
;
1432 LLVMValueRef num_vertices
=
1433 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1435 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1436 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1437 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1438 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1443 draw_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
1444 struct lp_build_tgsi_context
* bld_base
,
1445 LLVMValueRef total_emitted_vertices_vec
,
1446 LLVMValueRef emitted_prims_vec
)
1448 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1449 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1450 struct gallivm_state
*gallivm
= variant
->gallivm
;
1451 LLVMBuilderRef builder
= gallivm
->builder
;
1452 LLVMValueRef emitted_verts_ptr
=
1453 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1454 LLVMValueRef emitted_prims_ptr
=
1455 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1456 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1458 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &zero
, 0, "");
1459 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &zero
, 0, "");
1461 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1462 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1466 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1469 struct gallivm_state
*gallivm
= variant
->gallivm
;
1470 LLVMContextRef context
= gallivm
->context
;
1471 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1472 LLVMTypeRef arg_types
[10];
1473 unsigned num_arg_types
=
1474 elts
? Elements(arg_types
) : Elements(arg_types
) - 1;
1475 LLVMTypeRef func_type
;
1476 LLVMValueRef context_ptr
;
1477 LLVMBasicBlockRef block
;
1478 LLVMBuilderRef builder
;
1479 struct lp_type vs_type
;
1480 LLVMValueRef end
, start
;
1481 LLVMValueRef count
, fetch_elts
, fetch_elt_max
, fetch_count
;
1482 LLVMValueRef vertex_id_offset
;
1483 LLVMValueRef stride
, step
, io_itr
;
1484 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1485 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1486 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1487 struct draw_context
*draw
= llvm
->draw
;
1488 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1490 struct lp_build_context bld
;
1491 struct lp_build_loop_state lp_loop
;
1492 const int vector_length
= lp_native_vector_width
/ 32;
1493 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1494 LLVMValueRef fetch_max
;
1495 struct lp_build_sampler_soa
*sampler
= 0;
1496 LLVMValueRef ret
, clipmask_bool_ptr
;
1497 const struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
1498 struct draw_llvm_variant_key
*key
= &variant
->key
;
1499 /* If geometry shader is present we need to skip both the viewport
1500 * transformation and clipping otherwise the inputs to the geometry
1501 * shader will be incorrect.
1503 const boolean bypass_viewport
= gs
|| key
->bypass_viewport
;
1504 const boolean enable_cliptest
= !gs
&& (key
->clip_xy
||
1507 LLVMValueRef variant_func
;
1508 const unsigned pos
= llvm
->draw
->vs
.position_output
;
1509 const unsigned cv
= llvm
->draw
->vs
.clipvertex_output
;
1510 boolean have_clipdist
= FALSE
;
1511 struct lp_bld_tgsi_system_values system_values
;
1513 memset(&system_values
, 0, sizeof(system_values
));
1516 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1517 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1518 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1520 arg_types
[i
++] = LLVMPointerType(int32_type
, 0);/* fetch_elts */
1521 arg_types
[i
++] = int32_type
; /* fetch_elt_max */
1523 arg_types
[i
++] = int32_type
; /* start */
1524 arg_types
[i
++] = int32_type
; /* fetch_count / count */
1525 arg_types
[i
++] = int32_type
; /* stride */
1526 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1527 arg_types
[i
++] = int32_type
; /* instance_id */
1528 arg_types
[i
++] = int32_type
; /* vertex_id_offset */
1530 func_type
= LLVMFunctionType(int32_type
, arg_types
, num_arg_types
, 0);
1532 variant_func
= LLVMAddFunction(gallivm
->module
,
1533 elts
? "draw_llvm_shader_elts" : "draw_llvm_shader",
1537 variant
->function_elts
= variant_func
;
1539 variant
->function
= variant_func
;
1541 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1542 for (i
= 0; i
< num_arg_types
; ++i
)
1543 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1544 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1545 LLVMNoAliasAttribute
);
1547 context_ptr
= LLVMGetParam(variant_func
, 0);
1548 io_ptr
= LLVMGetParam(variant_func
, 1);
1549 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1550 stride
= LLVMGetParam(variant_func
, 5 + (elts
? 1 : 0));
1551 vb_ptr
= LLVMGetParam(variant_func
, 6 + (elts
? 1 : 0));
1552 system_values
.instance_id
= LLVMGetParam(variant_func
, 7 + (elts
? 1 : 0));
1553 vertex_id_offset
= LLVMGetParam(variant_func
, 8 + (elts
? 1 : 0));
1555 lp_build_name(context_ptr
, "context");
1556 lp_build_name(io_ptr
, "io");
1557 lp_build_name(vbuffers_ptr
, "vbuffers");
1558 lp_build_name(stride
, "stride");
1559 lp_build_name(vb_ptr
, "vb");
1560 lp_build_name(system_values
.instance_id
, "instance_id");
1561 lp_build_name(vertex_id_offset
, "vertex_id_offset");
1564 fetch_elts
= LLVMGetParam(variant_func
, 3);
1565 fetch_elt_max
= LLVMGetParam(variant_func
, 4);
1566 fetch_count
= LLVMGetParam(variant_func
, 5);
1567 lp_build_name(fetch_elts
, "fetch_elts");
1568 lp_build_name(fetch_elt_max
, "fetch_elt_max");
1569 lp_build_name(fetch_count
, "fetch_count");
1570 start
= count
= NULL
;
1573 start
= LLVMGetParam(variant_func
, 3);
1574 count
= LLVMGetParam(variant_func
, 4);
1575 lp_build_name(start
, "start");
1576 lp_build_name(count
, "count");
1577 fetch_elts
= fetch_count
= NULL
;
1584 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1585 builder
= gallivm
->builder
;
1586 LLVMPositionBuilderAtEnd(builder
, block
);
1588 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1590 memset(&vs_type
, 0, sizeof vs_type
);
1591 vs_type
.floating
= TRUE
; /* floating point values */
1592 vs_type
.sign
= TRUE
; /* values are signed */
1593 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1594 vs_type
.width
= 32; /* 32-bit float */
1595 vs_type
.length
= vector_length
;
1597 /* hold temporary "bool" clipmask */
1598 clipmask_bool_ptr
= lp_build_alloca(gallivm
, lp_build_int_vec_type(gallivm
, vs_type
), "");
1599 LLVMBuildStore(builder
, lp_build_zero(gallivm
, lp_int_type(vs_type
)), clipmask_bool_ptr
);
1601 /* code generated texture sampling */
1602 sampler
= draw_llvm_sampler_soa_create(
1603 draw_llvm_variant_key_samplers(key
),
1609 count
= fetch_count
;
1612 end
= lp_build_add(&bld
, start
, count
);
1615 step
= lp_build_const_int32(gallivm
, vector_length
);
1617 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1619 lp_build_loop_begin(&lp_loop
, gallivm
, zero
);
1621 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1622 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][LP_MAX_VECTOR_WIDTH
/ 32] = { { 0 } };
1624 LLVMValueRef clipmask
; /* holds the clipmask value */
1625 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1627 io_itr
= lp_loop
.counter
;
1629 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1631 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1632 io_itr
, io
, lp_loop
.counter
);
1634 system_values
.vertex_id
= lp_build_zero(gallivm
, lp_type_uint_vec(32, 32*vector_length
));
1635 for (i
= 0; i
< vector_length
; ++i
) {
1636 LLVMValueRef vert_index
=
1637 LLVMBuildAdd(builder
,
1639 lp_build_const_int32(gallivm
, i
), "");
1640 LLVMValueRef true_index
=
1641 LLVMBuildAdd(builder
, start
, vert_index
, "");
1642 LLVMValueRef vertex_id
;
1644 /* make sure we're not out of bounds which can happen
1645 * if fetch_count % 4 != 0, because on the last iteration
1646 * a few of the 4 vertex fetches will be out of bounds */
1647 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1650 LLVMValueRef fetch_ptr
;
1651 LLVMValueRef index_overflowed
;
1652 LLVMValueRef index_ptr
=
1655 lp_build_vec_type(gallivm
, lp_type_int(32)), "");
1656 struct lp_build_if_state if_ctx
;
1657 index_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGT
,
1658 true_index
, fetch_elt_max
,
1659 "index_overflowed");
1661 lp_build_if(&if_ctx
, gallivm
, index_overflowed
);
1663 /* Generate maximum possible index so that
1664 * generate_fetch can treat it just like
1665 * any other overflow and return zeros.
1666 * We don't have to worry about the restart
1667 * primitive index because it has already been
1671 lp_build_const_int32(gallivm
, 0xffffffff);
1672 LLVMBuildStore(builder
, val
, index_ptr
);
1674 lp_build_else(&if_ctx
);
1677 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1678 &true_index
, 1, "");
1679 val
= LLVMBuildLoad(builder
, fetch_ptr
, "");
1680 LLVMBuildStore(builder
, val
, index_ptr
);
1682 lp_build_endif(&if_ctx
);
1683 true_index
= LLVMBuildLoad(builder
, index_ptr
, "true_index");
1685 /* in the paths with elts vertex id has to be unaffected by the
1686 * index bias and because indices inside our elements array have
1687 * already had index bias applied we need to subtract it here to
1688 * get back to the original index.
1689 * in the linear paths vertex id has to be unaffected by the
1690 * original start index and because we abuse the 'start' variable
1691 * to either represent the actual start index or the index at which
1692 * the primitive was split (we split rendering into chunks of at
1693 * most 4095-vertices) we need to back out the original start
1694 * index out of our vertex id here.
1696 vertex_id
= LLVMBuildSub(builder
, true_index
, vertex_id_offset
, "");
1698 system_values
.vertex_id
= LLVMBuildInsertElement(
1700 system_values
.vertex_id
, vertex_id
,
1701 lp_build_const_int32(gallivm
, i
), "");
1703 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1704 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1705 LLVMValueRef vb_index
=
1706 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1707 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1708 generate_fetch(gallivm
, draw
, vbuffers_ptr
,
1709 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1710 system_values
.instance_id
);
1713 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1714 draw
->pt
.nr_vertex_elements
, vs_type
);
1716 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1717 generate_vs(variant
,
1725 key
->clamp_vertex_color
);
1727 if (pos
!= -1 && cv
!= -1) {
1728 /* store original positions in clip before further manipulation */
1729 store_clip(gallivm
, vs_type
, io
, outputs
, 0, cv
);
1730 store_clip(gallivm
, vs_type
, io
, outputs
, 1, pos
);
1733 if (enable_cliptest
) {
1734 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1735 /* allocate clipmask, assign it integer type */
1736 clipmask
= generate_clipmask(llvm
,
1745 context_ptr
, &have_clipdist
);
1746 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1747 /* store temporary clipping boolean value */
1748 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1751 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1754 /* do viewport mapping */
1755 if (!bypass_viewport
) {
1756 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1760 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1763 /* store clipmask in vertex header,
1764 * original positions in clip
1765 * and transformed positions in data
1767 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
1768 vs_info
->num_outputs
, vs_type
,
1771 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
1773 sampler
->destroy(sampler
);
1775 /* return clipping boolean value for function */
1776 ret
= clipmask_booli32(gallivm
, vs_type
, clipmask_bool_ptr
);
1778 LLVMBuildRet(builder
, ret
);
1780 gallivm_verify_function(gallivm
, variant_func
);
1784 struct draw_llvm_variant_key
*
1785 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1788 struct draw_llvm_variant_key
*key
;
1789 struct draw_sampler_static_state
*draw_sampler
;
1791 key
= (struct draw_llvm_variant_key
*)store
;
1793 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1795 /* Presumably all variants of the shader should have the same
1796 * number of vertex elements - ie the number of shader inputs.
1797 * NOTE: we NEED to store the needed number of needed inputs
1798 * here, not the number of provided elements to match keysize
1799 * (and the offset of sampler state in the key).
1801 key
->nr_vertex_elements
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
1802 assert(key
->nr_vertex_elements
<= llvm
->draw
->pt
.nr_vertex_elements
);
1804 /* will have to rig this up properly later */
1805 key
->clip_xy
= llvm
->draw
->clip_xy
;
1806 key
->clip_z
= llvm
->draw
->clip_z
;
1807 key
->clip_user
= llvm
->draw
->clip_user
;
1808 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1809 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
1810 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1811 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1812 key
->has_gs
= llvm
->draw
->gs
.geometry_shader
!= NULL
;
1813 key
->num_outputs
= draw_total_vs_outputs(llvm
->draw
);
1816 /* All variants of this shader will have the same value for
1817 * nr_samplers. Not yet trying to compact away holes in the
1820 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1821 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
1822 key
->nr_sampler_views
=
1823 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
1826 key
->nr_sampler_views
= key
->nr_samplers
;
1829 draw_sampler
= draw_llvm_variant_key_samplers(key
);
1831 memcpy(key
->vertex_element
,
1832 llvm
->draw
->pt
.vertex_element
,
1833 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1835 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
1837 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1838 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
1839 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
1841 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1842 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
1843 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
1851 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
1854 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
1856 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
1857 debug_printf("clip_xy = %u\n", key
->clip_xy
);
1858 debug_printf("clip_z = %u\n", key
->clip_z
);
1859 debug_printf("clip_user = %u\n", key
->clip_user
);
1860 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
1861 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
1862 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
1863 debug_printf("has_gs = %u\n", key
->has_gs
);
1864 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
1866 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
1867 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
1868 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
1869 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
1870 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
1873 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1874 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
1880 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1881 unsigned shader_stage
,
1883 uint32_t width
, uint32_t height
, uint32_t depth
,
1884 uint32_t first_level
, uint32_t last_level
,
1885 const void *base_ptr
,
1886 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1887 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1888 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
1891 struct draw_jit_texture
*jit_tex
;
1893 assert(shader_stage
== PIPE_SHADER_VERTEX
||
1894 shader_stage
== PIPE_SHADER_GEOMETRY
);
1896 if (shader_stage
== PIPE_SHADER_VERTEX
) {
1897 assert(sview_idx
< Elements(draw
->llvm
->jit_context
.textures
));
1899 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
1900 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
1901 assert(sview_idx
< Elements(draw
->llvm
->gs_jit_context
.textures
));
1903 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
1909 jit_tex
->width
= width
;
1910 jit_tex
->height
= height
;
1911 jit_tex
->depth
= depth
;
1912 jit_tex
->first_level
= first_level
;
1913 jit_tex
->last_level
= last_level
;
1914 jit_tex
->base
= base_ptr
;
1916 for (j
= first_level
; j
<= last_level
; j
++) {
1917 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
1918 jit_tex
->row_stride
[j
] = row_stride
[j
];
1919 jit_tex
->img_stride
[j
] = img_stride
[j
];
1925 draw_llvm_set_sampler_state(struct draw_context
*draw
,
1926 unsigned shader_type
)
1930 if (shader_type
== PIPE_SHADER_VERTEX
) {
1931 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
1932 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
1934 if (draw
->samplers
[i
]) {
1935 const struct pipe_sampler_state
*s
1936 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
1937 jit_sam
->min_lod
= s
->min_lod
;
1938 jit_sam
->max_lod
= s
->max_lod
;
1939 jit_sam
->lod_bias
= s
->lod_bias
;
1940 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1943 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
1944 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
1945 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
1947 if (draw
->samplers
[i
]) {
1948 const struct pipe_sampler_state
*s
1949 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
1950 jit_sam
->min_lod
= s
->min_lod
;
1951 jit_sam
->max_lod
= s
->max_lod
;
1952 jit_sam
->lod_bias
= s
->lod_bias
;
1953 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1961 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
1963 struct draw_llvm
*llvm
= variant
->llvm
;
1965 gallivm_destroy(variant
->gallivm
);
1967 remove_from_list(&variant
->list_item_local
);
1968 variant
->shader
->variants_cached
--;
1969 remove_from_list(&variant
->list_item_global
);
1970 llvm
->nr_variants
--;
1976 * Create LLVM types for various structures.
1979 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
1981 struct gallivm_state
*gallivm
= var
->gallivm
;
1982 LLVMTypeRef texture_type
, sampler_type
, context_type
;
1984 texture_type
= create_jit_texture_type(gallivm
, "texture");
1985 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
1987 context_type
= create_gs_jit_context_type(gallivm
,
1988 var
->shader
->base
.vector_length
,
1989 texture_type
, sampler_type
,
1990 "draw_gs_jit_context");
1991 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
1993 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
1997 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
1999 if (!variant
->context_ptr_type
)
2000 create_gs_jit_types(variant
);
2001 return variant
->context_ptr_type
;
2005 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2006 struct lp_type gs_type
)
2008 struct gallivm_state
*gallivm
= variant
->gallivm
;
2009 LLVMBuilderRef builder
= gallivm
->builder
;
2010 struct lp_type mask_type
= lp_int_type(gs_type
);
2011 LLVMValueRef num_prims
;
2012 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
2015 num_prims
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
),
2016 variant
->num_prims
);
2017 for (i
= 0; i
<= gs_type
.length
; i
++) {
2018 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
2019 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, idx
, idx
, "");
2021 mask_val
= lp_build_compare(gallivm
, mask_type
,
2022 PIPE_FUNC_GREATER
, num_prims
, mask_val
);
2028 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2029 struct draw_gs_llvm_variant
*variant
)
2031 struct gallivm_state
*gallivm
= variant
->gallivm
;
2032 LLVMContextRef context
= gallivm
->context
;
2033 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2034 LLVMTypeRef arg_types
[6];
2035 LLVMTypeRef func_type
;
2036 LLVMValueRef variant_func
;
2037 LLVMValueRef context_ptr
;
2038 LLVMValueRef prim_id_ptr
;
2039 LLVMBasicBlockRef block
;
2040 LLVMBuilderRef builder
;
2041 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2042 struct lp_build_sampler_soa
*sampler
= 0;
2043 struct lp_build_context bld
;
2044 struct lp_bld_tgsi_system_values system_values
;
2045 struct lp_type gs_type
;
2047 struct draw_gs_llvm_iface gs_iface
;
2048 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2049 LLVMValueRef consts_ptr
, num_consts_ptr
;
2050 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2051 struct lp_build_mask_context mask
;
2052 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2053 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2055 memset(&system_values
, 0, sizeof(system_values
));
2057 assert(variant
->vertex_header_ptr_type
);
2059 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2060 arg_types
[1] = variant
->input_array_type
; /* input */
2061 arg_types
[2] = variant
->vertex_header_ptr_type
; /* vertex_header */
2062 arg_types
[3] = int32_type
; /* num_prims */
2063 arg_types
[4] = int32_type
; /* instance_id */
2064 arg_types
[5] = LLVMPointerType(
2065 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2067 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
2069 variant_func
= LLVMAddFunction(gallivm
->module
, "draw_geometry_shader",
2071 variant
->function
= variant_func
;
2073 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2075 for (i
= 0; i
< Elements(arg_types
); ++i
)
2076 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2077 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
2078 LLVMNoAliasAttribute
);
2080 context_ptr
= LLVMGetParam(variant_func
, 0);
2081 input_array
= LLVMGetParam(variant_func
, 1);
2082 io_ptr
= LLVMGetParam(variant_func
, 2);
2083 num_prims
= LLVMGetParam(variant_func
, 3);
2084 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2085 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2087 lp_build_name(context_ptr
, "context");
2088 lp_build_name(input_array
, "input");
2089 lp_build_name(io_ptr
, "io");
2090 lp_build_name(num_prims
, "num_prims");
2091 lp_build_name(system_values
.instance_id
, "instance_id");
2092 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2094 variant
->context_ptr
= context_ptr
;
2095 variant
->io_ptr
= io_ptr
;
2096 variant
->num_prims
= num_prims
;
2098 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2099 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2100 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2101 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2102 gs_iface
.input
= input_array
;
2103 gs_iface
.variant
= variant
;
2109 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2110 builder
= gallivm
->builder
;
2111 LLVMPositionBuilderAtEnd(builder
, block
);
2113 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2115 memset(&gs_type
, 0, sizeof gs_type
);
2116 gs_type
.floating
= TRUE
; /* floating point values */
2117 gs_type
.sign
= TRUE
; /* values are signed */
2118 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2119 gs_type
.width
= 32; /* 32-bit float */
2120 gs_type
.length
= vector_length
;
2122 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2124 draw_gs_jit_context_num_constants(variant
->gallivm
, context_ptr
);
2126 /* code generated texture sampling */
2127 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
,
2130 mask_val
= generate_mask_value(variant
, gs_type
);
2131 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2133 if (gs_info
->uses_primid
) {
2134 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");
2137 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2138 tgsi_dump(tokens
, 0);
2139 draw_gs_llvm_dump_variant_key(&variant
->key
);
2142 lp_build_tgsi_soa(variant
->gallivm
,
2152 &llvm
->draw
->gs
.geometry_shader
->info
,
2153 (const struct lp_build_tgsi_gs_iface
*)&gs_iface
);
2155 sampler
->destroy(sampler
);
2157 lp_build_mask_end(&mask
);
2159 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2161 gallivm_verify_function(gallivm
, variant_func
);
2165 struct draw_gs_llvm_variant
*
2166 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2167 unsigned num_outputs
,
2168 const struct draw_gs_llvm_variant_key
*key
)
2170 struct draw_gs_llvm_variant
*variant
;
2171 struct llvm_geometry_shader
*shader
=
2172 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2173 LLVMTypeRef vertex_header
;
2175 variant
= MALLOC(sizeof *variant
+
2176 shader
->variant_key_size
-
2177 sizeof variant
->key
);
2178 if (variant
== NULL
)
2181 variant
->llvm
= llvm
;
2182 variant
->shader
= shader
;
2184 variant
->gallivm
= gallivm_create();
2186 create_gs_jit_types(variant
);
2188 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2190 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2192 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2194 draw_gs_llvm_generate(llvm
, variant
);
2196 gallivm_compile_module(variant
->gallivm
);
2198 variant
->jit_func
= (draw_gs_jit_func
)
2199 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2201 gallivm_free_ir(variant
->gallivm
);
2203 variant
->list_item_global
.base
= variant
;
2204 variant
->list_item_local
.base
= variant
;
2205 /*variant->no = */shader
->variants_created
++;
2206 variant
->list_item_global
.base
= variant
;
2212 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2214 struct draw_llvm
*llvm
= variant
->llvm
;
2216 gallivm_destroy(variant
->gallivm
);
2218 remove_from_list(&variant
->list_item_local
);
2219 variant
->shader
->variants_cached
--;
2220 remove_from_list(&variant
->list_item_global
);
2221 llvm
->nr_gs_variants
--;
2225 struct draw_gs_llvm_variant_key
*
2226 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2229 struct draw_gs_llvm_variant_key
*key
;
2230 struct draw_sampler_static_state
*draw_sampler
;
2232 key
= (struct draw_gs_llvm_variant_key
*)store
;
2234 key
->num_outputs
= draw_total_gs_outputs(llvm
->draw
);
2236 /* All variants of this shader will have the same value for
2237 * nr_samplers. Not yet trying to compact away holes in the
2240 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2241 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2242 key
->nr_sampler_views
=
2243 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2246 key
->nr_sampler_views
= key
->nr_samplers
;
2249 draw_sampler
= key
->samplers
;
2251 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2253 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2254 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2255 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2257 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2258 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2259 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2266 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2269 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2271 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2272 debug_printf("sampler[%i].src_format = %s\n", i
,
2273 util_format_name(sampler
[i
].texture_state
.format
));