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_swizzle.h"
41 #include "gallivm/lp_bld_struct.h"
42 #include "gallivm/lp_bld_type.h"
43 #include "gallivm/lp_bld_flow.h"
44 #include "gallivm/lp_bld_debug.h"
45 #include "gallivm/lp_bld_tgsi.h"
46 #include "gallivm/lp_bld_nir.h"
47 #include "gallivm/lp_bld_printf.h"
48 #include "gallivm/lp_bld_intr.h"
49 #include "gallivm/lp_bld_init.h"
50 #include "gallivm/lp_bld_type.h"
51 #include "gallivm/lp_bld_pack.h"
52 #include "gallivm/lp_bld_format.h"
54 #include "tgsi/tgsi_exec.h"
55 #include "tgsi/tgsi_dump.h"
57 #include "util/u_math.h"
58 #include "util/u_pointer.h"
59 #include "util/u_string.h"
60 #include "util/simple_list.h"
67 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
);
70 struct draw_gs_llvm_iface
{
71 struct lp_build_gs_iface base
;
73 struct draw_gs_llvm_variant
*variant
;
77 static inline const struct draw_gs_llvm_iface
*
78 draw_gs_llvm_iface(const struct lp_build_gs_iface
*iface
)
80 return (const struct draw_gs_llvm_iface
*)iface
;
84 * Create LLVM type for draw_vertex_buffer.
87 create_jit_dvbuffer_type(struct gallivm_state
*gallivm
,
88 const char *struct_name
)
90 LLVMTargetDataRef target
= gallivm
->target
;
91 LLVMTypeRef dvbuffer_type
;
92 LLVMTypeRef elem_types
[DRAW_JIT_DVBUFFER_NUM_FIELDS
];
93 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
95 elem_types
[DRAW_JIT_DVBUFFER_MAP
] =
96 LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
97 elem_types
[DRAW_JIT_DVBUFFER_SIZE
] = int32_type
;
99 dvbuffer_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
100 ARRAY_SIZE(elem_types
), 0);
102 (void) target
; /* silence unused var warning for non-debug build */
103 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, map
,
104 target
, dvbuffer_type
,
105 DRAW_JIT_DVBUFFER_MAP
);
106 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer
, size
,
107 target
, dvbuffer_type
,
108 DRAW_JIT_DVBUFFER_SIZE
);
110 return dvbuffer_type
;
114 * Create LLVM type for struct draw_jit_texture
117 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
119 LLVMTargetDataRef target
= gallivm
->target
;
120 LLVMTypeRef texture_type
;
121 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
122 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
124 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
125 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
126 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
127 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
128 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
129 elem_types
[DRAW_JIT_TEXTURE_BASE
] =
130 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
131 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
132 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
133 elem_types
[DRAW_JIT_TEXTURE_MIP_OFFSETS
] =
134 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
136 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
137 ARRAY_SIZE(elem_types
), 0);
139 (void) target
; /* silence unused var warning for non-debug build */
140 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
141 target
, texture_type
,
142 DRAW_JIT_TEXTURE_WIDTH
);
143 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
144 target
, texture_type
,
145 DRAW_JIT_TEXTURE_HEIGHT
);
146 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
147 target
, texture_type
,
148 DRAW_JIT_TEXTURE_DEPTH
);
149 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, base
,
150 target
, texture_type
,
151 DRAW_JIT_TEXTURE_BASE
);
152 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
153 target
, texture_type
,
154 DRAW_JIT_TEXTURE_ROW_STRIDE
);
155 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
156 target
, texture_type
,
157 DRAW_JIT_TEXTURE_IMG_STRIDE
);
158 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
159 target
, texture_type
,
160 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
161 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
162 target
, texture_type
,
163 DRAW_JIT_TEXTURE_LAST_LEVEL
);
164 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, mip_offsets
,
165 target
, texture_type
,
166 DRAW_JIT_TEXTURE_MIP_OFFSETS
);
168 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
175 * Create LLVM type for struct draw_jit_sampler
178 create_jit_sampler_type(struct gallivm_state
*gallivm
, const char *struct_name
)
180 LLVMTargetDataRef target
= gallivm
->target
;
181 LLVMTypeRef sampler_type
;
182 LLVMTypeRef elem_types
[DRAW_JIT_SAMPLER_NUM_FIELDS
];
184 elem_types
[DRAW_JIT_SAMPLER_MIN_LOD
] =
185 elem_types
[DRAW_JIT_SAMPLER_MAX_LOD
] =
186 elem_types
[DRAW_JIT_SAMPLER_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
187 elem_types
[DRAW_JIT_SAMPLER_BORDER_COLOR
] =
188 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
190 sampler_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
191 ARRAY_SIZE(elem_types
), 0);
193 (void) target
; /* silence unused var warning for non-debug build */
194 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, min_lod
,
195 target
, sampler_type
,
196 DRAW_JIT_SAMPLER_MIN_LOD
);
197 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, max_lod
,
198 target
, sampler_type
,
199 DRAW_JIT_SAMPLER_MAX_LOD
);
200 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, lod_bias
,
201 target
, sampler_type
,
202 DRAW_JIT_SAMPLER_LOD_BIAS
);
203 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, border_color
,
204 target
, sampler_type
,
205 DRAW_JIT_SAMPLER_BORDER_COLOR
);
207 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler
, target
, sampler_type
);
213 * Create LLVM type for struct draw_jit_texture
216 create_jit_image_type(struct gallivm_state
*gallivm
, const char *struct_name
)
218 LLVMTargetDataRef target
= gallivm
->target
;
219 LLVMTypeRef image_type
;
220 LLVMTypeRef elem_types
[DRAW_JIT_IMAGE_NUM_FIELDS
];
221 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
223 elem_types
[DRAW_JIT_IMAGE_WIDTH
] =
224 elem_types
[DRAW_JIT_IMAGE_HEIGHT
] =
225 elem_types
[DRAW_JIT_IMAGE_DEPTH
] =
226 elem_types
[DRAW_JIT_IMAGE_ROW_STRIDE
] =
227 elem_types
[DRAW_JIT_IMAGE_IMG_STRIDE
] = int32_type
;
228 elem_types
[DRAW_JIT_IMAGE_BASE
] =
229 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
231 image_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
232 ARRAY_SIZE(elem_types
), 0);
234 (void) target
; /* silence unused var warning for non-debug build */
235 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, width
,
237 DRAW_JIT_IMAGE_WIDTH
);
238 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, height
,
240 DRAW_JIT_IMAGE_HEIGHT
);
241 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, depth
,
243 DRAW_JIT_IMAGE_DEPTH
);
244 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, base
,
246 DRAW_JIT_IMAGE_BASE
);
247 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, row_stride
,
249 DRAW_JIT_IMAGE_ROW_STRIDE
);
250 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image
, img_stride
,
252 DRAW_JIT_IMAGE_IMG_STRIDE
);
254 LP_CHECK_STRUCT_SIZE(struct draw_jit_image
, target
, image_type
);
260 * Create LLVM type for struct draw_jit_context
263 create_jit_context_type(struct gallivm_state
*gallivm
,
264 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
265 LLVMTypeRef image_type
,
266 const char *struct_name
)
268 LLVMTargetDataRef target
= gallivm
->target
;
269 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
270 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
271 LLVMTypeRef elem_types
[DRAW_JIT_CTX_NUM_FIELDS
];
272 LLVMTypeRef context_type
;
274 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
275 LP_MAX_TGSI_CONST_BUFFERS
);
276 elem_types
[1] = LLVMArrayType(int_type
, /* num_vs_constants */
277 LP_MAX_TGSI_CONST_BUFFERS
);
278 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
279 DRAW_TOTAL_CLIP_PLANES
), 0);
280 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewports */
281 elem_types
[4] = LLVMArrayType(texture_type
,
282 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
283 elem_types
[5] = LLVMArrayType(sampler_type
,
284 PIPE_MAX_SAMPLERS
); /* samplers */
285 elem_types
[6] = LLVMArrayType(image_type
,
286 PIPE_MAX_SHADER_IMAGES
); /* images */
287 elem_types
[7] = LLVMArrayType(LLVMPointerType(int_type
, 0), /* vs_ssbo */
288 LP_MAX_TGSI_SHADER_BUFFERS
);
289 elem_types
[8] = LLVMArrayType(int_type
, /* num_vs_ssbos */
290 LP_MAX_TGSI_SHADER_BUFFERS
);
291 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
292 ARRAY_SIZE(elem_types
), 0);
294 (void) target
; /* silence unused var warning for non-debug build */
295 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
296 target
, context_type
, DRAW_JIT_CTX_CONSTANTS
);
297 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, num_vs_constants
,
298 target
, context_type
, DRAW_JIT_CTX_NUM_CONSTANTS
);
299 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
300 target
, context_type
, DRAW_JIT_CTX_PLANES
);
301 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewports
,
302 target
, context_type
, DRAW_JIT_CTX_VIEWPORT
);
303 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
304 target
, context_type
,
305 DRAW_JIT_CTX_TEXTURES
);
306 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
307 target
, context_type
,
308 DRAW_JIT_CTX_SAMPLERS
);
309 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, images
,
310 target
, context_type
, DRAW_JIT_CTX_IMAGES
);
311 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_ssbos
,
312 target
, context_type
, DRAW_JIT_CTX_SSBOS
);
313 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, num_vs_ssbos
,
314 target
, context_type
, DRAW_JIT_CTX_NUM_SSBOS
);
315 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
316 target
, context_type
);
323 * Create LLVM type for struct draw_gs_jit_context
326 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
327 unsigned vector_length
,
328 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
329 LLVMTypeRef image_type
,
330 const char *struct_name
)
332 LLVMTargetDataRef target
= gallivm
->target
;
333 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
334 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
335 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
336 LLVMTypeRef context_type
;
338 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
339 LP_MAX_TGSI_CONST_BUFFERS
);
340 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
341 LP_MAX_TGSI_CONST_BUFFERS
);
342 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
343 DRAW_TOTAL_CLIP_PLANES
), 0);
344 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewports */
346 elem_types
[4] = LLVMArrayType(texture_type
,
347 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
348 elem_types
[5] = LLVMArrayType(sampler_type
,
349 PIPE_MAX_SAMPLERS
); /* samplers */
350 elem_types
[6] = LLVMArrayType(image_type
,
351 PIPE_MAX_SHADER_IMAGES
); /* images */
352 elem_types
[7] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
353 elem_types
[8] = LLVMPointerType(LLVMVectorType(int_type
,
355 elem_types
[9] = LLVMPointerType(LLVMVectorType(int_type
,
358 elem_types
[10] = LLVMArrayType(LLVMPointerType(int_type
, 0), /* ssbos */
359 LP_MAX_TGSI_SHADER_BUFFERS
);
360 elem_types
[11] = LLVMArrayType(int_type
, /* num_ssbos */
361 LP_MAX_TGSI_SHADER_BUFFERS
);
363 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
364 ARRAY_SIZE(elem_types
), 0);
366 (void) target
; /* silence unused var warning for non-debug build */
367 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
368 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
369 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, num_constants
,
370 target
, context_type
, DRAW_GS_JIT_CTX_NUM_CONSTANTS
);
371 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
372 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
373 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewports
,
374 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
375 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
376 target
, context_type
,
377 DRAW_GS_JIT_CTX_TEXTURES
);
378 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
379 target
, context_type
,
380 DRAW_GS_JIT_CTX_SAMPLERS
);
381 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
382 target
, context_type
,
383 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
384 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
385 target
, context_type
,
386 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
387 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
388 target
, context_type
,
389 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
390 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, ssbos
,
391 target
, context_type
, DRAW_GS_JIT_CTX_SSBOS
);
392 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, num_ssbos
,
393 target
, context_type
, DRAW_GS_JIT_CTX_NUM_SSBOS
);
394 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, images
,
395 target
, context_type
, DRAW_GS_JIT_CTX_IMAGES
);
396 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
397 target
, context_type
);
404 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
406 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
407 LLVMTypeRef input_array
;
409 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
410 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
411 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
412 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
418 * Create LLVM type for struct pipe_vertex_buffer
421 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
,
422 const char *struct_name
)
424 LLVMTargetDataRef target
= gallivm
->target
;
425 LLVMTypeRef elem_types
[4];
428 elem_types
[0] = LLVMInt16TypeInContext(gallivm
->context
);
429 elem_types
[1] = LLVMInt8TypeInContext(gallivm
->context
);
430 elem_types
[2] = LLVMInt32TypeInContext(gallivm
->context
);
431 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
433 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
434 ARRAY_SIZE(elem_types
), 0);
436 (void) target
; /* silence unused var warning for non-debug build */
437 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
439 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, is_user_buffer
,
441 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
443 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer
.resource
,
446 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
453 * Create LLVM type for struct vertex_header;
456 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
458 LLVMTargetDataRef target
= gallivm
->target
;
459 LLVMTypeRef elem_types
[3];
460 LLVMTypeRef vertex_header
;
461 char struct_name
[24];
463 snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
465 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
466 elem_types
[DRAW_JIT_VERTEX_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
467 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
469 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
470 ARRAY_SIZE(elem_types
), 0);
472 /* these are bit-fields and we can't take address of them
473 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
474 target, vertex_header,
475 DRAW_JIT_VERTEX_CLIPMASK);
476 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
477 target, vertex_header,
478 DRAW_JIT_VERTEX_EDGEFLAG);
479 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
480 target, vertex_header,
481 DRAW_JIT_VERTEX_PAD);
482 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
483 target, vertex_header,
484 DRAW_JIT_VERTEX_VERTEX_ID);
486 (void) target
; /* silence unused var warning for non-debug build */
487 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip_pos
,
488 target
, vertex_header
,
489 DRAW_JIT_VERTEX_CLIP_POS
);
490 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
491 target
, vertex_header
,
492 DRAW_JIT_VERTEX_DATA
);
494 assert(LLVMABISizeOfType(target
, vertex_header
) ==
495 offsetof(struct vertex_header
, data
[data_elems
]));
497 return vertex_header
;
501 * Create LLVM type for struct draw_tcs_jit_context
504 create_tcs_jit_context_type(struct gallivm_state
*gallivm
,
505 unsigned vector_length
,
506 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
507 LLVMTypeRef image_type
,
508 const char *struct_name
)
510 LLVMTargetDataRef target
= gallivm
->target
;
511 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
512 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
513 LLVMTypeRef elem_types
[DRAW_TCS_JIT_CTX_NUM_FIELDS
];
514 LLVMTypeRef context_type
;
516 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
517 LP_MAX_TGSI_CONST_BUFFERS
);
518 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
519 LP_MAX_TGSI_CONST_BUFFERS
);
520 elem_types
[2] = LLVMInt32TypeInContext(gallivm
->context
);
521 elem_types
[3] = LLVMInt32TypeInContext(gallivm
->context
);
523 elem_types
[4] = LLVMArrayType(texture_type
,
524 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
525 elem_types
[5] = LLVMArrayType(sampler_type
,
526 PIPE_MAX_SAMPLERS
); /* samplers */
527 elem_types
[6] = LLVMArrayType(image_type
,
528 PIPE_MAX_SHADER_IMAGES
); /* images */
530 elem_types
[7] = LLVMArrayType(LLVMPointerType(int_type
, 0), /* ssbos */
531 LP_MAX_TGSI_SHADER_BUFFERS
);
532 elem_types
[8] = LLVMArrayType(int_type
, /* num_ssbos */
533 LP_MAX_TGSI_SHADER_BUFFERS
);
535 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
536 ARRAY_SIZE(elem_types
), 0);
538 (void) target
; /* silence unused var warning for non-debug build */
539 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, constants
,
540 target
, context_type
, DRAW_TCS_JIT_CTX_CONSTANTS
);
541 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, num_constants
,
542 target
, context_type
, DRAW_TCS_JIT_CTX_NUM_CONSTANTS
);
543 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, textures
,
544 target
, context_type
,
545 DRAW_TCS_JIT_CTX_TEXTURES
);
546 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, samplers
,
547 target
, context_type
,
548 DRAW_TCS_JIT_CTX_SAMPLERS
);
549 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, ssbos
,
550 target
, context_type
, DRAW_TCS_JIT_CTX_SSBOS
);
551 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, num_ssbos
,
552 target
, context_type
, DRAW_TCS_JIT_CTX_NUM_SSBOS
);
553 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context
, images
,
554 target
, context_type
, DRAW_TCS_JIT_CTX_IMAGES
);
555 LP_CHECK_STRUCT_SIZE(struct draw_tcs_jit_context
,
556 target
, context_type
);
562 create_tcs_jit_input_type(struct gallivm_state
*gallivm
)
564 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
565 LLVMTypeRef input_array
;
567 input_array
= LLVMArrayType(float_type
, TGSI_NUM_CHANNELS
); /* num channels */
568 input_array
= LLVMArrayType(input_array
, NUM_TCS_INPUTS
); /* num attrs per vertex */
569 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
575 create_tcs_jit_output_type(struct gallivm_state
*gallivm
)
577 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
578 LLVMTypeRef output_array
;
580 output_array
= LLVMArrayType(float_type
, TGSI_NUM_CHANNELS
); /* num channels */
581 output_array
= LLVMArrayType(output_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
582 output_array
= LLVMPointerType(output_array
, 0); /* num vertices per prim */
588 create_tes_jit_input_type(struct gallivm_state
*gallivm
)
590 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
591 LLVMTypeRef input_array
;
593 input_array
= LLVMArrayType(float_type
, TGSI_NUM_CHANNELS
); /* num channels */
594 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
595 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
601 * Create LLVM type for struct draw_tes_jit_context
604 create_tes_jit_context_type(struct gallivm_state
*gallivm
,
605 unsigned vector_length
,
606 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
607 LLVMTypeRef image_type
,
608 const char *struct_name
)
610 LLVMTargetDataRef target
= gallivm
->target
;
611 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
612 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
613 LLVMTypeRef elem_types
[DRAW_TCS_JIT_CTX_NUM_FIELDS
];
614 LLVMTypeRef context_type
;
616 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
617 LP_MAX_TGSI_CONST_BUFFERS
);
618 elem_types
[1] = LLVMArrayType(int_type
, /* num_constants */
619 LP_MAX_TGSI_CONST_BUFFERS
);
620 elem_types
[2] = LLVMInt32TypeInContext(gallivm
->context
);
621 elem_types
[3] = LLVMInt32TypeInContext(gallivm
->context
);
623 elem_types
[4] = LLVMArrayType(texture_type
,
624 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
625 elem_types
[5] = LLVMArrayType(sampler_type
,
626 PIPE_MAX_SAMPLERS
); /* samplers */
627 elem_types
[6] = LLVMArrayType(image_type
,
628 PIPE_MAX_SHADER_IMAGES
); /* images */
630 elem_types
[7] = LLVMArrayType(LLVMPointerType(int_type
, 0), /* ssbos */
631 LP_MAX_TGSI_SHADER_BUFFERS
);
632 elem_types
[8] = LLVMArrayType(int_type
, /* num_ssbos */
633 LP_MAX_TGSI_SHADER_BUFFERS
);
635 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
636 ARRAY_SIZE(elem_types
), 0);
638 (void) target
; /* silence unused var warning for non-debug build */
639 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, constants
,
640 target
, context_type
, DRAW_TCS_JIT_CTX_CONSTANTS
);
641 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, num_constants
,
642 target
, context_type
, DRAW_TCS_JIT_CTX_NUM_CONSTANTS
);
643 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, textures
,
644 target
, context_type
,
645 DRAW_TCS_JIT_CTX_TEXTURES
);
646 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, samplers
,
647 target
, context_type
,
648 DRAW_TCS_JIT_CTX_SAMPLERS
);
649 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, ssbos
,
650 target
, context_type
, DRAW_TCS_JIT_CTX_SSBOS
);
651 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, num_ssbos
,
652 target
, context_type
, DRAW_TCS_JIT_CTX_NUM_SSBOS
);
653 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context
, images
,
654 target
, context_type
, DRAW_TCS_JIT_CTX_IMAGES
);
655 LP_CHECK_STRUCT_SIZE(struct draw_tes_jit_context
,
656 target
, context_type
);
662 * Create LLVM types for various structures.
665 create_jit_types(struct draw_llvm_variant
*variant
)
667 struct gallivm_state
*gallivm
= variant
->gallivm
;
668 LLVMTypeRef texture_type
, sampler_type
, context_type
, buffer_type
,
671 texture_type
= create_jit_texture_type(gallivm
, "texture");
672 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
673 image_type
= create_jit_image_type(gallivm
, "image");
675 context_type
= create_jit_context_type(gallivm
, texture_type
, sampler_type
,
678 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
680 buffer_type
= create_jit_dvbuffer_type(gallivm
, "draw_vertex_buffer");
681 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
683 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
684 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
689 get_context_ptr_type(struct draw_llvm_variant
*variant
)
691 if (!variant
->context_ptr_type
)
692 create_jit_types(variant
);
693 return variant
->context_ptr_type
;
698 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
700 if (!variant
->buffer_ptr_type
)
701 create_jit_types(variant
);
702 return variant
->buffer_ptr_type
;
707 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
709 if (!variant
->vb_ptr_type
)
710 create_jit_types(variant
);
711 return variant
->vb_ptr_type
;
715 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
717 if (!variant
->vertex_header_ptr_type
)
718 create_jit_types(variant
);
719 return variant
->vertex_header_ptr_type
;
724 * Create per-context LLVM info.
727 draw_llvm_create(struct draw_context
*draw
, LLVMContextRef context
)
729 struct draw_llvm
*llvm
;
731 if (!lp_build_init())
734 llvm
= CALLOC_STRUCT( draw_llvm
);
740 llvm
->context
= context
;
741 if (!llvm
->context
) {
742 llvm
->context
= LLVMContextCreate();
743 llvm
->context_owned
= true;
748 llvm
->nr_variants
= 0;
749 make_empty_list(&llvm
->vs_variants_list
);
751 llvm
->nr_gs_variants
= 0;
752 make_empty_list(&llvm
->gs_variants_list
);
757 draw_llvm_destroy(llvm
);
763 * Free per-context LLVM info.
766 draw_llvm_destroy(struct draw_llvm
*llvm
)
768 if (llvm
->context_owned
)
769 LLVMContextDispose(llvm
->context
);
770 llvm
->context
= NULL
;
772 /* XXX free other draw_llvm data? */
778 * Create LLVM-generated code for a vertex shader.
780 struct draw_llvm_variant
*
781 draw_llvm_create_variant(struct draw_llvm
*llvm
,
783 const struct draw_llvm_variant_key
*key
)
785 struct draw_llvm_variant
*variant
;
786 struct llvm_vertex_shader
*shader
=
787 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
788 LLVMTypeRef vertex_header
;
789 char module_name
[64];
791 variant
= MALLOC(sizeof *variant
+
792 shader
->variant_key_size
-
793 sizeof variant
->key
);
797 variant
->llvm
= llvm
;
798 variant
->shader
= shader
;
800 snprintf(module_name
, sizeof(module_name
), "draw_llvm_vs_variant%u",
801 variant
->shader
->variants_cached
);
803 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
805 create_jit_types(variant
);
807 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
809 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
810 if (llvm
->draw
->vs
.vertex_shader
->state
.type
== PIPE_SHADER_IR_TGSI
)
811 tgsi_dump(llvm
->draw
->vs
.vertex_shader
->state
.tokens
, 0);
813 nir_print_shader(llvm
->draw
->vs
.vertex_shader
->state
.ir
.nir
, stderr
);
814 draw_llvm_dump_variant_key(&variant
->key
);
817 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
819 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
821 draw_llvm_generate(llvm
, variant
);
823 gallivm_compile_module(variant
->gallivm
);
825 variant
->jit_func
= (draw_jit_vert_func
)
826 gallivm_jit_function(variant
->gallivm
, variant
->function
);
828 gallivm_free_ir(variant
->gallivm
);
830 variant
->list_item_global
.base
= variant
;
831 variant
->list_item_local
.base
= variant
;
832 /*variant->no = */shader
->variants_created
++;
833 variant
->list_item_global
.base
= variant
;
840 generate_vs(struct draw_llvm_variant
*variant
,
841 LLVMBuilderRef builder
,
842 struct lp_type vs_type
,
843 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
844 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
845 const struct lp_bld_tgsi_system_values
*system_values
,
846 LLVMValueRef context_ptr
,
847 const struct lp_build_sampler_soa
*draw_sampler
,
848 const struct lp_build_image_soa
*draw_image
,
849 boolean clamp_vertex_color
,
850 struct lp_build_mask_context
*bld_mask
)
852 struct draw_llvm
*llvm
= variant
->llvm
;
853 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
854 LLVMValueRef consts_ptr
=
855 draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
856 LLVMValueRef num_consts_ptr
=
857 draw_jit_context_num_vs_constants(variant
->gallivm
, context_ptr
);
858 LLVMValueRef ssbos_ptr
=
859 draw_jit_context_vs_ssbos(variant
->gallivm
, context_ptr
);
860 LLVMValueRef num_ssbos_ptr
=
861 draw_jit_context_num_vs_ssbos(variant
->gallivm
, context_ptr
);
863 struct lp_build_tgsi_params params
;
864 memset(¶ms
, 0, sizeof(params
));
866 params
.type
= vs_type
;
867 params
.mask
= bld_mask
;
868 params
.consts_ptr
= consts_ptr
;
869 params
.const_sizes_ptr
= num_consts_ptr
;
870 params
.system_values
= system_values
;
871 params
.inputs
= inputs
;
872 params
.context_ptr
= context_ptr
;
873 params
.sampler
= draw_sampler
;
874 params
.info
= &llvm
->draw
->vs
.vertex_shader
->info
;
875 params
.ssbo_ptr
= ssbos_ptr
;
876 params
.ssbo_sizes_ptr
= num_ssbos_ptr
;
877 params
.image
= draw_image
;
879 if (llvm
->draw
->vs
.vertex_shader
->state
.ir
.nir
&&
880 llvm
->draw
->vs
.vertex_shader
->state
.type
== PIPE_SHADER_IR_NIR
)
881 lp_build_nir_soa(variant
->gallivm
,
882 llvm
->draw
->vs
.vertex_shader
->state
.ir
.nir
,
886 lp_build_tgsi_soa(variant
->gallivm
,
893 unsigned chan
, attrib
;
894 struct lp_build_context bld
;
895 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
896 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
898 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
899 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
900 if (outputs
[attrib
][chan
]) {
901 switch (info
->output_semantic_name
[attrib
]) {
902 case TGSI_SEMANTIC_COLOR
:
903 case TGSI_SEMANTIC_BCOLOR
:
904 if (clamp_vertex_color
) {
905 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
906 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
907 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
919 fetch_instanced(struct gallivm_state
*gallivm
,
920 const struct util_format_description
*format_desc
,
921 struct lp_type vs_type
,
922 LLVMValueRef vb_stride
,
923 LLVMValueRef map_ptr
,
924 LLVMValueRef buffer_size_adj
,
925 LLVMValueRef
*inputs
,
928 LLVMTypeRef i32_t
= LLVMInt32TypeInContext(gallivm
->context
);
929 LLVMTypeRef aosf_t
, aosi_t
;
930 LLVMValueRef zero
= LLVMConstNull(i32_t
);
931 LLVMBuilderRef builder
= gallivm
->builder
;
932 LLVMValueRef stride
, buffer_overflowed
, aos
, index_valid
;
935 aosf_t
= lp_build_vec_type(gallivm
, lp_float32_vec4_type());
936 aosi_t
= lp_build_vec_type(gallivm
, lp_int32_vec4_type());
938 /* This mul can overflow. Wraparound is ok. */
939 stride
= LLVMBuildMul(builder
, vb_stride
, index
, "");
941 buffer_overflowed
= LLVMBuildICmp(builder
, LLVMIntUGE
,
942 stride
, buffer_size_adj
,
943 "buffer_overflowed");
946 lp_build_print_value(gallivm
, " instance index = ", index
);
947 lp_build_print_value(gallivm
, " buffer overflowed = ", buffer_overflowed
);
950 index_valid
= LLVMBuildNot(builder
, buffer_overflowed
, "");
951 index_valid
= LLVMBuildSExt(builder
, index_valid
, i32_t
, "");
952 stride
= LLVMBuildAnd(builder
, stride
, index_valid
, "");
954 aos
= lp_build_fetch_rgba_aos(gallivm
,
956 lp_float32_vec4_type(),
962 index_valid
= lp_build_broadcast(gallivm
, aosi_t
, index_valid
);
963 aos
= LLVMBuildBitCast(builder
, aos
, aosi_t
, "");
964 aos
= LLVMBuildAnd(builder
, aos
, index_valid
, "");
965 aos
= LLVMBuildBitCast(builder
, aos
, aosf_t
, "");
967 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
968 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
969 inputs
[i
] = lp_build_extract_broadcast(gallivm
,
970 lp_float32_vec4_type(),
971 vs_type
, aos
, index
);
977 fetch_vector(struct gallivm_state
*gallivm
,
978 const struct util_format_description
*format_desc
,
979 struct lp_type vs_type
,
980 LLVMValueRef vb_stride
,
981 LLVMValueRef map_ptr
,
982 LLVMValueRef buffer_size_adj
,
983 LLVMValueRef
*inputs
,
984 LLVMValueRef indices
)
986 LLVMBuilderRef builder
= gallivm
->builder
;
987 struct lp_build_context blduivec
;
988 struct lp_type fetch_type
= vs_type
;
989 LLVMValueRef offset
, valid_mask
;
992 lp_build_context_init(&blduivec
, gallivm
, lp_uint_type(vs_type
));
994 vb_stride
= lp_build_broadcast_scalar(&blduivec
, vb_stride
);
995 buffer_size_adj
= lp_build_broadcast_scalar(&blduivec
, buffer_size_adj
);
997 /* This mul can overflow. Wraparound is ok. */
998 offset
= lp_build_mul(&blduivec
, vb_stride
, indices
);
1000 valid_mask
= lp_build_compare(gallivm
, blduivec
.type
,
1001 PIPE_FUNC_LESS
, offset
, buffer_size_adj
);
1003 /* not valid elements use offset 0 */
1004 offset
= LLVMBuildAnd(builder
, offset
, valid_mask
, "");
1007 lp_build_print_value(gallivm
, " indices = ", indices
);
1008 lp_build_print_value(gallivm
, " offsets = ", offset
);
1009 lp_build_print_value(gallivm
, " valid_mask = ", valid_mask
);
1013 * Unlike fetch_instanced, use SoA fetch instead of multiple AoS fetches.
1014 * This should always produce better code.
1017 /* The type handling is annoying here... */
1018 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
&&
1019 format_desc
->channel
[0].pure_integer
) {
1020 if (format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_SIGNED
) {
1021 fetch_type
= lp_type_int_vec(vs_type
.width
, vs_type
.width
* vs_type
.length
);
1023 else if (format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
1024 fetch_type
= lp_type_uint_vec(vs_type
.width
, vs_type
.width
* vs_type
.length
);
1028 lp_build_fetch_rgba_soa(gallivm
, format_desc
,
1029 fetch_type
, FALSE
, map_ptr
, offset
,
1030 blduivec
.zero
, blduivec
.zero
,
1033 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1034 inputs
[i
] = LLVMBuildBitCast(builder
, inputs
[i
],
1035 lp_build_vec_type(gallivm
, vs_type
), "");
1038 /* out-of-bound fetches return all zeros */
1039 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1040 inputs
[i
] = LLVMBuildBitCast(builder
, inputs
[i
], blduivec
.vec_type
, "");
1041 inputs
[i
] = LLVMBuildAnd(builder
, inputs
[i
], valid_mask
, "");
1042 inputs
[i
] = LLVMBuildBitCast(builder
, inputs
[i
],
1043 lp_build_vec_type(gallivm
, vs_type
), "");
1049 store_aos(struct gallivm_state
*gallivm
,
1050 LLVMValueRef io_ptr
,
1054 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
1055 LLVMBuilderRef builder
= gallivm
->builder
;
1056 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
1057 LLVMValueRef indices
[3];
1059 indices
[0] = lp_build_const_int32(gallivm
, 0);
1061 indices
[2] = lp_build_const_int32(gallivm
, 0);
1063 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
1064 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
1067 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
1070 /* Unaligned store due to the vertex header */
1071 LLVMSetAlignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
1075 * Adjust the mask to architecture endianess. The mask will the store in struct:
1077 * struct vertex_header {
1078 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
1079 * unsigned edgeflag:1;
1081 * unsigned vertex_id:16;
1085 * On little-endian machine nothing needs to done, however on bit-endian machine
1086 * the mask's fields need to be adjusted with the algorithm:
1088 * uint32_t reverse (uint32_t x)
1090 * return (x >> 16) | // vertex_id
1091 * ((x & 0x3fff) << 18) | // clipmask
1092 * ((x & 0x4000) << 3) | // pad
1093 * ((x & 0x8000) << 1); // edgeflag
1097 adjust_mask(struct gallivm_state
*gallivm
,
1100 #if UTIL_ARCH_BIG_ENDIAN
1101 LLVMBuilderRef builder
= gallivm
->builder
;
1102 LLVMValueRef vertex_id
;
1103 LLVMValueRef clipmask
;
1105 LLVMValueRef edgeflag
;
1107 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
1108 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
1109 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
1111 pad
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
1112 pad
= LLVMBuildShl(builder
, pad
, lp_build_const_int32(gallivm
, 3), "");
1114 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
1115 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
1117 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
1119 mask
= LLVMBuildOr(builder
, mask
, pad
, "");
1121 mask
= LLVMBuildOr(builder
, mask
, edgeflag
, "");
1127 store_aos_array(struct gallivm_state
*gallivm
,
1128 struct lp_type soa_type
,
1129 LLVMValueRef io_ptr
,
1130 LLVMValueRef
*indices
,
1134 LLVMValueRef clipmask
,
1135 boolean need_edgeflag
)
1137 LLVMBuilderRef builder
= gallivm
->builder
;
1138 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
1139 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1140 LLVMValueRef linear_inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1141 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1142 int vector_length
= soa_type
.length
;
1145 debug_assert(TGSI_NUM_CHANNELS
== 4);
1147 for (i
= 0; i
< vector_length
; i
++) {
1148 linear_inds
[i
] = lp_build_const_int32(gallivm
, i
);
1150 inds
[i
] = indices
[i
];
1152 inds
[i
] = linear_inds
[i
];
1154 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1158 /* store vertex header for each of the n vertices */
1159 LLVMValueRef val
, cliptmp
;
1160 int vertex_id_pad_edgeflag
;
1162 /* If this assertion fails, it means we need to update the bit twidding
1163 * code here. See struct vertex_header in draw_private.h.
1165 assert(DRAW_TOTAL_CLIP_PLANES
==14);
1166 /* initialize vertex id:16 = 0xffff, pad:1 = 0, edgeflag:1 = 1 */
1167 if (!need_edgeflag
) {
1168 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
1171 vertex_id_pad_edgeflag
= (0xffff << 16);
1173 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
),
1174 vertex_id_pad_edgeflag
);
1175 /* OR with the clipmask */
1176 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
1177 for (i
= 0; i
< vector_length
; i
++) {
1178 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
1179 val
= LLVMBuildExtractElement(builder
, cliptmp
, linear_inds
[i
], "");
1180 val
= adjust_mask(gallivm
, val
);
1182 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
1183 io_ptrs
[i
], inds
[i
], val
);
1185 LLVMBuildStore(builder
, val
, id_ptr
);
1189 /* store for each of the n vertices */
1190 for (i
= 0; i
< vector_length
; i
++) {
1191 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
1197 convert_to_aos(struct gallivm_state
*gallivm
,
1199 LLVMValueRef
*indices
,
1200 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1201 LLVMValueRef clipmask
,
1203 struct lp_type soa_type
,
1204 boolean need_edgeflag
)
1206 LLVMBuilderRef builder
= gallivm
->builder
;
1207 unsigned chan
, attrib
, i
;
1210 lp_build_printf(gallivm
, " # storing begin\n");
1212 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
1213 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
1214 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
1215 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
1216 if (outputs
[attrib
][chan
]) {
1217 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
1218 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
1220 lp_build_printf(gallivm
, "output %d : %d ",
1221 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
1223 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
1225 lp_build_print_value(gallivm
, "val = ", out
);
1228 LLVMBuildBitCast(builder
, out
, lp_build_int_vec_type(gallivm
, soa_type
), "");
1230 lp_build_print_value(gallivm
, " ival = ", iv
);
1241 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
1242 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
1244 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
1246 for (i
= 0; i
< soa_type
.length
; ++i
) {
1247 aos
[i
] = lp_build_extract_range(gallivm
,
1248 soa
[i
% TGSI_NUM_CHANNELS
],
1249 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1254 store_aos_array(gallivm
,
1264 lp_build_printf(gallivm
, " # storing end\n");
1270 * Stores original vertex positions in clip coordinates
1273 store_clip(struct gallivm_state
*gallivm
,
1274 const struct lp_type vs_type
,
1275 LLVMValueRef io_ptr
,
1276 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1279 LLVMBuilderRef builder
= gallivm
->builder
;
1280 LLVMValueRef soa
[4];
1281 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
1282 LLVMValueRef indices
[2];
1283 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1284 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
1285 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
1286 LLVMTypeRef clip_ptr_type
=
1287 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
),
1292 indices
[1] = lp_build_const_int32(gallivm
, 0);
1294 for (i
= 0; i
< vs_type
.length
; i
++) {
1295 inds
[i
] = lp_build_const_int32(gallivm
, i
);
1296 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
1299 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
1300 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
1301 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
1302 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
1304 for (i
= 0; i
< vs_type
.length
; i
++) {
1305 clip_ptrs
[i
] = draw_jit_header_clip_pos(gallivm
, io_ptrs
[i
]);
1308 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
1309 for (i
= 0; i
< vs_type
.length
; ++i
) {
1310 aos
[i
] = lp_build_extract_range(gallivm
,
1311 soa
[i
% TGSI_NUM_CHANNELS
],
1312 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
1316 for (j
= 0; j
< vs_type
.length
; j
++) {
1317 LLVMValueRef clip_ptr
;
1319 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
1320 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
1322 /* Unaligned store */
1323 LLVMSetAlignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
1329 * Transforms the outputs for viewport mapping
1332 generate_viewport(struct draw_llvm_variant
*variant
,
1333 LLVMBuilderRef builder
,
1334 struct lp_type vs_type
,
1335 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1336 LLVMValueRef context_ptr
)
1339 struct gallivm_state
*gallivm
= variant
->gallivm
;
1340 struct lp_type f32_type
= vs_type
;
1341 const unsigned pos
= variant
->llvm
->draw
->vs
.position_output
;
1342 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1343 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn*/
1344 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1345 LLVMValueRef vp_ptr
= draw_jit_context_viewports(gallivm
, context_ptr
);
1347 /* We treat pipe_viewport_state as a float array */
1348 const int scale_index_offset
= offsetof(struct pipe_viewport_state
, scale
) / sizeof(float);
1349 const int trans_index_offset
= offsetof(struct pipe_viewport_state
, translate
) / sizeof(float);
1351 /* for 1/w convention*/
1352 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1353 LLVMBuildStore(builder
, out3
, outputs
[pos
][3]);
1355 /* Viewport Mapping */
1356 for (i
=0; i
<3; i
++) {
1357 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[pos
][i
], ""); /*x0 x1 .. xn*/
1360 LLVMValueRef scale_i
;
1361 LLVMValueRef trans_i
;
1364 index
= lp_build_const_int32(gallivm
, i
+ scale_index_offset
);
1365 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1367 index
= lp_build_const_int32(gallivm
, i
+ trans_index_offset
);
1368 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1370 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1371 LLVMBuildLoad(builder
, scale_i
, "scale"));
1372 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1373 LLVMBuildLoad(builder
, trans_i
, "trans"));
1376 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1377 /* mult by scale, add translation */
1378 out
= lp_build_fmuladd(builder
, out
, scale
, trans
);
1380 /* store transformed outputs */
1381 LLVMBuildStore(builder
, out
, outputs
[pos
][i
]);
1388 * Returns clipmask as nxi32 bitmask for the n vertices
1391 generate_clipmask(struct draw_llvm
*llvm
,
1392 struct gallivm_state
*gallivm
,
1393 struct lp_type vs_type
,
1394 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1395 struct draw_llvm_variant_key
*key
,
1396 LLVMValueRef context_ptr
,
1397 boolean
*have_clipdist
)
1399 LLVMBuilderRef builder
= gallivm
->builder
;
1400 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1401 LLVMValueRef test
, temp
;
1402 LLVMValueRef zero
, shift
;
1403 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1404 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1405 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1406 struct lp_type f32_type
= vs_type
;
1407 struct lp_type i32_type
= lp_int_type(vs_type
);
1408 const unsigned pos
= llvm
->draw
->vs
.position_output
;
1409 const unsigned cv
= llvm
->draw
->vs
.clipvertex_output
;
1410 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1411 boolean have_cd
= false;
1412 boolean clip_user
= key
->clip_user
;
1413 unsigned ucp_enable
= key
->ucp_enable
;
1416 cd
[0] = llvm
->draw
->vs
.ccdistance_output
[0];
1417 cd
[1] = llvm
->draw
->vs
.ccdistance_output
[1];
1419 if (cd
[0] != pos
|| cd
[1] != pos
)
1422 if (num_written_clipdistance
&& !clip_user
) {
1424 ucp_enable
= (1 << num_written_clipdistance
) - 1;
1427 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1428 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1429 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1430 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1433 * load clipvertex and position from correct locations.
1434 * if they are the same just load them once.
1436 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1437 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1438 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1439 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1441 if (clip_user
&& cv
!= pos
) {
1442 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1443 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1444 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1445 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1454 * Be careful with the comparisons and NaNs (using llvm's unordered
1455 * comparisons here).
1457 /* Cliptest, for hardwired planes */
1459 * XXX should take guardband into account (currently not in key).
1460 * Otherwise might run the draw pipeline stages for nothing.
1464 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1466 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1470 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1471 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1472 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1473 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1474 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1477 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1478 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1479 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1480 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1483 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1484 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1485 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1486 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1487 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1491 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1492 if (key
->clip_halfz
) {
1494 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1495 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1496 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1500 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1501 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1502 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1503 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1506 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1507 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1508 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1509 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1513 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1514 LLVMValueRef indices
[3];
1515 LLVMValueRef is_nan_or_inf
;
1517 /* userclip planes */
1518 while (ucp_enable
) {
1519 unsigned plane_idx
= ffs(ucp_enable
)-1;
1520 ucp_enable
&= ~(1 << plane_idx
);
1523 if (have_cd
&& num_written_clipdistance
) {
1524 LLVMValueRef clipdist
;
1528 *have_clipdist
= TRUE
;
1530 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1532 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1534 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1535 is_nan_or_inf
= lp_build_is_inf_or_nan(gallivm
, vs_type
, clipdist
);
1536 test
= LLVMBuildOr(builder
, test
, is_nan_or_inf
, "");
1537 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1LL << plane_idx
);
1538 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1539 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1541 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1542 indices
[0] = lp_build_const_int32(gallivm
, 0);
1543 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1545 indices
[2] = lp_build_const_int32(gallivm
, 0);
1546 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1547 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1548 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1549 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1551 indices
[2] = lp_build_const_int32(gallivm
, 1);
1552 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1553 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1554 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1555 sum
= lp_build_fmuladd(builder
, planes
, cv_y
, sum
);
1557 indices
[2] = lp_build_const_int32(gallivm
, 2);
1558 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1559 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1560 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1561 sum
= lp_build_fmuladd(builder
, planes
, cv_z
, sum
);
1563 indices
[2] = lp_build_const_int32(gallivm
, 3);
1564 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1565 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1566 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1567 sum
= lp_build_fmuladd(builder
, planes
, cv_w
, sum
);
1569 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1570 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1LL << plane_idx
);
1571 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1572 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1576 if (key
->need_edgeflags
) {
1578 * This isn't really part of clipmask but stored the same in vertex
1579 * header later, so do it here.
1581 unsigned edge_attr
= llvm
->draw
->vs
.edgeflag_output
;
1582 LLVMValueRef one
= lp_build_const_vec(gallivm
, f32_type
, 1.0);
1583 LLVMValueRef edgeflag
= LLVMBuildLoad(builder
, outputs
[edge_attr
][0], "");
1584 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_EQUAL
, one
, edgeflag
);
1585 temp
= lp_build_const_int_vec(gallivm
, i32_type
,
1586 1LL << DRAW_TOTAL_CLIP_PLANES
);
1587 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1588 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1595 * Returns boolean if any clipping has occurred
1596 * Used zero/one i8 value to represent boolean
1599 clipmask_booli8(struct gallivm_state
*gallivm
,
1600 const struct lp_type vs_type
,
1601 LLVMValueRef clipmask_bool_ptr
,
1602 boolean edgeflag_in_clipmask
)
1604 LLVMBuilderRef builder
= gallivm
->builder
;
1605 LLVMTypeRef int8_type
= LLVMInt8TypeInContext(gallivm
->context
);
1606 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1608 struct lp_build_context bldivec
;
1610 lp_build_context_init(&bldivec
, gallivm
, lp_int_type(vs_type
));
1613 * We need to invert the edgeflag bit from the clipmask here
1614 * (because the result is really if we want to run the pipeline or not
1615 * and we (may) need it if edgeflag was 0).
1617 if (edgeflag_in_clipmask
) {
1618 LLVMValueRef edge
= lp_build_const_int_vec(gallivm
, bldivec
.type
,
1619 1LL << DRAW_TOTAL_CLIP_PLANES
);
1620 clipmask_bool
= LLVMBuildXor(builder
, clipmask_bool
, edge
, "");
1624 * XXX: probably should mask off bits from the mask which come from
1625 * vertices which were beyond the count (i.e. indices_valid for
1626 * linear fetches, for elts ones we don't have the correct mask
1627 * right now). Otherwise might run the pipeline for nothing,
1628 * though everything should still work.
1630 ret
= lp_build_any_true_range(&bldivec
, vs_type
.length
, clipmask_bool
);
1631 ret
= LLVMBuildZExt(builder
, ret
, int8_type
, "");
1636 draw_gs_llvm_fetch_input(const struct lp_build_gs_iface
*gs_iface
,
1637 struct lp_build_context
* bld
,
1638 boolean is_vindex_indirect
,
1639 LLVMValueRef vertex_index
,
1640 boolean is_aindex_indirect
,
1641 LLVMValueRef attrib_index
,
1642 LLVMValueRef swizzle_index
)
1644 const struct draw_gs_llvm_iface
*gs
= draw_gs_llvm_iface(gs_iface
);
1645 struct gallivm_state
*gallivm
= bld
->gallivm
;
1646 LLVMBuilderRef builder
= gallivm
->builder
;
1647 LLVMValueRef indices
[3];
1649 struct lp_type type
= bld
->type
;
1651 if (is_vindex_indirect
|| is_aindex_indirect
) {
1654 for (i
= 0; i
< type
.length
; ++i
) {
1655 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
1656 LLVMValueRef vert_chan_index
= vertex_index
;
1657 LLVMValueRef attr_chan_index
= attrib_index
;
1658 LLVMValueRef channel_vec
, value
;
1660 if (is_vindex_indirect
) {
1661 vert_chan_index
= LLVMBuildExtractElement(builder
,
1662 vertex_index
, idx
, "");
1664 if (is_aindex_indirect
) {
1665 attr_chan_index
= LLVMBuildExtractElement(builder
,
1666 attrib_index
, idx
, "");
1669 indices
[0] = vert_chan_index
;
1670 indices
[1] = attr_chan_index
;
1671 indices
[2] = swizzle_index
;
1673 channel_vec
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1674 channel_vec
= LLVMBuildLoad(builder
, channel_vec
, "");
1675 value
= LLVMBuildExtractElement(builder
, channel_vec
, idx
, "");
1677 res
= LLVMBuildInsertElement(builder
, res
, value
, idx
, "");
1680 indices
[0] = vertex_index
;
1681 indices
[1] = attrib_index
;
1682 indices
[2] = swizzle_index
;
1684 res
= LLVMBuildGEP(builder
, gs
->input
, indices
, 3, "");
1685 res
= LLVMBuildLoad(builder
, res
, "");
1692 draw_gs_llvm_emit_vertex(const struct lp_build_gs_iface
*gs_base
,
1693 struct lp_build_context
* bld
,
1694 LLVMValueRef (*outputs
)[4],
1695 LLVMValueRef emitted_vertices_vec
,
1696 LLVMValueRef stream_id
)
1698 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1699 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1700 struct gallivm_state
*gallivm
= variant
->gallivm
;
1701 LLVMBuilderRef builder
= gallivm
->builder
;
1702 struct lp_type gs_type
= bld
->type
;
1703 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1704 lp_int_type(gs_type
), 0);
1705 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1706 LLVMValueRef next_prim_offset
=
1707 lp_build_const_int32(gallivm
, variant
->shader
->base
.primitive_boundary
);
1708 LLVMValueRef io
= variant
->io_ptr
;
1710 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1712 for (i
= 0; i
< gs_type
.length
; ++i
) {
1713 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1714 LLVMValueRef currently_emitted
=
1715 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1716 indices
[i
] = LLVMBuildMul(builder
, ind
, next_prim_offset
, "");
1717 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1720 LLVMValueRef stream_idx
= LLVMBuildExtractElement(builder
, stream_id
, lp_build_const_int32(gallivm
, 0), "");
1721 LLVMValueRef cnd
= LLVMBuildICmp(builder
, LLVMIntULT
, stream_idx
, lp_build_const_int32(gallivm
, variant
->shader
->base
.num_vertex_streams
), "");
1722 struct lp_build_if_state if_ctx
;
1723 lp_build_if(&if_ctx
, gallivm
, cnd
);
1724 io
= lp_build_pointer_get(builder
, io
, LLVMBuildExtractElement(builder
, stream_id
, lp_build_const_int32(gallivm
, 0), ""));
1726 convert_to_aos(gallivm
, io
, indices
,
1728 gs_info
->num_outputs
, gs_type
,
1730 lp_build_endif(&if_ctx
);
1734 draw_gs_llvm_end_primitive(const struct lp_build_gs_iface
*gs_base
,
1735 struct lp_build_context
* bld
,
1736 LLVMValueRef total_emitted_vertices_vec_ptr
,
1737 LLVMValueRef verts_per_prim_vec
,
1738 LLVMValueRef emitted_prims_vec
,
1739 LLVMValueRef mask_vec
)
1741 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1742 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1743 struct gallivm_state
*gallivm
= variant
->gallivm
;
1744 LLVMBuilderRef builder
= gallivm
->builder
;
1745 LLVMValueRef prim_lengts_ptr
=
1746 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1749 for (i
= 0; i
< bld
->type
.length
; ++i
) {
1750 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1751 LLVMValueRef prims_emitted
=
1752 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1753 LLVMValueRef store_ptr
;
1754 LLVMValueRef num_vertices
=
1755 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1757 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1758 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1759 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1760 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1765 draw_gs_llvm_epilogue(const struct lp_build_gs_iface
*gs_base
,
1766 LLVMValueRef total_emitted_vertices_vec
,
1767 LLVMValueRef emitted_prims_vec
, unsigned stream
)
1769 const struct draw_gs_llvm_iface
*gs_iface
= draw_gs_llvm_iface(gs_base
);
1770 struct draw_gs_llvm_variant
*variant
= gs_iface
->variant
;
1771 struct gallivm_state
*gallivm
= variant
->gallivm
;
1772 LLVMBuilderRef builder
= gallivm
->builder
;
1773 LLVMValueRef emitted_verts_ptr
=
1774 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1775 LLVMValueRef emitted_prims_ptr
=
1776 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1777 LLVMValueRef stream_val
= lp_build_const_int32(gallivm
, stream
);
1779 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &stream_val
, 1, "");
1780 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &stream_val
, 1, "");
1782 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1783 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1787 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
)
1789 struct gallivm_state
*gallivm
= variant
->gallivm
;
1790 LLVMContextRef context
= gallivm
->context
;
1791 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1792 LLVMTypeRef arg_types
[12];
1793 unsigned num_arg_types
= ARRAY_SIZE(arg_types
);
1794 LLVMTypeRef func_type
;
1795 LLVMValueRef context_ptr
;
1796 LLVMBasicBlockRef block
;
1797 LLVMBuilderRef builder
;
1799 struct lp_type vs_type
;
1800 LLVMValueRef count
, fetch_elts
, start_or_maxelt
;
1801 LLVMValueRef vertex_id_offset
;
1802 LLVMValueRef stride
, step
, io_itr
;
1803 LLVMValueRef ind_vec
, start_vec
, have_elts
, fetch_max
, tmp
;
1804 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1805 LLVMValueRef vb_stride
[PIPE_MAX_ATTRIBS
];
1806 LLVMValueRef map_ptr
[PIPE_MAX_ATTRIBS
];
1807 LLVMValueRef buffer_size_adj
[PIPE_MAX_ATTRIBS
];
1808 LLVMValueRef instance_index
[PIPE_MAX_ATTRIBS
];
1809 LLVMValueRef fake_buf_ptr
, fake_buf
;
1811 struct draw_context
*draw
= llvm
->draw
;
1812 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1814 struct lp_build_context bld
, blduivec
;
1815 struct lp_build_loop_state lp_loop
;
1816 struct lp_build_if_state if_ctx
;
1817 const int vector_length
= lp_native_vector_width
/ 32;
1818 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1819 struct lp_build_sampler_soa
*sampler
= 0;
1820 struct lp_build_image_soa
*image
= NULL
;
1821 LLVMValueRef ret
, clipmask_bool_ptr
;
1822 struct draw_llvm_variant_key
*key
= &variant
->key
;
1823 /* If geometry shader is present we need to skip both the viewport
1824 * transformation and clipping otherwise the inputs to the geometry
1825 * shader will be incorrect.
1826 * The code can't handle vp transform when vs writes vp index neither
1827 * (though this would be fixable here, but couldn't just broadcast
1830 const boolean bypass_viewport
= key
->has_gs
|| key
->bypass_viewport
||
1831 vs_info
->writes_viewport_index
;
1832 const boolean enable_cliptest
= !key
->has_gs
&& (key
->clip_xy
||
1835 key
->need_edgeflags
);
1836 LLVMValueRef variant_func
;
1837 const unsigned pos
= draw
->vs
.position_output
;
1838 const unsigned cv
= draw
->vs
.clipvertex_output
;
1839 boolean have_clipdist
= FALSE
;
1840 struct lp_bld_tgsi_system_values system_values
;
1842 memset(&system_values
, 0, sizeof(system_values
));
1843 memset(&outputs
, 0, sizeof(outputs
));
1844 snprintf(func_name
, sizeof(func_name
), "draw_llvm_vs_variant%u",
1845 variant
->shader
->variants_cached
);
1848 arg_types
[i
++] = get_context_ptr_type(variant
); /* context */
1849 arg_types
[i
++] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1850 arg_types
[i
++] = get_buffer_ptr_type(variant
); /* vbuffers */
1851 arg_types
[i
++] = int32_type
; /* count */
1852 arg_types
[i
++] = int32_type
; /* start/fetch_elt_max */
1853 arg_types
[i
++] = int32_type
; /* stride */
1854 arg_types
[i
++] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1855 arg_types
[i
++] = int32_type
; /* instance_id */
1856 arg_types
[i
++] = int32_type
; /* vertex_id_offset */
1857 arg_types
[i
++] = int32_type
; /* start_instance */
1858 arg_types
[i
++] = LLVMPointerType(int32_type
, 0); /* fetch_elts */
1859 arg_types
[i
++] = int32_type
; /* draw_id */
1861 func_type
= LLVMFunctionType(LLVMInt8TypeInContext(context
),
1862 arg_types
, num_arg_types
, 0);
1864 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
1865 variant
->function
= variant_func
;
1867 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1868 for (i
= 0; i
< num_arg_types
; ++i
)
1869 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1870 lp_add_function_attr(variant_func
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
1872 context_ptr
= LLVMGetParam(variant_func
, 0);
1873 io_ptr
= LLVMGetParam(variant_func
, 1);
1874 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1875 count
= LLVMGetParam(variant_func
, 3);
1877 * XXX: the maxelt part is unused. Not really useful, since we cannot
1878 * get index buffer overflows due to vsplit (which provides its own
1879 * elts buffer, with a different size than what's passed in here).
1881 start_or_maxelt
= LLVMGetParam(variant_func
, 4);
1883 * XXX: stride is actually unused. The stride we use is strictly calculated
1884 * from the number of outputs (including the draw_extra outputs).
1885 * Should probably fix some day (we need a new vs just because of extra
1886 * outputs which the generated vs won't touch).
1888 stride
= LLVMGetParam(variant_func
, 5);
1889 vb_ptr
= LLVMGetParam(variant_func
, 6);
1890 system_values
.instance_id
= LLVMGetParam(variant_func
, 7);
1891 vertex_id_offset
= LLVMGetParam(variant_func
, 8);
1892 system_values
.base_instance
= LLVMGetParam(variant_func
, 9);
1893 fetch_elts
= LLVMGetParam(variant_func
, 10);
1894 system_values
.draw_id
= LLVMGetParam(variant_func
, 11);
1896 lp_build_name(context_ptr
, "context");
1897 lp_build_name(io_ptr
, "io");
1898 lp_build_name(vbuffers_ptr
, "vbuffers");
1899 lp_build_name(count
, "count");
1900 lp_build_name(start_or_maxelt
, "start_or_maxelt");
1901 lp_build_name(stride
, "stride");
1902 lp_build_name(vb_ptr
, "vb");
1903 lp_build_name(system_values
.instance_id
, "instance_id");
1904 lp_build_name(vertex_id_offset
, "vertex_id_offset");
1905 lp_build_name(system_values
.base_instance
, "start_instance");
1906 lp_build_name(fetch_elts
, "fetch_elts");
1907 lp_build_name(system_values
.draw_id
, "draw_id");
1913 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1914 builder
= gallivm
->builder
;
1915 LLVMPositionBuilderAtEnd(builder
, block
);
1917 memset(&vs_type
, 0, sizeof vs_type
);
1918 vs_type
.floating
= TRUE
; /* floating point values */
1919 vs_type
.sign
= TRUE
; /* values are signed */
1920 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1921 vs_type
.width
= 32; /* 32-bit float */
1922 vs_type
.length
= vector_length
;
1924 lp_build_context_init(&bld
, gallivm
, lp_type_uint(32));
1925 lp_build_context_init(&blduivec
, gallivm
, lp_uint_type(vs_type
));
1927 /* hold temporary "bool" clipmask */
1928 clipmask_bool_ptr
= lp_build_alloca(gallivm
, blduivec
.vec_type
, "");
1930 fake_buf
= lp_build_alloca_undef(gallivm
,
1931 LLVMVectorType(LLVMInt64TypeInContext(context
), 4), "");
1932 fake_buf
= LLVMBuildBitCast(builder
, fake_buf
,
1933 LLVMPointerType(LLVMInt8TypeInContext(context
), 0), "");
1934 fake_buf_ptr
= LLVMBuildGEP(builder
, fake_buf
, &bld
.zero
, 1, "");
1936 /* code generated texture sampling */
1937 sampler
= draw_llvm_sampler_soa_create(draw_llvm_variant_key_samplers(key
));
1939 image
= draw_llvm_image_soa_create(draw_llvm_variant_key_images(key
));
1941 step
= lp_build_const_int32(gallivm
, vector_length
);
1943 ind_vec
= blduivec
.undef
;
1944 for (i
= 0; i
< vs_type
.length
; i
++) {
1945 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
1946 ind_vec
= LLVMBuildInsertElement(builder
, ind_vec
, index
, index
, "");
1949 have_elts
= LLVMBuildICmp(builder
, LLVMIntNE
,
1950 LLVMConstPointerNull(arg_types
[10]), fetch_elts
, "");
1952 fetch_max
= LLVMBuildSub(builder
, count
, bld
.one
, "fetch_max");
1953 fetch_max
= lp_build_broadcast_scalar(&blduivec
, fetch_max
);
1955 * Only needed for non-indexed path.
1957 start_vec
= lp_build_broadcast_scalar(&blduivec
, start_or_maxelt
);
1960 * Pre-calculate everything which is constant per shader invocation.
1962 for (j
= 0; j
< key
->nr_vertex_elements
; ++j
) {
1963 LLVMValueRef vb_buffer_offset
, buffer_size
, temp_ptr
;
1964 LLVMValueRef vb_info
, vbuffer_ptr
, buf_offset
, ofbit
;
1965 struct pipe_vertex_element
*velem
= &key
->vertex_element
[j
];
1966 LLVMValueRef vb_index
=
1967 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1968 LLVMValueRef bsize
= lp_build_const_int32(gallivm
,
1969 util_format_get_blocksize(velem
->src_format
));
1970 LLVMValueRef src_offset
= lp_build_const_int32(gallivm
,
1972 struct lp_build_if_state if_ctx
;
1974 if (velem
->src_format
!= PIPE_FORMAT_NONE
) {
1975 vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
, &vb_index
, 1, "");
1976 vb_info
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1977 vb_stride
[j
] = draw_jit_vbuffer_stride(gallivm
, vb_info
);
1978 vb_stride
[j
] = LLVMBuildZExt(gallivm
->builder
, vb_stride
[j
],
1979 LLVMInt32TypeInContext(context
), "");
1980 vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vb_info
);
1981 map_ptr
[j
] = draw_jit_dvbuffer_map(gallivm
, vbuffer_ptr
);
1982 buffer_size
= draw_jit_dvbuffer_size(gallivm
, vbuffer_ptr
);
1986 * We'll set buffer_size_adj to zero if we have of, so it will
1987 * always overflow later automatically without having to keep ofbit.
1988 * Overflows (with normal wraparound) doing the actual offset
1989 * calculation should be ok, just not for the buffer size calc.
1990 * It would also be possible to detect such overflows and return
1991 * zeros if that happens, but this would be more complex.
1993 buf_offset
= lp_build_add(&bld
, vb_buffer_offset
, src_offset
);
1994 tmp
= lp_build_sub(&bld
, bsize
, bld
.one
);
1995 buffer_size_adj
[j
] = lp_build_usub_overflow(gallivm
, buffer_size
, tmp
,
1997 buffer_size_adj
[j
] = lp_build_usub_overflow(gallivm
, buffer_size_adj
[j
],
1998 buf_offset
, &ofbit
);
2001 * We can't easily set fake vertex buffers outside the generated code.
2002 * Hence, set fake vertex buffers here instead basically, so fetch
2003 * code can always fetch using offset 0, eliminating all control flow
2004 * inside the main loop.
2005 * (Alternatively, could have control flow per vector skipping fetch
2006 * if ofbit is true.)
2008 if (velem
->instance_divisor
) {
2010 * Index is equal to the start instance plus the number of current
2011 * instance divided by the divisor. In this case we compute it as:
2012 * index = start_instance + (instance_id / divisor).
2013 * Note we could actually do the fetch here, outside the loop -
2014 * it's all constant, hopefully llvm recognizes this.
2016 LLVMValueRef current_instance
;
2017 current_instance
= LLVMBuildUDiv(builder
, system_values
.instance_id
,
2018 lp_build_const_int32(gallivm
,
2019 velem
->instance_divisor
),
2020 "instance_divisor");
2021 instance_index
[j
] = lp_build_uadd_overflow(gallivm
, system_values
.base_instance
,
2022 current_instance
, &ofbit
);
2025 buffer_size_adj
[j
] = LLVMBuildSelect(builder
, ofbit
, bld
.zero
,
2026 buffer_size_adj
[j
], "");
2028 temp_ptr
= lp_build_alloca_undef(gallivm
,
2029 LLVMPointerType(LLVMInt8TypeInContext(context
), 0), "");
2031 lp_build_if(&if_ctx
, gallivm
, ofbit
);
2033 LLVMBuildStore(builder
, fake_buf_ptr
, temp_ptr
);
2035 lp_build_else(&if_ctx
);
2037 map_ptr
[j
] = LLVMBuildGEP(builder
, map_ptr
[j
], &buf_offset
, 1, "");
2038 LLVMBuildStore(builder
, map_ptr
[j
], temp_ptr
);
2040 lp_build_endif(&if_ctx
);
2041 map_ptr
[j
] = LLVMBuildLoad(builder
, temp_ptr
, "map_ptr");
2044 lp_build_printf(gallivm
, "velem %d, vbuf index = %u, vb_stride = %u\n",
2045 lp_build_const_int32(gallivm
, j
),
2046 vb_index
, vb_stride
[j
]);
2047 lp_build_printf(gallivm
,
2048 " vb_buffer_offset = %u, src_offset = %u, buf_offset = %u\n",
2049 vb_buffer_offset
, src_offset
, buf_offset
);
2050 lp_build_printf(gallivm
, " buffer size = %u, blocksize = %u\n",
2051 buffer_size
, bsize
);
2052 lp_build_printf(gallivm
, " instance_id = %u\n", system_values
.instance_id
);
2057 lp_build_loop_begin(&lp_loop
, gallivm
, bld
.zero
);
2059 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
2061 LLVMValueRef clipmask
; /* holds the clipmask value */
2062 LLVMValueRef true_index_array
, index_store
;
2063 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
2065 io_itr
= lp_loop
.counter
;
2067 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
2069 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
2070 io_itr
, io
, lp_loop
.counter
);
2073 true_index_array
= lp_build_broadcast_scalar(&blduivec
, lp_loop
.counter
);
2074 true_index_array
= LLVMBuildAdd(builder
, true_index_array
, ind_vec
, "");
2076 LLVMValueRef exec_mask
= lp_build_cmp(&blduivec
, PIPE_FUNC_LEQUAL
, true_index_array
, fetch_max
);
2078 * Limit indices to fetch_max, otherwise might try to access indices
2079 * beyond index buffer (or rather vsplit elt buffer) size.
2080 * Could probably safely (?) skip this for non-indexed draws and
2081 * simplify things minimally (by removing it could combine the ind_vec
2082 * and start_vec adds). I think the only effect for non-indexed draws will
2083 * be that for the invalid elements they will be all fetched from the
2084 * same location as the last valid one, but noone should really care.
2086 true_index_array
= lp_build_min(&blduivec
, true_index_array
, fetch_max
);
2088 index_store
= lp_build_alloca_undef(gallivm
, blduivec
.vec_type
, "index_store");
2090 lp_build_if(&if_ctx
, gallivm
, have_elts
);
2093 * Note: you'd expect some comparison/clamp against fetch_elt_max
2095 * There used to be one here but it was incorrect: overflow was
2096 * detected if index > fetch_elt_max - but the correct condition
2097 * would be index >= fetch_elt_max (since this is just size of elts
2098 * buffer / element size).
2099 * Using the correct condition however will cause failures - due to
2100 * vsplit/vcache code which rebases indices. So, as an example, if
2101 * fetch_elt_max is just 1 and fetch_count 2, vsplit cache will
2102 * replace all invalid indices with 0 - which in case of elt_bias
2103 * not being zero will get a different fetch index than the valid
2104 * index 0. So, just rely on vsplit code preventing out-of-bounds
2105 * fetches. This is also why it's safe to do elts fetch even if there
2106 * was no index buffer bound - the real buffer is never seen here, at
2107 * least not if there are index buffer overflows...
2111 * XXX should not have to do this, as scale can be handled
2112 * natively by loads (hits asserts though).
2114 tmp
= lp_build_shl_imm(&blduivec
, true_index_array
, 2);
2115 fetch_elts
= LLVMBuildBitCast(builder
, fetch_elts
,
2116 LLVMPointerType(LLVMInt8TypeInContext(context
),
2118 tmp
= lp_build_gather(gallivm
, vs_type
.length
,
2120 fetch_elts
, tmp
, FALSE
);
2121 LLVMBuildStore(builder
, tmp
, index_store
);
2123 lp_build_else(&if_ctx
);
2125 tmp
= LLVMBuildAdd(builder
, true_index_array
, start_vec
, "");
2126 LLVMBuildStore(builder
, tmp
, index_store
);
2128 lp_build_endif(&if_ctx
);
2130 true_index_array
= LLVMBuildLoad(builder
, index_store
, "");
2132 for (j
= 0; j
< key
->nr_vertex_elements
; ++j
) {
2133 struct pipe_vertex_element
*velem
= &key
->vertex_element
[j
];
2134 const struct util_format_description
*format_desc
=
2135 util_format_description(velem
->src_format
);
2137 if (format_desc
->format
== PIPE_FORMAT_NONE
) {
2138 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
2139 inputs
[j
][i
] = lp_build_zero(gallivm
, vs_type
);
2142 else if (velem
->instance_divisor
) {
2143 fetch_instanced(gallivm
, format_desc
, vs_type
,
2144 vb_stride
[j
], map_ptr
[j
],
2146 inputs
[j
], instance_index
[j
]);
2149 fetch_vector(gallivm
, format_desc
, vs_type
,
2150 vb_stride
[j
], map_ptr
[j
],
2152 inputs
[j
], true_index_array
);
2156 struct lp_build_mask_context mask
;
2158 lp_build_mask_begin(&mask
, gallivm
, vs_type
, exec_mask
);
2159 /* In the paths with elts vertex id has to be unaffected by the
2160 * index bias and because indices inside our elements array have
2161 * already had index bias applied we need to subtract it here to
2162 * get back to the original index.
2163 * in the linear paths vertex id has to be unaffected by the
2164 * original start index and because we abuse the 'start' variable
2165 * to either represent the actual start index or the index at which
2166 * the primitive was split (we split rendering into chunks of at
2167 * most 4095-vertices) we need to back out the original start
2168 * index out of our vertex id here.
2169 * for ARB_shader_draw_parameters, base_vertex should be 0 for non-indexed draws.
2171 LLVMValueRef base_vertex
= lp_build_select(&bld
, have_elts
, vertex_id_offset
, lp_build_const_int32(gallivm
, 0));;
2172 system_values
.basevertex
= lp_build_broadcast_scalar(&blduivec
, base_vertex
);
2173 system_values
.vertex_id
= true_index_array
;
2174 system_values
.vertex_id_nobase
= LLVMBuildSub(builder
, true_index_array
,
2175 lp_build_broadcast_scalar(&blduivec
, vertex_id_offset
), "");
2177 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
2178 generate_vs(variant
,
2187 key
->clamp_vertex_color
,
2190 lp_build_mask_end(&mask
);
2191 if (pos
!= -1 && cv
!= -1) {
2192 /* store original positions in clip before further manipulation */
2193 store_clip(gallivm
, vs_type
, io
, outputs
, pos
);
2196 if (enable_cliptest
) {
2197 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
2198 /* allocate clipmask, assign it integer type */
2199 clipmask
= generate_clipmask(llvm
,
2204 context_ptr
, &have_clipdist
);
2205 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
2206 /* store temporary clipping boolean value */
2207 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
2210 clipmask
= blduivec
.zero
;
2213 /* do viewport mapping */
2214 if (!bypass_viewport
) {
2215 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
2219 clipmask
= blduivec
.zero
;
2222 /* store clipmask in vertex header,
2223 * original positions in clip
2224 * and transformed positions in data
2226 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
2227 vs_info
->num_outputs
, vs_type
,
2228 enable_cliptest
&& key
->need_edgeflags
);
2230 lp_build_loop_end_cond(&lp_loop
, count
, step
, LLVMIntUGE
);
2232 sampler
->destroy(sampler
);
2233 image
->destroy(image
);
2235 /* return clipping boolean value for function */
2236 ret
= clipmask_booli8(gallivm
, vs_type
, clipmask_bool_ptr
,
2237 enable_cliptest
&& key
->need_edgeflags
);
2239 LLVMBuildRet(builder
, ret
);
2241 gallivm_verify_function(gallivm
, variant_func
);
2245 struct draw_llvm_variant_key
*
2246 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2249 struct draw_llvm_variant_key
*key
;
2250 struct draw_sampler_static_state
*draw_sampler
;
2251 struct draw_image_static_state
*draw_image
;
2253 key
= (struct draw_llvm_variant_key
*)store
;
2255 memset(key
, 0, offsetof(struct draw_llvm_variant_key
, vertex_element
[0]));
2257 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
2259 /* will have to rig this up properly later */
2260 key
->clip_xy
= llvm
->draw
->clip_xy
;
2261 key
->clip_z
= llvm
->draw
->clip_z
;
2262 key
->clip_user
= llvm
->draw
->clip_user
;
2263 key
->bypass_viewport
= llvm
->draw
->bypass_viewport
;
2264 key
->clip_halfz
= llvm
->draw
->rasterizer
->clip_halfz
;
2265 /* XXX assumes edgeflag output not at 0 */
2266 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
2267 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
2268 key
->has_gs
= llvm
->draw
->gs
.geometry_shader
!= NULL
;
2269 key
->num_outputs
= draw_total_vs_outputs(llvm
->draw
);
2271 /* All variants of this shader will have the same value for
2272 * nr_samplers. Not yet trying to compact away holes in the
2275 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2276 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2277 key
->nr_sampler_views
=
2278 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2281 key
->nr_sampler_views
= key
->nr_samplers
;
2284 key
->nr_images
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_IMAGE
] + 1;
2286 /* Presumably all variants of the shader should have the same
2287 * number of vertex elements - ie the number of shader inputs.
2288 * NOTE: we NEED to store the needed number of needed inputs
2289 * here, not the number of provided elements to match keysize
2290 * (and the offset of sampler state in the key).
2291 * If we have excess number of vertex elements, this is valid,
2292 * but the excess ones don't matter.
2293 * If we don't have enough vertex elements (which looks not really
2294 * valid but we'll handle it gracefully) fill out missing ones with
2295 * zero (we'll recognize these later by PIPE_FORMAT_NONE).
2297 key
->nr_vertex_elements
=
2298 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
2300 if (llvm
->draw
->pt
.nr_vertex_elements
< key
->nr_vertex_elements
) {
2301 debug_printf("draw: vs with %d inputs but only have %d vertex elements\n",
2302 key
->nr_vertex_elements
, llvm
->draw
->pt
.nr_vertex_elements
);
2303 memset(key
->vertex_element
, 0,
2304 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
2306 memcpy(key
->vertex_element
,
2307 llvm
->draw
->pt
.vertex_element
,
2308 sizeof(struct pipe_vertex_element
) *
2309 MIN2(key
->nr_vertex_elements
, llvm
->draw
->pt
.nr_vertex_elements
));
2311 draw_sampler
= draw_llvm_variant_key_samplers(key
);
2312 memset(draw_sampler
, 0,
2313 MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2315 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2316 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2317 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
2319 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2320 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2321 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
2324 draw_image
= draw_llvm_variant_key_images(key
);
2325 memset(draw_image
, 0,
2326 key
->nr_images
* sizeof *draw_image
);
2327 for (i
= 0; i
< key
->nr_images
; i
++) {
2328 lp_sampler_static_texture_state_image(&draw_image
[i
].image_state
,
2329 llvm
->draw
->images
[PIPE_SHADER_VERTEX
][i
]);
2336 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
2339 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
2340 struct draw_image_static_state
*image
= draw_llvm_variant_key_images(key
);
2341 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
2342 debug_printf("clip_xy = %u\n", key
->clip_xy
);
2343 debug_printf("clip_z = %u\n", key
->clip_z
);
2344 debug_printf("clip_user = %u\n", key
->clip_user
);
2345 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
2346 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
2347 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
2348 debug_printf("has_gs = %u\n", key
->has_gs
);
2349 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
2351 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
2352 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
2353 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
2354 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
2355 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
2358 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2359 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
2362 for (i
= 0 ; i
< key
->nr_images
; i
++)
2363 debug_printf("images[%i].format = %s\n", i
, util_format_name(image
[i
].image_state
.format
));
2368 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
2369 enum pipe_shader_type shader_stage
,
2371 uint32_t width
, uint32_t height
, uint32_t depth
,
2372 uint32_t first_level
, uint32_t last_level
,
2373 const void *base_ptr
,
2374 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
2375 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
2376 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
2379 struct draw_jit_texture
*jit_tex
;
2381 assert(shader_stage
== PIPE_SHADER_VERTEX
||
2382 shader_stage
== PIPE_SHADER_GEOMETRY
);
2384 if (shader_stage
== PIPE_SHADER_VERTEX
) {
2385 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->jit_context
.textures
));
2387 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
2388 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
2389 assert(sview_idx
< ARRAY_SIZE(draw
->llvm
->gs_jit_context
.textures
));
2391 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
2397 jit_tex
->width
= width
;
2398 jit_tex
->height
= height
;
2399 jit_tex
->depth
= depth
;
2400 jit_tex
->first_level
= first_level
;
2401 jit_tex
->last_level
= last_level
;
2402 jit_tex
->base
= base_ptr
;
2404 for (j
= first_level
; j
<= last_level
; j
++) {
2405 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
2406 jit_tex
->row_stride
[j
] = row_stride
[j
];
2407 jit_tex
->img_stride
[j
] = img_stride
[j
];
2412 draw_llvm_set_mapped_image(struct draw_context
*draw
,
2413 enum pipe_shader_type shader_stage
,
2415 uint32_t width
, uint32_t height
, uint32_t depth
,
2416 const void *base_ptr
,
2417 uint32_t row_stride
,
2418 uint32_t img_stride
)
2420 struct draw_jit_image
*jit_image
;
2422 assert(shader_stage
== PIPE_SHADER_VERTEX
||
2423 shader_stage
== PIPE_SHADER_GEOMETRY
);
2425 if (shader_stage
== PIPE_SHADER_VERTEX
) {
2426 assert(idx
< ARRAY_SIZE(draw
->llvm
->jit_context
.images
));
2428 jit_image
= &draw
->llvm
->jit_context
.images
[idx
];
2429 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
2430 assert(idx
< ARRAY_SIZE(draw
->llvm
->gs_jit_context
.images
));
2432 jit_image
= &draw
->llvm
->gs_jit_context
.images
[idx
];
2438 jit_image
->width
= width
;
2439 jit_image
->height
= height
;
2440 jit_image
->depth
= depth
;
2441 jit_image
->base
= base_ptr
;
2443 jit_image
->row_stride
= row_stride
;
2444 jit_image
->img_stride
= img_stride
;
2449 draw_llvm_set_sampler_state(struct draw_context
*draw
,
2450 enum pipe_shader_type shader_type
)
2454 if (shader_type
== PIPE_SHADER_VERTEX
) {
2455 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
2456 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
2458 if (draw
->samplers
[PIPE_SHADER_VERTEX
][i
]) {
2459 const struct pipe_sampler_state
*s
2460 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
2461 jit_sam
->min_lod
= s
->min_lod
;
2462 jit_sam
->max_lod
= s
->max_lod
;
2463 jit_sam
->lod_bias
= s
->lod_bias
;
2464 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2467 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
2468 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
2469 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
2471 if (draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]) {
2472 const struct pipe_sampler_state
*s
2473 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
2474 jit_sam
->min_lod
= s
->min_lod
;
2475 jit_sam
->max_lod
= s
->max_lod
;
2476 jit_sam
->lod_bias
= s
->lod_bias
;
2477 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
2485 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
2487 struct draw_llvm
*llvm
= variant
->llvm
;
2489 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2490 debug_printf("Deleting VS variant: %u vs variants,\t%u total variants\n",
2491 variant
->shader
->variants_cached
, llvm
->nr_variants
);
2494 gallivm_destroy(variant
->gallivm
);
2496 remove_from_list(&variant
->list_item_local
);
2497 variant
->shader
->variants_cached
--;
2498 remove_from_list(&variant
->list_item_global
);
2499 llvm
->nr_variants
--;
2505 * Create LLVM types for various structures.
2508 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
2510 struct gallivm_state
*gallivm
= var
->gallivm
;
2511 LLVMTypeRef texture_type
, sampler_type
, image_type
, context_type
;
2513 texture_type
= create_jit_texture_type(gallivm
, "texture");
2514 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
2515 image_type
= create_jit_image_type(gallivm
, "image");
2517 context_type
= create_gs_jit_context_type(gallivm
,
2518 var
->shader
->base
.vector_length
,
2519 texture_type
, sampler_type
,
2521 "draw_gs_jit_context");
2522 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
2524 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
2528 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
2530 if (!variant
->context_ptr_type
)
2531 create_gs_jit_types(variant
);
2532 return variant
->context_ptr_type
;
2536 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
2537 struct lp_type gs_type
)
2539 struct gallivm_state
*gallivm
= variant
->gallivm
;
2540 LLVMBuilderRef builder
= gallivm
->builder
;
2541 struct lp_type mask_type
= lp_int_type(gs_type
);
2542 LLVMValueRef num_prims
;
2543 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
, mask_type
, 0);
2546 num_prims
= lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, mask_type
),
2547 variant
->num_prims
);
2548 for (i
= 0; i
< gs_type
.length
; i
++) {
2549 LLVMValueRef idx
= lp_build_const_int32(gallivm
, i
);
2550 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, idx
, idx
, "");
2552 mask_val
= lp_build_compare(gallivm
, mask_type
,
2553 PIPE_FUNC_GREATER
, num_prims
, mask_val
);
2559 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
2560 struct draw_gs_llvm_variant
*variant
)
2562 struct gallivm_state
*gallivm
= variant
->gallivm
;
2563 LLVMContextRef context
= gallivm
->context
;
2564 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
2565 LLVMTypeRef arg_types
[7];
2566 LLVMTypeRef func_type
;
2567 LLVMValueRef variant_func
;
2568 LLVMValueRef context_ptr
;
2569 LLVMValueRef prim_id_ptr
;
2570 LLVMBasicBlockRef block
;
2571 LLVMBuilderRef builder
;
2572 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
2573 struct lp_build_sampler_soa
*sampler
= 0;
2574 struct lp_build_image_soa
*image
= NULL
;
2575 struct lp_build_context bld
;
2576 struct lp_bld_tgsi_system_values system_values
;
2578 struct lp_type gs_type
;
2580 struct draw_gs_llvm_iface gs_iface
;
2581 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
2582 LLVMValueRef consts_ptr
, num_consts_ptr
;
2583 LLVMValueRef ssbos_ptr
, num_ssbos_ptr
;
2584 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2585 struct lp_build_mask_context mask
;
2586 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
2587 unsigned vector_length
= variant
->shader
->base
.vector_length
;
2589 memset(&system_values
, 0, sizeof(system_values
));
2590 memset(&outputs
, 0, sizeof(outputs
));
2592 snprintf(func_name
, sizeof(func_name
), "draw_llvm_gs_variant%u",
2593 variant
->shader
->variants_cached
);
2595 assert(variant
->vertex_header_ptr_type
);
2597 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
2598 arg_types
[1] = variant
->input_array_type
; /* input */
2599 arg_types
[2] = LLVMPointerType(variant
->vertex_header_ptr_type
, 0); /* vertex_header */
2600 arg_types
[3] = int32_type
; /* num_prims */
2601 arg_types
[4] = int32_type
; /* instance_id */
2602 arg_types
[5] = LLVMPointerType(
2603 LLVMVectorType(int32_type
, vector_length
), 0); /* prim_id_ptr */
2604 arg_types
[6] = int32_type
;
2606 func_type
= LLVMFunctionType(int32_type
, arg_types
, ARRAY_SIZE(arg_types
), 0);
2608 variant_func
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
2610 variant
->function
= variant_func
;
2612 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
2614 for (i
= 0; i
< ARRAY_SIZE(arg_types
); ++i
)
2615 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
2616 lp_add_function_attr(variant_func
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
2618 context_ptr
= LLVMGetParam(variant_func
, 0);
2619 input_array
= LLVMGetParam(variant_func
, 1);
2620 io_ptr
= LLVMGetParam(variant_func
, 2);
2621 num_prims
= LLVMGetParam(variant_func
, 3);
2622 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
2623 prim_id_ptr
= LLVMGetParam(variant_func
, 5);
2624 system_values
.invocation_id
= LLVMGetParam(variant_func
, 6);
2626 lp_build_name(context_ptr
, "context");
2627 lp_build_name(input_array
, "input");
2628 lp_build_name(io_ptr
, "io");
2629 lp_build_name(num_prims
, "num_prims");
2630 lp_build_name(system_values
.instance_id
, "instance_id");
2631 lp_build_name(prim_id_ptr
, "prim_id_ptr");
2632 lp_build_name(system_values
.invocation_id
, "invocation_id");
2634 variant
->context_ptr
= context_ptr
;
2635 variant
->io_ptr
= io_ptr
;
2636 variant
->num_prims
= num_prims
;
2638 gs_iface
.base
.fetch_input
= draw_gs_llvm_fetch_input
;
2639 gs_iface
.base
.emit_vertex
= draw_gs_llvm_emit_vertex
;
2640 gs_iface
.base
.end_primitive
= draw_gs_llvm_end_primitive
;
2641 gs_iface
.base
.gs_epilogue
= draw_gs_llvm_epilogue
;
2642 gs_iface
.input
= input_array
;
2643 gs_iface
.variant
= variant
;
2649 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
2650 builder
= gallivm
->builder
;
2651 LLVMPositionBuilderAtEnd(builder
, block
);
2653 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
2655 memset(&gs_type
, 0, sizeof gs_type
);
2656 gs_type
.floating
= TRUE
; /* floating point values */
2657 gs_type
.sign
= TRUE
; /* values are signed */
2658 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
2659 gs_type
.width
= 32; /* 32-bit float */
2660 gs_type
.length
= vector_length
;
2662 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
2664 draw_gs_jit_context_num_constants(variant
->gallivm
, context_ptr
);
2666 ssbos_ptr
= draw_gs_jit_context_ssbos(variant
->gallivm
, context_ptr
);
2668 draw_gs_jit_context_num_ssbos(variant
->gallivm
, context_ptr
);
2670 /* code generated texture sampling */
2671 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
);
2672 image
= draw_llvm_image_soa_create(draw_gs_llvm_variant_key_images(&variant
->key
));
2673 mask_val
= generate_mask_value(variant
, gs_type
);
2674 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
2676 if (gs_info
->uses_primid
) {
2677 system_values
.prim_id
= LLVMBuildLoad(builder
, prim_id_ptr
, "prim_id");
2680 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2681 if (llvm
->draw
->gs
.geometry_shader
->state
.type
== PIPE_SHADER_IR_TGSI
)
2682 tgsi_dump(tokens
, 0);
2684 nir_print_shader(llvm
->draw
->gs
.geometry_shader
->state
.ir
.nir
, stderr
);
2685 draw_gs_llvm_dump_variant_key(&variant
->key
);
2688 struct lp_build_tgsi_params params
;
2689 memset(¶ms
, 0, sizeof(params
));
2691 params
.type
= gs_type
;
2692 params
.mask
= &mask
;
2693 params
.consts_ptr
= consts_ptr
;
2694 params
.const_sizes_ptr
= num_consts_ptr
;
2695 params
.system_values
= &system_values
;
2696 params
.context_ptr
= context_ptr
;
2697 params
.sampler
= sampler
;
2698 params
.info
= &llvm
->draw
->gs
.geometry_shader
->info
;
2699 params
.gs_iface
= (const struct lp_build_gs_iface
*)&gs_iface
;
2700 params
.ssbo_ptr
= ssbos_ptr
;
2701 params
.ssbo_sizes_ptr
= num_ssbos_ptr
;
2702 params
.image
= image
;
2704 if (llvm
->draw
->gs
.geometry_shader
->state
.type
== PIPE_SHADER_IR_TGSI
)
2705 lp_build_tgsi_soa(variant
->gallivm
,
2710 lp_build_nir_soa(variant
->gallivm
,
2711 llvm
->draw
->gs
.geometry_shader
->state
.ir
.nir
,
2715 sampler
->destroy(sampler
);
2716 image
->destroy(image
);
2718 lp_build_mask_end(&mask
);
2720 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
2722 gallivm_verify_function(gallivm
, variant_func
);
2726 struct draw_gs_llvm_variant
*
2727 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
2728 unsigned num_outputs
,
2729 const struct draw_gs_llvm_variant_key
*key
)
2731 struct draw_gs_llvm_variant
*variant
;
2732 struct llvm_geometry_shader
*shader
=
2733 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
2734 LLVMTypeRef vertex_header
;
2735 char module_name
[64];
2737 variant
= MALLOC(sizeof *variant
+
2738 shader
->variant_key_size
-
2739 sizeof variant
->key
);
2743 variant
->llvm
= llvm
;
2744 variant
->shader
= shader
;
2746 snprintf(module_name
, sizeof(module_name
), "draw_llvm_gs_variant%u",
2747 variant
->shader
->variants_cached
);
2749 variant
->gallivm
= gallivm_create(module_name
, llvm
->context
);
2751 create_gs_jit_types(variant
);
2753 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
2755 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
2757 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
2759 draw_gs_llvm_generate(llvm
, variant
);
2761 gallivm_compile_module(variant
->gallivm
);
2763 variant
->jit_func
= (draw_gs_jit_func
)
2764 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2766 gallivm_free_ir(variant
->gallivm
);
2768 variant
->list_item_global
.base
= variant
;
2769 variant
->list_item_local
.base
= variant
;
2770 /*variant->no = */shader
->variants_created
++;
2771 variant
->list_item_global
.base
= variant
;
2777 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2779 struct draw_llvm
*llvm
= variant
->llvm
;
2781 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
2782 debug_printf("Deleting GS variant: %u gs variants,\t%u total variants\n",
2783 variant
->shader
->variants_cached
, llvm
->nr_gs_variants
);
2786 gallivm_destroy(variant
->gallivm
);
2788 remove_from_list(&variant
->list_item_local
);
2789 variant
->shader
->variants_cached
--;
2790 remove_from_list(&variant
->list_item_global
);
2791 llvm
->nr_gs_variants
--;
2795 struct draw_gs_llvm_variant_key
*
2796 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2799 struct draw_gs_llvm_variant_key
*key
;
2800 struct draw_sampler_static_state
*draw_sampler
;
2801 struct draw_image_static_state
*draw_image
;
2803 key
= (struct draw_gs_llvm_variant_key
*)store
;
2805 memset(key
, 0, offsetof(struct draw_gs_llvm_variant_key
, samplers
[0]));
2807 key
->num_outputs
= draw_total_gs_outputs(llvm
->draw
);
2809 /* All variants of this shader will have the same value for
2810 * nr_samplers. Not yet trying to compact away holes in the
2813 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2814 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2815 key
->nr_sampler_views
=
2816 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2819 key
->nr_sampler_views
= key
->nr_samplers
;
2822 key
->nr_images
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_IMAGE
] + 1;
2824 draw_sampler
= key
->samplers
;
2826 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2828 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2829 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2830 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2832 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2833 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2834 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2837 draw_image
= draw_gs_llvm_variant_key_images(key
);
2838 memset(draw_image
, 0,
2839 key
->nr_images
* sizeof *draw_image
);
2840 for (i
= 0; i
< key
->nr_images
; i
++) {
2841 lp_sampler_static_texture_state_image(&draw_image
[i
].image_state
,
2842 llvm
->draw
->images
[PIPE_SHADER_GEOMETRY
][i
]);
2848 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2851 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2852 struct draw_image_static_state
*image
= draw_gs_llvm_variant_key_images(key
);
2853 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2854 debug_printf("sampler[%i].src_format = %s\n", i
,
2855 util_format_name(sampler
[i
].texture_state
.format
));
2858 for (i
= 0 ; i
< key
->nr_images
; i
++)
2859 debug_printf("images[%i].format = %s\n", i
, util_format_name(image
[i
].image_state
.format
));