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_bitarit.h"
37 #include "gallivm/lp_bld_gather.h"
38 #include "gallivm/lp_bld_logic.h"
39 #include "gallivm/lp_bld_const.h"
40 #include "gallivm/lp_bld_coro.h"
41 #include "gallivm/lp_bld_swizzle.h"
42 #include "gallivm/lp_bld_struct.h"
43 #include "gallivm/lp_bld_type.h"
44 #include "gallivm/lp_bld_flow.h"
45 #include "gallivm/lp_bld_debug.h"
46 #include "gallivm/lp_bld_tgsi.h"
47 #include "gallivm/lp_bld_nir.h"
48 #include "gallivm/lp_bld_printf.h"
49 #include "gallivm/lp_bld_intr.h"
50 #include "gallivm/lp_bld_init.h"
51 #include "gallivm/lp_bld_type.h"
52 #include "gallivm/lp_bld_pack.h"
53 #include "gallivm/lp_bld_format.h"
55 #include "tgsi/tgsi_exec.h"
56 #include "tgsi/tgsi_dump.h"
58 #include "util/u_math.h"
59 #include "util/u_pointer.h"
60 #include "util/u_string.h"
61 #include "util/simple_list.h"
68 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
);
71 struct draw_gs_llvm_iface
{
72 struct lp_build_gs_iface base
;
74 struct draw_gs_llvm_variant
*variant
;
78 static inline const struct draw_gs_llvm_iface
*
79 draw_gs_llvm_iface(const struct lp_build_gs_iface
*iface
)
81 return (const struct draw_gs_llvm_iface
*)iface
;
84 struct draw_tcs_llvm_iface
{
85 struct lp_build_tcs_iface base
;
87 struct draw_tcs_llvm_variant
*variant
;
92 static inline const struct draw_tcs_llvm_iface
*
93 draw_tcs_llvm_iface(const struct lp_build_tcs_iface
*iface
)
95 return (const struct draw_tcs_llvm_iface
*)iface
;
98 struct draw_tes_llvm_iface
{
99 struct lp_build_tes_iface base
;
101 struct draw_tes_llvm_variant
*variant
;
105 static inline const struct draw_tes_llvm_iface
*
106 draw_tes_llvm_iface(const struct lp_build_tes_iface
*iface
)
108 return (const struct draw_tes_llvm_iface
*)iface
;
112 * Create LLVM type for draw_vertex_buffer.
115 create_jit_dvbuffer_type(struct gallivm_state
*gallivm
,
116 const char *struct_name
)
118 LLVMTargetDataRef target
= gallivm
->target
;
119 LLVMTypeRef dvbuffer_type
;
120 LLVMTypeRef elem_types
[DRAW_JIT_DVBUFFER_NUM_FIELDS
];
121 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
123 elem_types
[DRAW_JIT_DVBUFFER_MAP
] =
124 LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
125 elem_types
[DRAW_JIT_DVBUFFER_SIZE
] = int32_type
;
127 dvbuffer_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
128 ARRAY_SIZE(elem_types
), 0);
130 (void) target
; /* silence unused var warning for non-debug build */
131 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, map
,
132 target
, dvbuffer_type
,
133 DRAW_JIT_DVBUFFER_MAP
);
134 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, size
,
135 target
, dvbuffer_type
,
136 DRAW_JIT_DVBUFFER_SIZE
);
138 return dvbuffer_type
;
142 * Create LLVM type for struct draw_jit_texture
145 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
147 LLVMTargetDataRef target
= gallivm
->target
;
148 LLVMTypeRef texture_type
;
149 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
150 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
152 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
153 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
154 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
155 elem_types
[DRAW_JIT_TEXTURE_NUM_SAMPLES
] =
156 elem_types
[DRAW_JIT_TEXTURE_SAMPLE_STRIDE
] =
157 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
158 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
159 elem_types
[DRAW_JIT_TEXTURE_BASE
] =
160 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
161 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
162 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
163 elem_types
[DRAW_JIT_TEXTURE_MIP_OFFSETS
] =
164 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
166 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
167 ARRAY_SIZE(elem_types
), 0);
169 (void) target
; /* silence unused var warning for non-debug build */
170 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
171 target
, texture_type
,
172 DRAW_JIT_TEXTURE_WIDTH
);
173 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
174 target
, texture_type
,
175 DRAW_JIT_TEXTURE_HEIGHT
);
176 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
177 target
, texture_type
,
178 DRAW_JIT_TEXTURE_DEPTH
);
179 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, base
,
180 target
, texture_type
,
181 DRAW_JIT_TEXTURE_BASE
);
182 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
183 target
, texture_type
,
184 DRAW_JIT_TEXTURE_ROW_STRIDE
);
185 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
186 target
, texture_type
,
187 DRAW_JIT_TEXTURE_IMG_STRIDE
);
188 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
189 target
, texture_type
,
190 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
191 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
192 target
, texture_type
,
193 DRAW_JIT_TEXTURE_LAST_LEVEL
);
194 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, mip_offsets
,
195 target
, texture_type
,
196 DRAW_JIT_TEXTURE_MIP_OFFSETS
);
197 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, num_samples
,
198 target
, texture_type
,
199 DRAW_JIT_TEXTURE_NUM_SAMPLES
);
200 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, sample_stride
,
201 target
, texture_type
,
202 DRAW_JIT_TEXTURE_SAMPLE_STRIDE
);
204 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
211 * Create LLVM type for struct draw_jit_sampler
214 create_jit_sampler_type(struct gallivm_state
*gallivm
, const char *struct_name
)
216 LLVMTargetDataRef target
= gallivm
->target
;
217 LLVMTypeRef sampler_type
;
218 LLVMTypeRef elem_types
[DRAW_JIT_SAMPLER_NUM_FIELDS
];
220 elem_types
[DRAW_JIT_SAMPLER_MIN_LOD
] =
221 elem_types
[DRAW_JIT_SAMPLER_MAX_LOD
] =
222 elem_types
[DRAW_JIT_SAMPLER_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
223 elem_types
[DRAW_JIT_SAMPLER_BORDER_COLOR
] =
224 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
226 sampler_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
227 ARRAY_SIZE(elem_types
), 0);
229 (void) target
; /* silence unused var warning for non-debug build */
230 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, min_lod
,
231 target
, sampler_type
,
232 DRAW_JIT_SAMPLER_MIN_LOD
);
233 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, max_lod
,
234 target
, sampler_type
,
235 DRAW_JIT_SAMPLER_MAX_LOD
);
236 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, lod_bias
,
237 target
, sampler_type
,
238 DRAW_JIT_SAMPLER_LOD_BIAS
);
239 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, border_color
,
240 target
, sampler_type
,
241 DRAW_JIT_SAMPLER_BORDER_COLOR
);
243 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler
, target
, sampler_type
);
249 * Create LLVM type for struct draw_jit_texture
252 create_jit_image_type(struct gallivm_state
*gallivm
, const char *struct_name
)
254 LLVMTargetDataRef target
= gallivm
->target
;
255 LLVMTypeRef image_type
;
256 LLVMTypeRef elem_types
[DRAW_JIT_IMAGE_NUM_FIELDS
];
257 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
259 elem_types
[DRAW_JIT_IMAGE_WIDTH
] =
260 elem_types
[DRAW_JIT_IMAGE_HEIGHT
] =
261 elem_types
[DRAW_JIT_IMAGE_DEPTH
] =
262 elem_types
[DRAW_JIT_IMAGE_ROW_STRIDE
] =
263 elem_types
[DRAW_JIT_IMAGE_IMG_STRIDE
] = int32_type
;
264 elem_types
[DRAW_JIT_IMAGE_BASE
] =
265 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
267 image_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
268 ARRAY_SIZE(elem_types
), 0);
270 (void) target
; /* silence unused var warning for non-debug build */
271 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, width
,
273 DRAW_JIT_IMAGE_WIDTH
);
274 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, height
,
276 DRAW_JIT_IMAGE_HEIGHT
);
277 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, depth
,
279 DRAW_JIT_IMAGE_DEPTH
);
280 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, base
,
282 DRAW_JIT_IMAGE_BASE
);
283 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, row_stride
,
285 DRAW_JIT_IMAGE_ROW_STRIDE
);
286 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, img_stride
,
288 DRAW_JIT_IMAGE_IMG_STRIDE
);
290 LP_CHECK_STRUCT_SIZE(struct draw_jit_image
, target
, image_type
);
296 * Create LLVM type for struct draw_jit_context
299 create_jit_context_type(struct gallivm_state
*gallivm
,
300 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
301 LLVMTypeRef image_type
,
302 const char *struct_name
)
304 LLVMTargetDataRef target
= gallivm
->target
;
305 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
306 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
307 LLVMTypeRef elem_types
[DRAW_JIT_CTX_NUM_FIELDS
];
308 LLVMTypeRef context_type
;
310 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
311 LP_MAX_TGSI_CONST_BUFFERS
);
312 elem_types
[1] = LLVMArrayType(int_type
, /* num_vs_constants */
313 LP_MAX_TGSI_CONST_BUFFERS
);
314 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
315 DRAW_TOTAL_CLIP_PLANES
), 0);
316 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewports */
317 elem_types
[4] = LLVMArrayType(texture_type
,
318 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
319 elem_types
[5] = LLVMArrayType(sampler_type
,
320 PIPE_MAX_SAMPLERS
); /* samplers */
321 elem_types
[6] = LLVMArrayType(image_type
,
322 PIPE_MAX_SHADER_IMAGES
); /* images */
323 elem_types
[7] = LLVMArrayType(LLVMPointerType(int_type
, 0), /* vs_ssbo */
324 LP_MAX_TGSI_SHADER_BUFFERS
);
325 elem_types
[8] = LLVMArrayType(int_type
, /* num_vs_ssbos */
326 LP_MAX_TGSI_SHADER_BUFFERS
);
327 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
328 ARRAY_SIZE(elem_types
), 0);
330 (void) target
; /* silence unused var warning for non-debug build */
331 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
332 target
, context_type
, DRAW_JIT_CTX_CONSTANTS
);
333 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, num_vs_constants
,
334 target
, context_type
, DRAW_JIT_CTX_NUM_CONSTANTS
);
335 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
336 target
, context_type
, DRAW_JIT_CTX_PLANES
);
337 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewports
,
338 target
, context_type
, DRAW_JIT_CTX_VIEWPORT
);
339 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
340 target
, context_type
,
341 DRAW_JIT_CTX_TEXTURES
);
342 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
343 target
, context_type
,
344 DRAW_JIT_CTX_SAMPLERS
);
345 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, images
,
346 target
, context_type
, DRAW_JIT_CTX_IMAGES
);
347 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_ssbos
,
348 target
, context_type
, DRAW_JIT_CTX_SSBOS
);
349 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, num_vs_ssbos
,
350 target
, context_type
, DRAW_JIT_CTX_NUM_SSBOS
);
351 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
352 target
, context_type
);
359 * Create LLVM type for struct draw_gs_jit_context
362 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
363 unsigned vector_length
,
364 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
365 LLVMTypeRef image_type
,
366 const char *struct_name
)
368 LLVMTargetDataRef target
= gallivm
->target
;
369 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
370 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
371 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
372 LLVMTypeRef context_type
;
374 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
375 LP_MAX_TGSI_CONST_BUFFERS
);
376 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
377 LP_MAX_TGSI_CONST_BUFFERS
);
378 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
379 DRAW_TOTAL_CLIP_PLANES
), 0);
380 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewports */
382 elem_types
[4] = LLVMArrayType(texture_type
,
383 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
384 elem_types
[5] = LLVMArrayType(sampler_type
,
385 PIPE_MAX_SAMPLERS
); /* samplers */
386 elem_types
[6] = LLVMArrayType(image_type
,
387 PIPE_MAX_SHADER_IMAGES
); /* images */
388 elem_types
[7] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
389 elem_types
[8] = LLVMPointerType(LLVMVectorType(int_type
,
391 elem_types
[9] = LLVMPointerType(LLVMVectorType(int_type
,
394 elem_types
[10] = LLVMArrayType(LLVMPointerType(int_type
, 0), /* ssbos */
395 LP_MAX_TGSI_SHADER_BUFFERS
);
396 elem_types
[11] = LLVMArrayType(int_type
, /* num_ssbos */
397 LP_MAX_TGSI_SHADER_BUFFERS
);
399 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
400 ARRAY_SIZE(elem_types
), 0);
402 (void) target
; /* silence unused var warning for non-debug build */
403 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
404 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
405 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, num_constants
,
406 target
, context_type
, DRAW_GS_JIT_CTX_NUM_CONSTANTS
);
407 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
408 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
409 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewports
,
410 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
411 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
412 target
, context_type
,
413 DRAW_GS_JIT_CTX_TEXTURES
);
414 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
415 target
, context_type
,
416 DRAW_GS_JIT_CTX_SAMPLERS
);
417 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
418 target
, context_type
,
419 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
420 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
421 target
, context_type
,
422 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
423 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
424 target
, context_type
,
425 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
426 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, ssbos
,
427 target
, context_type
, DRAW_GS_JIT_CTX_SSBOS
);
428 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, num_ssbos
,
429 target
, context_type
, DRAW_GS_JIT_CTX_NUM_SSBOS
);
430 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, images
,
431 target
, context_type
, DRAW_GS_JIT_CTX_IMAGES
);
432 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
433 target
, context_type
);
440 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
442 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
443 LLVMTypeRef input_array
;
445 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
446 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
447 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
448 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
454 * Create LLVM type for struct pipe_vertex_buffer
457 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
,
458 const char *struct_name
)
460 LLVMTargetDataRef target
= gallivm
->target
;
461 LLVMTypeRef elem_types
[4];
464 elem_types
[0] = LLVMInt16TypeInContext(gallivm
->context
);
465 elem_types
[1] = LLVMInt8TypeInContext(gallivm
->context
);
466 elem_types
[2] = LLVMInt32TypeInContext(gallivm
->context
);
467 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
469 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
470 ARRAY_SIZE(elem_types
), 0);
472 (void) target
; /* silence unused var warning for non-debug build */
473 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
475 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, is_user_buffer
,
477 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
479 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer
.resource
,
482 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
489 * Create LLVM type for struct vertex_header;
492 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
494 LLVMTargetDataRef target
= gallivm
->target
;
495 LLVMTypeRef elem_types
[3];
496 LLVMTypeRef vertex_header
;
497 char struct_name
[24];
499 snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
501 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
502 elem_types
[DRAW_JIT_VERTEX_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
503 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
505 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
506 ARRAY_SIZE(elem_types
), 0);
508 /* these are bit-fields and we can't take address of them
509 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
510 target, vertex_header,
511 DRAW_JIT_VERTEX_CLIPMASK);
512 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
513 target, vertex_header,
514 DRAW_JIT_VERTEX_EDGEFLAG);
515 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
516 target, vertex_header,
517 DRAW_JIT_VERTEX_PAD);
518 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
519 target, vertex_header,
520 DRAW_JIT_VERTEX_VERTEX_ID);
522 (void) target
; /* silence unused var warning for non-debug build */
523 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip_pos
,
524 target
, vertex_header
,
525 DRAW_JIT_VERTEX_CLIP_POS
);
526 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
527 target
, vertex_header
,
528 DRAW_JIT_VERTEX_DATA
);
530 assert(LLVMABISizeOfType(target
, vertex_header
) ==
531 offsetof(struct vertex_header
, data
[data_elems
]));
533 return vertex_header
;
537 * Create LLVM type for struct draw_tcs_jit_context
540 create_tcs_jit_context_type(struct gallivm_state
*gallivm
,
541 unsigned vector_length
,
542 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
543 LLVMTypeRef image_type
,
544 const char *struct_name
)
546 LLVMTargetDataRef target
= gallivm
->target
;
547 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
548 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
549 LLVMTypeRef elem_types
[DRAW_TCS_JIT_CTX_NUM_FIELDS
];
550 LLVMTypeRef context_type
;
552 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
553 LP_MAX_TGSI_CONST_BUFFERS
);
554 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
555 LP_MAX_TGSI_CONST_BUFFERS
);
556 elem_types
[2] = LLVMInt32TypeInContext(gallivm
->context
);
557 elem_types
[3] = LLVMInt32TypeInContext(gallivm
->context
);
559 elem_types
[4] = LLVMArrayType(texture_type
,
560 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
561 elem_types
[5] = LLVMArrayType(sampler_type
,
562 PIPE_MAX_SAMPLERS
); /* samplers */
563 elem_types
[6] = LLVMArrayType(image_type
,
564 PIPE_MAX_SHADER_IMAGES
); /* images */
566 elem_types
[7] = LLVMArrayType(LLVMPointerType(int_type
, 0), /* ssbos */
567 LP_MAX_TGSI_SHADER_BUFFERS
);
568 elem_types
[8] = LLVMArrayType(int_type
, /* num_ssbos */
569 LP_MAX_TGSI_SHADER_BUFFERS
);
571 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
572 ARRAY_SIZE(elem_types
), 0);
574 (void) target
; /* silence unused var warning for non-debug build */
575 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, constants
,
576 target
, context_type
, DRAW_TCS_JIT_CTX_CONSTANTS
);
577 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, num_constants
,
578 target
, context_type
, DRAW_TCS_JIT_CTX_NUM_CONSTANTS
);
579 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, textures
,
580 target
, context_type
,
581 DRAW_TCS_JIT_CTX_TEXTURES
);
582 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, samplers
,
583 target
, context_type
,
584 DRAW_TCS_JIT_CTX_SAMPLERS
);
585 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, ssbos
,
586 target
, context_type
, DRAW_TCS_JIT_CTX_SSBOS
);
587 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, num_ssbos
,
588 target
, context_type
, DRAW_TCS_JIT_CTX_NUM_SSBOS
);
589 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, images
,
590 target
, context_type
, DRAW_TCS_JIT_CTX_IMAGES
);
591 LP_CHECK_STRUCT_SIZE(struct draw_tcs_jit_context
,
592 target
, context_type
);
598 create_tcs_jit_input_type(struct gallivm_state
*gallivm
)
600 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
601 LLVMTypeRef input_array
;
603 input_array
= LLVMArrayType(float_type
, TGSI_NUM_CHANNELS
); /* num channels */
604 input_array
= LLVMArrayType(input_array
, NUM_TCS_INPUTS
); /* num attrs per vertex */
605 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
611 create_tcs_jit_output_type(struct gallivm_state
*gallivm
)
613 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
614 LLVMTypeRef output_array
;
616 output_array
= LLVMArrayType(float_type
, TGSI_NUM_CHANNELS
); /* num channels */
617 output_array
= LLVMArrayType(output_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
618 output_array
= LLVMPointerType(output_array
, 0); /* num vertices per prim */
624 create_tes_jit_input_type(struct gallivm_state
*gallivm
)
626 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
627 LLVMTypeRef input_array
;
629 input_array
= LLVMArrayType(float_type
, TGSI_NUM_CHANNELS
); /* num channels */
630 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
631 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
637 * Create LLVM type for struct draw_tes_jit_context
640 create_tes_jit_context_type(struct gallivm_state
*gallivm
,
641 unsigned vector_length
,
642 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
643 LLVMTypeRef image_type
,
644 const char *struct_name
)
646 LLVMTargetDataRef target
= gallivm
->target
;
647 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
648 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
649 LLVMTypeRef elem_types
[DRAW_TCS_JIT_CTX_NUM_FIELDS
];
650 LLVMTypeRef context_type
;
652 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
653 LP_MAX_TGSI_CONST_BUFFERS
);
654 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
655 LP_MAX_TGSI_CONST_BUFFERS
);
656 elem_types
[2] = LLVMInt32TypeInContext(gallivm
->context
);
657 elem_types
[3] = LLVMInt32TypeInContext(gallivm
->context
);
659 elem_types
[4] = LLVMArrayType(texture_type
,
660 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
661 elem_types
[5] = LLVMArrayType(sampler_type
,
662 PIPE_MAX_SAMPLERS
); /* samplers */
663 elem_types
[6] = LLVMArrayType(image_type
,
664 PIPE_MAX_SHADER_IMAGES
); /* images */
666 elem_types
[7] = LLVMArrayType(LLVMPointerType(int_type
, 0), /* ssbos */
667 LP_MAX_TGSI_SHADER_BUFFERS
);
668 elem_types
[8] = LLVMArrayType(int_type
, /* num_ssbos */
669 LP_MAX_TGSI_SHADER_BUFFERS
);
671 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
672 ARRAY_SIZE(elem_types
), 0);
674 (void) target
; /* silence unused var warning for non-debug build */
675 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, constants
,
676 target
, context_type
, DRAW_TCS_JIT_CTX_CONSTANTS
);
677 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, num_constants
,
678 target
, context_type
, DRAW_TCS_JIT_CTX_NUM_CONSTANTS
);
679 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, textures
,
680 target
, context_type
,
681 DRAW_TCS_JIT_CTX_TEXTURES
);
682 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, samplers
,
683 target
, context_type
,
684 DRAW_TCS_JIT_CTX_SAMPLERS
);
685 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, ssbos
,
686 target
, context_type
, DRAW_TCS_JIT_CTX_SSBOS
);
687 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, num_ssbos
,
688 target
, context_type
, DRAW_TCS_JIT_CTX_NUM_SSBOS
);
689 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, images
,
690 target
, context_type
, DRAW_TCS_JIT_CTX_IMAGES
);
691 LP_CHECK_STRUCT_SIZE(struct draw_tes_jit_context
,
692 target
, context_type
);
698 * Create LLVM types for various structures.
701 create_jit_types(struct draw_llvm_variant
*variant
)
703 struct gallivm_state
*gallivm
= variant
->gallivm
;
704 LLVMTypeRef texture_type
, sampler_type
, context_type
, buffer_type
,
707 texture_type
= create_jit_texture_type(gallivm
, "texture");
708 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
709 image_type
= create_jit_image_type(gallivm
, "image");
711 context_type
= create_jit_context_type(gallivm
, texture_type
, sampler_type
,
714 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
716 buffer_type
= create_jit_dvbuffer_type(gallivm
, "draw_vertex_buffer");
717 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
719 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
720 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
725 get_context_ptr_type(struct draw_llvm_variant
*variant
)
727 if (!variant
->context_ptr_type
)
728 create_jit_types(variant
);
729 return variant
->context_ptr_type
;
734 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
736 if (!variant
->buffer_ptr_type
)
737 create_jit_types(variant
);
738 return variant
->buffer_ptr_type
;
743 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
745 if (!variant
->vb_ptr_type
)
746 create_jit_types(variant
);
747 return variant
->vb_ptr_type
;
751 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
753 if (!variant
->vertex_header_ptr_type
)
754 create_jit_types(variant
);
755 return variant
->vertex_header_ptr_type
;
760 * Create per-context LLVM info.
763 draw_llvm_create(struct draw_context
*draw
, LLVMContextRef context
)
765 struct draw_llvm
*llvm
;
767 if (!lp_build_init())
770 llvm
= CALLOC_STRUCT( draw_llvm
);
776 llvm
->context
= context
;
777 if (!llvm
->context
) {
778 llvm
->context
= LLVMContextCreate();
779 llvm
->context_owned
= true;
784 llvm
->nr_variants
= 0;
785 make_empty_list(&llvm
->vs_variants_list
);
787 llvm
->nr_gs_variants
= 0;
788 make_empty_list(&llvm
->gs_variants_list
);
790 llvm
->nr_tcs_variants
= 0;
791 make_empty_list(&llvm
->tcs_variants_list
);
793 llvm
->nr_tes_variants
= 0;
794 make_empty_list(&llvm
->tes_variants_list
);
799 draw_llvm_destroy(llvm
);
805 * Free per-context LLVM info.
808 draw_llvm_destroy(struct draw_llvm
*llvm
)
810 if (llvm
->context_owned
)
811 LLVMContextDispose(llvm
->context
);
812 llvm
->context
= NULL
;
814 /* XXX free other draw_llvm data? */
820 * Create LLVM-generated code for a vertex shader.
822 struct draw_llvm_variant
*
823 draw_llvm_create_variant(struct draw_llvm
*llvm
,
825 const struct draw_llvm_variant_key
*key
)
827 struct draw_llvm_variant
*variant
;
828 struct llvm_vertex_shader
*shader
=
829 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
830 LLVMTypeRef vertex_header
;
831 char module_name
[64];
833 variant
= MALLOC(sizeof *variant
+
834 shader
->variant_key_size
-
835 sizeof variant
->key
);
839 variant
->llvm
= llvm
;
840 variant
->shader
= shader
;
842 snprintf(module_name
, sizeof(module_name
), "draw_llvm_vs_variant%u",
843 variant
->shader
->variants_cached
);
845 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
847 create_jit_types(variant
);
849 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
851 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
852 if (llvm
->draw
->vs
.vertex_shader
->state
.type
== PIPE_SHADER_IR_TGSI
)
853 tgsi_dump(llvm
->draw
->vs
.vertex_shader
->state
.tokens
, 0);
855 nir_print_shader(llvm
->draw
->vs
.vertex_shader
->state
.ir
.nir
, stderr
);
856 draw_llvm_dump_variant_key(&variant
->key
);
859 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
861 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
863 draw_llvm_generate(llvm
, variant
);
865 gallivm_compile_module(variant
->gallivm
);
867 variant
->jit_func
= (draw_jit_vert_func
)
868 gallivm_jit_function(variant
->gallivm
, variant
->function
);
870 gallivm_free_ir(variant
->gallivm
);
872 variant
->list_item_global
.base
= variant
;
873 variant
->list_item_local
.base
= variant
;
874 /*variant->no = */shader
->variants_created
++;
875 variant
->list_item_global
.base
= variant
;
882 generate_vs(struct draw_llvm_variant
*variant
,
883 LLVMBuilderRef builder
,
884 struct lp_type vs_type
,
885 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
886 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
887 const struct lp_bld_tgsi_system_values
*system_values
,
888 LLVMValueRef context_ptr
,
889 const struct lp_build_sampler_soa
*draw_sampler
,
890 const struct lp_build_image_soa
*draw_image
,
891 boolean clamp_vertex_color
,
892 struct lp_build_mask_context
*bld_mask
)
894 struct draw_llvm
*llvm
= variant
->llvm
;
895 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
896 LLVMValueRef consts_ptr
=
897 draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
898 LLVMValueRef num_consts_ptr
=
899 draw_jit_context_num_vs_constants(variant
->gallivm
, context_ptr
);
900 LLVMValueRef ssbos_ptr
=
901 draw_jit_context_vs_ssbos(variant
->gallivm
, context_ptr
);
902 LLVMValueRef num_ssbos_ptr
=
903 draw_jit_context_num_vs_ssbos(variant
->gallivm
, context_ptr
);
905 struct lp_build_tgsi_params params
;
906 memset(¶ms
, 0, sizeof(params
));
908 params
.type
= vs_type
;
909 params
.mask
= bld_mask
;
910 params
.consts_ptr
= consts_ptr
;
911 params
.const_sizes_ptr
= num_consts_ptr
;
912 params
.system_values
= system_values
;
913 params
.inputs
= inputs
;
914 params
.context_ptr
= context_ptr
;
915 params
.sampler
= draw_sampler
;
916 params
.info
= &llvm
->draw
->vs
.vertex_shader
->info
;
917 params
.ssbo_ptr
= ssbos_ptr
;
918 params
.ssbo_sizes_ptr
= num_ssbos_ptr
;
919 params
.image
= draw_image
;
921 if (llvm
->draw
->vs
.vertex_shader
->state
.ir
.nir
&&
922 llvm
->draw
->vs
.vertex_shader
->state
.type
== PIPE_SHADER_IR_NIR
)
923 lp_build_nir_soa(variant
->gallivm
,
924 llvm
->draw
->vs
.vertex_shader
->state
.ir
.nir
,
928 lp_build_tgsi_soa(variant
->gallivm
,
935 unsigned chan
, attrib
;
936 struct lp_build_context bld
;
937 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
938 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
940 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
941 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
942 if (outputs
[attrib
][chan
]) {
943 switch (info
->output_semantic_name
[attrib
]) {
944 case TGSI_SEMANTIC_COLOR
:
945 case TGSI_SEMANTIC_BCOLOR
:
946 if (clamp_vertex_color
) {
947 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
948 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
949 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
961 fetch_instanced(struct gallivm_state
*gallivm
,
962 const struct util_format_description
*format_desc
,
963 struct lp_type vs_type
,
964 LLVMValueRef vb_stride
,
965 LLVMValueRef map_ptr
,
966 LLVMValueRef buffer_size_adj
,
967 LLVMValueRef
*inputs
,
970 LLVMTypeRef i32_t
= LLVMInt32TypeInContext(gallivm
->context
);
971 LLVMTypeRef aosf_t
, aosi_t
;
972 LLVMValueRef zero
= LLVMConstNull(i32_t
);
973 LLVMBuilderRef builder
= gallivm
->builder
;
974 LLVMValueRef stride
, buffer_overflowed
, aos
, index_valid
;
977 aosf_t
= lp_build_vec_type(gallivm
, lp_float32_vec4_type());
978 aosi_t
= lp_build_vec_type(gallivm
, lp_int32_vec4_type());
980 /* This mul can overflow. Wraparound is ok. */
981 stride
= LLVMBuildMul(builder
, vb_stride
, index
, "");
983 buffer_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGE
,
984 stride
, buffer_size_adj
,
985 "buffer_overflowed");
988 lp_build_print_value(gallivm
, " instance index = ", index
);
989 lp_build_print_value(gallivm
, " buffer overflowed = ", buffer_overflowed
);
992 index_valid
= LLVMBuildNot(builder
, buffer_overflowed
, "");
993 index_valid
= LLVMBuildSExt(builder
, index_valid
, i32_t
, "");
994 stride
= LLVMBuildAnd(builder
, stride
, index_valid
, "");
996 aos
= lp_build_fetch_rgba_aos(gallivm
,
998 lp_float32_vec4_type(),
1004 index_valid
= lp_build_broadcast(gallivm
, aosi_t
, index_valid
);
1005 aos
= LLVMBuildBitCast(builder
, aos
, aosi_t
, "");
1006 aos
= LLVMBuildAnd(builder
, aos
, index_valid
, "");
1007 aos
= LLVMBuildBitCast(builder
, aos
, aosf_t
, "");
1009 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1010 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
1011 inputs
[i
] = lp_build_extract_broadcast(gallivm
,
1012 lp_float32_vec4_type(),
1013 vs_type
, aos
, index
);
1019 fetch_vector(struct gallivm_state
*gallivm
,
1020 const struct util_format_description
*format_desc
,
1021 struct lp_type vs_type
,
1022 LLVMValueRef vb_stride
,
1023 LLVMValueRef map_ptr
,
1024 LLVMValueRef buffer_size_adj
,
1025 LLVMValueRef
*inputs
,
1026 LLVMValueRef indices
)
1028 LLVMBuilderRef builder
= gallivm
->builder
;
1029 struct lp_build_context blduivec
;
1030 struct lp_type fetch_type
= vs_type
;
1031 LLVMValueRef offset
, valid_mask
;
1034 lp_build_context_init(&blduivec
, gallivm
, lp_uint_type(vs_type
));
1036 vb_stride
= lp_build_broadcast_scalar(&blduivec
, vb_stride
);
1037 buffer_size_adj
= lp_build_broadcast_scalar(&blduivec
, buffer_size_adj
);
1039 /* This mul can overflow. Wraparound is ok. */
1040 offset
= lp_build_mul(&blduivec
, vb_stride
, indices
);
1042 valid_mask
= lp_build_compare(gallivm
, blduivec
.type
,
1043 PIPE_FUNC_LESS
, offset
, buffer_size_adj
);
1045 /* not valid elements use offset 0 */
1046 offset
= LLVMBuildAnd(builder
, offset
, valid_mask
, "");
1049 lp_build_print_value(gallivm
, " indices = ", indices
);
1050 lp_build_print_value(gallivm
, " offsets = ", offset
);
1051 lp_build_print_value(gallivm
, " valid_mask = ", valid_mask
);
1055 * Unlike fetch_instanced, use SoA fetch instead of multiple AoS fetches.
1056 * This should always produce better code.
1059 /* The type handling is annoying here... */
1060 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
&&
1061 format_desc
->channel
[0].pure_integer
) {
1062 if (format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_SIGNED
) {
1063 fetch_type
= lp_type_int_vec(vs_type
.width
, vs_type
.width
* vs_type
.length
);
1065 else if (format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
1066 fetch_type
= lp_type_uint_vec(vs_type
.width
, vs_type
.width
* vs_type
.length
);
1070 lp_build_fetch_rgba_soa(gallivm
, format_desc
,
1071 fetch_type
, FALSE
, map_ptr
, offset
,
1072 blduivec
.zero
, blduivec
.zero
,
1075 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1076 inputs
[i
] = LLVMBuildBitCast(builder
, inputs
[i
],
1077 lp_build_vec_type(gallivm
, vs_type
), "");
1080 /* out-of-bound fetches return all zeros */
1081 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1082 inputs
[i
] = LLVMBuildBitCast(builder
, inputs
[i
], blduivec
.vec_type
, "");
1083 inputs
[i
] = LLVMBuildAnd(builder
, inputs
[i
], valid_mask
, "");
1084 inputs
[i
] = LLVMBuildBitCast(builder
, inputs
[i
],
1085 lp_build_vec_type(gallivm
, vs_type
), "");
1091 store_aos(struct gallivm_state
*gallivm
,
1092 LLVMValueRef io_ptr
,
1096 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
1097 LLVMBuilderRef builder
= gallivm
->builder
;
1098 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
1099 LLVMValueRef indices
[3];
1101 indices
[0] = lp_build_const_int32(gallivm
, 0);
1103 indices
[2] = lp_build_const_int32(gallivm
, 0);
1105 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
1106 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
1109 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
1112 /* Unaligned store due to the vertex header */
1113 LLVMSetAlignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
1117 * Adjust the mask to architecture endianess. The mask will the store in struct:
1119 * struct vertex_header {
1120 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
1121 * unsigned edgeflag:1;
1123 * unsigned vertex_id:16;
1127 * On little-endian machine nothing needs to done, however on bit-endian machine
1128 * the mask's fields need to be adjusted with the algorithm:
1130 * uint32_t reverse (uint32_t x)
1132 * return (x >> 16) | // vertex_id
1133 * ((x & 0x3fff) << 18) | // clipmask
1134 * ((x & 0x4000) << 3) | // pad
1135 * ((x & 0x8000) << 1); // edgeflag
1139 adjust_mask(struct gallivm_state
*gallivm
,
1142 #if UTIL_ARCH_BIG_ENDIAN
1143 LLVMBuilderRef builder
= gallivm
->builder
;
1144 LLVMValueRef vertex_id
;
1145 LLVMValueRef clipmask
;
1147 LLVMValueRef edgeflag
;
1149 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
1150 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
1151 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
1153 pad
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
1154 pad
= LLVMBuildShl(builder
, pad
, lp_build_const_int32(gallivm
, 3), "");
1156 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
1157 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
1159 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
1161 mask
= LLVMBuildOr(builder
, mask
, pad
, "");
1163 mask
= LLVMBuildOr(builder
, mask
, edgeflag
, "");
1169 store_aos_array(struct gallivm_state
*gallivm
,
1170 struct lp_type soa_type
,
1171 LLVMValueRef io_ptr
,
1172 LLVMValueRef
*indices
,
1176 LLVMValueRef clipmask
,
1177 boolean need_edgeflag
)
1179 LLVMBuilderRef builder
= gallivm
->builder
;
1180 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
1181 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1182 LLVMValueRef linear_inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1183 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1184 int vector_length
= soa_type
.length
;
1187 debug_assert(TGSI_NUM_CHANNELS
== 4);
1189 for (i
= 0; i
< vector_length
; i
++) {
1190 linear_inds
[i
] = lp_build_const_int32(gallivm
, i
);
1192 inds
[i
] = indices
[i
];
1194 inds
[i
] = linear_inds
[i
];
1196 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1200 /* store vertex header for each of the n vertices */
1201 LLVMValueRef val
, cliptmp
;
1202 int vertex_id_pad_edgeflag
;
1204 /* If this assertion fails, it means we need to update the bit twidding
1205 * code here. See struct vertex_header in draw_private.h.
1207 assert(DRAW_TOTAL_CLIP_PLANES
==14);
1208 /* initialize vertex id:16 = 0xffff, pad:1 = 0, edgeflag:1 = 1 */
1209 if (!need_edgeflag
) {
1210 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
1213 vertex_id_pad_edgeflag
= (0xffff << 16);
1215 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
),
1216 vertex_id_pad_edgeflag
);
1217 /* OR with the clipmask */
1218 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
1219 for (i
= 0; i
< vector_length
; i
++) {
1220 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
1221 val
= LLVMBuildExtractElement(builder
, cliptmp
, linear_inds
[i
], "");
1222 val
= adjust_mask(gallivm
, val
);
1224 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
1225 io_ptrs
[i
], inds
[i
], val
);
1227 LLVMBuildStore(builder
, val
, id_ptr
);
1231 /* store for each of the n vertices */
1232 for (i
= 0; i
< vector_length
; i
++) {
1233 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
1239 convert_to_aos(struct gallivm_state
*gallivm
,
1241 LLVMValueRef
*indices
,
1242 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1243 LLVMValueRef clipmask
,
1245 struct lp_type soa_type
,
1246 boolean need_edgeflag
)
1248 LLVMBuilderRef builder
= gallivm
->builder
;
1249 unsigned chan
, attrib
, i
;
1252 lp_build_printf(gallivm
, " # storing begin\n");
1254 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
1255 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
1256 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
1257 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
1258 if (outputs
[attrib
][chan
]) {
1259 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
1260 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
1262 lp_build_printf(gallivm
, "output %d : %d ",
1263 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
1265 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
1267 lp_build_print_value(gallivm
, "val = ", out
);
1270 LLVMBuildBitCast(builder
, out
, lp_build_int_vec_type(gallivm
, soa_type
), "");
1272 lp_build_print_value(gallivm
, " ival = ", iv
);
1283 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
1284 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
1286 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
1288 for (i
= 0; i
< soa_type
.length
; ++i
) {
1289 aos
[i
] = lp_build_extract_range(gallivm
,
1290 soa
[i
% TGSI_NUM_CHANNELS
],
1291 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1296 store_aos_array(gallivm
,
1306 lp_build_printf(gallivm
, " # storing end\n");
1312 * Stores original vertex positions in clip coordinates
1315 store_clip(struct gallivm_state
*gallivm
,
1316 const struct lp_type vs_type
,
1317 LLVMValueRef io_ptr
,
1318 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1321 LLVMBuilderRef builder
= gallivm
->builder
;
1322 LLVMValueRef soa
[4];
1323 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
1324 LLVMValueRef indices
[2];
1325 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1326 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1327 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1328 LLVMTypeRef clip_ptr_type
=
1329 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
),
1334 indices
[1] = lp_build_const_int32(gallivm
, 0);
1336 for (i
= 0; i
< vs_type
.length
; i
++) {
1337 inds
[i
] = lp_build_const_int32(gallivm
, i
);
1338 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1341 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
1342 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
1343 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
1344 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
1346 for (i
= 0; i
< vs_type
.length
; i
++) {
1347 clip_ptrs
[i
] = draw_jit_header_clip_pos(gallivm
, io_ptrs
[i
]);
1350 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
1351 for (i
= 0; i
< vs_type
.length
; ++i
) {
1352 aos
[i
] = lp_build_extract_range(gallivm
,
1353 soa
[i
% TGSI_NUM_CHANNELS
],
1354 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1358 for (j
= 0; j
< vs_type
.length
; j
++) {
1359 LLVMValueRef clip_ptr
;
1361 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
1362 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
1364 /* Unaligned store */
1365 LLVMSetAlignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
1371 * Transforms the outputs for viewport mapping
1374 generate_viewport(struct draw_llvm_variant
*variant
,
1375 LLVMBuilderRef builder
,
1376 struct lp_type vs_type
,
1377 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1378 LLVMValueRef context_ptr
)
1381 struct gallivm_state
*gallivm
= variant
->gallivm
;
1382 struct lp_type f32_type
= vs_type
;
1383 const unsigned pos
= variant
->llvm
->draw
->vs
.position_output
;
1384 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1385 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn*/
1386 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1387 LLVMValueRef vp_ptr
= draw_jit_context_viewports(gallivm
, context_ptr
);
1389 /* We treat pipe_viewport_state as a float array */
1390 const int scale_index_offset
= offsetof(struct pipe_viewport_state
, scale
) / sizeof(float);
1391 const int trans_index_offset
= offsetof(struct pipe_viewport_state
, translate
) / sizeof(float);
1393 /* for 1/w convention*/
1394 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1395 LLVMBuildStore(builder
, out3
, outputs
[pos
][3]);
1397 /* Viewport Mapping */
1398 for (i
=0; i
<3; i
++) {
1399 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[pos
][i
], ""); /*x0 x1 .. xn*/
1402 LLVMValueRef scale_i
;
1403 LLVMValueRef trans_i
;
1406 index
= lp_build_const_int32(gallivm
, i
+ scale_index_offset
);
1407 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1409 index
= lp_build_const_int32(gallivm
, i
+ trans_index_offset
);
1410 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1412 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1413 LLVMBuildLoad(builder
, scale_i
, "scale"));
1414 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1415 LLVMBuildLoad(builder
, trans_i
, "trans"));
1418 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1419 /* mult by scale, add translation */
1420 out
= lp_build_fmuladd(builder
, out
, scale
, trans
);
1422 /* store transformed outputs */
1423 LLVMBuildStore(builder
, out
, outputs
[pos
][i
]);
1430 * Returns clipmask as nxi32 bitmask for the n vertices
1433 generate_clipmask(struct draw_llvm
*llvm
,
1434 struct gallivm_state
*gallivm
,
1435 struct lp_type vs_type
,
1436 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1437 struct draw_llvm_variant_key
*key
,
1438 LLVMValueRef context_ptr
,
1439 boolean
*have_clipdist
)
1441 LLVMBuilderRef builder
= gallivm
->builder
;
1442 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1443 LLVMValueRef test
, temp
;
1444 LLVMValueRef zero
, shift
;
1445 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1446 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1447 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1448 struct lp_type f32_type
= vs_type
;
1449 struct lp_type i32_type
= lp_int_type(vs_type
);
1450 const unsigned pos
= llvm
->draw
->vs
.position_output
;
1451 const unsigned cv
= llvm
->draw
->vs
.clipvertex_output
;
1452 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1453 boolean have_cd
= false;
1454 boolean clip_user
= key
->clip_user
;
1455 unsigned ucp_enable
= key
->ucp_enable
;
1458 cd
[0] = llvm
->draw
->vs
.ccdistance_output
[0];
1459 cd
[1] = llvm
->draw
->vs
.ccdistance_output
[1];
1461 if (cd
[0] != pos
|| cd
[1] != pos
)
1464 if (num_written_clipdistance
&& !clip_user
) {
1466 ucp_enable
= (1 << num_written_clipdistance
) - 1;
1469 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1470 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1471 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1472 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1475 * load clipvertex and position from correct locations.
1476 * if they are the same just load them once.
1478 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1479 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1480 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1481 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1483 if (clip_user
&& cv
!= pos
) {
1484 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1485 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1486 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1487 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1496 * Be careful with the comparisons and NaNs (using llvm's unordered
1497 * comparisons here).
1499 /* Cliptest, for hardwired planes */
1501 * XXX should take guardband into account (currently not in key).
1502 * Otherwise might run the draw pipeline stages for nothing.
1506 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1508 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1512 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1513 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1514 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1515 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1516 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1519 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1520 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1521 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1522 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1525 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1526 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1527 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1528 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1529 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1533 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1534 if (key
->clip_halfz
) {
1536 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1537 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1538 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1542 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1543 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1544 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1545 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1548 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1549 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1550 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1551 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1555 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1556 LLVMValueRef indices
[3];
1557 LLVMValueRef is_nan_or_inf
;
1559 /* userclip planes */
1560 while (ucp_enable
) {
1561 unsigned plane_idx
= ffs(ucp_enable
)-1;
1562 ucp_enable
&= ~(1 << plane_idx
);
1565 if (have_cd
&& num_written_clipdistance
) {
1566 LLVMValueRef clipdist
;
1570 *have_clipdist
= TRUE
;
1572 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1574 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1576 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1577 is_nan_or_inf
= lp_build_is_inf_or_nan(gallivm
, vs_type
, clipdist
);
1578 test
= LLVMBuildOr(builder
, test
, is_nan_or_inf
, "");
1579 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1LL << plane_idx
);
1580 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1581 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1583 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1584 indices
[0] = lp_build_const_int32(gallivm
, 0);
1585 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1587 indices
[2] = lp_build_const_int32(gallivm
, 0);
1588 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1589 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1590 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1591 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1593 indices
[2] = lp_build_const_int32(gallivm
, 1);
1594 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1595 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1596 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1597 sum
= lp_build_fmuladd(builder
, planes
, cv_y
, sum
);
1599 indices
[2] = lp_build_const_int32(gallivm
, 2);
1600 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1601 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1602 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1603 sum
= lp_build_fmuladd(builder
, planes
, cv_z
, sum
);
1605 indices
[2] = lp_build_const_int32(gallivm
, 3);
1606 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1607 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1608 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1609 sum
= lp_build_fmuladd(builder
, planes
, cv_w
, sum
);
1611 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1612 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1LL << plane_idx
);
1613 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1614 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1618 if (key
->need_edgeflags
) {
1620 * This isn't really part of clipmask but stored the same in vertex
1621 * header later, so do it here.
1623 unsigned edge_attr
= llvm
->draw
->vs
.edgeflag_output
;
1624 LLVMValueRef one
= lp_build_const_vec(gallivm
, f32_type
, 1.0);
1625 LLVMValueRef edgeflag
= LLVMBuildLoad(builder
, outputs
[edge_attr
][0], "");
1626 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_EQUAL
, one
, edgeflag
);
1627 temp
= lp_build_const_int_vec(gallivm
, i32_type
,
1628 1LL << DRAW_TOTAL_CLIP_PLANES
);
1629 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1630 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1637 * Returns boolean if any clipping has occurred
1638 * Used zero/one i8 value to represent boolean
1641 clipmask_booli8(struct gallivm_state
*gallivm
,
1642 const struct lp_type vs_type
,
1643 LLVMValueRef clipmask_bool_ptr
,
1644 boolean edgeflag_in_clipmask
)
1646 LLVMBuilderRef builder
= gallivm
->builder
;
1647 LLVMTypeRef int8_type
= LLVMInt8TypeInContext(gallivm
->context
);
1648 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1650 struct lp_build_context bldivec
;
1652 lp_build_context_init(&bldivec
, gallivm
, lp_int_type(vs_type
));
1655 * We need to invert the edgeflag bit from the clipmask here
1656 * (because the result is really if we want to run the pipeline or not
1657 * and we (may) need it if edgeflag was 0).
1659 if (edgeflag_in_clipmask
) {
1660 LLVMValueRef edge
= lp_build_const_int_vec(gallivm
, bldivec
.type
,
1661 1LL << DRAW_TOTAL_CLIP_PLANES
);
1662 clipmask_bool
= LLVMBuildXor(builder
, clipmask_bool
, edge
, "");
1666 * XXX: probably should mask off bits from the mask which come from
1667 * vertices which were beyond the count (i.e. indices_valid for
1668 * linear fetches, for elts ones we don't have the correct mask
1669 * right now). Otherwise might run the pipeline for nothing,
1670 * though everything should still work.
1672 ret
= lp_build_any_true_range(&bldivec
, vs_type
.length
, clipmask_bool
);
1673 ret
= LLVMBuildZExt(builder
, ret
, int8_type
, "");
1678 draw_gs_llvm_fetch_input(const struct lp_build_gs_iface
*gs_iface
,
1679 struct lp_build_context
* bld
,
1680 boolean is_vindex_indirect
,
1681 LLVMValueRef vertex_index
,
1682 boolean is_aindex_indirect
,
1683 LLVMValueRef attrib_index
,
1684 LLVMValueRef swizzle_index
)
1686 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1687 struct gallivm_state
*gallivm
= bld
->gallivm
;
1688 LLVMBuilderRef builder
= gallivm
->builder
;
1689 LLVMValueRef indices
[3];
1691 struct lp_type type
= bld
->type
;
1693 if (is_vindex_indirect
|| is_aindex_indirect
) {
1696 for (i
= 0; i
< type
.length
; ++i
) {
1697 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1698 LLVMValueRef vert_chan_index
= vertex_index
;
1699 LLVMValueRef attr_chan_index
= attrib_index
;
1700 LLVMValueRef channel_vec
, value
;
1702 if (is_vindex_indirect
) {
1703 vert_chan_index
= LLVMBuildExtractElement(builder
,
1704 vertex_index
, idx
, "");
1706 if (is_aindex_indirect
) {
1707 attr_chan_index
= LLVMBuildExtractElement(builder
,
1708 attrib_index
, idx
, "");
1711 indices
[0] = vert_chan_index
;
1712 indices
[1] = attr_chan_index
;
1713 indices
[2] = swizzle_index
;
1715 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1716 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1717 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1719 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1722 indices
[0] = vertex_index
;
1723 indices
[1] = attrib_index
;
1724 indices
[2] = swizzle_index
;
1726 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1727 res
= LLVMBuildLoad(builder
, res
, "");
1734 draw_gs_llvm_emit_vertex(const struct lp_build_gs_iface
*gs_base
,
1735 struct lp_build_context
* bld
,
1736 LLVMValueRef (*outputs
)[4],
1737 LLVMValueRef emitted_vertices_vec
,
1738 LLVMValueRef stream_id
)
1740 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1741 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1742 struct gallivm_state
*gallivm
= variant
->gallivm
;
1743 LLVMBuilderRef builder
= gallivm
->builder
;
1744 struct lp_type gs_type
= bld
->type
;
1745 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1746 lp_int_type(gs_type
), 0);
1747 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1748 LLVMValueRef next_prim_offset
=
1749 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1750 LLVMValueRef io
= variant
->io_ptr
;
1752 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1754 for (i
= 0; i
< gs_type
.length
; ++i
) {
1755 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1756 LLVMValueRef currently_emitted
=
1757 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1758 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1759 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1762 LLVMValueRef stream_idx
= LLVMBuildExtractElement(builder
, stream_id
, lp_build_const_int32(gallivm
, 0), "");
1763 LLVMValueRef cnd
= LLVMBuildICmp(builder
, LLVMIntULT
, stream_idx
, lp_build_const_int32(gallivm
, variant
->shader
->base
.num_vertex_streams
), "");
1764 struct lp_build_if_state if_ctx
;
1765 lp_build_if(&if_ctx
, gallivm
, cnd
);
1766 io
= lp_build_pointer_get(builder
, io
, LLVMBuildExtractElement(builder
, stream_id
, lp_build_const_int32(gallivm
, 0), ""));
1768 convert_to_aos(gallivm
, io
, indices
,
1770 gs_info
->num_outputs
, gs_type
,
1772 lp_build_endif(&if_ctx
);
1776 draw_gs_llvm_end_primitive(const struct lp_build_gs_iface
*gs_base
,
1777 struct lp_build_context
* bld
,
1778 LLVMValueRef total_emitted_vertices_vec_ptr
,
1779 LLVMValueRef verts_per_prim_vec
,
1780 LLVMValueRef emitted_prims_vec
,
1781 LLVMValueRef mask_vec
)
1783 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1784 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1785 struct gallivm_state
*gallivm
= variant
->gallivm
;
1786 LLVMBuilderRef builder
= gallivm
->builder
;
1787 LLVMValueRef prim_lengts_ptr
=
1788 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1791 for (i
= 0; i
< bld
->type
.length
; ++i
) {
1792 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1793 LLVMValueRef prims_emitted
=
1794 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1795 LLVMValueRef store_ptr
;
1796 LLVMValueRef num_vertices
=
1797 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1799 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1800 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1801 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1802 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1807 draw_gs_llvm_epilogue(const struct lp_build_gs_iface
*gs_base
,
1808 LLVMValueRef total_emitted_vertices_vec
,
1809 LLVMValueRef emitted_prims_vec
, unsigned stream
)
1811 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1812 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1813 struct gallivm_state
*gallivm
= variant
->gallivm
;
1814 LLVMBuilderRef builder
= gallivm
->builder
;
1815 LLVMValueRef emitted_verts_ptr
=
1816 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1817 LLVMValueRef emitted_prims_ptr
=
1818 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1819 LLVMValueRef stream_val
= lp_build_const_int32(gallivm
, stream
);
1821 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &stream_val
, 1, "");
1822 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &stream_val
, 1, "");
1824 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1825 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1829 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
)
1831 struct gallivm_state
*gallivm
= variant
->gallivm
;
1832 LLVMContextRef context
= gallivm
->context
;
1833 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1834 LLVMTypeRef arg_types
[12];
1835 unsigned num_arg_types
= ARRAY_SIZE(arg_types
);
1836 LLVMTypeRef func_type
;
1837 LLVMValueRef context_ptr
;
1838 LLVMBasicBlockRef block
;
1839 LLVMBuilderRef builder
;
1841 struct lp_type vs_type
;
1842 LLVMValueRef count
, fetch_elts
, start_or_maxelt
;
1843 LLVMValueRef vertex_id_offset
;
1844 LLVMValueRef stride
, step
, io_itr
;
1845 LLVMValueRef ind_vec
, start_vec
, have_elts
, fetch_max
, tmp
;
1846 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1847 LLVMValueRef vb_stride
[PIPE_MAX_ATTRIBS
];
1848 LLVMValueRef map_ptr
[PIPE_MAX_ATTRIBS
];
1849 LLVMValueRef buffer_size_adj
[PIPE_MAX_ATTRIBS
];
1850 LLVMValueRef instance_index
[PIPE_MAX_ATTRIBS
];
1851 LLVMValueRef fake_buf_ptr
, fake_buf
;
1853 struct draw_context
*draw
= llvm
->draw
;
1854 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1856 struct lp_build_context bld
, blduivec
;
1857 struct lp_build_loop_state lp_loop
;
1858 struct lp_build_if_state if_ctx
;
1859 const int vector_length
= lp_native_vector_width
/ 32;
1860 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1861 struct lp_build_sampler_soa
*sampler
= 0;
1862 struct lp_build_image_soa
*image
= NULL
;
1863 LLVMValueRef ret
, clipmask_bool_ptr
;
1864 struct draw_llvm_variant_key
*key
= &variant
->key
;
1865 /* If geometry shader is present we need to skip both the viewport
1866 * transformation and clipping otherwise the inputs to the geometry
1867 * shader will be incorrect.
1868 * The code can't handle vp transform when vs writes vp index neither
1869 * (though this would be fixable here, but couldn't just broadcast
1872 const boolean bypass_viewport
= key
->has_gs_or_tes
|| key
->bypass_viewport
||
1873 vs_info
->writes_viewport_index
;
1874 const boolean enable_cliptest
= !key
->has_gs_or_tes
&& (key
->clip_xy
||
1877 key
->need_edgeflags
);
1878 LLVMValueRef variant_func
;
1879 const unsigned pos
= draw
->vs
.position_output
;
1880 const unsigned cv
= draw
->vs
.clipvertex_output
;
1881 boolean have_clipdist
= FALSE
;
1882 struct lp_bld_tgsi_system_values system_values
;
1884 memset(&system_values
, 0, sizeof(system_values
));
1885 memset(&outputs
, 0, sizeof(outputs
));
1886 snprintf(func_name
, sizeof(func_name
), "draw_llvm_vs_variant%u",
1887 variant
->shader
->variants_cached
);
1890 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1891 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1892 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1893 arg_types
[i
++] = int32_type
; /* count */
1894 arg_types
[i
++] = int32_type
; /* start/fetch_elt_max */
1895 arg_types
[i
++] = int32_type
; /* stride */
1896 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1897 arg_types
[i
++] = int32_type
; /* instance_id */
1898 arg_types
[i
++] = int32_type
; /* vertex_id_offset */
1899 arg_types
[i
++] = int32_type
; /* start_instance */
1900 arg_types
[i
++] = LLVMPointerType(int32_type
, 0); /* fetch_elts */
1901 arg_types
[i
++] = int32_type
; /* draw_id */
1903 func_type
= LLVMFunctionType(LLVMInt8TypeInContext(context
),
1904 arg_types
, num_arg_types
, 0);
1906 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
1907 variant
->function
= variant_func
;
1909 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1910 for (i
= 0; i
< num_arg_types
; ++i
)
1911 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1912 lp_add_function_attr(variant_func
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
1914 context_ptr
= LLVMGetParam(variant_func
, 0);
1915 io_ptr
= LLVMGetParam(variant_func
, 1);
1916 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1917 count
= LLVMGetParam(variant_func
, 3);
1919 * XXX: the maxelt part is unused. Not really useful, since we cannot
1920 * get index buffer overflows due to vsplit (which provides its own
1921 * elts buffer, with a different size than what's passed in here).
1923 start_or_maxelt
= LLVMGetParam(variant_func
, 4);
1925 * XXX: stride is actually unused. The stride we use is strictly calculated
1926 * from the number of outputs (including the draw_extra outputs).
1927 * Should probably fix some day (we need a new vs just because of extra
1928 * outputs which the generated vs won't touch).
1930 stride
= LLVMGetParam(variant_func
, 5);
1931 vb_ptr
= LLVMGetParam(variant_func
, 6);
1932 system_values
.instance_id
= LLVMGetParam(variant_func
, 7);
1933 vertex_id_offset
= LLVMGetParam(variant_func
, 8);
1934 system_values
.base_instance
= LLVMGetParam(variant_func
, 9);
1935 fetch_elts
= LLVMGetParam(variant_func
, 10);
1936 system_values
.draw_id
= LLVMGetParam(variant_func
, 11);
1938 lp_build_name(context_ptr
, "context");
1939 lp_build_name(io_ptr
, "io");
1940 lp_build_name(vbuffers_ptr
, "vbuffers");
1941 lp_build_name(count
, "count");
1942 lp_build_name(start_or_maxelt
, "start_or_maxelt");
1943 lp_build_name(stride
, "stride");
1944 lp_build_name(vb_ptr
, "vb");
1945 lp_build_name(system_values
.instance_id
, "instance_id");
1946 lp_build_name(vertex_id_offset
, "vertex_id_offset");
1947 lp_build_name(system_values
.base_instance
, "start_instance");
1948 lp_build_name(fetch_elts
, "fetch_elts");
1949 lp_build_name(system_values
.draw_id
, "draw_id");
1955 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1956 builder
= gallivm
->builder
;
1957 LLVMPositionBuilderAtEnd(builder
, block
);
1959 memset(&vs_type
, 0, sizeof vs_type
);
1960 vs_type
.floating
= TRUE
; /* floating point values */
1961 vs_type
.sign
= TRUE
; /* values are signed */
1962 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1963 vs_type
.width
= 32; /* 32-bit float */
1964 vs_type
.length
= vector_length
;
1966 lp_build_context_init(&bld
, gallivm
, lp_type_uint(32));
1967 lp_build_context_init(&blduivec
, gallivm
, lp_uint_type(vs_type
));
1969 /* hold temporary "bool" clipmask */
1970 clipmask_bool_ptr
= lp_build_alloca(gallivm
, blduivec
.vec_type
, "");
1972 fake_buf
= lp_build_alloca_undef(gallivm
,
1973 LLVMVectorType(LLVMInt64TypeInContext(context
), 4), "");
1974 fake_buf
= LLVMBuildBitCast(builder
, fake_buf
,
1975 LLVMPointerType(LLVMInt8TypeInContext(context
), 0), "");
1976 fake_buf_ptr
= LLVMBuildGEP(builder
, fake_buf
, &bld
.zero
, 1, "");
1978 /* code generated texture sampling */
1979 sampler
= draw_llvm_sampler_soa_create(draw_llvm_variant_key_samplers(key
));
1981 image
= draw_llvm_image_soa_create(draw_llvm_variant_key_images(key
));
1983 step
= lp_build_const_int32(gallivm
, vector_length
);
1985 ind_vec
= blduivec
.undef
;
1986 for (i
= 0; i
< vs_type
.length
; i
++) {
1987 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
1988 ind_vec
= LLVMBuildInsertElement(builder
, ind_vec
, index
, index
, "");
1991 have_elts
= LLVMBuildICmp(builder
, LLVMIntNE
,
1992 LLVMConstPointerNull(arg_types
[10]), fetch_elts
, "");
1994 fetch_max
= LLVMBuildSub(builder
, count
, bld
.one
, "fetch_max");
1995 fetch_max
= lp_build_broadcast_scalar(&blduivec
, fetch_max
);
1997 * Only needed for non-indexed path.
1999 start_vec
= lp_build_broadcast_scalar(&blduivec
, start_or_maxelt
);
2002 * Pre-calculate everything which is constant per shader invocation.
2004 for (j
= 0; j
< key
->nr_vertex_elements
; ++j
) {
2005 LLVMValueRef vb_buffer_offset
, buffer_size
, temp_ptr
;
2006 LLVMValueRef vb_info
, vbuffer_ptr
, buf_offset
, ofbit
;
2007 struct pipe_vertex_element
*velem
= &key
->vertex_element
[j
];
2008 LLVMValueRef vb_index
=
2009 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
2010 LLVMValueRef bsize
= lp_build_const_int32(gallivm
,
2011 util_format_get_blocksize(velem
->src_format
));
2012 LLVMValueRef src_offset
= lp_build_const_int32(gallivm
,
2014 struct lp_build_if_state if_ctx
;
2016 if (velem
->src_format
!= PIPE_FORMAT_NONE
) {
2017 vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
, &vb_index
, 1, "");
2018 vb_info
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
2019 vb_stride
[j
] = draw_jit_vbuffer_stride(gallivm
, vb_info
);
2020 vb_stride
[j
] = LLVMBuildZExt(gallivm
->builder
, vb_stride
[j
],
2021 LLVMInt32TypeInContext(context
), "");
2022 vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vb_info
);
2023 map_ptr
[j
] = draw_jit_dvbuffer_map(gallivm
, vbuffer_ptr
);
2024 buffer_size
= draw_jit_dvbuffer_size(gallivm
, vbuffer_ptr
);
2028 * We'll set buffer_size_adj to zero if we have of, so it will
2029 * always overflow later automatically without having to keep ofbit.
2030 * Overflows (with normal wraparound) doing the actual offset
2031 * calculation should be ok, just not for the buffer size calc.
2032 * It would also be possible to detect such overflows and return
2033 * zeros if that happens, but this would be more complex.
2035 buf_offset
= lp_build_add(&bld
, vb_buffer_offset
, src_offset
);
2036 tmp
= lp_build_sub(&bld
, bsize
, bld
.one
);
2037 buffer_size_adj
[j
] = lp_build_usub_overflow(gallivm
, buffer_size
, tmp
,
2039 buffer_size_adj
[j
] = lp_build_usub_overflow(gallivm
, buffer_size_adj
[j
],
2040 buf_offset
, &ofbit
);
2043 * We can't easily set fake vertex buffers outside the generated code.
2044 * Hence, set fake vertex buffers here instead basically, so fetch
2045 * code can always fetch using offset 0, eliminating all control flow
2046 * inside the main loop.
2047 * (Alternatively, could have control flow per vector skipping fetch
2048 * if ofbit is true.)
2050 if (velem
->instance_divisor
) {
2052 * Index is equal to the start instance plus the number of current
2053 * instance divided by the divisor. In this case we compute it as:
2054 * index = start_instance + (instance_id / divisor).
2055 * Note we could actually do the fetch here, outside the loop -
2056 * it's all constant, hopefully llvm recognizes this.
2058 LLVMValueRef current_instance
;
2059 current_instance
= LLVMBuildUDiv(builder
, system_values
.instance_id
,
2060 lp_build_const_int32(gallivm
,
2061 velem
->instance_divisor
),
2062 "instance_divisor");
2063 instance_index
[j
] = lp_build_uadd_overflow(gallivm
, system_values
.base_instance
,
2064 current_instance
, &ofbit
);
2067 buffer_size_adj
[j
] = LLVMBuildSelect(builder
, ofbit
, bld
.zero
,
2068 buffer_size_adj
[j
], "");
2070 temp_ptr
= lp_build_alloca_undef(gallivm
,
2071 LLVMPointerType(LLVMInt8TypeInContext(context
), 0), "");
2073 lp_build_if(&if_ctx
, gallivm
, ofbit
);
2075 LLVMBuildStore(builder
, fake_buf_ptr
, temp_ptr
);
2077 lp_build_else(&if_ctx
);
2079 map_ptr
[j
] = LLVMBuildGEP(builder
, map_ptr
[j
], &buf_offset
, 1, "");
2080 LLVMBuildStore(builder
, map_ptr
[j
], temp_ptr
);
2082 lp_build_endif(&if_ctx
);
2083 map_ptr
[j
] = LLVMBuildLoad(builder
, temp_ptr
, "map_ptr");
2086 lp_build_printf(gallivm
, "velem %d, vbuf index = %u, vb_stride = %u\n",
2087 lp_build_const_int32(gallivm
, j
),
2088 vb_index
, vb_stride
[j
]);
2089 lp_build_printf(gallivm
,
2090 " vb_buffer_offset = %u, src_offset = %u, buf_offset = %u\n",
2091 vb_buffer_offset
, src_offset
, buf_offset
);
2092 lp_build_printf(gallivm
, " buffer size = %u, blocksize = %u\n",
2093 buffer_size
, bsize
);
2094 lp_build_printf(gallivm
, " instance_id = %u\n", system_values
.instance_id
);
2099 lp_build_loop_begin(&lp_loop
, gallivm
, bld
.zero
);
2101 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
2103 LLVMValueRef clipmask
; /* holds the clipmask value */
2104 LLVMValueRef true_index_array
, index_store
;
2105 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
2107 io_itr
= lp_loop
.counter
;
2109 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
2111 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
2112 io_itr
, io
, lp_loop
.counter
);
2115 true_index_array
= lp_build_broadcast_scalar(&blduivec
, lp_loop
.counter
);
2116 true_index_array
= LLVMBuildAdd(builder
, true_index_array
, ind_vec
, "");
2118 LLVMValueRef exec_mask
= lp_build_cmp(&blduivec
, PIPE_FUNC_LEQUAL
, true_index_array
, fetch_max
);
2120 * Limit indices to fetch_max, otherwise might try to access indices
2121 * beyond index buffer (or rather vsplit elt buffer) size.
2122 * Could probably safely (?) skip this for non-indexed draws and
2123 * simplify things minimally (by removing it could combine the ind_vec
2124 * and start_vec adds). I think the only effect for non-indexed draws will
2125 * be that for the invalid elements they will be all fetched from the
2126 * same location as the last valid one, but noone should really care.
2128 true_index_array
= lp_build_min(&blduivec
, true_index_array
, fetch_max
);
2130 index_store
= lp_build_alloca_undef(gallivm
, blduivec
.vec_type
, "index_store");
2132 lp_build_if(&if_ctx
, gallivm
, have_elts
);
2135 * Note: you'd expect some comparison/clamp against fetch_elt_max
2137 * There used to be one here but it was incorrect: overflow was
2138 * detected if index > fetch_elt_max - but the correct condition
2139 * would be index >= fetch_elt_max (since this is just size of elts
2140 * buffer / element size).
2141 * Using the correct condition however will cause failures - due to
2142 * vsplit/vcache code which rebases indices. So, as an example, if
2143 * fetch_elt_max is just 1 and fetch_count 2, vsplit cache will
2144 * replace all invalid indices with 0 - which in case of elt_bias
2145 * not being zero will get a different fetch index than the valid
2146 * index 0. So, just rely on vsplit code preventing out-of-bounds
2147 * fetches. This is also why it's safe to do elts fetch even if there
2148 * was no index buffer bound - the real buffer is never seen here, at
2149 * least not if there are index buffer overflows...
2153 * XXX should not have to do this, as scale can be handled
2154 * natively by loads (hits asserts though).
2156 tmp
= lp_build_shl_imm(&blduivec
, true_index_array
, 2);
2157 fetch_elts
= LLVMBuildBitCast(builder
, fetch_elts
,
2158 LLVMPointerType(LLVMInt8TypeInContext(context
),
2160 tmp
= lp_build_gather(gallivm
, vs_type
.length
,
2162 fetch_elts
, tmp
, FALSE
);
2163 LLVMBuildStore(builder
, tmp
, index_store
);
2165 lp_build_else(&if_ctx
);
2167 tmp
= LLVMBuildAdd(builder
, true_index_array
, start_vec
, "");
2168 LLVMBuildStore(builder
, tmp
, index_store
);
2170 lp_build_endif(&if_ctx
);
2172 true_index_array
= LLVMBuildLoad(builder
, index_store
, "");
2174 for (j
= 0; j
< key
->nr_vertex_elements
; ++j
) {
2175 struct pipe_vertex_element
*velem
= &key
->vertex_element
[j
];
2176 const struct util_format_description
*format_desc
=
2177 util_format_description(velem
->src_format
);
2179 if (format_desc
->format
== PIPE_FORMAT_NONE
) {
2180 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
2181 inputs
[j
][i
] = lp_build_zero(gallivm
, vs_type
);
2184 else if (velem
->instance_divisor
) {
2185 fetch_instanced(gallivm
, format_desc
, vs_type
,
2186 vb_stride
[j
], map_ptr
[j
],
2188 inputs
[j
], instance_index
[j
]);
2191 fetch_vector(gallivm
, format_desc
, vs_type
,
2192 vb_stride
[j
], map_ptr
[j
],
2194 inputs
[j
], true_index_array
);
2198 struct lp_build_mask_context mask
;
2200 lp_build_mask_begin(&mask
, gallivm
, vs_type
, exec_mask
);
2201 /* In the paths with elts vertex id has to be unaffected by the
2202 * index bias and because indices inside our elements array have
2203 * already had index bias applied we need to subtract it here to
2204 * get back to the original index.
2205 * in the linear paths vertex id has to be unaffected by the
2206 * original start index and because we abuse the 'start' variable
2207 * to either represent the actual start index or the index at which
2208 * the primitive was split (we split rendering into chunks of at
2209 * most 4095-vertices) we need to back out the original start
2210 * index out of our vertex id here.
2211 * for ARB_shader_draw_parameters, base_vertex should be 0 for non-indexed draws.
2213 LLVMValueRef base_vertex
= lp_build_select(&bld
, have_elts
, vertex_id_offset
, lp_build_const_int32(gallivm
, 0));;
2214 system_values
.basevertex
= lp_build_broadcast_scalar(&blduivec
, base_vertex
);
2215 system_values
.vertex_id
= true_index_array
;
2216 system_values
.vertex_id_nobase
= LLVMBuildSub(builder
, true_index_array
,
2217 lp_build_broadcast_scalar(&blduivec
, vertex_id_offset
), "");
2219 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
2220 generate_vs(variant
,
2229 key
->clamp_vertex_color
,
2232 lp_build_mask_end(&mask
);
2233 if (pos
!= -1 && cv
!= -1) {
2234 /* store original positions in clip before further manipulation */
2235 store_clip(gallivm
, vs_type
, io
, outputs
, pos
);
2238 if (enable_cliptest
) {
2239 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
2240 /* allocate clipmask, assign it integer type */
2241 clipmask
= generate_clipmask(llvm
,
2246 context_ptr
, &have_clipdist
);
2247 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
2248 /* store temporary clipping boolean value */
2249 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
2252 clipmask
= blduivec
.zero
;
2255 /* do viewport mapping */
2256 if (!bypass_viewport
) {
2257 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
2261 clipmask
= blduivec
.zero
;
2264 /* store clipmask in vertex header,
2265 * original positions in clip
2266 * and transformed positions in data
2268 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
2269 vs_info
->num_outputs
, vs_type
,
2270 enable_cliptest
&& key
->need_edgeflags
);
2272 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
2274 sampler
->destroy(sampler
);
2275 image
->destroy(image
);
2277 /* return clipping boolean value for function */
2278 ret
= clipmask_booli8(gallivm
, vs_type
, clipmask_bool_ptr
,
2279 enable_cliptest
&& key
->need_edgeflags
);
2281 LLVMBuildRet(builder
, ret
);
2283 gallivm_verify_function(gallivm
, variant_func
);
2287 struct draw_llvm_variant_key
*
2288 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2291 struct draw_llvm_variant_key
*key
;
2292 struct draw_sampler_static_state
*draw_sampler
;
2293 struct draw_image_static_state
*draw_image
;
2295 key
= (struct draw_llvm_variant_key
*)store
;
2297 memset(key
, 0, offsetof(struct draw_llvm_variant_key
, vertex_element
[0]));
2299 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
2301 /* will have to rig this up properly later */
2302 key
->clip_xy
= llvm
->draw
->clip_xy
;
2303 key
->clip_z
= llvm
->draw
->clip_z
;
2304 key
->clip_user
= llvm
->draw
->clip_user
;
2305 key
->bypass_viewport
= llvm
->draw
->bypass_viewport
;
2306 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
2307 /* XXX assumes edgeflag output not at 0 */
2308 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
2309 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
2310 key
->has_gs_or_tes
= llvm
->draw
->gs
.geometry_shader
!= NULL
|| llvm
->draw
->tes
.tess_eval_shader
!= NULL
;
2311 key
->num_outputs
= draw_total_vs_outputs(llvm
->draw
);
2313 /* All variants of this shader will have the same value for
2314 * nr_samplers. Not yet trying to compact away holes in the
2317 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2318 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2319 key
->nr_sampler_views
=
2320 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2323 key
->nr_sampler_views
= key
->nr_samplers
;
2326 key
->nr_images
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_IMAGE
] + 1;
2328 /* Presumably all variants of the shader should have the same
2329 * number of vertex elements - ie the number of shader inputs.
2330 * NOTE: we NEED to store the needed number of needed inputs
2331 * here, not the number of provided elements to match keysize
2332 * (and the offset of sampler state in the key).
2333 * If we have excess number of vertex elements, this is valid,
2334 * but the excess ones don't matter.
2335 * If we don't have enough vertex elements (which looks not really
2336 * valid but we'll handle it gracefully) fill out missing ones with
2337 * zero (we'll recognize these later by PIPE_FORMAT_NONE).
2339 key
->nr_vertex_elements
=
2340 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
2342 if (llvm
->draw
->pt
.nr_vertex_elements
< key
->nr_vertex_elements
) {
2343 debug_printf("draw: vs with %d inputs but only have %d vertex elements\n",
2344 key
->nr_vertex_elements
, llvm
->draw
->pt
.nr_vertex_elements
);
2345 memset(key
->vertex_element
, 0,
2346 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
2348 memcpy(key
->vertex_element
,
2349 llvm
->draw
->pt
.vertex_element
,
2350 sizeof(struct pipe_vertex_element
) *
2351 MIN2(key
->nr_vertex_elements
, llvm
->draw
->pt
.nr_vertex_elements
));
2353 draw_sampler
= draw_llvm_variant_key_samplers(key
);
2354 memset(draw_sampler
, 0,
2355 MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2357 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2358 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2359 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
2361 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2362 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2363 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
2366 draw_image
= draw_llvm_variant_key_images(key
);
2367 memset(draw_image
, 0,
2368 key
->nr_images
* sizeof *draw_image
);
2369 for (i
= 0; i
< key
->nr_images
; i
++) {
2370 lp_sampler_static_texture_state_image(&draw_image
[i
].image_state
,
2371 llvm
->draw
->images
[PIPE_SHADER_VERTEX
][i
]);
2378 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
2381 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
2382 struct draw_image_static_state
*image
= draw_llvm_variant_key_images(key
);
2383 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
2384 debug_printf("clip_xy = %u\n", key
->clip_xy
);
2385 debug_printf("clip_z = %u\n", key
->clip_z
);
2386 debug_printf("clip_user = %u\n", key
->clip_user
);
2387 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
2388 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
2389 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
2390 debug_printf("has_gs_or_tes = %u\n", key
->has_gs_or_tes
);
2391 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
2393 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
2394 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
2395 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
2396 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
2397 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
2400 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2401 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
2404 for (i
= 0 ; i
< key
->nr_images
; i
++)
2405 debug_printf("images[%i].format = %s\n", i
, util_format_name(image
[i
].image_state
.format
));
2410 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
2411 enum pipe_shader_type shader_stage
,
2413 uint32_t width
, uint32_t height
, uint32_t depth
,
2414 uint32_t first_level
, uint32_t last_level
,
2415 uint32_t num_samples
,
2416 uint32_t sample_stride
,
2417 const void *base_ptr
,
2418 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
2419 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
2420 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
2423 struct draw_jit_texture
*jit_tex
;
2425 switch (shader_stage
) {
2426 case PIPE_SHADER_VERTEX
:
2427 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->jit_context
.textures
));
2428 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
2430 case PIPE_SHADER_GEOMETRY
:
2431 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->gs_jit_context
.textures
));
2432 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
2434 case PIPE_SHADER_TESS_CTRL
:
2435 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->tcs_jit_context
.textures
));
2436 jit_tex
= &draw
->llvm
->tcs_jit_context
.textures
[sview_idx
];
2438 case PIPE_SHADER_TESS_EVAL
:
2439 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->tes_jit_context
.textures
));
2440 jit_tex
= &draw
->llvm
->tes_jit_context
.textures
[sview_idx
];
2447 jit_tex
->width
= width
;
2448 jit_tex
->height
= height
;
2449 jit_tex
->depth
= depth
;
2450 jit_tex
->first_level
= first_level
;
2451 jit_tex
->last_level
= last_level
;
2452 jit_tex
->base
= base_ptr
;
2453 jit_tex
->num_samples
= num_samples
;
2454 jit_tex
->sample_stride
= sample_stride
;
2456 for (j
= first_level
; j
<= last_level
; j
++) {
2457 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
2458 jit_tex
->row_stride
[j
] = row_stride
[j
];
2459 jit_tex
->img_stride
[j
] = img_stride
[j
];
2464 draw_llvm_set_mapped_image(struct draw_context
*draw
,
2465 enum pipe_shader_type shader_stage
,
2467 uint32_t width
, uint32_t height
, uint32_t depth
,
2468 const void *base_ptr
,
2469 uint32_t row_stride
,
2470 uint32_t img_stride
)
2472 struct draw_jit_image
*jit_image
;
2474 switch (shader_stage
) {
2475 case PIPE_SHADER_VERTEX
:
2476 assert(idx
< ARRAY_SIZE(draw
->llvm
->jit_context
.images
));
2477 jit_image
= &draw
->llvm
->jit_context
.images
[idx
];
2479 case PIPE_SHADER_GEOMETRY
:
2480 assert(idx
< ARRAY_SIZE(draw
->llvm
->gs_jit_context
.images
));
2481 jit_image
= &draw
->llvm
->gs_jit_context
.images
[idx
];
2483 case PIPE_SHADER_TESS_CTRL
:
2484 assert(idx
< ARRAY_SIZE(draw
->llvm
->tcs_jit_context
.images
));
2485 jit_image
= &draw
->llvm
->tcs_jit_context
.images
[idx
];
2487 case PIPE_SHADER_TESS_EVAL
:
2488 assert(idx
< ARRAY_SIZE(draw
->llvm
->tes_jit_context
.images
));
2489 jit_image
= &draw
->llvm
->tes_jit_context
.images
[idx
];
2496 jit_image
->width
= width
;
2497 jit_image
->height
= height
;
2498 jit_image
->depth
= depth
;
2499 jit_image
->base
= base_ptr
;
2501 jit_image
->row_stride
= row_stride
;
2502 jit_image
->img_stride
= img_stride
;
2507 draw_llvm_set_sampler_state(struct draw_context
*draw
,
2508 enum pipe_shader_type shader_type
)
2512 switch (shader_type
) {
2513 case PIPE_SHADER_VERTEX
:
2514 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
2515 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
2517 if (draw
->samplers
[PIPE_SHADER_VERTEX
][i
]) {
2518 const struct pipe_sampler_state
*s
2519 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
2520 jit_sam
->min_lod
= s
->min_lod
;
2521 jit_sam
->max_lod
= s
->max_lod
;
2522 jit_sam
->lod_bias
= s
->lod_bias
;
2523 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2527 case PIPE_SHADER_GEOMETRY
:
2528 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
2529 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
2531 if (draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]) {
2532 const struct pipe_sampler_state
*s
2533 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
2534 jit_sam
->min_lod
= s
->min_lod
;
2535 jit_sam
->max_lod
= s
->max_lod
;
2536 jit_sam
->lod_bias
= s
->lod_bias
;
2537 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2541 case PIPE_SHADER_TESS_CTRL
:
2542 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_TESS_CTRL
]; i
++) {
2543 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->tcs_jit_context
.samplers
[i
];
2545 if (draw
->samplers
[PIPE_SHADER_TESS_CTRL
][i
]) {
2546 const struct pipe_sampler_state
*s
2547 = draw
->samplers
[PIPE_SHADER_TESS_CTRL
][i
];
2548 jit_sam
->min_lod
= s
->min_lod
;
2549 jit_sam
->max_lod
= s
->max_lod
;
2550 jit_sam
->lod_bias
= s
->lod_bias
;
2551 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2555 case PIPE_SHADER_TESS_EVAL
:
2556 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_TESS_EVAL
]; i
++) {
2557 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->tes_jit_context
.samplers
[i
];
2559 if (draw
->samplers
[PIPE_SHADER_TESS_EVAL
][i
]) {
2560 const struct pipe_sampler_state
*s
2561 = draw
->samplers
[PIPE_SHADER_TESS_EVAL
][i
];
2562 jit_sam
->min_lod
= s
->min_lod
;
2563 jit_sam
->max_lod
= s
->max_lod
;
2564 jit_sam
->lod_bias
= s
->lod_bias
;
2565 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2577 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
2579 struct draw_llvm
*llvm
= variant
->llvm
;
2581 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2582 debug_printf("Deleting VS variant: %u vs variants,\t%u total variants\n",
2583 variant
->shader
->variants_cached
, llvm
->nr_variants
);
2586 gallivm_destroy(variant
->gallivm
);
2588 remove_from_list(&variant
->list_item_local
);
2589 variant
->shader
->variants_cached
--;
2590 remove_from_list(&variant
->list_item_global
);
2591 llvm
->nr_variants
--;
2597 * Create LLVM types for various structures.
2600 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
2602 struct gallivm_state
*gallivm
= var
->gallivm
;
2603 LLVMTypeRef texture_type
, sampler_type
, image_type
, context_type
;
2605 texture_type
= create_jit_texture_type(gallivm
, "texture");
2606 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2607 image_type
= create_jit_image_type(gallivm
, "image");
2609 context_type
= create_gs_jit_context_type(gallivm
,
2610 var
->shader
->base
.vector_length
,
2611 texture_type
, sampler_type
,
2613 "draw_gs_jit_context");
2614 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2616 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
2620 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
2622 if (!variant
->context_ptr_type
)
2623 create_gs_jit_types(variant
);
2624 return variant
->context_ptr_type
;
2628 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2629 struct lp_type gs_type
)
2631 struct gallivm_state
*gallivm
= variant
->gallivm
;
2632 LLVMBuilderRef builder
= gallivm
->builder
;
2633 struct lp_type mask_type
= lp_int_type(gs_type
);
2634 LLVMValueRef num_prims
;
2635 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
2638 num_prims
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
),
2639 variant
->num_prims
);
2640 for (i
= 0; i
< gs_type
.length
; i
++) {
2641 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
2642 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, idx
, idx
, "");
2644 mask_val
= lp_build_compare(gallivm
, mask_type
,
2645 PIPE_FUNC_GREATER
, num_prims
, mask_val
);
2651 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2652 struct draw_gs_llvm_variant
*variant
)
2654 struct gallivm_state
*gallivm
= variant
->gallivm
;
2655 LLVMContextRef context
= gallivm
->context
;
2656 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2657 LLVMTypeRef arg_types
[7];
2658 LLVMTypeRef func_type
;
2659 LLVMValueRef variant_func
;
2660 LLVMValueRef context_ptr
;
2661 LLVMValueRef prim_id_ptr
;
2662 LLVMBasicBlockRef block
;
2663 LLVMBuilderRef builder
;
2664 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2665 struct lp_build_sampler_soa
*sampler
= 0;
2666 struct lp_build_image_soa
*image
= NULL
;
2667 struct lp_build_context bld
;
2668 struct lp_bld_tgsi_system_values system_values
;
2670 struct lp_type gs_type
;
2672 struct draw_gs_llvm_iface gs_iface
;
2673 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2674 LLVMValueRef consts_ptr
, num_consts_ptr
;
2675 LLVMValueRef ssbos_ptr
, num_ssbos_ptr
;
2676 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2677 struct lp_build_mask_context mask
;
2678 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2679 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2681 memset(&system_values
, 0, sizeof(system_values
));
2682 memset(&outputs
, 0, sizeof(outputs
));
2684 snprintf(func_name
, sizeof(func_name
), "draw_llvm_gs_variant%u",
2685 variant
->shader
->variants_cached
);
2687 assert(variant
->vertex_header_ptr_type
);
2689 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2690 arg_types
[1] = variant
->input_array_type
; /* input */
2691 arg_types
[2] = LLVMPointerType(variant
->vertex_header_ptr_type
, 0); /* vertex_header */
2692 arg_types
[3] = int32_type
; /* num_prims */
2693 arg_types
[4] = int32_type
; /* instance_id */
2694 arg_types
[5] = LLVMPointerType(
2695 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2696 arg_types
[6] = int32_type
;
2698 func_type
= LLVMFunctionType(int32_type
, arg_types
, ARRAY_SIZE(arg_types
), 0);
2700 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
2702 variant
->function
= variant_func
;
2704 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2706 for (i
= 0; i
< ARRAY_SIZE(arg_types
); ++i
)
2707 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2708 lp_add_function_attr(variant_func
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
2710 context_ptr
= LLVMGetParam(variant_func
, 0);
2711 input_array
= LLVMGetParam(variant_func
, 1);
2712 io_ptr
= LLVMGetParam(variant_func
, 2);
2713 num_prims
= LLVMGetParam(variant_func
, 3);
2714 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2715 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2716 system_values
.invocation_id
= LLVMGetParam(variant_func
, 6);
2718 lp_build_name(context_ptr
, "context");
2719 lp_build_name(input_array
, "input");
2720 lp_build_name(io_ptr
, "io");
2721 lp_build_name(num_prims
, "num_prims");
2722 lp_build_name(system_values
.instance_id
, "instance_id");
2723 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2724 lp_build_name(system_values
.invocation_id
, "invocation_id");
2726 variant
->context_ptr
= context_ptr
;
2727 variant
->io_ptr
= io_ptr
;
2728 variant
->num_prims
= num_prims
;
2730 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2731 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2732 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2733 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2734 gs_iface
.input
= input_array
;
2735 gs_iface
.variant
= variant
;
2741 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2742 builder
= gallivm
->builder
;
2743 LLVMPositionBuilderAtEnd(builder
, block
);
2745 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2747 memset(&gs_type
, 0, sizeof gs_type
);
2748 gs_type
.floating
= TRUE
; /* floating point values */
2749 gs_type
.sign
= TRUE
; /* values are signed */
2750 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2751 gs_type
.width
= 32; /* 32-bit float */
2752 gs_type
.length
= vector_length
;
2754 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2756 draw_gs_jit_context_num_constants(variant
->gallivm
, context_ptr
);
2758 ssbos_ptr
= draw_gs_jit_context_ssbos(variant
->gallivm
, context_ptr
);
2760 draw_gs_jit_context_num_ssbos(variant
->gallivm
, context_ptr
);
2762 /* code generated texture sampling */
2763 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
);
2764 image
= draw_llvm_image_soa_create(draw_gs_llvm_variant_key_images(&variant
->key
));
2765 mask_val
= generate_mask_value(variant
, gs_type
);
2766 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2768 if (gs_info
->uses_primid
) {
2769 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");
2772 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2773 if (llvm
->draw
->gs
.geometry_shader
->state
.type
== PIPE_SHADER_IR_TGSI
)
2774 tgsi_dump(tokens
, 0);
2776 nir_print_shader(llvm
->draw
->gs
.geometry_shader
->state
.ir
.nir
, stderr
);
2777 draw_gs_llvm_dump_variant_key(&variant
->key
);
2780 struct lp_build_tgsi_params params
;
2781 memset(¶ms
, 0, sizeof(params
));
2783 params
.type
= gs_type
;
2784 params
.mask
= &mask
;
2785 params
.consts_ptr
= consts_ptr
;
2786 params
.const_sizes_ptr
= num_consts_ptr
;
2787 params
.system_values
= &system_values
;
2788 params
.context_ptr
= context_ptr
;
2789 params
.sampler
= sampler
;
2790 params
.info
= &llvm
->draw
->gs
.geometry_shader
->info
;
2791 params
.gs_iface
= (const struct lp_build_gs_iface
*)&gs_iface
;
2792 params
.ssbo_ptr
= ssbos_ptr
;
2793 params
.ssbo_sizes_ptr
= num_ssbos_ptr
;
2794 params
.image
= image
;
2796 if (llvm
->draw
->gs
.geometry_shader
->state
.type
== PIPE_SHADER_IR_TGSI
)
2797 lp_build_tgsi_soa(variant
->gallivm
,
2802 lp_build_nir_soa(variant
->gallivm
,
2803 llvm
->draw
->gs
.geometry_shader
->state
.ir
.nir
,
2807 sampler
->destroy(sampler
);
2808 image
->destroy(image
);
2810 lp_build_mask_end(&mask
);
2812 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2814 gallivm_verify_function(gallivm
, variant_func
);
2818 struct draw_gs_llvm_variant
*
2819 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2820 unsigned num_outputs
,
2821 const struct draw_gs_llvm_variant_key
*key
)
2823 struct draw_gs_llvm_variant
*variant
;
2824 struct llvm_geometry_shader
*shader
=
2825 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2826 LLVMTypeRef vertex_header
;
2827 char module_name
[64];
2829 variant
= MALLOC(sizeof *variant
+
2830 shader
->variant_key_size
-
2831 sizeof variant
->key
);
2835 variant
->llvm
= llvm
;
2836 variant
->shader
= shader
;
2838 snprintf(module_name
, sizeof(module_name
), "draw_llvm_gs_variant%u",
2839 variant
->shader
->variants_cached
);
2841 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
2843 create_gs_jit_types(variant
);
2845 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2847 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2849 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2851 draw_gs_llvm_generate(llvm
, variant
);
2853 gallivm_compile_module(variant
->gallivm
);
2855 variant
->jit_func
= (draw_gs_jit_func
)
2856 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2858 gallivm_free_ir(variant
->gallivm
);
2860 variant
->list_item_global
.base
= variant
;
2861 variant
->list_item_local
.base
= variant
;
2862 /*variant->no = */shader
->variants_created
++;
2863 variant
->list_item_global
.base
= variant
;
2869 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2871 struct draw_llvm
*llvm
= variant
->llvm
;
2873 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2874 debug_printf("Deleting GS variant: %u gs variants,\t%u total variants\n",
2875 variant
->shader
->variants_cached
, llvm
->nr_gs_variants
);
2878 gallivm_destroy(variant
->gallivm
);
2880 remove_from_list(&variant
->list_item_local
);
2881 variant
->shader
->variants_cached
--;
2882 remove_from_list(&variant
->list_item_global
);
2883 llvm
->nr_gs_variants
--;
2887 struct draw_gs_llvm_variant_key
*
2888 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2891 struct draw_gs_llvm_variant_key
*key
;
2892 struct draw_sampler_static_state
*draw_sampler
;
2893 struct draw_image_static_state
*draw_image
;
2895 key
= (struct draw_gs_llvm_variant_key
*)store
;
2897 memset(key
, 0, offsetof(struct draw_gs_llvm_variant_key
, samplers
[0]));
2899 key
->num_outputs
= draw_total_gs_outputs(llvm
->draw
);
2901 /* All variants of this shader will have the same value for
2902 * nr_samplers. Not yet trying to compact away holes in the
2905 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2906 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2907 key
->nr_sampler_views
=
2908 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2911 key
->nr_sampler_views
= key
->nr_samplers
;
2914 key
->nr_images
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_IMAGE
] + 1;
2916 draw_sampler
= key
->samplers
;
2918 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2920 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2921 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2922 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2924 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2925 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2926 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2929 draw_image
= draw_gs_llvm_variant_key_images(key
);
2930 memset(draw_image
, 0,
2931 key
->nr_images
* sizeof *draw_image
);
2932 for (i
= 0; i
< key
->nr_images
; i
++) {
2933 lp_sampler_static_texture_state_image(&draw_image
[i
].image_state
,
2934 llvm
->draw
->images
[PIPE_SHADER_GEOMETRY
][i
]);
2940 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2943 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2944 struct draw_image_static_state
*image
= draw_gs_llvm_variant_key_images(key
);
2945 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2946 debug_printf("sampler[%i].src_format = %s\n", i
,
2947 util_format_name(sampler
[i
].texture_state
.format
));
2950 for (i
= 0 ; i
< key
->nr_images
; i
++)
2951 debug_printf("images[%i].format = %s\n", i
, util_format_name(image
[i
].image_state
.format
));
2956 create_tcs_jit_types(struct draw_tcs_llvm_variant
*var
)
2958 struct gallivm_state
*gallivm
= var
->gallivm
;
2959 LLVMTypeRef texture_type
, sampler_type
, image_type
, context_type
;
2961 texture_type
= create_jit_texture_type(gallivm
, "texture");
2962 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2963 image_type
= create_jit_image_type(gallivm
, "image");
2965 context_type
= create_tcs_jit_context_type(gallivm
,
2967 texture_type
, sampler_type
,
2969 "draw_tcs_jit_context");
2970 var
->input_array_type
= create_tcs_jit_input_type(gallivm
);
2971 var
->output_array_type
= create_tcs_jit_output_type(gallivm
);
2972 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2976 get_tcs_context_ptr_type(struct draw_tcs_llvm_variant
*variant
)
2978 if (!variant
->context_ptr_type
)
2979 create_tcs_jit_types(variant
);
2980 return variant
->context_ptr_type
;
2984 draw_tcs_llvm_emit_fetch_input(const struct lp_build_tcs_iface
*tes_iface
,
2985 struct lp_build_context
*bld
,
2986 boolean is_vindex_indirect
,
2987 LLVMValueRef vertex_index
,
2988 boolean is_aindex_indirect
,
2989 LLVMValueRef attrib_index
,
2990 LLVMValueRef swizzle_index
)
2992 const struct draw_tcs_llvm_iface
*tcs
= draw_tcs_llvm_iface(tes_iface
);
2993 struct gallivm_state
*gallivm
= bld
->gallivm
;
2994 LLVMBuilderRef builder
= gallivm
->builder
;
2995 LLVMValueRef indices
[3];
2997 struct lp_type type
= bld
->type
;
2999 if (is_vindex_indirect
|| is_aindex_indirect
) {
3003 for (i
= 0; i
< type
.length
; ++i
) {
3004 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3005 LLVMValueRef vert_chan_index
= vertex_index
;
3006 LLVMValueRef attr_chan_index
= attrib_index
;
3007 LLVMValueRef channel_vec
;
3009 if (is_vindex_indirect
) {
3010 vert_chan_index
= LLVMBuildExtractElement(builder
,
3011 vertex_index
, idx
, "");
3013 if (is_aindex_indirect
) {
3014 attr_chan_index
= LLVMBuildExtractElement(builder
,
3015 attrib_index
, idx
, "");
3018 indices
[0] = vert_chan_index
;
3019 indices
[1] = attr_chan_index
;
3020 indices
[2] = swizzle_index
;
3022 channel_vec
= LLVMBuildGEP(builder
, tcs
->input
, indices
, 3, "");
3023 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
3025 res
= LLVMBuildInsertElement(builder
, res
, channel_vec
, idx
, "");
3028 indices
[0] = vertex_index
;
3029 indices
[1] = attrib_index
;
3030 indices
[2] = swizzle_index
;
3032 res
= LLVMBuildGEP(builder
, tcs
->input
, indices
, 3, "");
3033 res
= LLVMBuildLoad(builder
, res
, "");
3034 res
= lp_build_broadcast_scalar(bld
, res
);
3040 draw_tcs_llvm_emit_fetch_output(const struct lp_build_tcs_iface
*tes_iface
,
3041 struct lp_build_context
*bld
,
3042 boolean is_vindex_indirect
,
3043 LLVMValueRef vertex_index
,
3044 boolean is_aindex_indirect
,
3045 LLVMValueRef attrib_index
,
3046 LLVMValueRef swizzle_index
,
3049 const struct draw_tcs_llvm_iface
*tcs
= draw_tcs_llvm_iface(tes_iface
);
3050 struct gallivm_state
*gallivm
= bld
->gallivm
;
3051 LLVMBuilderRef builder
= gallivm
->builder
;
3052 LLVMValueRef indices
[3];
3054 struct lp_type type
= bld
->type
;
3056 if (is_vindex_indirect
|| is_aindex_indirect
) {
3060 for (i
= 0; i
< type
.length
; ++i
) {
3061 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3062 LLVMValueRef vert_chan_index
= vertex_index
;
3063 LLVMValueRef attr_chan_index
= attrib_index
;
3064 LLVMValueRef channel_vec
;
3066 if (is_vindex_indirect
) {
3067 vert_chan_index
= LLVMBuildExtractElement(builder
,
3068 vertex_index
, idx
, "");
3070 if (is_aindex_indirect
) {
3071 attr_chan_index
= LLVMBuildExtractElement(builder
,
3072 attrib_index
, idx
, "");
3075 indices
[0] = vert_chan_index
;
3076 indices
[1] = attr_chan_index
;
3077 indices
[2] = swizzle_index
;
3079 channel_vec
= LLVMBuildGEP(builder
, tcs
->output
, indices
, 3, "");
3080 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
3082 res
= LLVMBuildInsertElement(builder
, res
, channel_vec
, idx
, "");
3085 indices
[0] = vertex_index
? vertex_index
: lp_build_const_int32(gallivm
, 0);
3086 indices
[1] = attrib_index
;
3087 indices
[2] = swizzle_index
;
3089 res
= LLVMBuildGEP(builder
, tcs
->output
, indices
, 3, "");
3090 res
= LLVMBuildLoad(builder
, res
, "");
3091 res
= lp_build_broadcast_scalar(bld
, res
);
3097 draw_tcs_llvm_emit_store_output(const struct lp_build_tcs_iface
*tes_iface
,
3098 struct lp_build_context
*bld
,
3100 boolean is_vindex_indirect
,
3101 LLVMValueRef vertex_index
,
3102 boolean is_aindex_indirect
,
3103 LLVMValueRef attrib_index
,
3104 LLVMValueRef swizzle_index
,
3106 LLVMValueRef mask_vec
)
3108 const struct draw_tcs_llvm_iface
*tcs
= draw_tcs_llvm_iface(tes_iface
);
3109 struct gallivm_state
*gallivm
= bld
->gallivm
;
3110 LLVMBuilderRef builder
= gallivm
->builder
;
3111 LLVMValueRef indices
[3];
3113 struct lp_type type
= bld
->type
;
3115 if (is_vindex_indirect
|| is_aindex_indirect
) {
3118 for (i
= 0; i
< type
.length
; ++i
) {
3119 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3120 LLVMValueRef vert_chan_index
= vertex_index
? vertex_index
: lp_build_const_int32(gallivm
, 0);
3121 LLVMValueRef attr_chan_index
= attrib_index
;
3122 LLVMValueRef channel_vec
;
3124 if (is_vindex_indirect
) {
3125 vert_chan_index
= LLVMBuildExtractElement(builder
,
3126 vertex_index
, idx
, "");
3128 if (is_aindex_indirect
) {
3129 attr_chan_index
= LLVMBuildExtractElement(builder
,
3130 attrib_index
, idx
, "");
3133 indices
[0] = vert_chan_index
;
3134 indices
[1] = attr_chan_index
;
3135 indices
[2] = swizzle_index
;
3137 channel_vec
= LLVMBuildGEP(builder
, tcs
->output
, indices
, 3, "");
3139 res
= LLVMBuildExtractElement(builder
, value
, idx
, "");
3141 struct lp_build_if_state ifthen
;
3142 LLVMValueRef cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
, mask_vec
, lp_build_const_int_vec(gallivm
, bld
->type
, 0), "");
3143 cond
= LLVMBuildExtractElement(gallivm
->builder
, cond
, idx
, "");
3144 lp_build_if(&ifthen
, gallivm
, cond
);
3145 LLVMBuildStore(builder
, res
, channel_vec
);
3146 lp_build_endif(&ifthen
);
3149 indices
[0] = vertex_index
? vertex_index
: lp_build_const_int32(gallivm
, 0);
3150 indices
[1] = attrib_index
;
3151 indices
[2] = swizzle_index
;
3153 res
= LLVMBuildGEP(builder
, tcs
->output
, indices
, 3, "");
3154 for (unsigned i
= 0; i
< type
.length
; ++i
) {
3155 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3156 LLVMValueRef val
= LLVMBuildExtractElement(builder
, value
, idx
, "");
3158 struct lp_build_if_state ifthen
;
3159 LLVMValueRef cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
, mask_vec
, lp_build_const_int_vec(gallivm
, bld
->type
, 0), "");
3160 cond
= LLVMBuildExtractElement(gallivm
->builder
, cond
, idx
, "");
3161 lp_build_if(&ifthen
, gallivm
, cond
);
3162 LLVMBuildStore(builder
, val
, res
);
3163 lp_build_endif(&ifthen
);
3170 generate_tcs_mask_value(struct draw_tcs_llvm_variant
*variant
,
3171 struct lp_type tcs_type
, LLVMValueRef limit
, LLVMValueRef loop_counter
)
3173 struct gallivm_state
*gallivm
= variant
->gallivm
;
3174 LLVMBuilderRef builder
= gallivm
->builder
;
3175 struct lp_type mask_type
= lp_int_type(tcs_type
);
3176 LLVMValueRef num_vecs
;
3177 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
3180 num_vecs
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
), limit
);
3181 for (i
= 0; i
< tcs_type
.length
; i
++) {
3182 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3183 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, LLVMBuildAdd(builder
, loop_counter
, idx
, ""), idx
, "");
3185 mask_val
= lp_build_compare(gallivm
, mask_type
,
3186 PIPE_FUNC_GREATER
, num_vecs
, mask_val
);
3192 draw_tcs_llvm_generate(struct draw_llvm
*llvm
,
3193 struct draw_tcs_llvm_variant
*variant
)
3195 struct gallivm_state
*gallivm
= variant
->gallivm
;
3196 LLVMContextRef context
= gallivm
->context
;
3197 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
3198 LLVMTypeRef arg_types
[6];
3199 LLVMTypeRef func_type
, coro_func_type
;
3200 LLVMValueRef variant_func
, variant_coro
;
3201 LLVMValueRef context_ptr
;
3202 LLVMValueRef input_array
, output_array
, prim_id
, patch_vertices_in
;
3203 LLVMValueRef mask_val
;
3204 LLVMBasicBlockRef block
;
3205 LLVMBuilderRef builder
;
3206 struct lp_build_context bld
, bldvec
;
3207 struct lp_build_sampler_soa
*sampler
= 0;
3208 struct lp_build_image_soa
*image
= NULL
;
3209 struct lp_bld_tgsi_system_values system_values
;
3210 char func_name
[64], func_name_coro
[64];
3212 struct draw_tcs_llvm_iface tcs_iface
;
3213 struct lp_build_mask_context mask
;
3214 LLVMValueRef consts_ptr
, num_consts_ptr
;
3215 LLVMValueRef ssbos_ptr
, num_ssbos_ptr
;
3216 struct lp_type tcs_type
;
3217 unsigned vector_length
= variant
->shader
->base
.vector_length
;
3219 memset(&system_values
, 0, sizeof(system_values
));
3221 snprintf(func_name
, sizeof(func_name
), "draw_llvm_tcs_variant%u",
3222 variant
->shader
->variants_cached
);
3224 snprintf(func_name_coro
, sizeof(func_name_coro
), "draw_llvm_tcs_coro_variant%u",
3225 variant
->shader
->variants_cached
);
3227 arg_types
[0] = get_tcs_context_ptr_type(variant
); /* context */
3228 arg_types
[1] = variant
->input_array_type
; /* input */
3229 arg_types
[2] = variant
->output_array_type
;
3230 arg_types
[3] = int32_type
;
3231 arg_types
[4] = int32_type
;
3232 arg_types
[5] = int32_type
; /* coroutine only */
3234 func_type
= LLVMFunctionType(int32_type
, arg_types
, ARRAY_SIZE(arg_types
) - 1, 0);
3236 coro_func_type
= LLVMFunctionType(LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0), arg_types
, ARRAY_SIZE(arg_types
), 0);
3238 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
3240 variant_coro
= LLVMAddFunction(gallivm
->module
, func_name_coro
, coro_func_type
);
3242 variant
->function
= variant_func
;
3243 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
3245 LLVMSetFunctionCallConv(variant_coro
, LLVMCCallConv
);
3247 for (i
= 0; i
< ARRAY_SIZE(arg_types
); ++i
) {
3248 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
) {
3249 lp_add_function_attr(variant_coro
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
3250 lp_add_function_attr(variant_func
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
3254 context_ptr
= LLVMGetParam(variant_func
, 0);
3255 input_array
= LLVMGetParam(variant_func
, 1);
3256 output_array
= LLVMGetParam(variant_func
, 2);
3257 prim_id
= LLVMGetParam(variant_func
, 3);
3258 patch_vertices_in
= LLVMGetParam(variant_func
, 4);
3260 lp_build_name(context_ptr
, "context");
3261 lp_build_name(input_array
, "input");
3262 lp_build_name(output_array
, "output");
3263 lp_build_name(prim_id
, "prim_id");
3264 lp_build_name(patch_vertices_in
, "patch_vertices_in");
3266 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
3267 builder
= gallivm
->builder
;
3268 LLVMPositionBuilderAtEnd(builder
, block
);
3270 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
3272 memset(&tcs_type
, 0, sizeof tcs_type
);
3273 tcs_type
.floating
= TRUE
; /* floating point values */
3274 tcs_type
.sign
= TRUE
; /* values are signed */
3275 tcs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
3276 tcs_type
.width
= 32; /* 32-bit float */
3277 tcs_type
.length
= vector_length
;
3279 lp_build_context_init(&bldvec
, variant
->gallivm
, lp_int_type(tcs_type
));
3281 LLVMValueRef count
= lp_build_const_int32(gallivm
, variant
->shader
->base
.vertices_out
);
3282 LLVMValueRef step
= lp_build_const_int32(gallivm
, vector_length
);
3284 struct lp_build_loop_state loop_state
[2];
3285 LLVMValueRef num_inner_loop
;
3286 unsigned count_align
= util_align_npot(variant
->shader
->base
.vertices_out
, tcs_type
.length
);
3287 num_inner_loop
= lp_build_const_int32(gallivm
, count_align
/ tcs_type
.length
);
3288 LLVMTypeRef hdl_ptr_type
= LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
3289 LLVMValueRef coro_hdls
= LLVMBuildArrayAlloca(gallivm
->builder
, hdl_ptr_type
, num_inner_loop
, "coro_hdls");
3290 unsigned end_coroutine
= INT_MAX
;
3291 lp_build_loop_begin(&loop_state
[1], gallivm
,
3292 lp_build_const_int32(gallivm
, 0)); /* coroutine reentry loop */
3293 lp_build_loop_begin(&loop_state
[0], gallivm
,
3294 lp_build_const_int32(gallivm
, 0)); /* inner loop */
3296 LLVMValueRef args
[6];
3297 args
[0] = context_ptr
;
3298 args
[1] = input_array
;
3299 args
[2] = output_array
;
3301 args
[4] = patch_vertices_in
;
3302 args
[5] = loop_state
[0].counter
;
3303 LLVMValueRef coro_entry
= LLVMBuildGEP(builder
, coro_hdls
, &loop_state
[0].counter
, 1, "");
3304 LLVMValueRef coro_hdl
= LLVMBuildLoad(builder
, coro_entry
, "coro_hdl");
3306 struct lp_build_if_state ifstate
;
3307 LLVMValueRef cmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, loop_state
[1].counter
,
3308 lp_build_const_int32(gallivm
, 0), "");
3309 /* first time here - call the coroutine function entry point */
3310 lp_build_if(&ifstate
, gallivm
, cmp
);
3311 LLVMValueRef coro_ret
= LLVMBuildCall(builder
, variant_coro
, args
, 6, "");
3312 LLVMBuildStore(builder
, coro_ret
, coro_entry
);
3313 lp_build_else(&ifstate
);
3314 /* subsequent calls for this invocation - check if done. */
3315 LLVMValueRef coro_done
= lp_build_coro_done(gallivm
, coro_hdl
);
3316 struct lp_build_if_state ifstate2
;
3317 lp_build_if(&ifstate2
, gallivm
, coro_done
);
3318 /* if done destroy and force loop exit */
3319 lp_build_coro_destroy(gallivm
, coro_hdl
);
3320 lp_build_loop_force_set_counter(&loop_state
[1], lp_build_const_int32(gallivm
, end_coroutine
- 1));
3321 lp_build_else(&ifstate2
);
3322 /* otherwise resume the coroutine */
3323 lp_build_coro_resume(gallivm
, coro_hdl
);
3324 lp_build_endif(&ifstate2
);
3325 lp_build_endif(&ifstate
);
3326 lp_build_loop_force_reload_counter(&loop_state
[1]);
3328 lp_build_loop_end_cond(&loop_state
[0],
3331 lp_build_loop_end_cond(&loop_state
[1],
3332 lp_build_const_int32(gallivm
, end_coroutine
),
3334 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
3336 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_coro
, "entry");
3337 LLVMPositionBuilderAtEnd(builder
, block
);
3339 context_ptr
= LLVMGetParam(variant_coro
, 0);
3340 input_array
= LLVMGetParam(variant_coro
, 1);
3341 output_array
= LLVMGetParam(variant_coro
, 2);
3342 prim_id
= LLVMGetParam(variant_coro
, 3);
3343 patch_vertices_in
= LLVMGetParam(variant_coro
, 4);
3345 consts_ptr
= draw_tcs_jit_context_constants(variant
->gallivm
, context_ptr
);
3347 draw_tcs_jit_context_num_constants(variant
->gallivm
, context_ptr
);
3349 ssbos_ptr
= draw_tcs_jit_context_ssbos(variant
->gallivm
, context_ptr
);
3351 draw_tcs_jit_context_num_ssbos(variant
->gallivm
, context_ptr
);
3352 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
);
3353 image
= draw_llvm_image_soa_create(draw_tcs_llvm_variant_key_images(&variant
->key
));
3355 LLVMValueRef counter
= LLVMGetParam(variant_coro
, 5);
3356 LLVMValueRef invocvec
= LLVMGetUndef(LLVMVectorType(int32_type
, vector_length
));
3357 for (i
= 0; i
< vector_length
; i
++) {
3358 LLVMValueRef idx
= LLVMBuildAdd(builder
, LLVMBuildMul(builder
, counter
, step
, ""), lp_build_const_int32(gallivm
, i
), "");
3359 invocvec
= LLVMBuildInsertElement(builder
, invocvec
, idx
, idx
, "");
3362 system_values
.invocation_id
= invocvec
;
3363 system_values
.prim_id
= lp_build_broadcast_scalar(&bldvec
, prim_id
);
3364 system_values
.vertices_in
= lp_build_broadcast_scalar(&bldvec
, patch_vertices_in
);
3365 tcs_iface
.input
= input_array
;
3366 tcs_iface
.output
= output_array
;
3367 tcs_iface
.base
.emit_fetch_input
= draw_tcs_llvm_emit_fetch_input
;
3368 tcs_iface
.base
.emit_fetch_output
= draw_tcs_llvm_emit_fetch_output
;
3369 tcs_iface
.base
.emit_store_output
= draw_tcs_llvm_emit_store_output
;
3373 LLVMValueRef coro_id
= lp_build_coro_id(gallivm
);
3374 LLVMValueRef coro_hdl
= lp_build_coro_begin_alloc_mem(gallivm
, coro_id
);
3376 mask_val
= generate_tcs_mask_value(variant
, tcs_type
, count
, LLVMBuildMul(builder
, counter
, step
, ""));
3377 lp_build_mask_begin(&mask
, gallivm
, tcs_type
, mask_val
);
3379 struct lp_build_coro_suspend_info coro_info
;
3381 LLVMBasicBlockRef sus_block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_coro
, "suspend");
3382 LLVMBasicBlockRef clean_block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_coro
, "cleanup");
3384 coro_info
.suspend
= sus_block
;
3385 coro_info
.cleanup
= clean_block
;
3387 struct lp_build_tgsi_params params
;
3388 memset(¶ms
, 0, sizeof(params
));
3390 params
.type
= tcs_type
;
3391 params
.mask
= &mask
;
3392 params
.consts_ptr
= consts_ptr
;
3393 params
.const_sizes_ptr
= num_consts_ptr
;
3394 params
.system_values
= &system_values
;
3395 params
.context_ptr
= context_ptr
;
3396 params
.sampler
= sampler
;
3397 params
.info
= &llvm
->draw
->tcs
.tess_ctrl_shader
->info
;
3398 params
.ssbo_ptr
= ssbos_ptr
;
3399 params
.ssbo_sizes_ptr
= num_ssbos_ptr
;
3400 params
.image
= image
;
3401 params
.coro
= &coro_info
;
3402 params
.tcs_iface
= &tcs_iface
.base
;
3404 lp_build_nir_soa(variant
->gallivm
,
3405 llvm
->draw
->tcs
.tess_ctrl_shader
->state
.ir
.nir
,
3408 lp_build_mask_end(&mask
);
3410 lp_build_coro_suspend_switch(gallivm
, &coro_info
, NULL
, true);
3411 LLVMPositionBuilderAtEnd(builder
, clean_block
);
3413 lp_build_coro_free_mem(gallivm
, coro_id
, coro_hdl
);
3415 LLVMBuildBr(builder
, sus_block
);
3416 LLVMPositionBuilderAtEnd(builder
, sus_block
);
3418 lp_build_coro_end(gallivm
, coro_hdl
);
3419 LLVMBuildRet(builder
, coro_hdl
);
3422 sampler
->destroy(sampler
);
3423 image
->destroy(image
);
3424 gallivm_verify_function(gallivm
, variant_func
);
3425 gallivm_verify_function(gallivm
, variant_coro
);
3428 struct draw_tcs_llvm_variant
*
3429 draw_tcs_llvm_create_variant(struct draw_llvm
*llvm
,
3430 unsigned num_outputs
,
3431 const struct draw_tcs_llvm_variant_key
*key
)
3433 struct draw_tcs_llvm_variant
*variant
;
3434 struct llvm_tess_ctrl_shader
*shader
= llvm_tess_ctrl_shader(llvm
->draw
->tcs
.tess_ctrl_shader
);
3435 char module_name
[64];
3437 variant
= MALLOC(sizeof *variant
+
3438 shader
->variant_key_size
- sizeof variant
->key
);
3442 variant
->llvm
= llvm
;
3443 variant
->shader
= shader
;
3445 snprintf(module_name
, sizeof(module_name
), "draw_llvm_tcs_variant%u",
3446 variant
->shader
->variants_cached
);
3448 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
3450 create_tcs_jit_types(variant
);
3452 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
3454 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
3455 nir_print_shader(llvm
->draw
->tcs
.tess_ctrl_shader
->state
.ir
.nir
, stderr
);
3456 draw_tcs_llvm_dump_variant_key(&variant
->key
);
3459 draw_tcs_llvm_generate(llvm
, variant
);
3461 gallivm_compile_module(variant
->gallivm
);
3463 variant
->jit_func
= (draw_tcs_jit_func
)
3464 gallivm_jit_function(variant
->gallivm
, variant
->function
);
3466 gallivm_free_ir(variant
->gallivm
);
3468 variant
->list_item_global
.base
= variant
;
3469 variant
->list_item_local
.base
= variant
;
3470 /*variant->no = */shader
->variants_created
++;
3471 variant
->list_item_global
.base
= variant
;
3477 draw_tcs_llvm_destroy_variant(struct draw_tcs_llvm_variant
*variant
)
3479 struct draw_llvm
*llvm
= variant
->llvm
;
3481 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
3482 debug_printf("Deleting TCS variant: %u tcs variants,\t%u total variants\n",
3483 variant
->shader
->variants_cached
, llvm
->nr_tcs_variants
);
3486 gallivm_destroy(variant
->gallivm
);
3488 remove_from_list(&variant
->list_item_local
);
3489 variant
->shader
->variants_cached
--;
3490 remove_from_list(&variant
->list_item_global
);
3491 llvm
->nr_tcs_variants
--;
3495 struct draw_tcs_llvm_variant_key
*
3496 draw_tcs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
3499 struct draw_tcs_llvm_variant_key
*key
;
3500 struct draw_sampler_static_state
*draw_sampler
;
3501 struct draw_image_static_state
*draw_image
;
3503 key
= (struct draw_tcs_llvm_variant_key
*)store
;
3505 memset(key
, 0, offsetof(struct draw_tcs_llvm_variant_key
, samplers
[0]));
3507 /* All variants of this shader will have the same value for
3508 * nr_samplers. Not yet trying to compact away holes in the
3511 key
->nr_samplers
= llvm
->draw
->tcs
.tess_ctrl_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
3512 if (llvm
->draw
->tcs
.tess_ctrl_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
3513 key
->nr_sampler_views
=
3514 llvm
->draw
->tcs
.tess_ctrl_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
3517 key
->nr_sampler_views
= key
->nr_samplers
;
3520 key
->nr_images
= llvm
->draw
->tcs
.tess_ctrl_shader
->info
.file_max
[TGSI_FILE_IMAGE
] + 1;
3522 draw_sampler
= key
->samplers
;
3524 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
3526 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
3527 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
3528 llvm
->draw
->samplers
[PIPE_SHADER_TESS_CTRL
][i
]);
3530 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
3531 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
3532 llvm
->draw
->sampler_views
[PIPE_SHADER_TESS_CTRL
][i
]);
3535 draw_image
= draw_tcs_llvm_variant_key_images(key
);
3536 memset(draw_image
, 0,
3537 key
->nr_images
* sizeof *draw_image
);
3538 for (i
= 0; i
< key
->nr_images
; i
++) {
3539 lp_sampler_static_texture_state_image(&draw_image
[i
].image_state
,
3540 llvm
->draw
->images
[PIPE_SHADER_TESS_CTRL
][i
]);
3546 draw_tcs_llvm_dump_variant_key(struct draw_tcs_llvm_variant_key
*key
)
3549 struct draw_sampler_static_state
*sampler
= key
->samplers
;
3550 struct draw_image_static_state
*image
= draw_tcs_llvm_variant_key_images(key
);
3551 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
3552 debug_printf("sampler[%i].src_format = %s\n", i
,
3553 util_format_name(sampler
[i
].texture_state
.format
));
3556 for (i
= 0 ; i
< key
->nr_images
; i
++)
3557 debug_printf("images[%i].format = %s\n", i
, util_format_name(image
[i
].image_state
.format
));
3562 create_tes_jit_types(struct draw_tes_llvm_variant
*var
)
3564 struct gallivm_state
*gallivm
= var
->gallivm
;
3565 LLVMTypeRef texture_type
, sampler_type
, image_type
, context_type
;
3567 texture_type
= create_jit_texture_type(gallivm
, "texture");
3568 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
3569 image_type
= create_jit_image_type(gallivm
, "image");
3571 context_type
= create_tes_jit_context_type(gallivm
,
3573 texture_type
, sampler_type
,
3575 "draw_tes_jit_context");
3576 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
3578 var
->input_array_type
= create_tes_jit_input_type(gallivm
);
3582 get_tes_context_ptr_type(struct draw_tes_llvm_variant
*variant
)
3584 if (!variant
->context_ptr_type
)
3585 create_tes_jit_types(variant
);
3586 return variant
->context_ptr_type
;
3590 generate_tes_mask_value(struct draw_tes_llvm_variant
*variant
,
3591 struct lp_type tes_type
, LLVMValueRef limit
, LLVMValueRef loop_counter
)
3593 struct gallivm_state
*gallivm
= variant
->gallivm
;
3594 LLVMBuilderRef builder
= gallivm
->builder
;
3595 struct lp_type mask_type
= lp_int_type(tes_type
);
3596 LLVMValueRef num_prims
;
3597 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
3600 num_prims
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
), limit
);
3601 for (i
= 0; i
< tes_type
.length
; i
++) {
3602 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3603 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, LLVMBuildAdd(builder
, loop_counter
, idx
, ""), idx
, "");
3605 mask_val
= lp_build_compare(gallivm
, mask_type
,
3606 PIPE_FUNC_GREATER
, num_prims
, mask_val
);
3612 draw_tes_llvm_fetch_vertex_input(const struct lp_build_tes_iface
*tes_iface
,
3613 struct lp_build_context
*bld
,
3614 boolean is_vindex_indirect
,
3615 LLVMValueRef vertex_index
,
3616 boolean is_aindex_indirect
,
3617 LLVMValueRef attrib_index
,
3618 LLVMValueRef swizzle_index
)
3620 const struct draw_tes_llvm_iface
*tes
= draw_tes_llvm_iface(tes_iface
);
3621 struct gallivm_state
*gallivm
= bld
->gallivm
;
3622 LLVMBuilderRef builder
= gallivm
->builder
;
3623 LLVMValueRef indices
[3];
3625 struct lp_type type
= bld
->type
;
3627 if (is_vindex_indirect
|| is_aindex_indirect
) {
3632 for (i
= 0; i
< type
.length
; ++i
) {
3633 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3634 LLVMValueRef vert_chan_index
= vertex_index
;
3635 LLVMValueRef attr_chan_index
= attrib_index
;
3636 LLVMValueRef channel_vec
;
3638 if (is_vindex_indirect
) {
3639 vert_chan_index
= LLVMBuildExtractElement(builder
,
3640 vertex_index
, idx
, "");
3642 if (is_aindex_indirect
) {
3643 attr_chan_index
= LLVMBuildExtractElement(builder
,
3644 attrib_index
, idx
, "");
3647 indices
[0] = vert_chan_index
;
3648 indices
[1] = attr_chan_index
;
3649 indices
[2] = swizzle_index
;
3651 channel_vec
= LLVMBuildGEP(builder
, tes
->input
, indices
, 3, "");
3652 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
3654 res
= LLVMBuildInsertElement(builder
, res
, channel_vec
, idx
, "");
3657 indices
[0] = vertex_index
;
3658 indices
[1] = attrib_index
;
3659 indices
[2] = swizzle_index
;
3661 res
= LLVMBuildGEP(builder
, tes
->input
, indices
, 3, "");
3662 res
= LLVMBuildLoad(builder
, res
, "");
3663 res
= lp_build_broadcast_scalar(bld
, res
);
3669 draw_tes_llvm_fetch_patch_input(const struct lp_build_tes_iface
*tes_iface
,
3670 struct lp_build_context
*bld
,
3671 boolean is_aindex_indirect
,
3672 LLVMValueRef attrib_index
,
3673 LLVMValueRef swizzle_index
)
3675 const struct draw_tes_llvm_iface
*tes
= draw_tes_llvm_iface(tes_iface
);
3676 struct gallivm_state
*gallivm
= bld
->gallivm
;
3677 LLVMBuilderRef builder
= gallivm
->builder
;
3678 LLVMValueRef indices
[3];
3680 struct lp_type type
= bld
->type
;
3682 if (is_aindex_indirect
) {
3687 for (i
= 0; i
< type
.length
; ++i
) {
3688 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3689 LLVMValueRef attr_chan_index
= attrib_index
;
3690 LLVMValueRef channel_vec
;
3692 if (is_aindex_indirect
) {
3693 attr_chan_index
= LLVMBuildExtractElement(builder
,
3694 attrib_index
, idx
, "");
3697 indices
[0] = lp_build_const_int32(gallivm
, 0);
3698 indices
[1] = attr_chan_index
;
3699 indices
[2] = swizzle_index
;
3701 channel_vec
= LLVMBuildGEP(builder
, tes
->input
, indices
, 3, "");
3702 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
3704 res
= LLVMBuildInsertElement(builder
, res
, channel_vec
, idx
, "");
3707 indices
[0] = lp_build_const_int32(gallivm
, 0);
3708 indices
[1] = attrib_index
;
3709 indices
[2] = swizzle_index
;
3711 res
= LLVMBuildGEP(builder
, tes
->input
, indices
, 3, "");
3712 res
= LLVMBuildLoad(builder
, res
, "");
3713 res
= lp_build_broadcast_scalar(bld
, res
);
3719 draw_tes_llvm_generate(struct draw_llvm
*llvm
,
3720 struct draw_tes_llvm_variant
*variant
)
3722 struct gallivm_state
*gallivm
= variant
->gallivm
;
3723 LLVMContextRef context
= gallivm
->context
;
3724 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
3725 LLVMTypeRef flt_type
= LLVMFloatTypeInContext(context
);
3726 LLVMTypeRef arg_types
[10];
3727 LLVMTypeRef func_type
;
3728 LLVMValueRef variant_func
;
3729 LLVMValueRef context_ptr
;
3730 LLVMValueRef tess_coord
[2], io_ptr
, input_array
, num_tess_coord
;
3731 LLVMValueRef tess_inner
, tess_outer
, prim_id
, patch_vertices_in
;
3732 LLVMBasicBlockRef block
;
3733 LLVMBuilderRef builder
;
3734 LLVMValueRef mask_val
;
3735 struct lp_build_context bld
, bldvec
;
3736 struct lp_build_sampler_soa
*sampler
= 0;
3737 struct lp_build_image_soa
*image
= NULL
;
3738 struct lp_bld_tgsi_system_values system_values
;
3741 struct draw_tes_llvm_iface tes_iface
;
3742 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
3743 struct lp_build_mask_context mask
;
3744 LLVMValueRef consts_ptr
, num_consts_ptr
;
3745 LLVMValueRef ssbos_ptr
, num_ssbos_ptr
;
3747 struct lp_type tes_type
;
3748 unsigned vector_length
= variant
->shader
->base
.vector_length
;
3750 memset(&system_values
, 0, sizeof(system_values
));
3751 memset(&outputs
, 0, sizeof(outputs
));
3753 snprintf(func_name
, sizeof(func_name
), "draw_llvm_tes_variant%u",
3754 variant
->shader
->variants_cached
);
3756 arg_types
[0] = get_tes_context_ptr_type(variant
); /* context */
3757 arg_types
[1] = variant
->input_array_type
; /* input */
3758 arg_types
[2] = variant
->vertex_header_ptr_type
;
3759 arg_types
[3] = int32_type
;
3760 arg_types
[4] = int32_type
;
3761 arg_types
[5] = LLVMPointerType(flt_type
, 0);
3762 arg_types
[6] = LLVMPointerType(flt_type
, 0);
3763 arg_types
[7] = LLVMPointerType(LLVMArrayType(flt_type
, 4), 0);
3764 arg_types
[8] = LLVMPointerType(LLVMArrayType(flt_type
, 2), 0);
3765 arg_types
[9] = int32_type
;
3767 func_type
= LLVMFunctionType(int32_type
, arg_types
, ARRAY_SIZE(arg_types
), 0);
3768 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
3770 variant
->function
= variant_func
;
3771 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
3773 for (i
= 0; i
< ARRAY_SIZE(arg_types
); ++i
)
3774 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
3775 lp_add_function_attr(variant_func
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
3777 context_ptr
= LLVMGetParam(variant_func
, 0);
3778 input_array
= LLVMGetParam(variant_func
, 1);
3779 io_ptr
= LLVMGetParam(variant_func
, 2);
3780 prim_id
= LLVMGetParam(variant_func
, 3);
3781 num_tess_coord
= LLVMGetParam(variant_func
, 4);
3782 tess_coord
[0] = LLVMGetParam(variant_func
, 5);
3783 tess_coord
[1] = LLVMGetParam(variant_func
, 6);
3784 tess_outer
= LLVMGetParam(variant_func
, 7);
3785 tess_inner
= LLVMGetParam(variant_func
, 8);
3786 patch_vertices_in
= LLVMGetParam(variant_func
, 9);
3788 lp_build_name(context_ptr
, "context");
3789 lp_build_name(input_array
, "input");
3790 lp_build_name(io_ptr
, "io");
3791 lp_build_name(prim_id
, "prim_id");
3792 lp_build_name(num_tess_coord
, "num_tess_coord");
3793 lp_build_name(tess_coord
[0], "tess_coord[0]");
3794 lp_build_name(tess_coord
[1], "tess_coord[1]");
3795 lp_build_name(tess_outer
, "tess_outer");
3796 lp_build_name(tess_inner
, "tess_inner");
3797 lp_build_name(patch_vertices_in
, "patch_vertices_in");
3799 tes_iface
.base
.fetch_vertex_input
= draw_tes_llvm_fetch_vertex_input
;
3800 tes_iface
.base
.fetch_patch_input
= draw_tes_llvm_fetch_patch_input
;
3801 tes_iface
.input
= input_array
;
3802 tes_iface
.variant
= variant
;
3804 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
3805 builder
= gallivm
->builder
;
3806 LLVMPositionBuilderAtEnd(builder
, block
);
3808 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
3810 memset(&tes_type
, 0, sizeof tes_type
);
3811 tes_type
.floating
= TRUE
; /* floating point values */
3812 tes_type
.sign
= TRUE
; /* values are signed */
3813 tes_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
3814 tes_type
.width
= 32; /* 32-bit float */
3815 tes_type
.length
= vector_length
;
3817 lp_build_context_init(&bldvec
, variant
->gallivm
, lp_int_type(tes_type
));
3818 consts_ptr
= draw_tes_jit_context_constants(variant
->gallivm
, context_ptr
);
3820 draw_tes_jit_context_num_constants(variant
->gallivm
, context_ptr
);
3822 ssbos_ptr
= draw_tes_jit_context_ssbos(variant
->gallivm
, context_ptr
);
3824 draw_tes_jit_context_num_ssbos(variant
->gallivm
, context_ptr
);
3825 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
);
3826 image
= draw_llvm_image_soa_create(draw_tes_llvm_variant_key_images(&variant
->key
));
3827 step
= lp_build_const_int32(gallivm
, vector_length
);
3829 system_values
.tess_outer
= LLVMBuildLoad(builder
, tess_outer
, "");
3830 system_values
.tess_inner
= LLVMBuildLoad(builder
, tess_inner
, "");
3832 system_values
.prim_id
= lp_build_broadcast_scalar(&bldvec
, prim_id
);
3834 system_values
.vertices_in
= lp_build_broadcast_scalar(&bldvec
, patch_vertices_in
);
3835 struct lp_build_loop_state lp_loop
;
3836 lp_build_loop_begin(&lp_loop
, gallivm
, bld
.zero
);
3840 io
= LLVMBuildGEP(builder
, io_ptr
, &lp_loop
.counter
, 1, "");
3841 mask_val
= generate_tes_mask_value(variant
, tes_type
, num_tess_coord
, lp_loop
.counter
);
3842 lp_build_mask_begin(&mask
, gallivm
, tes_type
, mask_val
);
3844 system_values
.tess_coord
= LLVMGetUndef(LLVMArrayType(LLVMVectorType(flt_type
, vector_length
), 3));
3845 for (i
= 0; i
< 3; i
++) {
3846 LLVMValueRef tess_coord_chan
= LLVMGetUndef(LLVMVectorType(flt_type
, vector_length
));
3847 for (unsigned j
= 0; j
< vector_length
; j
++) {
3848 LLVMValueRef idx
= LLVMBuildAdd(builder
, lp_loop
.counter
, lp_build_const_int32(gallivm
, j
), "");
3849 LLVMValueRef tc_val
;
3851 if (variant
->shader
->base
.prim_mode
== PIPE_PRIM_TRIANGLES
) {
3852 tc_val
= lp_build_const_float(gallivm
, 1.0);
3853 tc_val
= LLVMBuildFSub(builder
, tc_val
, lp_build_pointer_get(builder
, tess_coord
[0], idx
), "");
3854 tc_val
= LLVMBuildFSub(builder
, tc_val
, lp_build_pointer_get(builder
, tess_coord
[1], idx
), "");
3856 tc_val
= lp_build_const_float(gallivm
, 0.0);
3858 tc_val
= lp_build_pointer_get(builder
, tess_coord
[i
], idx
);
3860 tess_coord_chan
= LLVMBuildInsertElement(builder
, tess_coord_chan
, tc_val
, lp_build_const_int32(gallivm
, j
), "");
3862 system_values
.tess_coord
= LLVMBuildInsertValue(builder
, system_values
.tess_coord
, tess_coord_chan
, i
, "");
3865 struct lp_build_tgsi_params params
;
3866 memset(¶ms
, 0, sizeof(params
));
3868 params
.type
= tes_type
;
3869 params
.mask
= &mask
;
3870 params
.consts_ptr
= consts_ptr
;
3871 params
.const_sizes_ptr
= num_consts_ptr
;
3872 params
.system_values
= &system_values
;
3873 params
.context_ptr
= context_ptr
;
3874 params
.sampler
= sampler
;
3875 params
.info
= &llvm
->draw
->tes
.tess_eval_shader
->info
;
3876 params
.ssbo_ptr
= ssbos_ptr
;
3877 params
.ssbo_sizes_ptr
= num_ssbos_ptr
;
3878 params
.image
= image
;
3879 params
.tes_iface
= &tes_iface
.base
;
3881 lp_build_nir_soa(variant
->gallivm
,
3882 llvm
->draw
->tes
.tess_eval_shader
->state
.ir
.nir
,
3886 lp_build_mask_end(&mask
);
3887 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
3888 lp_int_type(tes_type
), 0);
3890 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
3891 params
.info
->num_outputs
, tes_type
, FALSE
);
3893 lp_build_loop_end_cond(&lp_loop
, num_tess_coord
, step
, LLVMIntUGE
);
3894 sampler
->destroy(sampler
);
3895 image
->destroy(image
);
3897 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
3898 gallivm_verify_function(gallivm
, variant_func
);
3901 struct draw_tes_llvm_variant
*
3902 draw_tes_llvm_create_variant(struct draw_llvm
*llvm
,
3903 unsigned num_outputs
,
3904 const struct draw_tes_llvm_variant_key
*key
)
3906 struct draw_tes_llvm_variant
*variant
;
3907 struct llvm_tess_eval_shader
*shader
= llvm_tess_eval_shader(llvm
->draw
->tes
.tess_eval_shader
);
3908 LLVMTypeRef vertex_header
;
3909 char module_name
[64];
3911 variant
= MALLOC(sizeof *variant
+
3912 shader
->variant_key_size
- sizeof variant
->key
);
3916 variant
->llvm
= llvm
;
3917 variant
->shader
= shader
;
3919 snprintf(module_name
, sizeof(module_name
), "draw_llvm_tes_variant%u",
3920 variant
->shader
->variants_cached
);
3922 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
3924 create_tes_jit_types(variant
);
3926 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
3928 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
3930 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
3932 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
3933 nir_print_shader(llvm
->draw
->tes
.tess_eval_shader
->state
.ir
.nir
, stderr
);
3934 draw_tes_llvm_dump_variant_key(&variant
->key
);
3937 draw_tes_llvm_generate(llvm
, variant
);
3939 gallivm_compile_module(variant
->gallivm
);
3941 variant
->jit_func
= (draw_tes_jit_func
)
3942 gallivm_jit_function(variant
->gallivm
, variant
->function
);
3944 gallivm_free_ir(variant
->gallivm
);
3946 variant
->list_item_global
.base
= variant
;
3947 variant
->list_item_local
.base
= variant
;
3948 /*variant->no = */shader
->variants_created
++;
3949 variant
->list_item_global
.base
= variant
;
3955 draw_tes_llvm_destroy_variant(struct draw_tes_llvm_variant
*variant
)
3957 struct draw_llvm
*llvm
= variant
->llvm
;
3959 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
3960 debug_printf("Deleting TES variant: %u tes variants,\t%u total variants\n",
3961 variant
->shader
->variants_cached
, llvm
->nr_tes_variants
);
3964 gallivm_destroy(variant
->gallivm
);
3966 remove_from_list(&variant
->list_item_local
);
3967 variant
->shader
->variants_cached
--;
3968 remove_from_list(&variant
->list_item_global
);
3969 llvm
->nr_tes_variants
--;
3973 struct draw_tes_llvm_variant_key
*
3974 draw_tes_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
3977 struct draw_tes_llvm_variant_key
*key
;
3978 struct draw_sampler_static_state
*draw_sampler
;
3979 struct draw_image_static_state
*draw_image
;
3981 key
= (struct draw_tes_llvm_variant_key
*)store
;
3983 memset(key
, 0, offsetof(struct draw_tes_llvm_variant_key
, samplers
[0]));
3985 /* All variants of this shader will have the same value for
3986 * nr_samplers. Not yet trying to compact away holes in the
3989 key
->nr_samplers
= llvm
->draw
->tes
.tess_eval_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
3990 if (llvm
->draw
->tes
.tess_eval_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
3991 key
->nr_sampler_views
=
3992 llvm
->draw
->tes
.tess_eval_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
3995 key
->nr_sampler_views
= key
->nr_samplers
;
3998 key
->nr_images
= llvm
->draw
->tes
.tess_eval_shader
->info
.file_max
[TGSI_FILE_IMAGE
] + 1;
4000 draw_sampler
= key
->samplers
;
4002 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
4004 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
4005 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
4006 llvm
->draw
->samplers
[PIPE_SHADER_TESS_EVAL
][i
]);
4008 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
4009 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
4010 llvm
->draw
->sampler_views
[PIPE_SHADER_TESS_EVAL
][i
]);
4013 draw_image
= draw_tes_llvm_variant_key_images(key
);
4014 memset(draw_image
, 0,
4015 key
->nr_images
* sizeof *draw_image
);
4016 for (i
= 0; i
< key
->nr_images
; i
++) {
4017 lp_sampler_static_texture_state_image(&draw_image
[i
].image_state
,
4018 llvm
->draw
->images
[PIPE_SHADER_TESS_EVAL
][i
]);
4024 draw_tes_llvm_dump_variant_key(struct draw_tes_llvm_variant_key
*key
)
4027 struct draw_sampler_static_state
*sampler
= key
->samplers
;
4028 struct draw_image_static_state
*image
= draw_tes_llvm_variant_key_images(key
);
4029 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
4030 debug_printf("sampler[%i].src_format = %s\n", i
,
4031 util_format_name(sampler
[i
].texture_state
.format
));
4034 for (i
= 0 ; i
< key
->nr_images
; i
++)
4035 debug_printf("images[%i].format = %s\n", i
, util_format_name(image
[i
].image_state
.format
));