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_FIRST_LEVEL
] =
156 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
157 elem_types
[DRAW_JIT_TEXTURE_BASE
] =
158 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
159 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
160 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
161 elem_types
[DRAW_JIT_TEXTURE_MIP_OFFSETS
] =
162 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
164 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
165 ARRAY_SIZE(elem_types
), 0);
167 (void) target
; /* silence unused var warning for non-debug build */
168 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
169 target
, texture_type
,
170 DRAW_JIT_TEXTURE_WIDTH
);
171 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
172 target
, texture_type
,
173 DRAW_JIT_TEXTURE_HEIGHT
);
174 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
175 target
, texture_type
,
176 DRAW_JIT_TEXTURE_DEPTH
);
177 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, base
,
178 target
, texture_type
,
179 DRAW_JIT_TEXTURE_BASE
);
180 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
181 target
, texture_type
,
182 DRAW_JIT_TEXTURE_ROW_STRIDE
);
183 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
184 target
, texture_type
,
185 DRAW_JIT_TEXTURE_IMG_STRIDE
);
186 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
187 target
, texture_type
,
188 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
189 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
190 target
, texture_type
,
191 DRAW_JIT_TEXTURE_LAST_LEVEL
);
192 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, mip_offsets
,
193 target
, texture_type
,
194 DRAW_JIT_TEXTURE_MIP_OFFSETS
);
196 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
203 * Create LLVM type for struct draw_jit_sampler
206 create_jit_sampler_type(struct gallivm_state
*gallivm
, const char *struct_name
)
208 LLVMTargetDataRef target
= gallivm
->target
;
209 LLVMTypeRef sampler_type
;
210 LLVMTypeRef elem_types
[DRAW_JIT_SAMPLER_NUM_FIELDS
];
212 elem_types
[DRAW_JIT_SAMPLER_MIN_LOD
] =
213 elem_types
[DRAW_JIT_SAMPLER_MAX_LOD
] =
214 elem_types
[DRAW_JIT_SAMPLER_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
215 elem_types
[DRAW_JIT_SAMPLER_BORDER_COLOR
] =
216 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
218 sampler_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
219 ARRAY_SIZE(elem_types
), 0);
221 (void) target
; /* silence unused var warning for non-debug build */
222 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, min_lod
,
223 target
, sampler_type
,
224 DRAW_JIT_SAMPLER_MIN_LOD
);
225 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, max_lod
,
226 target
, sampler_type
,
227 DRAW_JIT_SAMPLER_MAX_LOD
);
228 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, lod_bias
,
229 target
, sampler_type
,
230 DRAW_JIT_SAMPLER_LOD_BIAS
);
231 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, border_color
,
232 target
, sampler_type
,
233 DRAW_JIT_SAMPLER_BORDER_COLOR
);
235 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler
, target
, sampler_type
);
241 * Create LLVM type for struct draw_jit_texture
244 create_jit_image_type(struct gallivm_state
*gallivm
, const char *struct_name
)
246 LLVMTargetDataRef target
= gallivm
->target
;
247 LLVMTypeRef image_type
;
248 LLVMTypeRef elem_types
[DRAW_JIT_IMAGE_NUM_FIELDS
];
249 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
251 elem_types
[DRAW_JIT_IMAGE_WIDTH
] =
252 elem_types
[DRAW_JIT_IMAGE_HEIGHT
] =
253 elem_types
[DRAW_JIT_IMAGE_DEPTH
] =
254 elem_types
[DRAW_JIT_IMAGE_ROW_STRIDE
] =
255 elem_types
[DRAW_JIT_IMAGE_IMG_STRIDE
] = int32_type
;
256 elem_types
[DRAW_JIT_IMAGE_BASE
] =
257 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
259 image_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
260 ARRAY_SIZE(elem_types
), 0);
262 (void) target
; /* silence unused var warning for non-debug build */
263 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, width
,
265 DRAW_JIT_IMAGE_WIDTH
);
266 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, height
,
268 DRAW_JIT_IMAGE_HEIGHT
);
269 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, depth
,
271 DRAW_JIT_IMAGE_DEPTH
);
272 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, base
,
274 DRAW_JIT_IMAGE_BASE
);
275 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, row_stride
,
277 DRAW_JIT_IMAGE_ROW_STRIDE
);
278 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, img_stride
,
280 DRAW_JIT_IMAGE_IMG_STRIDE
);
282 LP_CHECK_STRUCT_SIZE(struct draw_jit_image
, target
, image_type
);
288 * Create LLVM type for struct draw_jit_context
291 create_jit_context_type(struct gallivm_state
*gallivm
,
292 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
293 LLVMTypeRef image_type
,
294 const char *struct_name
)
296 LLVMTargetDataRef target
= gallivm
->target
;
297 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
298 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
299 LLVMTypeRef elem_types
[DRAW_JIT_CTX_NUM_FIELDS
];
300 LLVMTypeRef context_type
;
302 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
303 LP_MAX_TGSI_CONST_BUFFERS
);
304 elem_types
[1] = LLVMArrayType(int_type
, /* num_vs_constants */
305 LP_MAX_TGSI_CONST_BUFFERS
);
306 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
307 DRAW_TOTAL_CLIP_PLANES
), 0);
308 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewports */
309 elem_types
[4] = LLVMArrayType(texture_type
,
310 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
311 elem_types
[5] = LLVMArrayType(sampler_type
,
312 PIPE_MAX_SAMPLERS
); /* samplers */
313 elem_types
[6] = LLVMArrayType(image_type
,
314 PIPE_MAX_SHADER_IMAGES
); /* images */
315 elem_types
[7] = LLVMArrayType(LLVMPointerType(int_type
, 0), /* vs_ssbo */
316 LP_MAX_TGSI_SHADER_BUFFERS
);
317 elem_types
[8] = LLVMArrayType(int_type
, /* num_vs_ssbos */
318 LP_MAX_TGSI_SHADER_BUFFERS
);
319 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
320 ARRAY_SIZE(elem_types
), 0);
322 (void) target
; /* silence unused var warning for non-debug build */
323 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
324 target
, context_type
, DRAW_JIT_CTX_CONSTANTS
);
325 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, num_vs_constants
,
326 target
, context_type
, DRAW_JIT_CTX_NUM_CONSTANTS
);
327 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
328 target
, context_type
, DRAW_JIT_CTX_PLANES
);
329 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewports
,
330 target
, context_type
, DRAW_JIT_CTX_VIEWPORT
);
331 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
332 target
, context_type
,
333 DRAW_JIT_CTX_TEXTURES
);
334 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
335 target
, context_type
,
336 DRAW_JIT_CTX_SAMPLERS
);
337 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, images
,
338 target
, context_type
, DRAW_JIT_CTX_IMAGES
);
339 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_ssbos
,
340 target
, context_type
, DRAW_JIT_CTX_SSBOS
);
341 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, num_vs_ssbos
,
342 target
, context_type
, DRAW_JIT_CTX_NUM_SSBOS
);
343 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
344 target
, context_type
);
351 * Create LLVM type for struct draw_gs_jit_context
354 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
355 unsigned vector_length
,
356 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
357 LLVMTypeRef image_type
,
358 const char *struct_name
)
360 LLVMTargetDataRef target
= gallivm
->target
;
361 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
362 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
363 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
364 LLVMTypeRef context_type
;
366 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
367 LP_MAX_TGSI_CONST_BUFFERS
);
368 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
369 LP_MAX_TGSI_CONST_BUFFERS
);
370 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
371 DRAW_TOTAL_CLIP_PLANES
), 0);
372 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewports */
374 elem_types
[4] = LLVMArrayType(texture_type
,
375 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
376 elem_types
[5] = LLVMArrayType(sampler_type
,
377 PIPE_MAX_SAMPLERS
); /* samplers */
378 elem_types
[6] = LLVMArrayType(image_type
,
379 PIPE_MAX_SHADER_IMAGES
); /* images */
380 elem_types
[7] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
381 elem_types
[8] = LLVMPointerType(LLVMVectorType(int_type
,
383 elem_types
[9] = LLVMPointerType(LLVMVectorType(int_type
,
386 elem_types
[10] = LLVMArrayType(LLVMPointerType(int_type
, 0), /* ssbos */
387 LP_MAX_TGSI_SHADER_BUFFERS
);
388 elem_types
[11] = LLVMArrayType(int_type
, /* num_ssbos */
389 LP_MAX_TGSI_SHADER_BUFFERS
);
391 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
392 ARRAY_SIZE(elem_types
), 0);
394 (void) target
; /* silence unused var warning for non-debug build */
395 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
396 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
397 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, num_constants
,
398 target
, context_type
, DRAW_GS_JIT_CTX_NUM_CONSTANTS
);
399 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
400 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
401 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewports
,
402 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
403 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
404 target
, context_type
,
405 DRAW_GS_JIT_CTX_TEXTURES
);
406 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
407 target
, context_type
,
408 DRAW_GS_JIT_CTX_SAMPLERS
);
409 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
410 target
, context_type
,
411 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
412 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
413 target
, context_type
,
414 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
415 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
416 target
, context_type
,
417 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
418 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, ssbos
,
419 target
, context_type
, DRAW_GS_JIT_CTX_SSBOS
);
420 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, num_ssbos
,
421 target
, context_type
, DRAW_GS_JIT_CTX_NUM_SSBOS
);
422 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, images
,
423 target
, context_type
, DRAW_GS_JIT_CTX_IMAGES
);
424 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
425 target
, context_type
);
432 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
434 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
435 LLVMTypeRef input_array
;
437 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
438 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
439 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
440 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
446 * Create LLVM type for struct pipe_vertex_buffer
449 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
,
450 const char *struct_name
)
452 LLVMTargetDataRef target
= gallivm
->target
;
453 LLVMTypeRef elem_types
[4];
456 elem_types
[0] = LLVMInt16TypeInContext(gallivm
->context
);
457 elem_types
[1] = LLVMInt8TypeInContext(gallivm
->context
);
458 elem_types
[2] = LLVMInt32TypeInContext(gallivm
->context
);
459 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
461 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
462 ARRAY_SIZE(elem_types
), 0);
464 (void) target
; /* silence unused var warning for non-debug build */
465 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
467 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, is_user_buffer
,
469 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
471 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer
.resource
,
474 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
481 * Create LLVM type for struct vertex_header;
484 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
486 LLVMTargetDataRef target
= gallivm
->target
;
487 LLVMTypeRef elem_types
[3];
488 LLVMTypeRef vertex_header
;
489 char struct_name
[24];
491 snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
493 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
494 elem_types
[DRAW_JIT_VERTEX_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
495 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
497 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
498 ARRAY_SIZE(elem_types
), 0);
500 /* these are bit-fields and we can't take address of them
501 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
502 target, vertex_header,
503 DRAW_JIT_VERTEX_CLIPMASK);
504 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
505 target, vertex_header,
506 DRAW_JIT_VERTEX_EDGEFLAG);
507 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
508 target, vertex_header,
509 DRAW_JIT_VERTEX_PAD);
510 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
511 target, vertex_header,
512 DRAW_JIT_VERTEX_VERTEX_ID);
514 (void) target
; /* silence unused var warning for non-debug build */
515 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip_pos
,
516 target
, vertex_header
,
517 DRAW_JIT_VERTEX_CLIP_POS
);
518 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
519 target
, vertex_header
,
520 DRAW_JIT_VERTEX_DATA
);
522 assert(LLVMABISizeOfType(target
, vertex_header
) ==
523 offsetof(struct vertex_header
, data
[data_elems
]));
525 return vertex_header
;
529 * Create LLVM type for struct draw_tcs_jit_context
532 create_tcs_jit_context_type(struct gallivm_state
*gallivm
,
533 unsigned vector_length
,
534 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
535 LLVMTypeRef image_type
,
536 const char *struct_name
)
538 LLVMTargetDataRef target
= gallivm
->target
;
539 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
540 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
541 LLVMTypeRef elem_types
[DRAW_TCS_JIT_CTX_NUM_FIELDS
];
542 LLVMTypeRef context_type
;
544 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
545 LP_MAX_TGSI_CONST_BUFFERS
);
546 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
547 LP_MAX_TGSI_CONST_BUFFERS
);
548 elem_types
[2] = LLVMInt32TypeInContext(gallivm
->context
);
549 elem_types
[3] = LLVMInt32TypeInContext(gallivm
->context
);
551 elem_types
[4] = LLVMArrayType(texture_type
,
552 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
553 elem_types
[5] = LLVMArrayType(sampler_type
,
554 PIPE_MAX_SAMPLERS
); /* samplers */
555 elem_types
[6] = LLVMArrayType(image_type
,
556 PIPE_MAX_SHADER_IMAGES
); /* images */
558 elem_types
[7] = LLVMArrayType(LLVMPointerType(int_type
, 0), /* ssbos */
559 LP_MAX_TGSI_SHADER_BUFFERS
);
560 elem_types
[8] = LLVMArrayType(int_type
, /* num_ssbos */
561 LP_MAX_TGSI_SHADER_BUFFERS
);
563 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
564 ARRAY_SIZE(elem_types
), 0);
566 (void) target
; /* silence unused var warning for non-debug build */
567 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, constants
,
568 target
, context_type
, DRAW_TCS_JIT_CTX_CONSTANTS
);
569 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, num_constants
,
570 target
, context_type
, DRAW_TCS_JIT_CTX_NUM_CONSTANTS
);
571 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, textures
,
572 target
, context_type
,
573 DRAW_TCS_JIT_CTX_TEXTURES
);
574 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, samplers
,
575 target
, context_type
,
576 DRAW_TCS_JIT_CTX_SAMPLERS
);
577 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, ssbos
,
578 target
, context_type
, DRAW_TCS_JIT_CTX_SSBOS
);
579 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, num_ssbos
,
580 target
, context_type
, DRAW_TCS_JIT_CTX_NUM_SSBOS
);
581 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, images
,
582 target
, context_type
, DRAW_TCS_JIT_CTX_IMAGES
);
583 LP_CHECK_STRUCT_SIZE(struct draw_tcs_jit_context
,
584 target
, context_type
);
590 create_tcs_jit_input_type(struct gallivm_state
*gallivm
)
592 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
593 LLVMTypeRef input_array
;
595 input_array
= LLVMArrayType(float_type
, TGSI_NUM_CHANNELS
); /* num channels */
596 input_array
= LLVMArrayType(input_array
, NUM_TCS_INPUTS
); /* num attrs per vertex */
597 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
603 create_tcs_jit_output_type(struct gallivm_state
*gallivm
)
605 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
606 LLVMTypeRef output_array
;
608 output_array
= LLVMArrayType(float_type
, TGSI_NUM_CHANNELS
); /* num channels */
609 output_array
= LLVMArrayType(output_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
610 output_array
= LLVMPointerType(output_array
, 0); /* num vertices per prim */
616 create_tes_jit_input_type(struct gallivm_state
*gallivm
)
618 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
619 LLVMTypeRef input_array
;
621 input_array
= LLVMArrayType(float_type
, TGSI_NUM_CHANNELS
); /* num channels */
622 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
623 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
629 * Create LLVM type for struct draw_tes_jit_context
632 create_tes_jit_context_type(struct gallivm_state
*gallivm
,
633 unsigned vector_length
,
634 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
635 LLVMTypeRef image_type
,
636 const char *struct_name
)
638 LLVMTargetDataRef target
= gallivm
->target
;
639 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
640 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
641 LLVMTypeRef elem_types
[DRAW_TCS_JIT_CTX_NUM_FIELDS
];
642 LLVMTypeRef context_type
;
644 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
645 LP_MAX_TGSI_CONST_BUFFERS
);
646 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
647 LP_MAX_TGSI_CONST_BUFFERS
);
648 elem_types
[2] = LLVMInt32TypeInContext(gallivm
->context
);
649 elem_types
[3] = LLVMInt32TypeInContext(gallivm
->context
);
651 elem_types
[4] = LLVMArrayType(texture_type
,
652 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
653 elem_types
[5] = LLVMArrayType(sampler_type
,
654 PIPE_MAX_SAMPLERS
); /* samplers */
655 elem_types
[6] = LLVMArrayType(image_type
,
656 PIPE_MAX_SHADER_IMAGES
); /* images */
658 elem_types
[7] = LLVMArrayType(LLVMPointerType(int_type
, 0), /* ssbos */
659 LP_MAX_TGSI_SHADER_BUFFERS
);
660 elem_types
[8] = LLVMArrayType(int_type
, /* num_ssbos */
661 LP_MAX_TGSI_SHADER_BUFFERS
);
663 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
664 ARRAY_SIZE(elem_types
), 0);
666 (void) target
; /* silence unused var warning for non-debug build */
667 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, constants
,
668 target
, context_type
, DRAW_TCS_JIT_CTX_CONSTANTS
);
669 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, num_constants
,
670 target
, context_type
, DRAW_TCS_JIT_CTX_NUM_CONSTANTS
);
671 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, textures
,
672 target
, context_type
,
673 DRAW_TCS_JIT_CTX_TEXTURES
);
674 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, samplers
,
675 target
, context_type
,
676 DRAW_TCS_JIT_CTX_SAMPLERS
);
677 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, ssbos
,
678 target
, context_type
, DRAW_TCS_JIT_CTX_SSBOS
);
679 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, num_ssbos
,
680 target
, context_type
, DRAW_TCS_JIT_CTX_NUM_SSBOS
);
681 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, images
,
682 target
, context_type
, DRAW_TCS_JIT_CTX_IMAGES
);
683 LP_CHECK_STRUCT_SIZE(struct draw_tes_jit_context
,
684 target
, context_type
);
690 * Create LLVM types for various structures.
693 create_jit_types(struct draw_llvm_variant
*variant
)
695 struct gallivm_state
*gallivm
= variant
->gallivm
;
696 LLVMTypeRef texture_type
, sampler_type
, context_type
, buffer_type
,
699 texture_type
= create_jit_texture_type(gallivm
, "texture");
700 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
701 image_type
= create_jit_image_type(gallivm
, "image");
703 context_type
= create_jit_context_type(gallivm
, texture_type
, sampler_type
,
706 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
708 buffer_type
= create_jit_dvbuffer_type(gallivm
, "draw_vertex_buffer");
709 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
711 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
712 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
717 get_context_ptr_type(struct draw_llvm_variant
*variant
)
719 if (!variant
->context_ptr_type
)
720 create_jit_types(variant
);
721 return variant
->context_ptr_type
;
726 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
728 if (!variant
->buffer_ptr_type
)
729 create_jit_types(variant
);
730 return variant
->buffer_ptr_type
;
735 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
737 if (!variant
->vb_ptr_type
)
738 create_jit_types(variant
);
739 return variant
->vb_ptr_type
;
743 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
745 if (!variant
->vertex_header_ptr_type
)
746 create_jit_types(variant
);
747 return variant
->vertex_header_ptr_type
;
752 * Create per-context LLVM info.
755 draw_llvm_create(struct draw_context
*draw
, LLVMContextRef context
)
757 struct draw_llvm
*llvm
;
759 if (!lp_build_init())
762 llvm
= CALLOC_STRUCT( draw_llvm
);
768 llvm
->context
= context
;
769 if (!llvm
->context
) {
770 llvm
->context
= LLVMContextCreate();
771 llvm
->context_owned
= true;
776 llvm
->nr_variants
= 0;
777 make_empty_list(&llvm
->vs_variants_list
);
779 llvm
->nr_gs_variants
= 0;
780 make_empty_list(&llvm
->gs_variants_list
);
782 llvm
->nr_tcs_variants
= 0;
783 make_empty_list(&llvm
->tcs_variants_list
);
785 llvm
->nr_tes_variants
= 0;
786 make_empty_list(&llvm
->tes_variants_list
);
791 draw_llvm_destroy(llvm
);
797 * Free per-context LLVM info.
800 draw_llvm_destroy(struct draw_llvm
*llvm
)
802 if (llvm
->context_owned
)
803 LLVMContextDispose(llvm
->context
);
804 llvm
->context
= NULL
;
806 /* XXX free other draw_llvm data? */
812 * Create LLVM-generated code for a vertex shader.
814 struct draw_llvm_variant
*
815 draw_llvm_create_variant(struct draw_llvm
*llvm
,
817 const struct draw_llvm_variant_key
*key
)
819 struct draw_llvm_variant
*variant
;
820 struct llvm_vertex_shader
*shader
=
821 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
822 LLVMTypeRef vertex_header
;
823 char module_name
[64];
825 variant
= MALLOC(sizeof *variant
+
826 shader
->variant_key_size
-
827 sizeof variant
->key
);
831 variant
->llvm
= llvm
;
832 variant
->shader
= shader
;
834 snprintf(module_name
, sizeof(module_name
), "draw_llvm_vs_variant%u",
835 variant
->shader
->variants_cached
);
837 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
839 create_jit_types(variant
);
841 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
843 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
844 if (llvm
->draw
->vs
.vertex_shader
->state
.type
== PIPE_SHADER_IR_TGSI
)
845 tgsi_dump(llvm
->draw
->vs
.vertex_shader
->state
.tokens
, 0);
847 nir_print_shader(llvm
->draw
->vs
.vertex_shader
->state
.ir
.nir
, stderr
);
848 draw_llvm_dump_variant_key(&variant
->key
);
851 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
853 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
855 draw_llvm_generate(llvm
, variant
);
857 gallivm_compile_module(variant
->gallivm
);
859 variant
->jit_func
= (draw_jit_vert_func
)
860 gallivm_jit_function(variant
->gallivm
, variant
->function
);
862 gallivm_free_ir(variant
->gallivm
);
864 variant
->list_item_global
.base
= variant
;
865 variant
->list_item_local
.base
= variant
;
866 /*variant->no = */shader
->variants_created
++;
867 variant
->list_item_global
.base
= variant
;
874 generate_vs(struct draw_llvm_variant
*variant
,
875 LLVMBuilderRef builder
,
876 struct lp_type vs_type
,
877 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
878 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
879 const struct lp_bld_tgsi_system_values
*system_values
,
880 LLVMValueRef context_ptr
,
881 const struct lp_build_sampler_soa
*draw_sampler
,
882 const struct lp_build_image_soa
*draw_image
,
883 boolean clamp_vertex_color
,
884 struct lp_build_mask_context
*bld_mask
)
886 struct draw_llvm
*llvm
= variant
->llvm
;
887 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
888 LLVMValueRef consts_ptr
=
889 draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
890 LLVMValueRef num_consts_ptr
=
891 draw_jit_context_num_vs_constants(variant
->gallivm
, context_ptr
);
892 LLVMValueRef ssbos_ptr
=
893 draw_jit_context_vs_ssbos(variant
->gallivm
, context_ptr
);
894 LLVMValueRef num_ssbos_ptr
=
895 draw_jit_context_num_vs_ssbos(variant
->gallivm
, context_ptr
);
897 struct lp_build_tgsi_params params
;
898 memset(¶ms
, 0, sizeof(params
));
900 params
.type
= vs_type
;
901 params
.mask
= bld_mask
;
902 params
.consts_ptr
= consts_ptr
;
903 params
.const_sizes_ptr
= num_consts_ptr
;
904 params
.system_values
= system_values
;
905 params
.inputs
= inputs
;
906 params
.context_ptr
= context_ptr
;
907 params
.sampler
= draw_sampler
;
908 params
.info
= &llvm
->draw
->vs
.vertex_shader
->info
;
909 params
.ssbo_ptr
= ssbos_ptr
;
910 params
.ssbo_sizes_ptr
= num_ssbos_ptr
;
911 params
.image
= draw_image
;
913 if (llvm
->draw
->vs
.vertex_shader
->state
.ir
.nir
&&
914 llvm
->draw
->vs
.vertex_shader
->state
.type
== PIPE_SHADER_IR_NIR
)
915 lp_build_nir_soa(variant
->gallivm
,
916 llvm
->draw
->vs
.vertex_shader
->state
.ir
.nir
,
920 lp_build_tgsi_soa(variant
->gallivm
,
927 unsigned chan
, attrib
;
928 struct lp_build_context bld
;
929 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
930 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
932 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
933 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
934 if (outputs
[attrib
][chan
]) {
935 switch (info
->output_semantic_name
[attrib
]) {
936 case TGSI_SEMANTIC_COLOR
:
937 case TGSI_SEMANTIC_BCOLOR
:
938 if (clamp_vertex_color
) {
939 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
940 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
941 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
953 fetch_instanced(struct gallivm_state
*gallivm
,
954 const struct util_format_description
*format_desc
,
955 struct lp_type vs_type
,
956 LLVMValueRef vb_stride
,
957 LLVMValueRef map_ptr
,
958 LLVMValueRef buffer_size_adj
,
959 LLVMValueRef
*inputs
,
962 LLVMTypeRef i32_t
= LLVMInt32TypeInContext(gallivm
->context
);
963 LLVMTypeRef aosf_t
, aosi_t
;
964 LLVMValueRef zero
= LLVMConstNull(i32_t
);
965 LLVMBuilderRef builder
= gallivm
->builder
;
966 LLVMValueRef stride
, buffer_overflowed
, aos
, index_valid
;
969 aosf_t
= lp_build_vec_type(gallivm
, lp_float32_vec4_type());
970 aosi_t
= lp_build_vec_type(gallivm
, lp_int32_vec4_type());
972 /* This mul can overflow. Wraparound is ok. */
973 stride
= LLVMBuildMul(builder
, vb_stride
, index
, "");
975 buffer_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGE
,
976 stride
, buffer_size_adj
,
977 "buffer_overflowed");
980 lp_build_print_value(gallivm
, " instance index = ", index
);
981 lp_build_print_value(gallivm
, " buffer overflowed = ", buffer_overflowed
);
984 index_valid
= LLVMBuildNot(builder
, buffer_overflowed
, "");
985 index_valid
= LLVMBuildSExt(builder
, index_valid
, i32_t
, "");
986 stride
= LLVMBuildAnd(builder
, stride
, index_valid
, "");
988 aos
= lp_build_fetch_rgba_aos(gallivm
,
990 lp_float32_vec4_type(),
996 index_valid
= lp_build_broadcast(gallivm
, aosi_t
, index_valid
);
997 aos
= LLVMBuildBitCast(builder
, aos
, aosi_t
, "");
998 aos
= LLVMBuildAnd(builder
, aos
, index_valid
, "");
999 aos
= LLVMBuildBitCast(builder
, aos
, aosf_t
, "");
1001 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1002 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
1003 inputs
[i
] = lp_build_extract_broadcast(gallivm
,
1004 lp_float32_vec4_type(),
1005 vs_type
, aos
, index
);
1011 fetch_vector(struct gallivm_state
*gallivm
,
1012 const struct util_format_description
*format_desc
,
1013 struct lp_type vs_type
,
1014 LLVMValueRef vb_stride
,
1015 LLVMValueRef map_ptr
,
1016 LLVMValueRef buffer_size_adj
,
1017 LLVMValueRef
*inputs
,
1018 LLVMValueRef indices
)
1020 LLVMBuilderRef builder
= gallivm
->builder
;
1021 struct lp_build_context blduivec
;
1022 struct lp_type fetch_type
= vs_type
;
1023 LLVMValueRef offset
, valid_mask
;
1026 lp_build_context_init(&blduivec
, gallivm
, lp_uint_type(vs_type
));
1028 vb_stride
= lp_build_broadcast_scalar(&blduivec
, vb_stride
);
1029 buffer_size_adj
= lp_build_broadcast_scalar(&blduivec
, buffer_size_adj
);
1031 /* This mul can overflow. Wraparound is ok. */
1032 offset
= lp_build_mul(&blduivec
, vb_stride
, indices
);
1034 valid_mask
= lp_build_compare(gallivm
, blduivec
.type
,
1035 PIPE_FUNC_LESS
, offset
, buffer_size_adj
);
1037 /* not valid elements use offset 0 */
1038 offset
= LLVMBuildAnd(builder
, offset
, valid_mask
, "");
1041 lp_build_print_value(gallivm
, " indices = ", indices
);
1042 lp_build_print_value(gallivm
, " offsets = ", offset
);
1043 lp_build_print_value(gallivm
, " valid_mask = ", valid_mask
);
1047 * Unlike fetch_instanced, use SoA fetch instead of multiple AoS fetches.
1048 * This should always produce better code.
1051 /* The type handling is annoying here... */
1052 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
&&
1053 format_desc
->channel
[0].pure_integer
) {
1054 if (format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_SIGNED
) {
1055 fetch_type
= lp_type_int_vec(vs_type
.width
, vs_type
.width
* vs_type
.length
);
1057 else if (format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
1058 fetch_type
= lp_type_uint_vec(vs_type
.width
, vs_type
.width
* vs_type
.length
);
1062 lp_build_fetch_rgba_soa(gallivm
, format_desc
,
1063 fetch_type
, FALSE
, map_ptr
, offset
,
1064 blduivec
.zero
, blduivec
.zero
,
1067 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1068 inputs
[i
] = LLVMBuildBitCast(builder
, inputs
[i
],
1069 lp_build_vec_type(gallivm
, vs_type
), "");
1072 /* out-of-bound fetches return all zeros */
1073 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1074 inputs
[i
] = LLVMBuildBitCast(builder
, inputs
[i
], blduivec
.vec_type
, "");
1075 inputs
[i
] = LLVMBuildAnd(builder
, inputs
[i
], valid_mask
, "");
1076 inputs
[i
] = LLVMBuildBitCast(builder
, inputs
[i
],
1077 lp_build_vec_type(gallivm
, vs_type
), "");
1083 store_aos(struct gallivm_state
*gallivm
,
1084 LLVMValueRef io_ptr
,
1088 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
1089 LLVMBuilderRef builder
= gallivm
->builder
;
1090 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
1091 LLVMValueRef indices
[3];
1093 indices
[0] = lp_build_const_int32(gallivm
, 0);
1095 indices
[2] = lp_build_const_int32(gallivm
, 0);
1097 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
1098 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
1101 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
1104 /* Unaligned store due to the vertex header */
1105 LLVMSetAlignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
1109 * Adjust the mask to architecture endianess. The mask will the store in struct:
1111 * struct vertex_header {
1112 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
1113 * unsigned edgeflag:1;
1115 * unsigned vertex_id:16;
1119 * On little-endian machine nothing needs to done, however on bit-endian machine
1120 * the mask's fields need to be adjusted with the algorithm:
1122 * uint32_t reverse (uint32_t x)
1124 * return (x >> 16) | // vertex_id
1125 * ((x & 0x3fff) << 18) | // clipmask
1126 * ((x & 0x4000) << 3) | // pad
1127 * ((x & 0x8000) << 1); // edgeflag
1131 adjust_mask(struct gallivm_state
*gallivm
,
1134 #if UTIL_ARCH_BIG_ENDIAN
1135 LLVMBuilderRef builder
= gallivm
->builder
;
1136 LLVMValueRef vertex_id
;
1137 LLVMValueRef clipmask
;
1139 LLVMValueRef edgeflag
;
1141 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
1142 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
1143 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
1145 pad
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
1146 pad
= LLVMBuildShl(builder
, pad
, lp_build_const_int32(gallivm
, 3), "");
1148 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
1149 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
1151 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
1153 mask
= LLVMBuildOr(builder
, mask
, pad
, "");
1155 mask
= LLVMBuildOr(builder
, mask
, edgeflag
, "");
1161 store_aos_array(struct gallivm_state
*gallivm
,
1162 struct lp_type soa_type
,
1163 LLVMValueRef io_ptr
,
1164 LLVMValueRef
*indices
,
1168 LLVMValueRef clipmask
,
1169 boolean need_edgeflag
)
1171 LLVMBuilderRef builder
= gallivm
->builder
;
1172 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
1173 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1174 LLVMValueRef linear_inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1175 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1176 int vector_length
= soa_type
.length
;
1179 debug_assert(TGSI_NUM_CHANNELS
== 4);
1181 for (i
= 0; i
< vector_length
; i
++) {
1182 linear_inds
[i
] = lp_build_const_int32(gallivm
, i
);
1184 inds
[i
] = indices
[i
];
1186 inds
[i
] = linear_inds
[i
];
1188 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1192 /* store vertex header for each of the n vertices */
1193 LLVMValueRef val
, cliptmp
;
1194 int vertex_id_pad_edgeflag
;
1196 /* If this assertion fails, it means we need to update the bit twidding
1197 * code here. See struct vertex_header in draw_private.h.
1199 assert(DRAW_TOTAL_CLIP_PLANES
==14);
1200 /* initialize vertex id:16 = 0xffff, pad:1 = 0, edgeflag:1 = 1 */
1201 if (!need_edgeflag
) {
1202 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
1205 vertex_id_pad_edgeflag
= (0xffff << 16);
1207 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
),
1208 vertex_id_pad_edgeflag
);
1209 /* OR with the clipmask */
1210 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
1211 for (i
= 0; i
< vector_length
; i
++) {
1212 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
1213 val
= LLVMBuildExtractElement(builder
, cliptmp
, linear_inds
[i
], "");
1214 val
= adjust_mask(gallivm
, val
);
1216 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
1217 io_ptrs
[i
], inds
[i
], val
);
1219 LLVMBuildStore(builder
, val
, id_ptr
);
1223 /* store for each of the n vertices */
1224 for (i
= 0; i
< vector_length
; i
++) {
1225 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
1231 convert_to_aos(struct gallivm_state
*gallivm
,
1233 LLVMValueRef
*indices
,
1234 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1235 LLVMValueRef clipmask
,
1237 struct lp_type soa_type
,
1238 boolean need_edgeflag
)
1240 LLVMBuilderRef builder
= gallivm
->builder
;
1241 unsigned chan
, attrib
, i
;
1244 lp_build_printf(gallivm
, " # storing begin\n");
1246 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
1247 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
1248 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
1249 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
1250 if (outputs
[attrib
][chan
]) {
1251 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
1252 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
1254 lp_build_printf(gallivm
, "output %d : %d ",
1255 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
1257 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
1259 lp_build_print_value(gallivm
, "val = ", out
);
1262 LLVMBuildBitCast(builder
, out
, lp_build_int_vec_type(gallivm
, soa_type
), "");
1264 lp_build_print_value(gallivm
, " ival = ", iv
);
1275 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
1276 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
1278 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
1280 for (i
= 0; i
< soa_type
.length
; ++i
) {
1281 aos
[i
] = lp_build_extract_range(gallivm
,
1282 soa
[i
% TGSI_NUM_CHANNELS
],
1283 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1288 store_aos_array(gallivm
,
1298 lp_build_printf(gallivm
, " # storing end\n");
1304 * Stores original vertex positions in clip coordinates
1307 store_clip(struct gallivm_state
*gallivm
,
1308 const struct lp_type vs_type
,
1309 LLVMValueRef io_ptr
,
1310 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1313 LLVMBuilderRef builder
= gallivm
->builder
;
1314 LLVMValueRef soa
[4];
1315 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
1316 LLVMValueRef indices
[2];
1317 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1318 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1319 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1320 LLVMTypeRef clip_ptr_type
=
1321 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
),
1326 indices
[1] = lp_build_const_int32(gallivm
, 0);
1328 for (i
= 0; i
< vs_type
.length
; i
++) {
1329 inds
[i
] = lp_build_const_int32(gallivm
, i
);
1330 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1333 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
1334 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
1335 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
1336 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
1338 for (i
= 0; i
< vs_type
.length
; i
++) {
1339 clip_ptrs
[i
] = draw_jit_header_clip_pos(gallivm
, io_ptrs
[i
]);
1342 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
1343 for (i
= 0; i
< vs_type
.length
; ++i
) {
1344 aos
[i
] = lp_build_extract_range(gallivm
,
1345 soa
[i
% TGSI_NUM_CHANNELS
],
1346 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1350 for (j
= 0; j
< vs_type
.length
; j
++) {
1351 LLVMValueRef clip_ptr
;
1353 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
1354 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
1356 /* Unaligned store */
1357 LLVMSetAlignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
1363 * Transforms the outputs for viewport mapping
1366 generate_viewport(struct draw_llvm_variant
*variant
,
1367 LLVMBuilderRef builder
,
1368 struct lp_type vs_type
,
1369 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1370 LLVMValueRef context_ptr
)
1373 struct gallivm_state
*gallivm
= variant
->gallivm
;
1374 struct lp_type f32_type
= vs_type
;
1375 const unsigned pos
= variant
->llvm
->draw
->vs
.position_output
;
1376 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1377 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn*/
1378 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1379 LLVMValueRef vp_ptr
= draw_jit_context_viewports(gallivm
, context_ptr
);
1381 /* We treat pipe_viewport_state as a float array */
1382 const int scale_index_offset
= offsetof(struct pipe_viewport_state
, scale
) / sizeof(float);
1383 const int trans_index_offset
= offsetof(struct pipe_viewport_state
, translate
) / sizeof(float);
1385 /* for 1/w convention*/
1386 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1387 LLVMBuildStore(builder
, out3
, outputs
[pos
][3]);
1389 /* Viewport Mapping */
1390 for (i
=0; i
<3; i
++) {
1391 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[pos
][i
], ""); /*x0 x1 .. xn*/
1394 LLVMValueRef scale_i
;
1395 LLVMValueRef trans_i
;
1398 index
= lp_build_const_int32(gallivm
, i
+ scale_index_offset
);
1399 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1401 index
= lp_build_const_int32(gallivm
, i
+ trans_index_offset
);
1402 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1404 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1405 LLVMBuildLoad(builder
, scale_i
, "scale"));
1406 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1407 LLVMBuildLoad(builder
, trans_i
, "trans"));
1410 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1411 /* mult by scale, add translation */
1412 out
= lp_build_fmuladd(builder
, out
, scale
, trans
);
1414 /* store transformed outputs */
1415 LLVMBuildStore(builder
, out
, outputs
[pos
][i
]);
1422 * Returns clipmask as nxi32 bitmask for the n vertices
1425 generate_clipmask(struct draw_llvm
*llvm
,
1426 struct gallivm_state
*gallivm
,
1427 struct lp_type vs_type
,
1428 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1429 struct draw_llvm_variant_key
*key
,
1430 LLVMValueRef context_ptr
,
1431 boolean
*have_clipdist
)
1433 LLVMBuilderRef builder
= gallivm
->builder
;
1434 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1435 LLVMValueRef test
, temp
;
1436 LLVMValueRef zero
, shift
;
1437 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1438 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1439 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1440 struct lp_type f32_type
= vs_type
;
1441 struct lp_type i32_type
= lp_int_type(vs_type
);
1442 const unsigned pos
= llvm
->draw
->vs
.position_output
;
1443 const unsigned cv
= llvm
->draw
->vs
.clipvertex_output
;
1444 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1445 boolean have_cd
= false;
1446 boolean clip_user
= key
->clip_user
;
1447 unsigned ucp_enable
= key
->ucp_enable
;
1450 cd
[0] = llvm
->draw
->vs
.ccdistance_output
[0];
1451 cd
[1] = llvm
->draw
->vs
.ccdistance_output
[1];
1453 if (cd
[0] != pos
|| cd
[1] != pos
)
1456 if (num_written_clipdistance
&& !clip_user
) {
1458 ucp_enable
= (1 << num_written_clipdistance
) - 1;
1461 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1462 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1463 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1464 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1467 * load clipvertex and position from correct locations.
1468 * if they are the same just load them once.
1470 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1471 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1472 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1473 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1475 if (clip_user
&& cv
!= pos
) {
1476 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1477 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1478 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1479 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1488 * Be careful with the comparisons and NaNs (using llvm's unordered
1489 * comparisons here).
1491 /* Cliptest, for hardwired planes */
1493 * XXX should take guardband into account (currently not in key).
1494 * Otherwise might run the draw pipeline stages for nothing.
1498 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1500 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1504 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1505 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1506 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1507 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1508 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1511 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1512 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1513 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1514 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1517 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1518 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1519 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1520 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1521 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1525 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1526 if (key
->clip_halfz
) {
1528 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1529 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1530 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1534 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1535 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1536 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1537 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1540 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1541 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1542 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1543 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1547 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1548 LLVMValueRef indices
[3];
1549 LLVMValueRef is_nan_or_inf
;
1551 /* userclip planes */
1552 while (ucp_enable
) {
1553 unsigned plane_idx
= ffs(ucp_enable
)-1;
1554 ucp_enable
&= ~(1 << plane_idx
);
1557 if (have_cd
&& num_written_clipdistance
) {
1558 LLVMValueRef clipdist
;
1562 *have_clipdist
= TRUE
;
1564 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1566 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1568 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1569 is_nan_or_inf
= lp_build_is_inf_or_nan(gallivm
, vs_type
, clipdist
);
1570 test
= LLVMBuildOr(builder
, test
, is_nan_or_inf
, "");
1571 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1LL << plane_idx
);
1572 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1573 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1575 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1576 indices
[0] = lp_build_const_int32(gallivm
, 0);
1577 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1579 indices
[2] = lp_build_const_int32(gallivm
, 0);
1580 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1581 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1582 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1583 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1585 indices
[2] = lp_build_const_int32(gallivm
, 1);
1586 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1587 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1588 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1589 sum
= lp_build_fmuladd(builder
, planes
, cv_y
, sum
);
1591 indices
[2] = lp_build_const_int32(gallivm
, 2);
1592 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1593 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1594 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1595 sum
= lp_build_fmuladd(builder
, planes
, cv_z
, sum
);
1597 indices
[2] = lp_build_const_int32(gallivm
, 3);
1598 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1599 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1600 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1601 sum
= lp_build_fmuladd(builder
, planes
, cv_w
, sum
);
1603 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1604 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1LL << plane_idx
);
1605 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1606 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1610 if (key
->need_edgeflags
) {
1612 * This isn't really part of clipmask but stored the same in vertex
1613 * header later, so do it here.
1615 unsigned edge_attr
= llvm
->draw
->vs
.edgeflag_output
;
1616 LLVMValueRef one
= lp_build_const_vec(gallivm
, f32_type
, 1.0);
1617 LLVMValueRef edgeflag
= LLVMBuildLoad(builder
, outputs
[edge_attr
][0], "");
1618 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_EQUAL
, one
, edgeflag
);
1619 temp
= lp_build_const_int_vec(gallivm
, i32_type
,
1620 1LL << DRAW_TOTAL_CLIP_PLANES
);
1621 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1622 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1629 * Returns boolean if any clipping has occurred
1630 * Used zero/one i8 value to represent boolean
1633 clipmask_booli8(struct gallivm_state
*gallivm
,
1634 const struct lp_type vs_type
,
1635 LLVMValueRef clipmask_bool_ptr
,
1636 boolean edgeflag_in_clipmask
)
1638 LLVMBuilderRef builder
= gallivm
->builder
;
1639 LLVMTypeRef int8_type
= LLVMInt8TypeInContext(gallivm
->context
);
1640 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1642 struct lp_build_context bldivec
;
1644 lp_build_context_init(&bldivec
, gallivm
, lp_int_type(vs_type
));
1647 * We need to invert the edgeflag bit from the clipmask here
1648 * (because the result is really if we want to run the pipeline or not
1649 * and we (may) need it if edgeflag was 0).
1651 if (edgeflag_in_clipmask
) {
1652 LLVMValueRef edge
= lp_build_const_int_vec(gallivm
, bldivec
.type
,
1653 1LL << DRAW_TOTAL_CLIP_PLANES
);
1654 clipmask_bool
= LLVMBuildXor(builder
, clipmask_bool
, edge
, "");
1658 * XXX: probably should mask off bits from the mask which come from
1659 * vertices which were beyond the count (i.e. indices_valid for
1660 * linear fetches, for elts ones we don't have the correct mask
1661 * right now). Otherwise might run the pipeline for nothing,
1662 * though everything should still work.
1664 ret
= lp_build_any_true_range(&bldivec
, vs_type
.length
, clipmask_bool
);
1665 ret
= LLVMBuildZExt(builder
, ret
, int8_type
, "");
1670 draw_gs_llvm_fetch_input(const struct lp_build_gs_iface
*gs_iface
,
1671 struct lp_build_context
* bld
,
1672 boolean is_vindex_indirect
,
1673 LLVMValueRef vertex_index
,
1674 boolean is_aindex_indirect
,
1675 LLVMValueRef attrib_index
,
1676 LLVMValueRef swizzle_index
)
1678 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1679 struct gallivm_state
*gallivm
= bld
->gallivm
;
1680 LLVMBuilderRef builder
= gallivm
->builder
;
1681 LLVMValueRef indices
[3];
1683 struct lp_type type
= bld
->type
;
1685 if (is_vindex_indirect
|| is_aindex_indirect
) {
1688 for (i
= 0; i
< type
.length
; ++i
) {
1689 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1690 LLVMValueRef vert_chan_index
= vertex_index
;
1691 LLVMValueRef attr_chan_index
= attrib_index
;
1692 LLVMValueRef channel_vec
, value
;
1694 if (is_vindex_indirect
) {
1695 vert_chan_index
= LLVMBuildExtractElement(builder
,
1696 vertex_index
, idx
, "");
1698 if (is_aindex_indirect
) {
1699 attr_chan_index
= LLVMBuildExtractElement(builder
,
1700 attrib_index
, idx
, "");
1703 indices
[0] = vert_chan_index
;
1704 indices
[1] = attr_chan_index
;
1705 indices
[2] = swizzle_index
;
1707 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1708 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1709 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1711 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1714 indices
[0] = vertex_index
;
1715 indices
[1] = attrib_index
;
1716 indices
[2] = swizzle_index
;
1718 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1719 res
= LLVMBuildLoad(builder
, res
, "");
1726 draw_gs_llvm_emit_vertex(const struct lp_build_gs_iface
*gs_base
,
1727 struct lp_build_context
* bld
,
1728 LLVMValueRef (*outputs
)[4],
1729 LLVMValueRef emitted_vertices_vec
,
1730 LLVMValueRef stream_id
)
1732 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1733 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1734 struct gallivm_state
*gallivm
= variant
->gallivm
;
1735 LLVMBuilderRef builder
= gallivm
->builder
;
1736 struct lp_type gs_type
= bld
->type
;
1737 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1738 lp_int_type(gs_type
), 0);
1739 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1740 LLVMValueRef next_prim_offset
=
1741 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1742 LLVMValueRef io
= variant
->io_ptr
;
1744 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1746 for (i
= 0; i
< gs_type
.length
; ++i
) {
1747 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1748 LLVMValueRef currently_emitted
=
1749 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1750 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1751 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1754 LLVMValueRef stream_idx
= LLVMBuildExtractElement(builder
, stream_id
, lp_build_const_int32(gallivm
, 0), "");
1755 LLVMValueRef cnd
= LLVMBuildICmp(builder
, LLVMIntULT
, stream_idx
, lp_build_const_int32(gallivm
, variant
->shader
->base
.num_vertex_streams
), "");
1756 struct lp_build_if_state if_ctx
;
1757 lp_build_if(&if_ctx
, gallivm
, cnd
);
1758 io
= lp_build_pointer_get(builder
, io
, LLVMBuildExtractElement(builder
, stream_id
, lp_build_const_int32(gallivm
, 0), ""));
1760 convert_to_aos(gallivm
, io
, indices
,
1762 gs_info
->num_outputs
, gs_type
,
1764 lp_build_endif(&if_ctx
);
1768 draw_gs_llvm_end_primitive(const struct lp_build_gs_iface
*gs_base
,
1769 struct lp_build_context
* bld
,
1770 LLVMValueRef total_emitted_vertices_vec_ptr
,
1771 LLVMValueRef verts_per_prim_vec
,
1772 LLVMValueRef emitted_prims_vec
,
1773 LLVMValueRef mask_vec
)
1775 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1776 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1777 struct gallivm_state
*gallivm
= variant
->gallivm
;
1778 LLVMBuilderRef builder
= gallivm
->builder
;
1779 LLVMValueRef prim_lengts_ptr
=
1780 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1783 for (i
= 0; i
< bld
->type
.length
; ++i
) {
1784 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1785 LLVMValueRef prims_emitted
=
1786 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1787 LLVMValueRef store_ptr
;
1788 LLVMValueRef num_vertices
=
1789 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1791 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1792 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1793 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1794 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1799 draw_gs_llvm_epilogue(const struct lp_build_gs_iface
*gs_base
,
1800 LLVMValueRef total_emitted_vertices_vec
,
1801 LLVMValueRef emitted_prims_vec
, unsigned stream
)
1803 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1804 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1805 struct gallivm_state
*gallivm
= variant
->gallivm
;
1806 LLVMBuilderRef builder
= gallivm
->builder
;
1807 LLVMValueRef emitted_verts_ptr
=
1808 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1809 LLVMValueRef emitted_prims_ptr
=
1810 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1811 LLVMValueRef stream_val
= lp_build_const_int32(gallivm
, stream
);
1813 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &stream_val
, 1, "");
1814 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &stream_val
, 1, "");
1816 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1817 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1821 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
)
1823 struct gallivm_state
*gallivm
= variant
->gallivm
;
1824 LLVMContextRef context
= gallivm
->context
;
1825 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1826 LLVMTypeRef arg_types
[12];
1827 unsigned num_arg_types
= ARRAY_SIZE(arg_types
);
1828 LLVMTypeRef func_type
;
1829 LLVMValueRef context_ptr
;
1830 LLVMBasicBlockRef block
;
1831 LLVMBuilderRef builder
;
1833 struct lp_type vs_type
;
1834 LLVMValueRef count
, fetch_elts
, start_or_maxelt
;
1835 LLVMValueRef vertex_id_offset
;
1836 LLVMValueRef stride
, step
, io_itr
;
1837 LLVMValueRef ind_vec
, start_vec
, have_elts
, fetch_max
, tmp
;
1838 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1839 LLVMValueRef vb_stride
[PIPE_MAX_ATTRIBS
];
1840 LLVMValueRef map_ptr
[PIPE_MAX_ATTRIBS
];
1841 LLVMValueRef buffer_size_adj
[PIPE_MAX_ATTRIBS
];
1842 LLVMValueRef instance_index
[PIPE_MAX_ATTRIBS
];
1843 LLVMValueRef fake_buf_ptr
, fake_buf
;
1845 struct draw_context
*draw
= llvm
->draw
;
1846 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1848 struct lp_build_context bld
, blduivec
;
1849 struct lp_build_loop_state lp_loop
;
1850 struct lp_build_if_state if_ctx
;
1851 const int vector_length
= lp_native_vector_width
/ 32;
1852 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1853 struct lp_build_sampler_soa
*sampler
= 0;
1854 struct lp_build_image_soa
*image
= NULL
;
1855 LLVMValueRef ret
, clipmask_bool_ptr
;
1856 struct draw_llvm_variant_key
*key
= &variant
->key
;
1857 /* If geometry shader is present we need to skip both the viewport
1858 * transformation and clipping otherwise the inputs to the geometry
1859 * shader will be incorrect.
1860 * The code can't handle vp transform when vs writes vp index neither
1861 * (though this would be fixable here, but couldn't just broadcast
1864 const boolean bypass_viewport
= key
->has_gs_or_tes
|| key
->bypass_viewport
||
1865 vs_info
->writes_viewport_index
;
1866 const boolean enable_cliptest
= !key
->has_gs_or_tes
&& (key
->clip_xy
||
1869 key
->need_edgeflags
);
1870 LLVMValueRef variant_func
;
1871 const unsigned pos
= draw
->vs
.position_output
;
1872 const unsigned cv
= draw
->vs
.clipvertex_output
;
1873 boolean have_clipdist
= FALSE
;
1874 struct lp_bld_tgsi_system_values system_values
;
1876 memset(&system_values
, 0, sizeof(system_values
));
1877 memset(&outputs
, 0, sizeof(outputs
));
1878 snprintf(func_name
, sizeof(func_name
), "draw_llvm_vs_variant%u",
1879 variant
->shader
->variants_cached
);
1882 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1883 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1884 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1885 arg_types
[i
++] = int32_type
; /* count */
1886 arg_types
[i
++] = int32_type
; /* start/fetch_elt_max */
1887 arg_types
[i
++] = int32_type
; /* stride */
1888 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1889 arg_types
[i
++] = int32_type
; /* instance_id */
1890 arg_types
[i
++] = int32_type
; /* vertex_id_offset */
1891 arg_types
[i
++] = int32_type
; /* start_instance */
1892 arg_types
[i
++] = LLVMPointerType(int32_type
, 0); /* fetch_elts */
1893 arg_types
[i
++] = int32_type
; /* draw_id */
1895 func_type
= LLVMFunctionType(LLVMInt8TypeInContext(context
),
1896 arg_types
, num_arg_types
, 0);
1898 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
1899 variant
->function
= variant_func
;
1901 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1902 for (i
= 0; i
< num_arg_types
; ++i
)
1903 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1904 lp_add_function_attr(variant_func
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
1906 context_ptr
= LLVMGetParam(variant_func
, 0);
1907 io_ptr
= LLVMGetParam(variant_func
, 1);
1908 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1909 count
= LLVMGetParam(variant_func
, 3);
1911 * XXX: the maxelt part is unused. Not really useful, since we cannot
1912 * get index buffer overflows due to vsplit (which provides its own
1913 * elts buffer, with a different size than what's passed in here).
1915 start_or_maxelt
= LLVMGetParam(variant_func
, 4);
1917 * XXX: stride is actually unused. The stride we use is strictly calculated
1918 * from the number of outputs (including the draw_extra outputs).
1919 * Should probably fix some day (we need a new vs just because of extra
1920 * outputs which the generated vs won't touch).
1922 stride
= LLVMGetParam(variant_func
, 5);
1923 vb_ptr
= LLVMGetParam(variant_func
, 6);
1924 system_values
.instance_id
= LLVMGetParam(variant_func
, 7);
1925 vertex_id_offset
= LLVMGetParam(variant_func
, 8);
1926 system_values
.base_instance
= LLVMGetParam(variant_func
, 9);
1927 fetch_elts
= LLVMGetParam(variant_func
, 10);
1928 system_values
.draw_id
= LLVMGetParam(variant_func
, 11);
1930 lp_build_name(context_ptr
, "context");
1931 lp_build_name(io_ptr
, "io");
1932 lp_build_name(vbuffers_ptr
, "vbuffers");
1933 lp_build_name(count
, "count");
1934 lp_build_name(start_or_maxelt
, "start_or_maxelt");
1935 lp_build_name(stride
, "stride");
1936 lp_build_name(vb_ptr
, "vb");
1937 lp_build_name(system_values
.instance_id
, "instance_id");
1938 lp_build_name(vertex_id_offset
, "vertex_id_offset");
1939 lp_build_name(system_values
.base_instance
, "start_instance");
1940 lp_build_name(fetch_elts
, "fetch_elts");
1941 lp_build_name(system_values
.draw_id
, "draw_id");
1947 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1948 builder
= gallivm
->builder
;
1949 LLVMPositionBuilderAtEnd(builder
, block
);
1951 memset(&vs_type
, 0, sizeof vs_type
);
1952 vs_type
.floating
= TRUE
; /* floating point values */
1953 vs_type
.sign
= TRUE
; /* values are signed */
1954 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1955 vs_type
.width
= 32; /* 32-bit float */
1956 vs_type
.length
= vector_length
;
1958 lp_build_context_init(&bld
, gallivm
, lp_type_uint(32));
1959 lp_build_context_init(&blduivec
, gallivm
, lp_uint_type(vs_type
));
1961 /* hold temporary "bool" clipmask */
1962 clipmask_bool_ptr
= lp_build_alloca(gallivm
, blduivec
.vec_type
, "");
1964 fake_buf
= lp_build_alloca_undef(gallivm
,
1965 LLVMVectorType(LLVMInt64TypeInContext(context
), 4), "");
1966 fake_buf
= LLVMBuildBitCast(builder
, fake_buf
,
1967 LLVMPointerType(LLVMInt8TypeInContext(context
), 0), "");
1968 fake_buf_ptr
= LLVMBuildGEP(builder
, fake_buf
, &bld
.zero
, 1, "");
1970 /* code generated texture sampling */
1971 sampler
= draw_llvm_sampler_soa_create(draw_llvm_variant_key_samplers(key
));
1973 image
= draw_llvm_image_soa_create(draw_llvm_variant_key_images(key
));
1975 step
= lp_build_const_int32(gallivm
, vector_length
);
1977 ind_vec
= blduivec
.undef
;
1978 for (i
= 0; i
< vs_type
.length
; i
++) {
1979 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
1980 ind_vec
= LLVMBuildInsertElement(builder
, ind_vec
, index
, index
, "");
1983 have_elts
= LLVMBuildICmp(builder
, LLVMIntNE
,
1984 LLVMConstPointerNull(arg_types
[10]), fetch_elts
, "");
1986 fetch_max
= LLVMBuildSub(builder
, count
, bld
.one
, "fetch_max");
1987 fetch_max
= lp_build_broadcast_scalar(&blduivec
, fetch_max
);
1989 * Only needed for non-indexed path.
1991 start_vec
= lp_build_broadcast_scalar(&blduivec
, start_or_maxelt
);
1994 * Pre-calculate everything which is constant per shader invocation.
1996 for (j
= 0; j
< key
->nr_vertex_elements
; ++j
) {
1997 LLVMValueRef vb_buffer_offset
, buffer_size
, temp_ptr
;
1998 LLVMValueRef vb_info
, vbuffer_ptr
, buf_offset
, ofbit
;
1999 struct pipe_vertex_element
*velem
= &key
->vertex_element
[j
];
2000 LLVMValueRef vb_index
=
2001 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
2002 LLVMValueRef bsize
= lp_build_const_int32(gallivm
,
2003 util_format_get_blocksize(velem
->src_format
));
2004 LLVMValueRef src_offset
= lp_build_const_int32(gallivm
,
2006 struct lp_build_if_state if_ctx
;
2008 if (velem
->src_format
!= PIPE_FORMAT_NONE
) {
2009 vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
, &vb_index
, 1, "");
2010 vb_info
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
2011 vb_stride
[j
] = draw_jit_vbuffer_stride(gallivm
, vb_info
);
2012 vb_stride
[j
] = LLVMBuildZExt(gallivm
->builder
, vb_stride
[j
],
2013 LLVMInt32TypeInContext(context
), "");
2014 vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vb_info
);
2015 map_ptr
[j
] = draw_jit_dvbuffer_map(gallivm
, vbuffer_ptr
);
2016 buffer_size
= draw_jit_dvbuffer_size(gallivm
, vbuffer_ptr
);
2020 * We'll set buffer_size_adj to zero if we have of, so it will
2021 * always overflow later automatically without having to keep ofbit.
2022 * Overflows (with normal wraparound) doing the actual offset
2023 * calculation should be ok, just not for the buffer size calc.
2024 * It would also be possible to detect such overflows and return
2025 * zeros if that happens, but this would be more complex.
2027 buf_offset
= lp_build_add(&bld
, vb_buffer_offset
, src_offset
);
2028 tmp
= lp_build_sub(&bld
, bsize
, bld
.one
);
2029 buffer_size_adj
[j
] = lp_build_usub_overflow(gallivm
, buffer_size
, tmp
,
2031 buffer_size_adj
[j
] = lp_build_usub_overflow(gallivm
, buffer_size_adj
[j
],
2032 buf_offset
, &ofbit
);
2035 * We can't easily set fake vertex buffers outside the generated code.
2036 * Hence, set fake vertex buffers here instead basically, so fetch
2037 * code can always fetch using offset 0, eliminating all control flow
2038 * inside the main loop.
2039 * (Alternatively, could have control flow per vector skipping fetch
2040 * if ofbit is true.)
2042 if (velem
->instance_divisor
) {
2044 * Index is equal to the start instance plus the number of current
2045 * instance divided by the divisor. In this case we compute it as:
2046 * index = start_instance + (instance_id / divisor).
2047 * Note we could actually do the fetch here, outside the loop -
2048 * it's all constant, hopefully llvm recognizes this.
2050 LLVMValueRef current_instance
;
2051 current_instance
= LLVMBuildUDiv(builder
, system_values
.instance_id
,
2052 lp_build_const_int32(gallivm
,
2053 velem
->instance_divisor
),
2054 "instance_divisor");
2055 instance_index
[j
] = lp_build_uadd_overflow(gallivm
, system_values
.base_instance
,
2056 current_instance
, &ofbit
);
2059 buffer_size_adj
[j
] = LLVMBuildSelect(builder
, ofbit
, bld
.zero
,
2060 buffer_size_adj
[j
], "");
2062 temp_ptr
= lp_build_alloca_undef(gallivm
,
2063 LLVMPointerType(LLVMInt8TypeInContext(context
), 0), "");
2065 lp_build_if(&if_ctx
, gallivm
, ofbit
);
2067 LLVMBuildStore(builder
, fake_buf_ptr
, temp_ptr
);
2069 lp_build_else(&if_ctx
);
2071 map_ptr
[j
] = LLVMBuildGEP(builder
, map_ptr
[j
], &buf_offset
, 1, "");
2072 LLVMBuildStore(builder
, map_ptr
[j
], temp_ptr
);
2074 lp_build_endif(&if_ctx
);
2075 map_ptr
[j
] = LLVMBuildLoad(builder
, temp_ptr
, "map_ptr");
2078 lp_build_printf(gallivm
, "velem %d, vbuf index = %u, vb_stride = %u\n",
2079 lp_build_const_int32(gallivm
, j
),
2080 vb_index
, vb_stride
[j
]);
2081 lp_build_printf(gallivm
,
2082 " vb_buffer_offset = %u, src_offset = %u, buf_offset = %u\n",
2083 vb_buffer_offset
, src_offset
, buf_offset
);
2084 lp_build_printf(gallivm
, " buffer size = %u, blocksize = %u\n",
2085 buffer_size
, bsize
);
2086 lp_build_printf(gallivm
, " instance_id = %u\n", system_values
.instance_id
);
2091 lp_build_loop_begin(&lp_loop
, gallivm
, bld
.zero
);
2093 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
2095 LLVMValueRef clipmask
; /* holds the clipmask value */
2096 LLVMValueRef true_index_array
, index_store
;
2097 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
2099 io_itr
= lp_loop
.counter
;
2101 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
2103 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
2104 io_itr
, io
, lp_loop
.counter
);
2107 true_index_array
= lp_build_broadcast_scalar(&blduivec
, lp_loop
.counter
);
2108 true_index_array
= LLVMBuildAdd(builder
, true_index_array
, ind_vec
, "");
2110 LLVMValueRef exec_mask
= lp_build_cmp(&blduivec
, PIPE_FUNC_LEQUAL
, true_index_array
, fetch_max
);
2112 * Limit indices to fetch_max, otherwise might try to access indices
2113 * beyond index buffer (or rather vsplit elt buffer) size.
2114 * Could probably safely (?) skip this for non-indexed draws and
2115 * simplify things minimally (by removing it could combine the ind_vec
2116 * and start_vec adds). I think the only effect for non-indexed draws will
2117 * be that for the invalid elements they will be all fetched from the
2118 * same location as the last valid one, but noone should really care.
2120 true_index_array
= lp_build_min(&blduivec
, true_index_array
, fetch_max
);
2122 index_store
= lp_build_alloca_undef(gallivm
, blduivec
.vec_type
, "index_store");
2124 lp_build_if(&if_ctx
, gallivm
, have_elts
);
2127 * Note: you'd expect some comparison/clamp against fetch_elt_max
2129 * There used to be one here but it was incorrect: overflow was
2130 * detected if index > fetch_elt_max - but the correct condition
2131 * would be index >= fetch_elt_max (since this is just size of elts
2132 * buffer / element size).
2133 * Using the correct condition however will cause failures - due to
2134 * vsplit/vcache code which rebases indices. So, as an example, if
2135 * fetch_elt_max is just 1 and fetch_count 2, vsplit cache will
2136 * replace all invalid indices with 0 - which in case of elt_bias
2137 * not being zero will get a different fetch index than the valid
2138 * index 0. So, just rely on vsplit code preventing out-of-bounds
2139 * fetches. This is also why it's safe to do elts fetch even if there
2140 * was no index buffer bound - the real buffer is never seen here, at
2141 * least not if there are index buffer overflows...
2145 * XXX should not have to do this, as scale can be handled
2146 * natively by loads (hits asserts though).
2148 tmp
= lp_build_shl_imm(&blduivec
, true_index_array
, 2);
2149 fetch_elts
= LLVMBuildBitCast(builder
, fetch_elts
,
2150 LLVMPointerType(LLVMInt8TypeInContext(context
),
2152 tmp
= lp_build_gather(gallivm
, vs_type
.length
,
2154 fetch_elts
, tmp
, FALSE
);
2155 LLVMBuildStore(builder
, tmp
, index_store
);
2157 lp_build_else(&if_ctx
);
2159 tmp
= LLVMBuildAdd(builder
, true_index_array
, start_vec
, "");
2160 LLVMBuildStore(builder
, tmp
, index_store
);
2162 lp_build_endif(&if_ctx
);
2164 true_index_array
= LLVMBuildLoad(builder
, index_store
, "");
2166 for (j
= 0; j
< key
->nr_vertex_elements
; ++j
) {
2167 struct pipe_vertex_element
*velem
= &key
->vertex_element
[j
];
2168 const struct util_format_description
*format_desc
=
2169 util_format_description(velem
->src_format
);
2171 if (format_desc
->format
== PIPE_FORMAT_NONE
) {
2172 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
2173 inputs
[j
][i
] = lp_build_zero(gallivm
, vs_type
);
2176 else if (velem
->instance_divisor
) {
2177 fetch_instanced(gallivm
, format_desc
, vs_type
,
2178 vb_stride
[j
], map_ptr
[j
],
2180 inputs
[j
], instance_index
[j
]);
2183 fetch_vector(gallivm
, format_desc
, vs_type
,
2184 vb_stride
[j
], map_ptr
[j
],
2186 inputs
[j
], true_index_array
);
2190 struct lp_build_mask_context mask
;
2192 lp_build_mask_begin(&mask
, gallivm
, vs_type
, exec_mask
);
2193 /* In the paths with elts vertex id has to be unaffected by the
2194 * index bias and because indices inside our elements array have
2195 * already had index bias applied we need to subtract it here to
2196 * get back to the original index.
2197 * in the linear paths vertex id has to be unaffected by the
2198 * original start index and because we abuse the 'start' variable
2199 * to either represent the actual start index or the index at which
2200 * the primitive was split (we split rendering into chunks of at
2201 * most 4095-vertices) we need to back out the original start
2202 * index out of our vertex id here.
2203 * for ARB_shader_draw_parameters, base_vertex should be 0 for non-indexed draws.
2205 LLVMValueRef base_vertex
= lp_build_select(&bld
, have_elts
, vertex_id_offset
, lp_build_const_int32(gallivm
, 0));;
2206 system_values
.basevertex
= lp_build_broadcast_scalar(&blduivec
, base_vertex
);
2207 system_values
.vertex_id
= true_index_array
;
2208 system_values
.vertex_id_nobase
= LLVMBuildSub(builder
, true_index_array
,
2209 lp_build_broadcast_scalar(&blduivec
, vertex_id_offset
), "");
2211 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
2212 generate_vs(variant
,
2221 key
->clamp_vertex_color
,
2224 lp_build_mask_end(&mask
);
2225 if (pos
!= -1 && cv
!= -1) {
2226 /* store original positions in clip before further manipulation */
2227 store_clip(gallivm
, vs_type
, io
, outputs
, pos
);
2230 if (enable_cliptest
) {
2231 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
2232 /* allocate clipmask, assign it integer type */
2233 clipmask
= generate_clipmask(llvm
,
2238 context_ptr
, &have_clipdist
);
2239 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
2240 /* store temporary clipping boolean value */
2241 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
2244 clipmask
= blduivec
.zero
;
2247 /* do viewport mapping */
2248 if (!bypass_viewport
) {
2249 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
2253 clipmask
= blduivec
.zero
;
2256 /* store clipmask in vertex header,
2257 * original positions in clip
2258 * and transformed positions in data
2260 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
2261 vs_info
->num_outputs
, vs_type
,
2262 enable_cliptest
&& key
->need_edgeflags
);
2264 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
2266 sampler
->destroy(sampler
);
2267 image
->destroy(image
);
2269 /* return clipping boolean value for function */
2270 ret
= clipmask_booli8(gallivm
, vs_type
, clipmask_bool_ptr
,
2271 enable_cliptest
&& key
->need_edgeflags
);
2273 LLVMBuildRet(builder
, ret
);
2275 gallivm_verify_function(gallivm
, variant_func
);
2279 struct draw_llvm_variant_key
*
2280 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2283 struct draw_llvm_variant_key
*key
;
2284 struct draw_sampler_static_state
*draw_sampler
;
2285 struct draw_image_static_state
*draw_image
;
2287 key
= (struct draw_llvm_variant_key
*)store
;
2289 memset(key
, 0, offsetof(struct draw_llvm_variant_key
, vertex_element
[0]));
2291 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
2293 /* will have to rig this up properly later */
2294 key
->clip_xy
= llvm
->draw
->clip_xy
;
2295 key
->clip_z
= llvm
->draw
->clip_z
;
2296 key
->clip_user
= llvm
->draw
->clip_user
;
2297 key
->bypass_viewport
= llvm
->draw
->bypass_viewport
;
2298 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
2299 /* XXX assumes edgeflag output not at 0 */
2300 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
2301 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
2302 key
->has_gs_or_tes
= llvm
->draw
->gs
.geometry_shader
!= NULL
|| llvm
->draw
->tes
.tess_eval_shader
!= NULL
;
2303 key
->num_outputs
= draw_total_vs_outputs(llvm
->draw
);
2305 /* All variants of this shader will have the same value for
2306 * nr_samplers. Not yet trying to compact away holes in the
2309 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2310 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2311 key
->nr_sampler_views
=
2312 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2315 key
->nr_sampler_views
= key
->nr_samplers
;
2318 key
->nr_images
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_IMAGE
] + 1;
2320 /* Presumably all variants of the shader should have the same
2321 * number of vertex elements - ie the number of shader inputs.
2322 * NOTE: we NEED to store the needed number of needed inputs
2323 * here, not the number of provided elements to match keysize
2324 * (and the offset of sampler state in the key).
2325 * If we have excess number of vertex elements, this is valid,
2326 * but the excess ones don't matter.
2327 * If we don't have enough vertex elements (which looks not really
2328 * valid but we'll handle it gracefully) fill out missing ones with
2329 * zero (we'll recognize these later by PIPE_FORMAT_NONE).
2331 key
->nr_vertex_elements
=
2332 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
2334 if (llvm
->draw
->pt
.nr_vertex_elements
< key
->nr_vertex_elements
) {
2335 debug_printf("draw: vs with %d inputs but only have %d vertex elements\n",
2336 key
->nr_vertex_elements
, llvm
->draw
->pt
.nr_vertex_elements
);
2337 memset(key
->vertex_element
, 0,
2338 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
2340 memcpy(key
->vertex_element
,
2341 llvm
->draw
->pt
.vertex_element
,
2342 sizeof(struct pipe_vertex_element
) *
2343 MIN2(key
->nr_vertex_elements
, llvm
->draw
->pt
.nr_vertex_elements
));
2345 draw_sampler
= draw_llvm_variant_key_samplers(key
);
2346 memset(draw_sampler
, 0,
2347 MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2349 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2350 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2351 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
2353 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2354 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2355 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
2358 draw_image
= draw_llvm_variant_key_images(key
);
2359 memset(draw_image
, 0,
2360 key
->nr_images
* sizeof *draw_image
);
2361 for (i
= 0; i
< key
->nr_images
; i
++) {
2362 lp_sampler_static_texture_state_image(&draw_image
[i
].image_state
,
2363 llvm
->draw
->images
[PIPE_SHADER_VERTEX
][i
]);
2370 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
2373 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
2374 struct draw_image_static_state
*image
= draw_llvm_variant_key_images(key
);
2375 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
2376 debug_printf("clip_xy = %u\n", key
->clip_xy
);
2377 debug_printf("clip_z = %u\n", key
->clip_z
);
2378 debug_printf("clip_user = %u\n", key
->clip_user
);
2379 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
2380 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
2381 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
2382 debug_printf("has_gs_or_tes = %u\n", key
->has_gs_or_tes
);
2383 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
2385 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
2386 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
2387 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
2388 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
2389 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
2392 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2393 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
2396 for (i
= 0 ; i
< key
->nr_images
; i
++)
2397 debug_printf("images[%i].format = %s\n", i
, util_format_name(image
[i
].image_state
.format
));
2402 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
2403 enum pipe_shader_type shader_stage
,
2405 uint32_t width
, uint32_t height
, uint32_t depth
,
2406 uint32_t first_level
, uint32_t last_level
,
2407 const void *base_ptr
,
2408 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
2409 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
2410 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
2413 struct draw_jit_texture
*jit_tex
;
2415 switch (shader_stage
) {
2416 case PIPE_SHADER_VERTEX
:
2417 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->jit_context
.textures
));
2418 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
2420 case PIPE_SHADER_GEOMETRY
:
2421 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->gs_jit_context
.textures
));
2422 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
2424 case PIPE_SHADER_TESS_CTRL
:
2425 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->tcs_jit_context
.textures
));
2426 jit_tex
= &draw
->llvm
->tcs_jit_context
.textures
[sview_idx
];
2428 case PIPE_SHADER_TESS_EVAL
:
2429 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->tes_jit_context
.textures
));
2430 jit_tex
= &draw
->llvm
->tes_jit_context
.textures
[sview_idx
];
2437 jit_tex
->width
= width
;
2438 jit_tex
->height
= height
;
2439 jit_tex
->depth
= depth
;
2440 jit_tex
->first_level
= first_level
;
2441 jit_tex
->last_level
= last_level
;
2442 jit_tex
->base
= base_ptr
;
2444 for (j
= first_level
; j
<= last_level
; j
++) {
2445 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
2446 jit_tex
->row_stride
[j
] = row_stride
[j
];
2447 jit_tex
->img_stride
[j
] = img_stride
[j
];
2452 draw_llvm_set_mapped_image(struct draw_context
*draw
,
2453 enum pipe_shader_type shader_stage
,
2455 uint32_t width
, uint32_t height
, uint32_t depth
,
2456 const void *base_ptr
,
2457 uint32_t row_stride
,
2458 uint32_t img_stride
)
2460 struct draw_jit_image
*jit_image
;
2462 switch (shader_stage
) {
2463 case PIPE_SHADER_VERTEX
:
2464 assert(idx
< ARRAY_SIZE(draw
->llvm
->jit_context
.images
));
2465 jit_image
= &draw
->llvm
->jit_context
.images
[idx
];
2467 case PIPE_SHADER_GEOMETRY
:
2468 assert(idx
< ARRAY_SIZE(draw
->llvm
->gs_jit_context
.images
));
2469 jit_image
= &draw
->llvm
->gs_jit_context
.images
[idx
];
2471 case PIPE_SHADER_TESS_CTRL
:
2472 assert(idx
< ARRAY_SIZE(draw
->llvm
->tcs_jit_context
.images
));
2473 jit_image
= &draw
->llvm
->tcs_jit_context
.images
[idx
];
2475 case PIPE_SHADER_TESS_EVAL
:
2476 assert(idx
< ARRAY_SIZE(draw
->llvm
->tes_jit_context
.images
));
2477 jit_image
= &draw
->llvm
->tes_jit_context
.images
[idx
];
2484 jit_image
->width
= width
;
2485 jit_image
->height
= height
;
2486 jit_image
->depth
= depth
;
2487 jit_image
->base
= base_ptr
;
2489 jit_image
->row_stride
= row_stride
;
2490 jit_image
->img_stride
= img_stride
;
2495 draw_llvm_set_sampler_state(struct draw_context
*draw
,
2496 enum pipe_shader_type shader_type
)
2500 switch (shader_type
) {
2501 case PIPE_SHADER_VERTEX
:
2502 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
2503 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
2505 if (draw
->samplers
[PIPE_SHADER_VERTEX
][i
]) {
2506 const struct pipe_sampler_state
*s
2507 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
2508 jit_sam
->min_lod
= s
->min_lod
;
2509 jit_sam
->max_lod
= s
->max_lod
;
2510 jit_sam
->lod_bias
= s
->lod_bias
;
2511 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2515 case PIPE_SHADER_GEOMETRY
:
2516 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
2517 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
2519 if (draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]) {
2520 const struct pipe_sampler_state
*s
2521 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
2522 jit_sam
->min_lod
= s
->min_lod
;
2523 jit_sam
->max_lod
= s
->max_lod
;
2524 jit_sam
->lod_bias
= s
->lod_bias
;
2525 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2529 case PIPE_SHADER_TESS_CTRL
:
2530 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_TESS_CTRL
]; i
++) {
2531 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->tcs_jit_context
.samplers
[i
];
2533 if (draw
->samplers
[PIPE_SHADER_TESS_CTRL
][i
]) {
2534 const struct pipe_sampler_state
*s
2535 = draw
->samplers
[PIPE_SHADER_TESS_CTRL
][i
];
2536 jit_sam
->min_lod
= s
->min_lod
;
2537 jit_sam
->max_lod
= s
->max_lod
;
2538 jit_sam
->lod_bias
= s
->lod_bias
;
2539 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2543 case PIPE_SHADER_TESS_EVAL
:
2544 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_TESS_EVAL
]; i
++) {
2545 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->tes_jit_context
.samplers
[i
];
2547 if (draw
->samplers
[PIPE_SHADER_TESS_EVAL
][i
]) {
2548 const struct pipe_sampler_state
*s
2549 = draw
->samplers
[PIPE_SHADER_TESS_EVAL
][i
];
2550 jit_sam
->min_lod
= s
->min_lod
;
2551 jit_sam
->max_lod
= s
->max_lod
;
2552 jit_sam
->lod_bias
= s
->lod_bias
;
2553 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2565 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
2567 struct draw_llvm
*llvm
= variant
->llvm
;
2569 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2570 debug_printf("Deleting VS variant: %u vs variants,\t%u total variants\n",
2571 variant
->shader
->variants_cached
, llvm
->nr_variants
);
2574 gallivm_destroy(variant
->gallivm
);
2576 remove_from_list(&variant
->list_item_local
);
2577 variant
->shader
->variants_cached
--;
2578 remove_from_list(&variant
->list_item_global
);
2579 llvm
->nr_variants
--;
2585 * Create LLVM types for various structures.
2588 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
2590 struct gallivm_state
*gallivm
= var
->gallivm
;
2591 LLVMTypeRef texture_type
, sampler_type
, image_type
, context_type
;
2593 texture_type
= create_jit_texture_type(gallivm
, "texture");
2594 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2595 image_type
= create_jit_image_type(gallivm
, "image");
2597 context_type
= create_gs_jit_context_type(gallivm
,
2598 var
->shader
->base
.vector_length
,
2599 texture_type
, sampler_type
,
2601 "draw_gs_jit_context");
2602 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2604 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
2608 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
2610 if (!variant
->context_ptr_type
)
2611 create_gs_jit_types(variant
);
2612 return variant
->context_ptr_type
;
2616 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2617 struct lp_type gs_type
)
2619 struct gallivm_state
*gallivm
= variant
->gallivm
;
2620 LLVMBuilderRef builder
= gallivm
->builder
;
2621 struct lp_type mask_type
= lp_int_type(gs_type
);
2622 LLVMValueRef num_prims
;
2623 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
2626 num_prims
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
),
2627 variant
->num_prims
);
2628 for (i
= 0; i
< gs_type
.length
; i
++) {
2629 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
2630 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, idx
, idx
, "");
2632 mask_val
= lp_build_compare(gallivm
, mask_type
,
2633 PIPE_FUNC_GREATER
, num_prims
, mask_val
);
2639 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2640 struct draw_gs_llvm_variant
*variant
)
2642 struct gallivm_state
*gallivm
= variant
->gallivm
;
2643 LLVMContextRef context
= gallivm
->context
;
2644 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2645 LLVMTypeRef arg_types
[7];
2646 LLVMTypeRef func_type
;
2647 LLVMValueRef variant_func
;
2648 LLVMValueRef context_ptr
;
2649 LLVMValueRef prim_id_ptr
;
2650 LLVMBasicBlockRef block
;
2651 LLVMBuilderRef builder
;
2652 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2653 struct lp_build_sampler_soa
*sampler
= 0;
2654 struct lp_build_image_soa
*image
= NULL
;
2655 struct lp_build_context bld
;
2656 struct lp_bld_tgsi_system_values system_values
;
2658 struct lp_type gs_type
;
2660 struct draw_gs_llvm_iface gs_iface
;
2661 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2662 LLVMValueRef consts_ptr
, num_consts_ptr
;
2663 LLVMValueRef ssbos_ptr
, num_ssbos_ptr
;
2664 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2665 struct lp_build_mask_context mask
;
2666 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2667 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2669 memset(&system_values
, 0, sizeof(system_values
));
2670 memset(&outputs
, 0, sizeof(outputs
));
2672 snprintf(func_name
, sizeof(func_name
), "draw_llvm_gs_variant%u",
2673 variant
->shader
->variants_cached
);
2675 assert(variant
->vertex_header_ptr_type
);
2677 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2678 arg_types
[1] = variant
->input_array_type
; /* input */
2679 arg_types
[2] = LLVMPointerType(variant
->vertex_header_ptr_type
, 0); /* vertex_header */
2680 arg_types
[3] = int32_type
; /* num_prims */
2681 arg_types
[4] = int32_type
; /* instance_id */
2682 arg_types
[5] = LLVMPointerType(
2683 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2684 arg_types
[6] = int32_type
;
2686 func_type
= LLVMFunctionType(int32_type
, arg_types
, ARRAY_SIZE(arg_types
), 0);
2688 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
2690 variant
->function
= variant_func
;
2692 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2694 for (i
= 0; i
< ARRAY_SIZE(arg_types
); ++i
)
2695 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2696 lp_add_function_attr(variant_func
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
2698 context_ptr
= LLVMGetParam(variant_func
, 0);
2699 input_array
= LLVMGetParam(variant_func
, 1);
2700 io_ptr
= LLVMGetParam(variant_func
, 2);
2701 num_prims
= LLVMGetParam(variant_func
, 3);
2702 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2703 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2704 system_values
.invocation_id
= LLVMGetParam(variant_func
, 6);
2706 lp_build_name(context_ptr
, "context");
2707 lp_build_name(input_array
, "input");
2708 lp_build_name(io_ptr
, "io");
2709 lp_build_name(num_prims
, "num_prims");
2710 lp_build_name(system_values
.instance_id
, "instance_id");
2711 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2712 lp_build_name(system_values
.invocation_id
, "invocation_id");
2714 variant
->context_ptr
= context_ptr
;
2715 variant
->io_ptr
= io_ptr
;
2716 variant
->num_prims
= num_prims
;
2718 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2719 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2720 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2721 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2722 gs_iface
.input
= input_array
;
2723 gs_iface
.variant
= variant
;
2729 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2730 builder
= gallivm
->builder
;
2731 LLVMPositionBuilderAtEnd(builder
, block
);
2733 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2735 memset(&gs_type
, 0, sizeof gs_type
);
2736 gs_type
.floating
= TRUE
; /* floating point values */
2737 gs_type
.sign
= TRUE
; /* values are signed */
2738 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2739 gs_type
.width
= 32; /* 32-bit float */
2740 gs_type
.length
= vector_length
;
2742 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2744 draw_gs_jit_context_num_constants(variant
->gallivm
, context_ptr
);
2746 ssbos_ptr
= draw_gs_jit_context_ssbos(variant
->gallivm
, context_ptr
);
2748 draw_gs_jit_context_num_ssbos(variant
->gallivm
, context_ptr
);
2750 /* code generated texture sampling */
2751 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
);
2752 image
= draw_llvm_image_soa_create(draw_gs_llvm_variant_key_images(&variant
->key
));
2753 mask_val
= generate_mask_value(variant
, gs_type
);
2754 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2756 if (gs_info
->uses_primid
) {
2757 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");
2760 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2761 if (llvm
->draw
->gs
.geometry_shader
->state
.type
== PIPE_SHADER_IR_TGSI
)
2762 tgsi_dump(tokens
, 0);
2764 nir_print_shader(llvm
->draw
->gs
.geometry_shader
->state
.ir
.nir
, stderr
);
2765 draw_gs_llvm_dump_variant_key(&variant
->key
);
2768 struct lp_build_tgsi_params params
;
2769 memset(¶ms
, 0, sizeof(params
));
2771 params
.type
= gs_type
;
2772 params
.mask
= &mask
;
2773 params
.consts_ptr
= consts_ptr
;
2774 params
.const_sizes_ptr
= num_consts_ptr
;
2775 params
.system_values
= &system_values
;
2776 params
.context_ptr
= context_ptr
;
2777 params
.sampler
= sampler
;
2778 params
.info
= &llvm
->draw
->gs
.geometry_shader
->info
;
2779 params
.gs_iface
= (const struct lp_build_gs_iface
*)&gs_iface
;
2780 params
.ssbo_ptr
= ssbos_ptr
;
2781 params
.ssbo_sizes_ptr
= num_ssbos_ptr
;
2782 params
.image
= image
;
2784 if (llvm
->draw
->gs
.geometry_shader
->state
.type
== PIPE_SHADER_IR_TGSI
)
2785 lp_build_tgsi_soa(variant
->gallivm
,
2790 lp_build_nir_soa(variant
->gallivm
,
2791 llvm
->draw
->gs
.geometry_shader
->state
.ir
.nir
,
2795 sampler
->destroy(sampler
);
2796 image
->destroy(image
);
2798 lp_build_mask_end(&mask
);
2800 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2802 gallivm_verify_function(gallivm
, variant_func
);
2806 struct draw_gs_llvm_variant
*
2807 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2808 unsigned num_outputs
,
2809 const struct draw_gs_llvm_variant_key
*key
)
2811 struct draw_gs_llvm_variant
*variant
;
2812 struct llvm_geometry_shader
*shader
=
2813 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2814 LLVMTypeRef vertex_header
;
2815 char module_name
[64];
2817 variant
= MALLOC(sizeof *variant
+
2818 shader
->variant_key_size
-
2819 sizeof variant
->key
);
2823 variant
->llvm
= llvm
;
2824 variant
->shader
= shader
;
2826 snprintf(module_name
, sizeof(module_name
), "draw_llvm_gs_variant%u",
2827 variant
->shader
->variants_cached
);
2829 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
2831 create_gs_jit_types(variant
);
2833 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2835 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2837 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2839 draw_gs_llvm_generate(llvm
, variant
);
2841 gallivm_compile_module(variant
->gallivm
);
2843 variant
->jit_func
= (draw_gs_jit_func
)
2844 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2846 gallivm_free_ir(variant
->gallivm
);
2848 variant
->list_item_global
.base
= variant
;
2849 variant
->list_item_local
.base
= variant
;
2850 /*variant->no = */shader
->variants_created
++;
2851 variant
->list_item_global
.base
= variant
;
2857 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2859 struct draw_llvm
*llvm
= variant
->llvm
;
2861 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2862 debug_printf("Deleting GS variant: %u gs variants,\t%u total variants\n",
2863 variant
->shader
->variants_cached
, llvm
->nr_gs_variants
);
2866 gallivm_destroy(variant
->gallivm
);
2868 remove_from_list(&variant
->list_item_local
);
2869 variant
->shader
->variants_cached
--;
2870 remove_from_list(&variant
->list_item_global
);
2871 llvm
->nr_gs_variants
--;
2875 struct draw_gs_llvm_variant_key
*
2876 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2879 struct draw_gs_llvm_variant_key
*key
;
2880 struct draw_sampler_static_state
*draw_sampler
;
2881 struct draw_image_static_state
*draw_image
;
2883 key
= (struct draw_gs_llvm_variant_key
*)store
;
2885 memset(key
, 0, offsetof(struct draw_gs_llvm_variant_key
, samplers
[0]));
2887 key
->num_outputs
= draw_total_gs_outputs(llvm
->draw
);
2889 /* All variants of this shader will have the same value for
2890 * nr_samplers. Not yet trying to compact away holes in the
2893 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2894 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2895 key
->nr_sampler_views
=
2896 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2899 key
->nr_sampler_views
= key
->nr_samplers
;
2902 key
->nr_images
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_IMAGE
] + 1;
2904 draw_sampler
= key
->samplers
;
2906 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2908 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2909 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2910 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2912 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2913 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2914 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2917 draw_image
= draw_gs_llvm_variant_key_images(key
);
2918 memset(draw_image
, 0,
2919 key
->nr_images
* sizeof *draw_image
);
2920 for (i
= 0; i
< key
->nr_images
; i
++) {
2921 lp_sampler_static_texture_state_image(&draw_image
[i
].image_state
,
2922 llvm
->draw
->images
[PIPE_SHADER_GEOMETRY
][i
]);
2928 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2931 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2932 struct draw_image_static_state
*image
= draw_gs_llvm_variant_key_images(key
);
2933 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2934 debug_printf("sampler[%i].src_format = %s\n", i
,
2935 util_format_name(sampler
[i
].texture_state
.format
));
2938 for (i
= 0 ; i
< key
->nr_images
; i
++)
2939 debug_printf("images[%i].format = %s\n", i
, util_format_name(image
[i
].image_state
.format
));
2944 create_tcs_jit_types(struct draw_tcs_llvm_variant
*var
)
2946 struct gallivm_state
*gallivm
= var
->gallivm
;
2947 LLVMTypeRef texture_type
, sampler_type
, image_type
, context_type
;
2949 texture_type
= create_jit_texture_type(gallivm
, "texture");
2950 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2951 image_type
= create_jit_image_type(gallivm
, "image");
2953 context_type
= create_tcs_jit_context_type(gallivm
,
2955 texture_type
, sampler_type
,
2957 "draw_tcs_jit_context");
2958 var
->input_array_type
= create_tcs_jit_input_type(gallivm
);
2959 var
->output_array_type
= create_tcs_jit_output_type(gallivm
);
2960 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2964 get_tcs_context_ptr_type(struct draw_tcs_llvm_variant
*variant
)
2966 if (!variant
->context_ptr_type
)
2967 create_tcs_jit_types(variant
);
2968 return variant
->context_ptr_type
;
2972 draw_tcs_llvm_emit_fetch_input(const struct lp_build_tcs_iface
*tes_iface
,
2973 struct lp_build_context
*bld
,
2974 boolean is_vindex_indirect
,
2975 LLVMValueRef vertex_index
,
2976 boolean is_aindex_indirect
,
2977 LLVMValueRef attrib_index
,
2978 LLVMValueRef swizzle_index
)
2980 const struct draw_tcs_llvm_iface
*tcs
= draw_tcs_llvm_iface(tes_iface
);
2981 struct gallivm_state
*gallivm
= bld
->gallivm
;
2982 LLVMBuilderRef builder
= gallivm
->builder
;
2983 LLVMValueRef indices
[3];
2985 struct lp_type type
= bld
->type
;
2987 if (is_vindex_indirect
|| is_aindex_indirect
) {
2991 for (i
= 0; i
< type
.length
; ++i
) {
2992 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
2993 LLVMValueRef vert_chan_index
= vertex_index
;
2994 LLVMValueRef attr_chan_index
= attrib_index
;
2995 LLVMValueRef channel_vec
;
2997 if (is_vindex_indirect
) {
2998 vert_chan_index
= LLVMBuildExtractElement(builder
,
2999 vertex_index
, idx
, "");
3001 if (is_aindex_indirect
) {
3002 attr_chan_index
= LLVMBuildExtractElement(builder
,
3003 attrib_index
, idx
, "");
3006 indices
[0] = vert_chan_index
;
3007 indices
[1] = attr_chan_index
;
3008 indices
[2] = swizzle_index
;
3010 channel_vec
= LLVMBuildGEP(builder
, tcs
->input
, indices
, 3, "");
3011 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
3013 res
= LLVMBuildInsertElement(builder
, res
, channel_vec
, idx
, "");
3016 indices
[0] = vertex_index
;
3017 indices
[1] = attrib_index
;
3018 indices
[2] = swizzle_index
;
3020 res
= LLVMBuildGEP(builder
, tcs
->input
, indices
, 3, "");
3021 res
= LLVMBuildLoad(builder
, res
, "");
3022 res
= lp_build_broadcast_scalar(bld
, res
);
3028 draw_tcs_llvm_emit_fetch_output(const struct lp_build_tcs_iface
*tes_iface
,
3029 struct lp_build_context
*bld
,
3030 boolean is_vindex_indirect
,
3031 LLVMValueRef vertex_index
,
3032 boolean is_aindex_indirect
,
3033 LLVMValueRef attrib_index
,
3034 LLVMValueRef swizzle_index
,
3037 const struct draw_tcs_llvm_iface
*tcs
= draw_tcs_llvm_iface(tes_iface
);
3038 struct gallivm_state
*gallivm
= bld
->gallivm
;
3039 LLVMBuilderRef builder
= gallivm
->builder
;
3040 LLVMValueRef indices
[3];
3042 struct lp_type type
= bld
->type
;
3044 if (is_vindex_indirect
|| is_aindex_indirect
) {
3048 for (i
= 0; i
< type
.length
; ++i
) {
3049 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3050 LLVMValueRef vert_chan_index
= vertex_index
;
3051 LLVMValueRef attr_chan_index
= attrib_index
;
3052 LLVMValueRef channel_vec
;
3054 if (is_vindex_indirect
) {
3055 vert_chan_index
= LLVMBuildExtractElement(builder
,
3056 vertex_index
, idx
, "");
3058 if (is_aindex_indirect
) {
3059 attr_chan_index
= LLVMBuildExtractElement(builder
,
3060 attrib_index
, idx
, "");
3063 indices
[0] = vert_chan_index
;
3064 indices
[1] = attr_chan_index
;
3065 indices
[2] = swizzle_index
;
3067 channel_vec
= LLVMBuildGEP(builder
, tcs
->output
, indices
, 3, "");
3068 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
3070 res
= LLVMBuildInsertElement(builder
, res
, channel_vec
, idx
, "");
3073 indices
[0] = vertex_index
? vertex_index
: lp_build_const_int32(gallivm
, 0);
3074 indices
[1] = attrib_index
;
3075 indices
[2] = swizzle_index
;
3077 res
= LLVMBuildGEP(builder
, tcs
->output
, indices
, 3, "");
3078 res
= LLVMBuildLoad(builder
, res
, "");
3079 res
= lp_build_broadcast_scalar(bld
, res
);
3085 draw_tcs_llvm_emit_store_output(const struct lp_build_tcs_iface
*tes_iface
,
3086 struct lp_build_context
*bld
,
3088 boolean is_vindex_indirect
,
3089 LLVMValueRef vertex_index
,
3090 boolean is_aindex_indirect
,
3091 LLVMValueRef attrib_index
,
3092 LLVMValueRef swizzle_index
,
3094 LLVMValueRef mask_vec
)
3096 const struct draw_tcs_llvm_iface
*tcs
= draw_tcs_llvm_iface(tes_iface
);
3097 struct gallivm_state
*gallivm
= bld
->gallivm
;
3098 LLVMBuilderRef builder
= gallivm
->builder
;
3099 LLVMValueRef indices
[3];
3101 struct lp_type type
= bld
->type
;
3103 if (is_vindex_indirect
|| is_aindex_indirect
) {
3106 for (i
= 0; i
< type
.length
; ++i
) {
3107 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3108 LLVMValueRef vert_chan_index
= vertex_index
? vertex_index
: lp_build_const_int32(gallivm
, 0);
3109 LLVMValueRef attr_chan_index
= attrib_index
;
3110 LLVMValueRef channel_vec
;
3112 if (is_vindex_indirect
) {
3113 vert_chan_index
= LLVMBuildExtractElement(builder
,
3114 vertex_index
, idx
, "");
3116 if (is_aindex_indirect
) {
3117 attr_chan_index
= LLVMBuildExtractElement(builder
,
3118 attrib_index
, idx
, "");
3121 indices
[0] = vert_chan_index
;
3122 indices
[1] = attr_chan_index
;
3123 indices
[2] = swizzle_index
;
3125 channel_vec
= LLVMBuildGEP(builder
, tcs
->output
, indices
, 3, "");
3127 res
= LLVMBuildExtractElement(builder
, value
, idx
, "");
3129 struct lp_build_if_state ifthen
;
3130 LLVMValueRef cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
, mask_vec
, lp_build_const_int_vec(gallivm
, bld
->type
, 0), "");
3131 cond
= LLVMBuildExtractElement(gallivm
->builder
, cond
, idx
, "");
3132 lp_build_if(&ifthen
, gallivm
, cond
);
3133 LLVMBuildStore(builder
, res
, channel_vec
);
3134 lp_build_endif(&ifthen
);
3137 indices
[0] = vertex_index
? vertex_index
: lp_build_const_int32(gallivm
, 0);
3138 indices
[1] = attrib_index
;
3139 indices
[2] = swizzle_index
;
3141 res
= LLVMBuildGEP(builder
, tcs
->output
, indices
, 3, "");
3142 for (unsigned i
= 0; i
< type
.length
; ++i
) {
3143 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3144 LLVMValueRef val
= LLVMBuildExtractElement(builder
, value
, idx
, "");
3146 struct lp_build_if_state ifthen
;
3147 LLVMValueRef cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
, mask_vec
, lp_build_const_int_vec(gallivm
, bld
->type
, 0), "");
3148 cond
= LLVMBuildExtractElement(gallivm
->builder
, cond
, idx
, "");
3149 lp_build_if(&ifthen
, gallivm
, cond
);
3150 LLVMBuildStore(builder
, val
, res
);
3151 lp_build_endif(&ifthen
);
3158 generate_tcs_mask_value(struct draw_tcs_llvm_variant
*variant
,
3159 struct lp_type tcs_type
, LLVMValueRef limit
, LLVMValueRef loop_counter
)
3161 struct gallivm_state
*gallivm
= variant
->gallivm
;
3162 LLVMBuilderRef builder
= gallivm
->builder
;
3163 struct lp_type mask_type
= lp_int_type(tcs_type
);
3164 LLVMValueRef num_vecs
;
3165 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
3168 num_vecs
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
), limit
);
3169 for (i
= 0; i
< tcs_type
.length
; i
++) {
3170 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3171 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, LLVMBuildAdd(builder
, loop_counter
, idx
, ""), idx
, "");
3173 mask_val
= lp_build_compare(gallivm
, mask_type
,
3174 PIPE_FUNC_GREATER
, num_vecs
, mask_val
);
3180 draw_tcs_llvm_generate(struct draw_llvm
*llvm
,
3181 struct draw_tcs_llvm_variant
*variant
)
3183 struct gallivm_state
*gallivm
= variant
->gallivm
;
3184 LLVMContextRef context
= gallivm
->context
;
3185 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
3186 LLVMTypeRef arg_types
[6];
3187 LLVMTypeRef func_type
, coro_func_type
;
3188 LLVMValueRef variant_func
, variant_coro
;
3189 LLVMValueRef context_ptr
;
3190 LLVMValueRef input_array
, output_array
, prim_id
, patch_vertices_in
;
3191 LLVMValueRef mask_val
;
3192 LLVMBasicBlockRef block
;
3193 LLVMBuilderRef builder
;
3194 struct lp_build_context bld
, bldvec
;
3195 struct lp_build_sampler_soa
*sampler
= 0;
3196 struct lp_build_image_soa
*image
= NULL
;
3197 struct lp_bld_tgsi_system_values system_values
;
3198 char func_name
[64], func_name_coro
[64];
3200 struct draw_tcs_llvm_iface tcs_iface
;
3201 struct lp_build_mask_context mask
;
3202 LLVMValueRef consts_ptr
, num_consts_ptr
;
3203 LLVMValueRef ssbos_ptr
, num_ssbos_ptr
;
3204 struct lp_type tcs_type
;
3205 unsigned vector_length
= variant
->shader
->base
.vector_length
;
3207 memset(&system_values
, 0, sizeof(system_values
));
3209 snprintf(func_name
, sizeof(func_name
), "draw_llvm_tcs_variant%u",
3210 variant
->shader
->variants_cached
);
3212 snprintf(func_name_coro
, sizeof(func_name_coro
), "draw_llvm_tcs_coro_variant%u",
3213 variant
->shader
->variants_cached
);
3215 arg_types
[0] = get_tcs_context_ptr_type(variant
); /* context */
3216 arg_types
[1] = variant
->input_array_type
; /* input */
3217 arg_types
[2] = variant
->output_array_type
;
3218 arg_types
[3] = int32_type
;
3219 arg_types
[4] = int32_type
;
3220 arg_types
[5] = int32_type
; /* coroutine only */
3222 func_type
= LLVMFunctionType(int32_type
, arg_types
, ARRAY_SIZE(arg_types
) - 1, 0);
3224 coro_func_type
= LLVMFunctionType(LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0), arg_types
, ARRAY_SIZE(arg_types
), 0);
3226 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
3228 variant_coro
= LLVMAddFunction(gallivm
->module
, func_name_coro
, coro_func_type
);
3230 variant
->function
= variant_func
;
3231 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
3233 LLVMSetFunctionCallConv(variant_coro
, LLVMCCallConv
);
3235 for (i
= 0; i
< ARRAY_SIZE(arg_types
); ++i
) {
3236 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
) {
3237 lp_add_function_attr(variant_coro
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
3238 lp_add_function_attr(variant_func
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
3242 context_ptr
= LLVMGetParam(variant_func
, 0);
3243 input_array
= LLVMGetParam(variant_func
, 1);
3244 output_array
= LLVMGetParam(variant_func
, 2);
3245 prim_id
= LLVMGetParam(variant_func
, 3);
3246 patch_vertices_in
= LLVMGetParam(variant_func
, 4);
3248 lp_build_name(context_ptr
, "context");
3249 lp_build_name(input_array
, "input");
3250 lp_build_name(output_array
, "output");
3251 lp_build_name(prim_id
, "prim_id");
3252 lp_build_name(patch_vertices_in
, "patch_vertices_in");
3254 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
3255 builder
= gallivm
->builder
;
3256 LLVMPositionBuilderAtEnd(builder
, block
);
3258 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
3260 memset(&tcs_type
, 0, sizeof tcs_type
);
3261 tcs_type
.floating
= TRUE
; /* floating point values */
3262 tcs_type
.sign
= TRUE
; /* values are signed */
3263 tcs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
3264 tcs_type
.width
= 32; /* 32-bit float */
3265 tcs_type
.length
= vector_length
;
3267 lp_build_context_init(&bldvec
, variant
->gallivm
, lp_int_type(tcs_type
));
3269 LLVMValueRef count
= lp_build_const_int32(gallivm
, variant
->shader
->base
.vertices_out
);
3270 LLVMValueRef step
= lp_build_const_int32(gallivm
, vector_length
);
3272 struct lp_build_loop_state loop_state
[2];
3273 LLVMValueRef num_inner_loop
;
3274 unsigned count_align
= util_align_npot(variant
->shader
->base
.vertices_out
, tcs_type
.length
);
3275 num_inner_loop
= lp_build_const_int32(gallivm
, count_align
/ tcs_type
.length
);
3276 LLVMTypeRef hdl_ptr_type
= LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
3277 LLVMValueRef coro_hdls
= LLVMBuildArrayAlloca(gallivm
->builder
, hdl_ptr_type
, num_inner_loop
, "coro_hdls");
3278 unsigned end_coroutine
= INT_MAX
;
3279 lp_build_loop_begin(&loop_state
[1], gallivm
,
3280 lp_build_const_int32(gallivm
, 0)); /* coroutine reentry loop */
3281 lp_build_loop_begin(&loop_state
[0], gallivm
,
3282 lp_build_const_int32(gallivm
, 0)); /* inner loop */
3284 LLVMValueRef args
[6];
3285 args
[0] = context_ptr
;
3286 args
[1] = input_array
;
3287 args
[2] = output_array
;
3289 args
[4] = patch_vertices_in
;
3290 args
[5] = loop_state
[0].counter
;
3291 LLVMValueRef coro_entry
= LLVMBuildGEP(builder
, coro_hdls
, &loop_state
[0].counter
, 1, "");
3292 LLVMValueRef coro_hdl
= LLVMBuildLoad(builder
, coro_entry
, "coro_hdl");
3294 struct lp_build_if_state ifstate
;
3295 LLVMValueRef cmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, loop_state
[1].counter
,
3296 lp_build_const_int32(gallivm
, 0), "");
3297 /* first time here - call the coroutine function entry point */
3298 lp_build_if(&ifstate
, gallivm
, cmp
);
3299 LLVMValueRef coro_ret
= LLVMBuildCall(builder
, variant_coro
, args
, 6, "");
3300 LLVMBuildStore(builder
, coro_ret
, coro_entry
);
3301 lp_build_else(&ifstate
);
3302 /* subsequent calls for this invocation - check if done. */
3303 LLVMValueRef coro_done
= lp_build_coro_done(gallivm
, coro_hdl
);
3304 struct lp_build_if_state ifstate2
;
3305 lp_build_if(&ifstate2
, gallivm
, coro_done
);
3306 /* if done destroy and force loop exit */
3307 lp_build_coro_destroy(gallivm
, coro_hdl
);
3308 lp_build_loop_force_set_counter(&loop_state
[1], lp_build_const_int32(gallivm
, end_coroutine
- 1));
3309 lp_build_else(&ifstate2
);
3310 /* otherwise resume the coroutine */
3311 lp_build_coro_resume(gallivm
, coro_hdl
);
3312 lp_build_endif(&ifstate2
);
3313 lp_build_endif(&ifstate
);
3314 lp_build_loop_force_reload_counter(&loop_state
[1]);
3316 lp_build_loop_end_cond(&loop_state
[0],
3319 lp_build_loop_end_cond(&loop_state
[1],
3320 lp_build_const_int32(gallivm
, end_coroutine
),
3322 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
3324 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_coro
, "entry");
3325 LLVMPositionBuilderAtEnd(builder
, block
);
3327 context_ptr
= LLVMGetParam(variant_coro
, 0);
3328 input_array
= LLVMGetParam(variant_coro
, 1);
3329 output_array
= LLVMGetParam(variant_coro
, 2);
3330 prim_id
= LLVMGetParam(variant_coro
, 3);
3331 patch_vertices_in
= LLVMGetParam(variant_coro
, 4);
3333 consts_ptr
= draw_tcs_jit_context_constants(variant
->gallivm
, context_ptr
);
3335 draw_tcs_jit_context_num_constants(variant
->gallivm
, context_ptr
);
3337 ssbos_ptr
= draw_tcs_jit_context_ssbos(variant
->gallivm
, context_ptr
);
3339 draw_tcs_jit_context_num_ssbos(variant
->gallivm
, context_ptr
);
3340 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
);
3341 image
= draw_llvm_image_soa_create(draw_tcs_llvm_variant_key_images(&variant
->key
));
3343 LLVMValueRef counter
= LLVMGetParam(variant_coro
, 5);
3344 LLVMValueRef invocvec
= LLVMGetUndef(LLVMVectorType(int32_type
, vector_length
));
3345 for (i
= 0; i
< vector_length
; i
++) {
3346 LLVMValueRef idx
= LLVMBuildAdd(builder
, LLVMBuildMul(builder
, counter
, step
, ""), lp_build_const_int32(gallivm
, i
), "");
3347 invocvec
= LLVMBuildInsertElement(builder
, invocvec
, idx
, idx
, "");
3350 system_values
.invocation_id
= invocvec
;
3351 system_values
.prim_id
= lp_build_broadcast_scalar(&bldvec
, prim_id
);
3352 system_values
.vertices_in
= lp_build_broadcast_scalar(&bldvec
, patch_vertices_in
);
3353 tcs_iface
.input
= input_array
;
3354 tcs_iface
.output
= output_array
;
3355 tcs_iface
.base
.emit_fetch_input
= draw_tcs_llvm_emit_fetch_input
;
3356 tcs_iface
.base
.emit_fetch_output
= draw_tcs_llvm_emit_fetch_output
;
3357 tcs_iface
.base
.emit_store_output
= draw_tcs_llvm_emit_store_output
;
3361 LLVMValueRef coro_id
= lp_build_coro_id(gallivm
);
3362 LLVMValueRef coro_hdl
= lp_build_coro_begin_alloc_mem(gallivm
, coro_id
);
3364 mask_val
= generate_tcs_mask_value(variant
, tcs_type
, count
, LLVMBuildMul(builder
, counter
, step
, ""));
3365 lp_build_mask_begin(&mask
, gallivm
, tcs_type
, mask_val
);
3367 struct lp_build_coro_suspend_info coro_info
;
3369 LLVMBasicBlockRef sus_block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_coro
, "suspend");
3370 LLVMBasicBlockRef clean_block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_coro
, "cleanup");
3372 coro_info
.suspend
= sus_block
;
3373 coro_info
.cleanup
= clean_block
;
3375 struct lp_build_tgsi_params params
;
3376 memset(¶ms
, 0, sizeof(params
));
3378 params
.type
= tcs_type
;
3379 params
.mask
= &mask
;
3380 params
.consts_ptr
= consts_ptr
;
3381 params
.const_sizes_ptr
= num_consts_ptr
;
3382 params
.system_values
= &system_values
;
3383 params
.context_ptr
= context_ptr
;
3384 params
.sampler
= sampler
;
3385 params
.info
= &llvm
->draw
->tcs
.tess_ctrl_shader
->info
;
3386 params
.ssbo_ptr
= ssbos_ptr
;
3387 params
.ssbo_sizes_ptr
= num_ssbos_ptr
;
3388 params
.image
= image
;
3389 params
.coro
= &coro_info
;
3390 params
.tcs_iface
= &tcs_iface
.base
;
3392 lp_build_nir_soa(variant
->gallivm
,
3393 llvm
->draw
->tcs
.tess_ctrl_shader
->state
.ir
.nir
,
3396 lp_build_mask_end(&mask
);
3398 lp_build_coro_suspend_switch(gallivm
, &coro_info
, NULL
, true);
3399 LLVMPositionBuilderAtEnd(builder
, clean_block
);
3401 lp_build_coro_free_mem(gallivm
, coro_id
, coro_hdl
);
3403 LLVMBuildBr(builder
, sus_block
);
3404 LLVMPositionBuilderAtEnd(builder
, sus_block
);
3406 lp_build_coro_end(gallivm
, coro_hdl
);
3407 LLVMBuildRet(builder
, coro_hdl
);
3410 sampler
->destroy(sampler
);
3411 image
->destroy(image
);
3412 gallivm_verify_function(gallivm
, variant_func
);
3413 gallivm_verify_function(gallivm
, variant_coro
);
3416 struct draw_tcs_llvm_variant
*
3417 draw_tcs_llvm_create_variant(struct draw_llvm
*llvm
,
3418 unsigned num_outputs
,
3419 const struct draw_tcs_llvm_variant_key
*key
)
3421 struct draw_tcs_llvm_variant
*variant
;
3422 struct llvm_tess_ctrl_shader
*shader
= llvm_tess_ctrl_shader(llvm
->draw
->tcs
.tess_ctrl_shader
);
3423 char module_name
[64];
3425 variant
= MALLOC(sizeof *variant
+
3426 shader
->variant_key_size
- sizeof variant
->key
);
3430 variant
->llvm
= llvm
;
3431 variant
->shader
= shader
;
3433 snprintf(module_name
, sizeof(module_name
), "draw_llvm_tcs_variant%u",
3434 variant
->shader
->variants_cached
);
3436 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
3438 create_tcs_jit_types(variant
);
3440 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
3442 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
3443 nir_print_shader(llvm
->draw
->tcs
.tess_ctrl_shader
->state
.ir
.nir
, stderr
);
3444 draw_tcs_llvm_dump_variant_key(&variant
->key
);
3447 draw_tcs_llvm_generate(llvm
, variant
);
3449 gallivm_compile_module(variant
->gallivm
);
3451 variant
->jit_func
= (draw_tcs_jit_func
)
3452 gallivm_jit_function(variant
->gallivm
, variant
->function
);
3454 gallivm_free_ir(variant
->gallivm
);
3456 variant
->list_item_global
.base
= variant
;
3457 variant
->list_item_local
.base
= variant
;
3458 /*variant->no = */shader
->variants_created
++;
3459 variant
->list_item_global
.base
= variant
;
3465 draw_tcs_llvm_destroy_variant(struct draw_tcs_llvm_variant
*variant
)
3467 struct draw_llvm
*llvm
= variant
->llvm
;
3469 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
3470 debug_printf("Deleting TCS variant: %u tcs variants,\t%u total variants\n",
3471 variant
->shader
->variants_cached
, llvm
->nr_tcs_variants
);
3474 gallivm_destroy(variant
->gallivm
);
3476 remove_from_list(&variant
->list_item_local
);
3477 variant
->shader
->variants_cached
--;
3478 remove_from_list(&variant
->list_item_global
);
3479 llvm
->nr_tcs_variants
--;
3483 struct draw_tcs_llvm_variant_key
*
3484 draw_tcs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
3487 struct draw_tcs_llvm_variant_key
*key
;
3488 struct draw_sampler_static_state
*draw_sampler
;
3489 struct draw_image_static_state
*draw_image
;
3491 key
= (struct draw_tcs_llvm_variant_key
*)store
;
3493 memset(key
, 0, offsetof(struct draw_tcs_llvm_variant_key
, samplers
[0]));
3495 /* All variants of this shader will have the same value for
3496 * nr_samplers. Not yet trying to compact away holes in the
3499 key
->nr_samplers
= llvm
->draw
->tcs
.tess_ctrl_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
3500 if (llvm
->draw
->tcs
.tess_ctrl_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
3501 key
->nr_sampler_views
=
3502 llvm
->draw
->tcs
.tess_ctrl_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
3505 key
->nr_sampler_views
= key
->nr_samplers
;
3508 key
->nr_images
= llvm
->draw
->tcs
.tess_ctrl_shader
->info
.file_max
[TGSI_FILE_IMAGE
] + 1;
3510 draw_sampler
= key
->samplers
;
3512 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
3514 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
3515 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
3516 llvm
->draw
->samplers
[PIPE_SHADER_TESS_CTRL
][i
]);
3518 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
3519 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
3520 llvm
->draw
->sampler_views
[PIPE_SHADER_TESS_CTRL
][i
]);
3523 draw_image
= draw_tcs_llvm_variant_key_images(key
);
3524 memset(draw_image
, 0,
3525 key
->nr_images
* sizeof *draw_image
);
3526 for (i
= 0; i
< key
->nr_images
; i
++) {
3527 lp_sampler_static_texture_state_image(&draw_image
[i
].image_state
,
3528 llvm
->draw
->images
[PIPE_SHADER_TESS_CTRL
][i
]);
3534 draw_tcs_llvm_dump_variant_key(struct draw_tcs_llvm_variant_key
*key
)
3537 struct draw_sampler_static_state
*sampler
= key
->samplers
;
3538 struct draw_image_static_state
*image
= draw_tcs_llvm_variant_key_images(key
);
3539 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
3540 debug_printf("sampler[%i].src_format = %s\n", i
,
3541 util_format_name(sampler
[i
].texture_state
.format
));
3544 for (i
= 0 ; i
< key
->nr_images
; i
++)
3545 debug_printf("images[%i].format = %s\n", i
, util_format_name(image
[i
].image_state
.format
));
3550 create_tes_jit_types(struct draw_tes_llvm_variant
*var
)
3552 struct gallivm_state
*gallivm
= var
->gallivm
;
3553 LLVMTypeRef texture_type
, sampler_type
, image_type
, context_type
;
3555 texture_type
= create_jit_texture_type(gallivm
, "texture");
3556 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
3557 image_type
= create_jit_image_type(gallivm
, "image");
3559 context_type
= create_tes_jit_context_type(gallivm
,
3561 texture_type
, sampler_type
,
3563 "draw_tes_jit_context");
3564 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
3566 var
->input_array_type
= create_tes_jit_input_type(gallivm
);
3570 get_tes_context_ptr_type(struct draw_tes_llvm_variant
*variant
)
3572 if (!variant
->context_ptr_type
)
3573 create_tes_jit_types(variant
);
3574 return variant
->context_ptr_type
;
3578 generate_tes_mask_value(struct draw_tes_llvm_variant
*variant
,
3579 struct lp_type tes_type
, LLVMValueRef limit
, LLVMValueRef loop_counter
)
3581 struct gallivm_state
*gallivm
= variant
->gallivm
;
3582 LLVMBuilderRef builder
= gallivm
->builder
;
3583 struct lp_type mask_type
= lp_int_type(tes_type
);
3584 LLVMValueRef num_prims
;
3585 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
3588 num_prims
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
), limit
);
3589 for (i
= 0; i
< tes_type
.length
; i
++) {
3590 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3591 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, LLVMBuildAdd(builder
, loop_counter
, idx
, ""), idx
, "");
3593 mask_val
= lp_build_compare(gallivm
, mask_type
,
3594 PIPE_FUNC_GREATER
, num_prims
, mask_val
);
3600 draw_tes_llvm_fetch_vertex_input(const struct lp_build_tes_iface
*tes_iface
,
3601 struct lp_build_context
*bld
,
3602 boolean is_vindex_indirect
,
3603 LLVMValueRef vertex_index
,
3604 boolean is_aindex_indirect
,
3605 LLVMValueRef attrib_index
,
3606 LLVMValueRef swizzle_index
)
3608 const struct draw_tes_llvm_iface
*tes
= draw_tes_llvm_iface(tes_iface
);
3609 struct gallivm_state
*gallivm
= bld
->gallivm
;
3610 LLVMBuilderRef builder
= gallivm
->builder
;
3611 LLVMValueRef indices
[3];
3613 struct lp_type type
= bld
->type
;
3615 if (is_vindex_indirect
|| is_aindex_indirect
) {
3620 for (i
= 0; i
< type
.length
; ++i
) {
3621 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3622 LLVMValueRef vert_chan_index
= vertex_index
;
3623 LLVMValueRef attr_chan_index
= attrib_index
;
3624 LLVMValueRef channel_vec
;
3626 if (is_vindex_indirect
) {
3627 vert_chan_index
= LLVMBuildExtractElement(builder
,
3628 vertex_index
, idx
, "");
3630 if (is_aindex_indirect
) {
3631 attr_chan_index
= LLVMBuildExtractElement(builder
,
3632 attrib_index
, idx
, "");
3635 indices
[0] = vert_chan_index
;
3636 indices
[1] = attr_chan_index
;
3637 indices
[2] = swizzle_index
;
3639 channel_vec
= LLVMBuildGEP(builder
, tes
->input
, indices
, 3, "");
3640 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
3642 res
= LLVMBuildInsertElement(builder
, res
, channel_vec
, idx
, "");
3645 indices
[0] = vertex_index
;
3646 indices
[1] = attrib_index
;
3647 indices
[2] = swizzle_index
;
3649 res
= LLVMBuildGEP(builder
, tes
->input
, indices
, 3, "");
3650 res
= LLVMBuildLoad(builder
, res
, "");
3651 res
= lp_build_broadcast_scalar(bld
, res
);
3657 draw_tes_llvm_fetch_patch_input(const struct lp_build_tes_iface
*tes_iface
,
3658 struct lp_build_context
*bld
,
3659 boolean is_aindex_indirect
,
3660 LLVMValueRef attrib_index
,
3661 LLVMValueRef swizzle_index
)
3663 const struct draw_tes_llvm_iface
*tes
= draw_tes_llvm_iface(tes_iface
);
3664 struct gallivm_state
*gallivm
= bld
->gallivm
;
3665 LLVMBuilderRef builder
= gallivm
->builder
;
3666 LLVMValueRef indices
[3];
3668 struct lp_type type
= bld
->type
;
3670 if (is_aindex_indirect
) {
3675 for (i
= 0; i
< type
.length
; ++i
) {
3676 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
3677 LLVMValueRef attr_chan_index
= attrib_index
;
3678 LLVMValueRef channel_vec
;
3680 if (is_aindex_indirect
) {
3681 attr_chan_index
= LLVMBuildExtractElement(builder
,
3682 attrib_index
, idx
, "");
3685 indices
[0] = lp_build_const_int32(gallivm
, 0);
3686 indices
[1] = attr_chan_index
;
3687 indices
[2] = swizzle_index
;
3689 channel_vec
= LLVMBuildGEP(builder
, tes
->input
, indices
, 3, "");
3690 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
3692 res
= LLVMBuildInsertElement(builder
, res
, channel_vec
, idx
, "");
3695 indices
[0] = lp_build_const_int32(gallivm
, 0);
3696 indices
[1] = attrib_index
;
3697 indices
[2] = swizzle_index
;
3699 res
= LLVMBuildGEP(builder
, tes
->input
, indices
, 3, "");
3700 res
= LLVMBuildLoad(builder
, res
, "");
3701 res
= lp_build_broadcast_scalar(bld
, res
);
3707 draw_tes_llvm_generate(struct draw_llvm
*llvm
,
3708 struct draw_tes_llvm_variant
*variant
)
3710 struct gallivm_state
*gallivm
= variant
->gallivm
;
3711 LLVMContextRef context
= gallivm
->context
;
3712 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
3713 LLVMTypeRef flt_type
= LLVMFloatTypeInContext(context
);
3714 LLVMTypeRef arg_types
[9];
3715 LLVMTypeRef func_type
;
3716 LLVMValueRef variant_func
;
3717 LLVMValueRef context_ptr
;
3718 LLVMValueRef tess_coord
[2], io_ptr
, input_array
, num_tess_coord
;
3719 LLVMValueRef tess_inner
, tess_outer
, prim_id
;
3720 LLVMBasicBlockRef block
;
3721 LLVMBuilderRef builder
;
3722 LLVMValueRef mask_val
;
3723 struct lp_build_context bld
, bldvec
;
3724 struct lp_build_sampler_soa
*sampler
= 0;
3725 struct lp_build_image_soa
*image
= NULL
;
3726 struct lp_bld_tgsi_system_values system_values
;
3729 struct draw_tes_llvm_iface tes_iface
;
3730 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
3731 struct lp_build_mask_context mask
;
3732 LLVMValueRef consts_ptr
, num_consts_ptr
;
3733 LLVMValueRef ssbos_ptr
, num_ssbos_ptr
;
3735 struct lp_type tes_type
;
3736 unsigned vector_length
= variant
->shader
->base
.vector_length
;
3738 memset(&system_values
, 0, sizeof(system_values
));
3739 memset(&outputs
, 0, sizeof(outputs
));
3741 snprintf(func_name
, sizeof(func_name
), "draw_llvm_tes_variant%u",
3742 variant
->shader
->variants_cached
);
3744 arg_types
[0] = get_tes_context_ptr_type(variant
); /* context */
3745 arg_types
[1] = variant
->input_array_type
; /* input */
3746 arg_types
[2] = variant
->vertex_header_ptr_type
;
3747 arg_types
[3] = int32_type
;
3748 arg_types
[4] = int32_type
;
3749 arg_types
[5] = LLVMPointerType(flt_type
, 0);
3750 arg_types
[6] = LLVMPointerType(flt_type
, 0);
3751 arg_types
[7] = LLVMPointerType(LLVMArrayType(flt_type
, 4), 0);
3752 arg_types
[8] = LLVMPointerType(LLVMArrayType(flt_type
, 2), 0);
3754 func_type
= LLVMFunctionType(int32_type
, arg_types
, ARRAY_SIZE(arg_types
), 0);
3755 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
3757 variant
->function
= variant_func
;
3758 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
3760 for (i
= 0; i
< ARRAY_SIZE(arg_types
); ++i
)
3761 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
3762 lp_add_function_attr(variant_func
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
3764 context_ptr
= LLVMGetParam(variant_func
, 0);
3765 input_array
= LLVMGetParam(variant_func
, 1);
3766 io_ptr
= LLVMGetParam(variant_func
, 2);
3767 prim_id
= LLVMGetParam(variant_func
, 3);
3768 num_tess_coord
= LLVMGetParam(variant_func
, 4);
3769 tess_coord
[0] = LLVMGetParam(variant_func
, 5);
3770 tess_coord
[1] = LLVMGetParam(variant_func
, 6);
3771 tess_outer
= LLVMGetParam(variant_func
, 7);
3772 tess_inner
= LLVMGetParam(variant_func
, 8);
3774 lp_build_name(context_ptr
, "context");
3775 lp_build_name(input_array
, "input");
3776 lp_build_name(io_ptr
, "io");
3777 lp_build_name(prim_id
, "prim_id");
3778 lp_build_name(num_tess_coord
, "num_tess_coord");
3779 lp_build_name(tess_coord
[0], "tess_coord[0]");
3780 lp_build_name(tess_coord
[1], "tess_coord[1]");
3781 lp_build_name(tess_outer
, "tess_outer");
3782 lp_build_name(tess_inner
, "tess_inner");
3784 tes_iface
.base
.fetch_vertex_input
= draw_tes_llvm_fetch_vertex_input
;
3785 tes_iface
.base
.fetch_patch_input
= draw_tes_llvm_fetch_patch_input
;
3786 tes_iface
.input
= input_array
;
3787 tes_iface
.variant
= variant
;
3789 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
3790 builder
= gallivm
->builder
;
3791 LLVMPositionBuilderAtEnd(builder
, block
);
3793 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
3795 memset(&tes_type
, 0, sizeof tes_type
);
3796 tes_type
.floating
= TRUE
; /* floating point values */
3797 tes_type
.sign
= TRUE
; /* values are signed */
3798 tes_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
3799 tes_type
.width
= 32; /* 32-bit float */
3800 tes_type
.length
= vector_length
;
3802 lp_build_context_init(&bldvec
, variant
->gallivm
, lp_int_type(tes_type
));
3803 consts_ptr
= draw_tes_jit_context_constants(variant
->gallivm
, context_ptr
);
3805 draw_tes_jit_context_num_constants(variant
->gallivm
, context_ptr
);
3807 ssbos_ptr
= draw_tes_jit_context_ssbos(variant
->gallivm
, context_ptr
);
3809 draw_tes_jit_context_num_ssbos(variant
->gallivm
, context_ptr
);
3810 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
);
3811 image
= draw_llvm_image_soa_create(draw_tes_llvm_variant_key_images(&variant
->key
));
3812 step
= lp_build_const_int32(gallivm
, vector_length
);
3814 system_values
.tess_outer
= LLVMBuildLoad(builder
, tess_outer
, "");
3815 system_values
.tess_inner
= LLVMBuildLoad(builder
, tess_inner
, "");
3817 system_values
.prim_id
= lp_build_broadcast_scalar(&bldvec
, prim_id
);
3818 struct lp_build_loop_state lp_loop
;
3819 lp_build_loop_begin(&lp_loop
, gallivm
, bld
.zero
);
3823 io
= LLVMBuildGEP(builder
, io_ptr
, &lp_loop
.counter
, 1, "");
3824 mask_val
= generate_tes_mask_value(variant
, tes_type
, num_tess_coord
, lp_loop
.counter
);
3825 lp_build_mask_begin(&mask
, gallivm
, tes_type
, mask_val
);
3827 system_values
.tess_coord
= LLVMGetUndef(LLVMArrayType(LLVMVectorType(flt_type
, vector_length
), 3));
3828 for (i
= 0; i
< 3; i
++) {
3829 LLVMValueRef tess_coord_chan
= LLVMGetUndef(LLVMVectorType(flt_type
, vector_length
));
3830 for (unsigned j
= 0; j
< vector_length
; j
++) {
3831 LLVMValueRef idx
= LLVMBuildAdd(builder
, lp_loop
.counter
, lp_build_const_int32(gallivm
, j
), "");
3832 LLVMValueRef tc_val
;
3834 if (variant
->shader
->base
.prim_mode
== PIPE_PRIM_TRIANGLES
) {
3835 tc_val
= lp_build_const_float(gallivm
, 1.0);
3836 tc_val
= LLVMBuildFSub(builder
, tc_val
, lp_build_pointer_get(builder
, tess_coord
[0], idx
), "");
3837 tc_val
= LLVMBuildFSub(builder
, tc_val
, lp_build_pointer_get(builder
, tess_coord
[1], idx
), "");
3839 tc_val
= lp_build_const_float(gallivm
, 0.0);
3841 tc_val
= lp_build_pointer_get(builder
, tess_coord
[i
], idx
);
3843 tess_coord_chan
= LLVMBuildInsertElement(builder
, tess_coord_chan
, tc_val
, lp_build_const_int32(gallivm
, j
), "");
3845 system_values
.tess_coord
= LLVMBuildInsertValue(builder
, system_values
.tess_coord
, tess_coord_chan
, i
, "");
3848 struct lp_build_tgsi_params params
;
3849 memset(¶ms
, 0, sizeof(params
));
3851 params
.type
= tes_type
;
3852 params
.mask
= &mask
;
3853 params
.consts_ptr
= consts_ptr
;
3854 params
.const_sizes_ptr
= num_consts_ptr
;
3855 params
.system_values
= &system_values
;
3856 params
.context_ptr
= context_ptr
;
3857 params
.sampler
= sampler
;
3858 params
.info
= &llvm
->draw
->tes
.tess_eval_shader
->info
;
3859 params
.ssbo_ptr
= ssbos_ptr
;
3860 params
.ssbo_sizes_ptr
= num_ssbos_ptr
;
3861 params
.image
= image
;
3862 params
.tes_iface
= &tes_iface
.base
;
3864 lp_build_nir_soa(variant
->gallivm
,
3865 llvm
->draw
->tes
.tess_eval_shader
->state
.ir
.nir
,
3869 lp_build_mask_end(&mask
);
3870 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
3871 lp_int_type(tes_type
), 0);
3873 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
3874 params
.info
->num_outputs
, tes_type
, FALSE
);
3876 lp_build_loop_end_cond(&lp_loop
, num_tess_coord
, step
, LLVMIntUGE
);
3877 sampler
->destroy(sampler
);
3878 image
->destroy(image
);
3880 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
3881 gallivm_verify_function(gallivm
, variant_func
);
3884 struct draw_tes_llvm_variant
*
3885 draw_tes_llvm_create_variant(struct draw_llvm
*llvm
,
3886 unsigned num_outputs
,
3887 const struct draw_tes_llvm_variant_key
*key
)
3889 struct draw_tes_llvm_variant
*variant
;
3890 struct llvm_tess_eval_shader
*shader
= llvm_tess_eval_shader(llvm
->draw
->tes
.tess_eval_shader
);
3891 LLVMTypeRef vertex_header
;
3892 char module_name
[64];
3894 variant
= MALLOC(sizeof *variant
+
3895 shader
->variant_key_size
- sizeof variant
->key
);
3899 variant
->llvm
= llvm
;
3900 variant
->shader
= shader
;
3902 snprintf(module_name
, sizeof(module_name
), "draw_llvm_tes_variant%u",
3903 variant
->shader
->variants_cached
);
3905 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
3907 create_tes_jit_types(variant
);
3909 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
3911 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
3913 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
3915 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
3916 nir_print_shader(llvm
->draw
->tes
.tess_eval_shader
->state
.ir
.nir
, stderr
);
3917 draw_tes_llvm_dump_variant_key(&variant
->key
);
3920 draw_tes_llvm_generate(llvm
, variant
);
3922 gallivm_compile_module(variant
->gallivm
);
3924 variant
->jit_func
= (draw_tes_jit_func
)
3925 gallivm_jit_function(variant
->gallivm
, variant
->function
);
3927 gallivm_free_ir(variant
->gallivm
);
3929 variant
->list_item_global
.base
= variant
;
3930 variant
->list_item_local
.base
= variant
;
3931 /*variant->no = */shader
->variants_created
++;
3932 variant
->list_item_global
.base
= variant
;
3938 draw_tes_llvm_destroy_variant(struct draw_tes_llvm_variant
*variant
)
3940 struct draw_llvm
*llvm
= variant
->llvm
;
3942 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
3943 debug_printf("Deleting TES variant: %u tes variants,\t%u total variants\n",
3944 variant
->shader
->variants_cached
, llvm
->nr_tes_variants
);
3947 gallivm_destroy(variant
->gallivm
);
3949 remove_from_list(&variant
->list_item_local
);
3950 variant
->shader
->variants_cached
--;
3951 remove_from_list(&variant
->list_item_global
);
3952 llvm
->nr_tes_variants
--;
3956 struct draw_tes_llvm_variant_key
*
3957 draw_tes_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
3960 struct draw_tes_llvm_variant_key
*key
;
3961 struct draw_sampler_static_state
*draw_sampler
;
3962 struct draw_image_static_state
*draw_image
;
3964 key
= (struct draw_tes_llvm_variant_key
*)store
;
3966 memset(key
, 0, offsetof(struct draw_tes_llvm_variant_key
, samplers
[0]));
3968 /* All variants of this shader will have the same value for
3969 * nr_samplers. Not yet trying to compact away holes in the
3972 key
->nr_samplers
= llvm
->draw
->tes
.tess_eval_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
3973 if (llvm
->draw
->tes
.tess_eval_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
3974 key
->nr_sampler_views
=
3975 llvm
->draw
->tes
.tess_eval_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
3978 key
->nr_sampler_views
= key
->nr_samplers
;
3981 key
->nr_images
= llvm
->draw
->tes
.tess_eval_shader
->info
.file_max
[TGSI_FILE_IMAGE
] + 1;
3983 draw_sampler
= key
->samplers
;
3985 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
3987 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
3988 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
3989 llvm
->draw
->samplers
[PIPE_SHADER_TESS_EVAL
][i
]);
3991 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
3992 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
3993 llvm
->draw
->sampler_views
[PIPE_SHADER_TESS_EVAL
][i
]);
3996 draw_image
= draw_tes_llvm_variant_key_images(key
);
3997 memset(draw_image
, 0,
3998 key
->nr_images
* sizeof *draw_image
);
3999 for (i
= 0; i
< key
->nr_images
; i
++) {
4000 lp_sampler_static_texture_state_image(&draw_image
[i
].image_state
,
4001 llvm
->draw
->images
[PIPE_SHADER_TESS_EVAL
][i
]);
4007 draw_tes_llvm_dump_variant_key(struct draw_tes_llvm_variant_key
*key
)
4010 struct draw_sampler_static_state
*sampler
= key
->samplers
;
4011 struct draw_image_static_state
*image
= draw_tes_llvm_variant_key_images(key
);
4012 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
4013 debug_printf("sampler[%i].src_format = %s\n", i
,
4014 util_format_name(sampler
[i
].texture_state
.format
));
4017 for (i
= 0 ; i
< key
->nr_images
; i
++)
4018 debug_printf("images[%i].format = %s\n", i
, util_format_name(image
[i
].image_state
.format
));