draw/tess: fix TES patch vertices in.
[mesa.git] / src / gallium / auxiliary / draw / draw_llvm.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include "draw_llvm.h"
29
30 #include "draw_context.h"
31 #include "draw_vs.h"
32 #include "draw_gs.h"
33
34 #include "gallivm/lp_bld_arit.h"
35 #include "gallivm/lp_bld_arit_overflow.h"
36 #include "gallivm/lp_bld_bitarit.h"
37 #include "gallivm/lp_bld_gather.h"
38 #include "gallivm/lp_bld_logic.h"
39 #include "gallivm/lp_bld_const.h"
40 #include "gallivm/lp_bld_coro.h"
41 #include "gallivm/lp_bld_swizzle.h"
42 #include "gallivm/lp_bld_struct.h"
43 #include "gallivm/lp_bld_type.h"
44 #include "gallivm/lp_bld_flow.h"
45 #include "gallivm/lp_bld_debug.h"
46 #include "gallivm/lp_bld_tgsi.h"
47 #include "gallivm/lp_bld_nir.h"
48 #include "gallivm/lp_bld_printf.h"
49 #include "gallivm/lp_bld_intr.h"
50 #include "gallivm/lp_bld_init.h"
51 #include "gallivm/lp_bld_type.h"
52 #include "gallivm/lp_bld_pack.h"
53 #include "gallivm/lp_bld_format.h"
54
55 #include "tgsi/tgsi_exec.h"
56 #include "tgsi/tgsi_dump.h"
57
58 #include "util/u_math.h"
59 #include "util/u_pointer.h"
60 #include "util/u_string.h"
61 #include "util/simple_list.h"
62
63
64 #define DEBUG_STORE 0
65
66
67 static void
68 draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *var);
69
70
71 struct draw_gs_llvm_iface {
72 struct lp_build_gs_iface base;
73
74 struct draw_gs_llvm_variant *variant;
75 LLVMValueRef input;
76 };
77
78 static inline const struct draw_gs_llvm_iface *
79 draw_gs_llvm_iface(const struct lp_build_gs_iface *iface)
80 {
81 return (const struct draw_gs_llvm_iface *)iface;
82 }
83
84 struct draw_tcs_llvm_iface {
85 struct lp_build_tcs_iface base;
86
87 struct draw_tcs_llvm_variant *variant;
88 LLVMValueRef input;
89 LLVMValueRef output;
90 };
91
92 static inline const struct draw_tcs_llvm_iface *
93 draw_tcs_llvm_iface(const struct lp_build_tcs_iface *iface)
94 {
95 return (const struct draw_tcs_llvm_iface *)iface;
96 }
97
98 struct draw_tes_llvm_iface {
99 struct lp_build_tes_iface base;
100
101 struct draw_tes_llvm_variant *variant;
102 LLVMValueRef input;
103 };
104
105 static inline const struct draw_tes_llvm_iface *
106 draw_tes_llvm_iface(const struct lp_build_tes_iface *iface)
107 {
108 return (const struct draw_tes_llvm_iface *)iface;
109 }
110
111 /**
112 * Create LLVM type for draw_vertex_buffer.
113 */
114 static LLVMTypeRef
115 create_jit_dvbuffer_type(struct gallivm_state *gallivm,
116 const char *struct_name)
117 {
118 LLVMTargetDataRef target = gallivm->target;
119 LLVMTypeRef dvbuffer_type;
120 LLVMTypeRef elem_types[DRAW_JIT_DVBUFFER_NUM_FIELDS];
121 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
122
123 elem_types[DRAW_JIT_DVBUFFER_MAP] =
124 LLVMPointerType(LLVMIntTypeInContext(gallivm->context, 8), 0);
125 elem_types[DRAW_JIT_DVBUFFER_SIZE] = int32_type;
126
127 dvbuffer_type = LLVMStructTypeInContext(gallivm->context, elem_types,
128 ARRAY_SIZE(elem_types), 0);
129
130 (void) target; /* silence unused var warning for non-debug build */
131 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer, map,
132 target, dvbuffer_type,
133 DRAW_JIT_DVBUFFER_MAP);
134 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer, size,
135 target, dvbuffer_type,
136 DRAW_JIT_DVBUFFER_SIZE);
137
138 return dvbuffer_type;
139 }
140
141 /**
142 * Create LLVM type for struct draw_jit_texture
143 */
144 static LLVMTypeRef
145 create_jit_texture_type(struct gallivm_state *gallivm, const char *struct_name)
146 {
147 LLVMTargetDataRef target = gallivm->target;
148 LLVMTypeRef texture_type;
149 LLVMTypeRef elem_types[DRAW_JIT_TEXTURE_NUM_FIELDS];
150 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
151
152 elem_types[DRAW_JIT_TEXTURE_WIDTH] =
153 elem_types[DRAW_JIT_TEXTURE_HEIGHT] =
154 elem_types[DRAW_JIT_TEXTURE_DEPTH] =
155 elem_types[DRAW_JIT_TEXTURE_FIRST_LEVEL] =
156 elem_types[DRAW_JIT_TEXTURE_LAST_LEVEL] = int32_type;
157 elem_types[DRAW_JIT_TEXTURE_BASE] =
158 LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0);
159 elem_types[DRAW_JIT_TEXTURE_ROW_STRIDE] =
160 elem_types[DRAW_JIT_TEXTURE_IMG_STRIDE] =
161 elem_types[DRAW_JIT_TEXTURE_MIP_OFFSETS] =
162 LLVMArrayType(int32_type, PIPE_MAX_TEXTURE_LEVELS);
163
164 texture_type = LLVMStructTypeInContext(gallivm->context, elem_types,
165 ARRAY_SIZE(elem_types), 0);
166
167 (void) target; /* silence unused var warning for non-debug build */
168 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, width,
169 target, texture_type,
170 DRAW_JIT_TEXTURE_WIDTH);
171 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, height,
172 target, texture_type,
173 DRAW_JIT_TEXTURE_HEIGHT);
174 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, depth,
175 target, texture_type,
176 DRAW_JIT_TEXTURE_DEPTH);
177 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, base,
178 target, texture_type,
179 DRAW_JIT_TEXTURE_BASE);
180 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, row_stride,
181 target, texture_type,
182 DRAW_JIT_TEXTURE_ROW_STRIDE);
183 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, img_stride,
184 target, texture_type,
185 DRAW_JIT_TEXTURE_IMG_STRIDE);
186 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, first_level,
187 target, texture_type,
188 DRAW_JIT_TEXTURE_FIRST_LEVEL);
189 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, last_level,
190 target, texture_type,
191 DRAW_JIT_TEXTURE_LAST_LEVEL);
192 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, mip_offsets,
193 target, texture_type,
194 DRAW_JIT_TEXTURE_MIP_OFFSETS);
195
196 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture, target, texture_type);
197
198 return texture_type;
199 }
200
201
202 /**
203 * Create LLVM type for struct draw_jit_sampler
204 */
205 static LLVMTypeRef
206 create_jit_sampler_type(struct gallivm_state *gallivm, const char *struct_name)
207 {
208 LLVMTargetDataRef target = gallivm->target;
209 LLVMTypeRef sampler_type;
210 LLVMTypeRef elem_types[DRAW_JIT_SAMPLER_NUM_FIELDS];
211
212 elem_types[DRAW_JIT_SAMPLER_MIN_LOD] =
213 elem_types[DRAW_JIT_SAMPLER_MAX_LOD] =
214 elem_types[DRAW_JIT_SAMPLER_LOD_BIAS] = LLVMFloatTypeInContext(gallivm->context);
215 elem_types[DRAW_JIT_SAMPLER_BORDER_COLOR] =
216 LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4);
217
218 sampler_type = LLVMStructTypeInContext(gallivm->context, elem_types,
219 ARRAY_SIZE(elem_types), 0);
220
221 (void) target; /* silence unused var warning for non-debug build */
222 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, min_lod,
223 target, sampler_type,
224 DRAW_JIT_SAMPLER_MIN_LOD);
225 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, max_lod,
226 target, sampler_type,
227 DRAW_JIT_SAMPLER_MAX_LOD);
228 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, lod_bias,
229 target, sampler_type,
230 DRAW_JIT_SAMPLER_LOD_BIAS);
231 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, border_color,
232 target, sampler_type,
233 DRAW_JIT_SAMPLER_BORDER_COLOR);
234
235 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler, target, sampler_type);
236
237 return sampler_type;
238 }
239
240 /**
241 * Create LLVM type for struct draw_jit_texture
242 */
243 static LLVMTypeRef
244 create_jit_image_type(struct gallivm_state *gallivm, const char *struct_name)
245 {
246 LLVMTargetDataRef target = gallivm->target;
247 LLVMTypeRef image_type;
248 LLVMTypeRef elem_types[DRAW_JIT_IMAGE_NUM_FIELDS];
249 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
250
251 elem_types[DRAW_JIT_IMAGE_WIDTH] =
252 elem_types[DRAW_JIT_IMAGE_HEIGHT] =
253 elem_types[DRAW_JIT_IMAGE_DEPTH] =
254 elem_types[DRAW_JIT_IMAGE_ROW_STRIDE] =
255 elem_types[DRAW_JIT_IMAGE_IMG_STRIDE] = int32_type;
256 elem_types[DRAW_JIT_IMAGE_BASE] =
257 LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0);
258
259 image_type = LLVMStructTypeInContext(gallivm->context, elem_types,
260 ARRAY_SIZE(elem_types), 0);
261
262 (void) target; /* silence unused var warning for non-debug build */
263 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, width,
264 target, image_type,
265 DRAW_JIT_IMAGE_WIDTH);
266 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, height,
267 target, image_type,
268 DRAW_JIT_IMAGE_HEIGHT);
269 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, depth,
270 target, image_type,
271 DRAW_JIT_IMAGE_DEPTH);
272 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, base,
273 target, image_type,
274 DRAW_JIT_IMAGE_BASE);
275 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, row_stride,
276 target, image_type,
277 DRAW_JIT_IMAGE_ROW_STRIDE);
278 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, img_stride,
279 target, image_type,
280 DRAW_JIT_IMAGE_IMG_STRIDE);
281
282 LP_CHECK_STRUCT_SIZE(struct draw_jit_image, target, image_type);
283
284 return image_type;
285 }
286
287 /**
288 * Create LLVM type for struct draw_jit_context
289 */
290 static LLVMTypeRef
291 create_jit_context_type(struct gallivm_state *gallivm,
292 LLVMTypeRef texture_type, LLVMTypeRef sampler_type,
293 LLVMTypeRef image_type,
294 const char *struct_name)
295 {
296 LLVMTargetDataRef target = gallivm->target;
297 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
298 LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context);
299 LLVMTypeRef elem_types[DRAW_JIT_CTX_NUM_FIELDS];
300 LLVMTypeRef context_type;
301
302 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* vs_constants */
303 LP_MAX_TGSI_CONST_BUFFERS);
304 elem_types[1] = LLVMArrayType(int_type, /* num_vs_constants */
305 LP_MAX_TGSI_CONST_BUFFERS);
306 elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4),
307 DRAW_TOTAL_CLIP_PLANES), 0);
308 elem_types[3] = LLVMPointerType(float_type, 0); /* viewports */
309 elem_types[4] = LLVMArrayType(texture_type,
310 PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */
311 elem_types[5] = LLVMArrayType(sampler_type,
312 PIPE_MAX_SAMPLERS); /* samplers */
313 elem_types[6] = LLVMArrayType(image_type,
314 PIPE_MAX_SHADER_IMAGES); /* images */
315 elem_types[7] = LLVMArrayType(LLVMPointerType(int_type, 0), /* vs_ssbo */
316 LP_MAX_TGSI_SHADER_BUFFERS);
317 elem_types[8] = LLVMArrayType(int_type, /* num_vs_ssbos */
318 LP_MAX_TGSI_SHADER_BUFFERS);
319 context_type = LLVMStructTypeInContext(gallivm->context, elem_types,
320 ARRAY_SIZE(elem_types), 0);
321
322 (void) target; /* silence unused var warning for non-debug build */
323 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, vs_constants,
324 target, context_type, DRAW_JIT_CTX_CONSTANTS);
325 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, num_vs_constants,
326 target, context_type, DRAW_JIT_CTX_NUM_CONSTANTS);
327 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, planes,
328 target, context_type, DRAW_JIT_CTX_PLANES);
329 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, viewports,
330 target, context_type, DRAW_JIT_CTX_VIEWPORT);
331 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, textures,
332 target, context_type,
333 DRAW_JIT_CTX_TEXTURES);
334 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, samplers,
335 target, context_type,
336 DRAW_JIT_CTX_SAMPLERS);
337 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, images,
338 target, context_type, DRAW_JIT_CTX_IMAGES);
339 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, vs_ssbos,
340 target, context_type, DRAW_JIT_CTX_SSBOS);
341 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, num_vs_ssbos,
342 target, context_type, DRAW_JIT_CTX_NUM_SSBOS);
343 LP_CHECK_STRUCT_SIZE(struct draw_jit_context,
344 target, context_type);
345
346 return context_type;
347 }
348
349
350 /**
351 * Create LLVM type for struct draw_gs_jit_context
352 */
353 static LLVMTypeRef
354 create_gs_jit_context_type(struct gallivm_state *gallivm,
355 unsigned vector_length,
356 LLVMTypeRef texture_type, LLVMTypeRef sampler_type,
357 LLVMTypeRef image_type,
358 const char *struct_name)
359 {
360 LLVMTargetDataRef target = gallivm->target;
361 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
362 LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context);
363 LLVMTypeRef elem_types[DRAW_GS_JIT_CTX_NUM_FIELDS];
364 LLVMTypeRef context_type;
365
366 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* constants */
367 LP_MAX_TGSI_CONST_BUFFERS);
368 elem_types[1] = LLVMArrayType(int_type, /* num_constants */
369 LP_MAX_TGSI_CONST_BUFFERS);
370 elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4),
371 DRAW_TOTAL_CLIP_PLANES), 0);
372 elem_types[3] = LLVMPointerType(float_type, 0); /* viewports */
373
374 elem_types[4] = LLVMArrayType(texture_type,
375 PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */
376 elem_types[5] = LLVMArrayType(sampler_type,
377 PIPE_MAX_SAMPLERS); /* samplers */
378 elem_types[6] = LLVMArrayType(image_type,
379 PIPE_MAX_SHADER_IMAGES); /* images */
380 elem_types[7] = LLVMPointerType(LLVMPointerType(int_type, 0), 0);
381 elem_types[8] = LLVMPointerType(LLVMVectorType(int_type,
382 vector_length), 0);
383 elem_types[9] = LLVMPointerType(LLVMVectorType(int_type,
384 vector_length), 0);
385
386 elem_types[10] = LLVMArrayType(LLVMPointerType(int_type, 0), /* ssbos */
387 LP_MAX_TGSI_SHADER_BUFFERS);
388 elem_types[11] = LLVMArrayType(int_type, /* num_ssbos */
389 LP_MAX_TGSI_SHADER_BUFFERS);
390
391 context_type = LLVMStructTypeInContext(gallivm->context, elem_types,
392 ARRAY_SIZE(elem_types), 0);
393
394 (void) target; /* silence unused var warning for non-debug build */
395 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, constants,
396 target, context_type, DRAW_GS_JIT_CTX_CONSTANTS);
397 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, num_constants,
398 target, context_type, DRAW_GS_JIT_CTX_NUM_CONSTANTS);
399 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, planes,
400 target, context_type, DRAW_GS_JIT_CTX_PLANES);
401 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, viewports,
402 target, context_type, DRAW_GS_JIT_CTX_VIEWPORT);
403 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, textures,
404 target, context_type,
405 DRAW_GS_JIT_CTX_TEXTURES);
406 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, samplers,
407 target, context_type,
408 DRAW_GS_JIT_CTX_SAMPLERS);
409 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, prim_lengths,
410 target, context_type,
411 DRAW_GS_JIT_CTX_PRIM_LENGTHS);
412 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, emitted_vertices,
413 target, context_type,
414 DRAW_GS_JIT_CTX_EMITTED_VERTICES);
415 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, emitted_prims,
416 target, context_type,
417 DRAW_GS_JIT_CTX_EMITTED_PRIMS);
418 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, ssbos,
419 target, context_type, DRAW_GS_JIT_CTX_SSBOS);
420 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, num_ssbos,
421 target, context_type, DRAW_GS_JIT_CTX_NUM_SSBOS);
422 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, images,
423 target, context_type, DRAW_GS_JIT_CTX_IMAGES);
424 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context,
425 target, context_type);
426
427 return context_type;
428 }
429
430
431 static LLVMTypeRef
432 create_gs_jit_input_type(struct gallivm_state *gallivm)
433 {
434 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
435 LLVMTypeRef input_array;
436
437 input_array = LLVMVectorType(float_type, TGSI_NUM_CHANNELS); /* num primitives */
438 input_array = LLVMArrayType(input_array, TGSI_NUM_CHANNELS); /* num channels */
439 input_array = LLVMArrayType(input_array, PIPE_MAX_SHADER_INPUTS); /* num attrs per vertex */
440 input_array = LLVMPointerType(input_array, 0); /* num vertices per prim */
441
442 return input_array;
443 }
444
445 /**
446 * Create LLVM type for struct pipe_vertex_buffer
447 */
448 static LLVMTypeRef
449 create_jit_vertex_buffer_type(struct gallivm_state *gallivm,
450 const char *struct_name)
451 {
452 LLVMTargetDataRef target = gallivm->target;
453 LLVMTypeRef elem_types[4];
454 LLVMTypeRef vb_type;
455
456 elem_types[0] = LLVMInt16TypeInContext(gallivm->context);
457 elem_types[1] = LLVMInt8TypeInContext(gallivm->context);
458 elem_types[2] = LLVMInt32TypeInContext(gallivm->context);
459 elem_types[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0);
460
461 vb_type = LLVMStructTypeInContext(gallivm->context, elem_types,
462 ARRAY_SIZE(elem_types), 0);
463
464 (void) target; /* silence unused var warning for non-debug build */
465 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, stride,
466 target, vb_type, 0);
467 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, is_user_buffer,
468 target, vb_type, 1);
469 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, buffer_offset,
470 target, vb_type, 2);
471 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, buffer.resource,
472 target, vb_type, 3);
473
474 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer, target, vb_type);
475
476 return vb_type;
477 }
478
479
480 /**
481 * Create LLVM type for struct vertex_header;
482 */
483 static LLVMTypeRef
484 create_jit_vertex_header(struct gallivm_state *gallivm, int data_elems)
485 {
486 LLVMTargetDataRef target = gallivm->target;
487 LLVMTypeRef elem_types[3];
488 LLVMTypeRef vertex_header;
489 char struct_name[24];
490
491 snprintf(struct_name, 23, "vertex_header%d", data_elems);
492
493 elem_types[DRAW_JIT_VERTEX_VERTEX_ID] = LLVMIntTypeInContext(gallivm->context, 32);
494 elem_types[DRAW_JIT_VERTEX_CLIP_POS] = LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4);
495 elem_types[DRAW_JIT_VERTEX_DATA] = LLVMArrayType(elem_types[1], data_elems);
496
497 vertex_header = LLVMStructTypeInContext(gallivm->context, elem_types,
498 ARRAY_SIZE(elem_types), 0);
499
500 /* these are bit-fields and we can't take address of them
501 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
502 target, vertex_header,
503 DRAW_JIT_VERTEX_CLIPMASK);
504 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
505 target, vertex_header,
506 DRAW_JIT_VERTEX_EDGEFLAG);
507 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
508 target, vertex_header,
509 DRAW_JIT_VERTEX_PAD);
510 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
511 target, vertex_header,
512 DRAW_JIT_VERTEX_VERTEX_ID);
513 */
514 (void) target; /* silence unused var warning for non-debug build */
515 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clip_pos,
516 target, vertex_header,
517 DRAW_JIT_VERTEX_CLIP_POS);
518 LP_CHECK_MEMBER_OFFSET(struct vertex_header, data,
519 target, vertex_header,
520 DRAW_JIT_VERTEX_DATA);
521
522 assert(LLVMABISizeOfType(target, vertex_header) ==
523 offsetof(struct vertex_header, data[data_elems]));
524
525 return vertex_header;
526 }
527
528 /**
529 * Create LLVM type for struct draw_tcs_jit_context
530 */
531 static LLVMTypeRef
532 create_tcs_jit_context_type(struct gallivm_state *gallivm,
533 unsigned vector_length,
534 LLVMTypeRef texture_type, LLVMTypeRef sampler_type,
535 LLVMTypeRef image_type,
536 const char *struct_name)
537 {
538 LLVMTargetDataRef target = gallivm->target;
539 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
540 LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context);
541 LLVMTypeRef elem_types[DRAW_TCS_JIT_CTX_NUM_FIELDS];
542 LLVMTypeRef context_type;
543
544 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* constants */
545 LP_MAX_TGSI_CONST_BUFFERS);
546 elem_types[1] = LLVMArrayType(int_type, /* num_constants */
547 LP_MAX_TGSI_CONST_BUFFERS);
548 elem_types[2] = LLVMInt32TypeInContext(gallivm->context);
549 elem_types[3] = LLVMInt32TypeInContext(gallivm->context);
550
551 elem_types[4] = LLVMArrayType(texture_type,
552 PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */
553 elem_types[5] = LLVMArrayType(sampler_type,
554 PIPE_MAX_SAMPLERS); /* samplers */
555 elem_types[6] = LLVMArrayType(image_type,
556 PIPE_MAX_SHADER_IMAGES); /* images */
557
558 elem_types[7] = LLVMArrayType(LLVMPointerType(int_type, 0), /* ssbos */
559 LP_MAX_TGSI_SHADER_BUFFERS);
560 elem_types[8] = LLVMArrayType(int_type, /* num_ssbos */
561 LP_MAX_TGSI_SHADER_BUFFERS);
562
563 context_type = LLVMStructTypeInContext(gallivm->context, elem_types,
564 ARRAY_SIZE(elem_types), 0);
565
566 (void) target; /* silence unused var warning for non-debug build */
567 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, constants,
568 target, context_type, DRAW_TCS_JIT_CTX_CONSTANTS);
569 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, num_constants,
570 target, context_type, DRAW_TCS_JIT_CTX_NUM_CONSTANTS);
571 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, textures,
572 target, context_type,
573 DRAW_TCS_JIT_CTX_TEXTURES);
574 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, samplers,
575 target, context_type,
576 DRAW_TCS_JIT_CTX_SAMPLERS);
577 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, ssbos,
578 target, context_type, DRAW_TCS_JIT_CTX_SSBOS);
579 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, num_ssbos,
580 target, context_type, DRAW_TCS_JIT_CTX_NUM_SSBOS);
581 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, images,
582 target, context_type, DRAW_TCS_JIT_CTX_IMAGES);
583 LP_CHECK_STRUCT_SIZE(struct draw_tcs_jit_context,
584 target, context_type);
585
586 return context_type;
587 }
588
589 static LLVMTypeRef
590 create_tcs_jit_input_type(struct gallivm_state *gallivm)
591 {
592 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
593 LLVMTypeRef input_array;
594
595 input_array = LLVMArrayType(float_type, TGSI_NUM_CHANNELS); /* num channels */
596 input_array = LLVMArrayType(input_array, NUM_TCS_INPUTS); /* num attrs per vertex */
597 input_array = LLVMPointerType(input_array, 0); /* num vertices per prim */
598
599 return input_array;
600 }
601
602 static LLVMTypeRef
603 create_tcs_jit_output_type(struct gallivm_state *gallivm)
604 {
605 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
606 LLVMTypeRef output_array;
607
608 output_array = LLVMArrayType(float_type, TGSI_NUM_CHANNELS); /* num channels */
609 output_array = LLVMArrayType(output_array, PIPE_MAX_SHADER_INPUTS); /* num attrs per vertex */
610 output_array = LLVMPointerType(output_array, 0); /* num vertices per prim */
611
612 return output_array;
613 }
614
615 static LLVMTypeRef
616 create_tes_jit_input_type(struct gallivm_state *gallivm)
617 {
618 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
619 LLVMTypeRef input_array;
620
621 input_array = LLVMArrayType(float_type, TGSI_NUM_CHANNELS); /* num channels */
622 input_array = LLVMArrayType(input_array, PIPE_MAX_SHADER_INPUTS); /* num attrs per vertex */
623 input_array = LLVMPointerType(input_array, 0); /* num vertices per prim */
624
625 return input_array;
626 }
627
628 /**
629 * Create LLVM type for struct draw_tes_jit_context
630 */
631 static LLVMTypeRef
632 create_tes_jit_context_type(struct gallivm_state *gallivm,
633 unsigned vector_length,
634 LLVMTypeRef texture_type, LLVMTypeRef sampler_type,
635 LLVMTypeRef image_type,
636 const char *struct_name)
637 {
638 LLVMTargetDataRef target = gallivm->target;
639 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
640 LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context);
641 LLVMTypeRef elem_types[DRAW_TCS_JIT_CTX_NUM_FIELDS];
642 LLVMTypeRef context_type;
643
644 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* constants */
645 LP_MAX_TGSI_CONST_BUFFERS);
646 elem_types[1] = LLVMArrayType(int_type, /* num_constants */
647 LP_MAX_TGSI_CONST_BUFFERS);
648 elem_types[2] = LLVMInt32TypeInContext(gallivm->context);
649 elem_types[3] = LLVMInt32TypeInContext(gallivm->context);
650
651 elem_types[4] = LLVMArrayType(texture_type,
652 PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */
653 elem_types[5] = LLVMArrayType(sampler_type,
654 PIPE_MAX_SAMPLERS); /* samplers */
655 elem_types[6] = LLVMArrayType(image_type,
656 PIPE_MAX_SHADER_IMAGES); /* images */
657
658 elem_types[7] = LLVMArrayType(LLVMPointerType(int_type, 0), /* ssbos */
659 LP_MAX_TGSI_SHADER_BUFFERS);
660 elem_types[8] = LLVMArrayType(int_type, /* num_ssbos */
661 LP_MAX_TGSI_SHADER_BUFFERS);
662
663 context_type = LLVMStructTypeInContext(gallivm->context, elem_types,
664 ARRAY_SIZE(elem_types), 0);
665
666 (void) target; /* silence unused var warning for non-debug build */
667 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, constants,
668 target, context_type, DRAW_TCS_JIT_CTX_CONSTANTS);
669 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, num_constants,
670 target, context_type, DRAW_TCS_JIT_CTX_NUM_CONSTANTS);
671 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, textures,
672 target, context_type,
673 DRAW_TCS_JIT_CTX_TEXTURES);
674 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, samplers,
675 target, context_type,
676 DRAW_TCS_JIT_CTX_SAMPLERS);
677 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, ssbos,
678 target, context_type, DRAW_TCS_JIT_CTX_SSBOS);
679 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, num_ssbos,
680 target, context_type, DRAW_TCS_JIT_CTX_NUM_SSBOS);
681 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, images,
682 target, context_type, DRAW_TCS_JIT_CTX_IMAGES);
683 LP_CHECK_STRUCT_SIZE(struct draw_tes_jit_context,
684 target, context_type);
685
686 return context_type;
687 }
688
689 /**
690 * Create LLVM types for various structures.
691 */
692 static void
693 create_jit_types(struct draw_llvm_variant *variant)
694 {
695 struct gallivm_state *gallivm = variant->gallivm;
696 LLVMTypeRef texture_type, sampler_type, context_type, buffer_type,
697 vb_type, image_type;
698
699 texture_type = create_jit_texture_type(gallivm, "texture");
700 sampler_type = create_jit_sampler_type(gallivm, "sampler");
701 image_type = create_jit_image_type(gallivm, "image");
702
703 context_type = create_jit_context_type(gallivm, texture_type, sampler_type,
704 image_type,
705 "draw_jit_context");
706 variant->context_ptr_type = LLVMPointerType(context_type, 0);
707
708 buffer_type = create_jit_dvbuffer_type(gallivm, "draw_vertex_buffer");
709 variant->buffer_ptr_type = LLVMPointerType(buffer_type, 0);
710
711 vb_type = create_jit_vertex_buffer_type(gallivm, "pipe_vertex_buffer");
712 variant->vb_ptr_type = LLVMPointerType(vb_type, 0);
713 }
714
715
716 static LLVMTypeRef
717 get_context_ptr_type(struct draw_llvm_variant *variant)
718 {
719 if (!variant->context_ptr_type)
720 create_jit_types(variant);
721 return variant->context_ptr_type;
722 }
723
724
725 static LLVMTypeRef
726 get_buffer_ptr_type(struct draw_llvm_variant *variant)
727 {
728 if (!variant->buffer_ptr_type)
729 create_jit_types(variant);
730 return variant->buffer_ptr_type;
731 }
732
733
734 static LLVMTypeRef
735 get_vb_ptr_type(struct draw_llvm_variant *variant)
736 {
737 if (!variant->vb_ptr_type)
738 create_jit_types(variant);
739 return variant->vb_ptr_type;
740 }
741
742 static LLVMTypeRef
743 get_vertex_header_ptr_type(struct draw_llvm_variant *variant)
744 {
745 if (!variant->vertex_header_ptr_type)
746 create_jit_types(variant);
747 return variant->vertex_header_ptr_type;
748 }
749
750
751 /**
752 * Create per-context LLVM info.
753 */
754 struct draw_llvm *
755 draw_llvm_create(struct draw_context *draw, LLVMContextRef context)
756 {
757 struct draw_llvm *llvm;
758
759 if (!lp_build_init())
760 return NULL;
761
762 llvm = CALLOC_STRUCT( draw_llvm );
763 if (!llvm)
764 return NULL;
765
766 llvm->draw = draw;
767
768 llvm->context = context;
769 if (!llvm->context) {
770 llvm->context = LLVMContextCreate();
771 llvm->context_owned = true;
772 }
773 if (!llvm->context)
774 goto fail;
775
776 llvm->nr_variants = 0;
777 make_empty_list(&llvm->vs_variants_list);
778
779 llvm->nr_gs_variants = 0;
780 make_empty_list(&llvm->gs_variants_list);
781
782 llvm->nr_tcs_variants = 0;
783 make_empty_list(&llvm->tcs_variants_list);
784
785 llvm->nr_tes_variants = 0;
786 make_empty_list(&llvm->tes_variants_list);
787
788 return llvm;
789
790 fail:
791 draw_llvm_destroy(llvm);
792 return NULL;
793 }
794
795
796 /**
797 * Free per-context LLVM info.
798 */
799 void
800 draw_llvm_destroy(struct draw_llvm *llvm)
801 {
802 if (llvm->context_owned)
803 LLVMContextDispose(llvm->context);
804 llvm->context = NULL;
805
806 /* XXX free other draw_llvm data? */
807 FREE(llvm);
808 }
809
810
811 /**
812 * Create LLVM-generated code for a vertex shader.
813 */
814 struct draw_llvm_variant *
815 draw_llvm_create_variant(struct draw_llvm *llvm,
816 unsigned num_inputs,
817 const struct draw_llvm_variant_key *key)
818 {
819 struct draw_llvm_variant *variant;
820 struct llvm_vertex_shader *shader =
821 llvm_vertex_shader(llvm->draw->vs.vertex_shader);
822 LLVMTypeRef vertex_header;
823 char module_name[64];
824
825 variant = MALLOC(sizeof *variant +
826 shader->variant_key_size -
827 sizeof variant->key);
828 if (!variant)
829 return NULL;
830
831 variant->llvm = llvm;
832 variant->shader = shader;
833
834 snprintf(module_name, sizeof(module_name), "draw_llvm_vs_variant%u",
835 variant->shader->variants_cached);
836
837 variant->gallivm = gallivm_create(module_name, llvm->context);
838
839 create_jit_types(variant);
840
841 memcpy(&variant->key, key, shader->variant_key_size);
842
843 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
844 if (llvm->draw->vs.vertex_shader->state.type == PIPE_SHADER_IR_TGSI)
845 tgsi_dump(llvm->draw->vs.vertex_shader->state.tokens, 0);
846 else
847 nir_print_shader(llvm->draw->vs.vertex_shader->state.ir.nir, stderr);
848 draw_llvm_dump_variant_key(&variant->key);
849 }
850
851 vertex_header = create_jit_vertex_header(variant->gallivm, num_inputs);
852
853 variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0);
854
855 draw_llvm_generate(llvm, variant);
856
857 gallivm_compile_module(variant->gallivm);
858
859 variant->jit_func = (draw_jit_vert_func)
860 gallivm_jit_function(variant->gallivm, variant->function);
861
862 gallivm_free_ir(variant->gallivm);
863
864 variant->list_item_global.base = variant;
865 variant->list_item_local.base = variant;
866 /*variant->no = */shader->variants_created++;
867 variant->list_item_global.base = variant;
868
869 return variant;
870 }
871
872
873 static void
874 generate_vs(struct draw_llvm_variant *variant,
875 LLVMBuilderRef builder,
876 struct lp_type vs_type,
877 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
878 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS],
879 const struct lp_bld_tgsi_system_values *system_values,
880 LLVMValueRef context_ptr,
881 const struct lp_build_sampler_soa *draw_sampler,
882 const struct lp_build_image_soa *draw_image,
883 boolean clamp_vertex_color,
884 struct lp_build_mask_context *bld_mask)
885 {
886 struct draw_llvm *llvm = variant->llvm;
887 const struct tgsi_token *tokens = llvm->draw->vs.vertex_shader->state.tokens;
888 LLVMValueRef consts_ptr =
889 draw_jit_context_vs_constants(variant->gallivm, context_ptr);
890 LLVMValueRef num_consts_ptr =
891 draw_jit_context_num_vs_constants(variant->gallivm, context_ptr);
892 LLVMValueRef ssbos_ptr =
893 draw_jit_context_vs_ssbos(variant->gallivm, context_ptr);
894 LLVMValueRef num_ssbos_ptr =
895 draw_jit_context_num_vs_ssbos(variant->gallivm, context_ptr);
896
897 struct lp_build_tgsi_params params;
898 memset(&params, 0, sizeof(params));
899
900 params.type = vs_type;
901 params.mask = bld_mask;
902 params.consts_ptr = consts_ptr;
903 params.const_sizes_ptr = num_consts_ptr;
904 params.system_values = system_values;
905 params.inputs = inputs;
906 params.context_ptr = context_ptr;
907 params.sampler = draw_sampler;
908 params.info = &llvm->draw->vs.vertex_shader->info;
909 params.ssbo_ptr = ssbos_ptr;
910 params.ssbo_sizes_ptr = num_ssbos_ptr;
911 params.image = draw_image;
912
913 if (llvm->draw->vs.vertex_shader->state.ir.nir &&
914 llvm->draw->vs.vertex_shader->state.type == PIPE_SHADER_IR_NIR)
915 lp_build_nir_soa(variant->gallivm,
916 llvm->draw->vs.vertex_shader->state.ir.nir,
917 &params,
918 outputs);
919 else
920 lp_build_tgsi_soa(variant->gallivm,
921 tokens,
922 &params,
923 outputs);
924
925 {
926 LLVMValueRef out;
927 unsigned chan, attrib;
928 struct lp_build_context bld;
929 struct tgsi_shader_info* info = &llvm->draw->vs.vertex_shader->info;
930 lp_build_context_init(&bld, variant->gallivm, vs_type);
931
932 for (attrib = 0; attrib < info->num_outputs; ++attrib) {
933 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
934 if (outputs[attrib][chan]) {
935 switch (info->output_semantic_name[attrib]) {
936 case TGSI_SEMANTIC_COLOR:
937 case TGSI_SEMANTIC_BCOLOR:
938 if (clamp_vertex_color) {
939 out = LLVMBuildLoad(builder, outputs[attrib][chan], "");
940 out = lp_build_clamp(&bld, out, bld.zero, bld.one);
941 LLVMBuildStore(builder, out, outputs[attrib][chan]);
942 }
943 break;
944 }
945 }
946 }
947 }
948 }
949 }
950
951
952 static void
953 fetch_instanced(struct gallivm_state *gallivm,
954 const struct util_format_description *format_desc,
955 struct lp_type vs_type,
956 LLVMValueRef vb_stride,
957 LLVMValueRef map_ptr,
958 LLVMValueRef buffer_size_adj,
959 LLVMValueRef *inputs,
960 LLVMValueRef index)
961 {
962 LLVMTypeRef i32_t = LLVMInt32TypeInContext(gallivm->context);
963 LLVMTypeRef aosf_t, aosi_t;
964 LLVMValueRef zero = LLVMConstNull(i32_t);
965 LLVMBuilderRef builder = gallivm->builder;
966 LLVMValueRef stride, buffer_overflowed, aos, index_valid;
967 unsigned i;
968
969 aosf_t = lp_build_vec_type(gallivm, lp_float32_vec4_type());
970 aosi_t = lp_build_vec_type(gallivm, lp_int32_vec4_type());
971
972 /* This mul can overflow. Wraparound is ok. */
973 stride = LLVMBuildMul(builder, vb_stride, index, "");
974
975 buffer_overflowed = LLVMBuildICmp(builder, LLVMIntUGE,
976 stride, buffer_size_adj,
977 "buffer_overflowed");
978
979 if (0) {
980 lp_build_print_value(gallivm, " instance index = ", index);
981 lp_build_print_value(gallivm, " buffer overflowed = ", buffer_overflowed);
982 }
983
984 index_valid = LLVMBuildNot(builder, buffer_overflowed, "");
985 index_valid = LLVMBuildSExt(builder, index_valid, i32_t, "");
986 stride = LLVMBuildAnd(builder, stride, index_valid, "");
987
988 aos = lp_build_fetch_rgba_aos(gallivm,
989 format_desc,
990 lp_float32_vec4_type(),
991 FALSE,
992 map_ptr,
993 stride, zero, zero,
994 NULL);
995
996 index_valid = lp_build_broadcast(gallivm, aosi_t, index_valid);
997 aos = LLVMBuildBitCast(builder, aos, aosi_t, "");
998 aos = LLVMBuildAnd(builder, aos, index_valid, "");
999 aos = LLVMBuildBitCast(builder, aos, aosf_t, "");
1000
1001 for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
1002 LLVMValueRef index = lp_build_const_int32(gallivm, i);
1003 inputs[i] = lp_build_extract_broadcast(gallivm,
1004 lp_float32_vec4_type(),
1005 vs_type, aos, index);
1006 }
1007 }
1008
1009
1010 static void
1011 fetch_vector(struct gallivm_state *gallivm,
1012 const struct util_format_description *format_desc,
1013 struct lp_type vs_type,
1014 LLVMValueRef vb_stride,
1015 LLVMValueRef map_ptr,
1016 LLVMValueRef buffer_size_adj,
1017 LLVMValueRef *inputs,
1018 LLVMValueRef indices)
1019 {
1020 LLVMBuilderRef builder = gallivm->builder;
1021 struct lp_build_context blduivec;
1022 struct lp_type fetch_type = vs_type;
1023 LLVMValueRef offset, valid_mask;
1024 unsigned i;
1025
1026 lp_build_context_init(&blduivec, gallivm, lp_uint_type(vs_type));
1027
1028 vb_stride = lp_build_broadcast_scalar(&blduivec, vb_stride);
1029 buffer_size_adj = lp_build_broadcast_scalar(&blduivec, buffer_size_adj);
1030
1031 /* This mul can overflow. Wraparound is ok. */
1032 offset = lp_build_mul(&blduivec, vb_stride, indices);
1033
1034 valid_mask = lp_build_compare(gallivm, blduivec.type,
1035 PIPE_FUNC_LESS, offset, buffer_size_adj);
1036
1037 /* not valid elements use offset 0 */
1038 offset = LLVMBuildAnd(builder, offset, valid_mask, "");
1039
1040 if (0) {
1041 lp_build_print_value(gallivm, " indices = ", indices);
1042 lp_build_print_value(gallivm, " offsets = ", offset);
1043 lp_build_print_value(gallivm, " valid_mask = ", valid_mask);
1044 }
1045
1046 /*
1047 * Unlike fetch_instanced, use SoA fetch instead of multiple AoS fetches.
1048 * This should always produce better code.
1049 */
1050
1051 /* The type handling is annoying here... */
1052 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB &&
1053 format_desc->channel[0].pure_integer) {
1054 if (format_desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
1055 fetch_type = lp_type_int_vec(vs_type.width, vs_type.width * vs_type.length);
1056 }
1057 else if (format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED) {
1058 fetch_type = lp_type_uint_vec(vs_type.width, vs_type.width * vs_type.length);
1059 }
1060 }
1061
1062 lp_build_fetch_rgba_soa(gallivm, format_desc,
1063 fetch_type, FALSE, map_ptr, offset,
1064 blduivec.zero, blduivec.zero,
1065 NULL, inputs);
1066
1067 for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
1068 inputs[i] = LLVMBuildBitCast(builder, inputs[i],
1069 lp_build_vec_type(gallivm, vs_type), "");
1070 }
1071
1072 /* out-of-bound fetches return all zeros */
1073 for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
1074 inputs[i] = LLVMBuildBitCast(builder, inputs[i], blduivec.vec_type, "");
1075 inputs[i] = LLVMBuildAnd(builder, inputs[i], valid_mask, "");
1076 inputs[i] = LLVMBuildBitCast(builder, inputs[i],
1077 lp_build_vec_type(gallivm, vs_type), "");
1078 }
1079 }
1080
1081
1082 static void
1083 store_aos(struct gallivm_state *gallivm,
1084 LLVMValueRef io_ptr,
1085 LLVMValueRef index,
1086 LLVMValueRef value)
1087 {
1088 LLVMTypeRef data_ptr_type = LLVMPointerType(lp_build_vec_type(gallivm, lp_float32_vec4_type()), 0);
1089 LLVMBuilderRef builder = gallivm->builder;
1090 LLVMValueRef data_ptr = draw_jit_header_data(gallivm, io_ptr);
1091 LLVMValueRef indices[3];
1092
1093 indices[0] = lp_build_const_int32(gallivm, 0);
1094 indices[1] = index;
1095 indices[2] = lp_build_const_int32(gallivm, 0);
1096
1097 data_ptr = LLVMBuildGEP(builder, data_ptr, indices, 3, "");
1098 data_ptr = LLVMBuildPointerCast(builder, data_ptr, data_ptr_type, "");
1099
1100 #if DEBUG_STORE
1101 lp_build_printf(gallivm, " ---- %p storing attribute %d (io = %p)\n", data_ptr, index, io_ptr);
1102 #endif
1103
1104 /* Unaligned store due to the vertex header */
1105 LLVMSetAlignment(LLVMBuildStore(builder, value, data_ptr), sizeof(float));
1106 }
1107
1108 /**
1109 * Adjust the mask to architecture endianess. The mask will the store in struct:
1110 *
1111 * struct vertex_header {
1112 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
1113 * unsigned edgeflag:1;
1114 * unsigned pad:1;
1115 * unsigned vertex_id:16;
1116 * [...]
1117 * }
1118 *
1119 * On little-endian machine nothing needs to done, however on bit-endian machine
1120 * the mask's fields need to be adjusted with the algorithm:
1121 *
1122 * uint32_t reverse (uint32_t x)
1123 * {
1124 * return (x >> 16) | // vertex_id
1125 * ((x & 0x3fff) << 18) | // clipmask
1126 * ((x & 0x4000) << 3) | // pad
1127 * ((x & 0x8000) << 1); // edgeflag
1128 * }
1129 */
1130 static LLVMValueRef
1131 adjust_mask(struct gallivm_state *gallivm,
1132 LLVMValueRef mask)
1133 {
1134 #if UTIL_ARCH_BIG_ENDIAN
1135 LLVMBuilderRef builder = gallivm->builder;
1136 LLVMValueRef vertex_id;
1137 LLVMValueRef clipmask;
1138 LLVMValueRef pad;
1139 LLVMValueRef edgeflag;
1140
1141 vertex_id = LLVMBuildLShr(builder, mask, lp_build_const_int32(gallivm, 16), "");
1142 clipmask = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x3fff), "");
1143 clipmask = LLVMBuildShl(builder, clipmask, lp_build_const_int32(gallivm, 18), "");
1144 if (0) {
1145 pad = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x4000), "");
1146 pad = LLVMBuildShl(builder, pad, lp_build_const_int32(gallivm, 3), "");
1147 }
1148 edgeflag = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x8000), "");
1149 edgeflag = LLVMBuildShl(builder, edgeflag, lp_build_const_int32(gallivm, 1), "");
1150
1151 mask = LLVMBuildOr(builder, vertex_id, clipmask, "");
1152 if (0) {
1153 mask = LLVMBuildOr(builder, mask, pad, "");
1154 }
1155 mask = LLVMBuildOr(builder, mask, edgeflag, "");
1156 #endif
1157 return mask;
1158 }
1159
1160 static void
1161 store_aos_array(struct gallivm_state *gallivm,
1162 struct lp_type soa_type,
1163 LLVMValueRef io_ptr,
1164 LLVMValueRef *indices,
1165 LLVMValueRef* aos,
1166 int attrib,
1167 int num_outputs,
1168 LLVMValueRef clipmask,
1169 boolean need_edgeflag)
1170 {
1171 LLVMBuilderRef builder = gallivm->builder;
1172 LLVMValueRef attr_index = lp_build_const_int32(gallivm, attrib);
1173 LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32];
1174 LLVMValueRef linear_inds[LP_MAX_VECTOR_WIDTH / 32];
1175 LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32];
1176 int vector_length = soa_type.length;
1177 int i;
1178
1179 debug_assert(TGSI_NUM_CHANNELS == 4);
1180
1181 for (i = 0; i < vector_length; i++) {
1182 linear_inds[i] = lp_build_const_int32(gallivm, i);
1183 if (indices) {
1184 inds[i] = indices[i];
1185 } else {
1186 inds[i] = linear_inds[i];
1187 }
1188 io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, "");
1189 }
1190
1191 if (attrib == 0) {
1192 /* store vertex header for each of the n vertices */
1193 LLVMValueRef val, cliptmp;
1194 int vertex_id_pad_edgeflag;
1195
1196 /* If this assertion fails, it means we need to update the bit twidding
1197 * code here. See struct vertex_header in draw_private.h.
1198 */
1199 assert(DRAW_TOTAL_CLIP_PLANES==14);
1200 /* initialize vertex id:16 = 0xffff, pad:1 = 0, edgeflag:1 = 1 */
1201 if (!need_edgeflag) {
1202 vertex_id_pad_edgeflag = (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES);
1203 }
1204 else {
1205 vertex_id_pad_edgeflag = (0xffff << 16);
1206 }
1207 val = lp_build_const_int_vec(gallivm, lp_int_type(soa_type),
1208 vertex_id_pad_edgeflag);
1209 /* OR with the clipmask */
1210 cliptmp = LLVMBuildOr(builder, val, clipmask, "");
1211 for (i = 0; i < vector_length; i++) {
1212 LLVMValueRef id_ptr = draw_jit_header_id(gallivm, io_ptrs[i]);
1213 val = LLVMBuildExtractElement(builder, cliptmp, linear_inds[i], "");
1214 val = adjust_mask(gallivm, val);
1215 #if DEBUG_STORE
1216 lp_build_printf(gallivm, "io = %p, index %d, clipmask = %x\n",
1217 io_ptrs[i], inds[i], val);
1218 #endif
1219 LLVMBuildStore(builder, val, id_ptr);
1220 }
1221 }
1222
1223 /* store for each of the n vertices */
1224 for (i = 0; i < vector_length; i++) {
1225 store_aos(gallivm, io_ptrs[i], attr_index, aos[i]);
1226 }
1227 }
1228
1229
1230 static void
1231 convert_to_aos(struct gallivm_state *gallivm,
1232 LLVMValueRef io,
1233 LLVMValueRef *indices,
1234 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
1235 LLVMValueRef clipmask,
1236 int num_outputs,
1237 struct lp_type soa_type,
1238 boolean need_edgeflag)
1239 {
1240 LLVMBuilderRef builder = gallivm->builder;
1241 unsigned chan, attrib, i;
1242
1243 #if DEBUG_STORE
1244 lp_build_printf(gallivm, " # storing begin\n");
1245 #endif
1246 for (attrib = 0; attrib < num_outputs; ++attrib) {
1247 LLVMValueRef soa[TGSI_NUM_CHANNELS];
1248 LLVMValueRef aos[LP_MAX_VECTOR_WIDTH / 32];
1249 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
1250 if (outputs[attrib][chan]) {
1251 LLVMValueRef out = LLVMBuildLoad(builder, outputs[attrib][chan], "");
1252 lp_build_name(out, "output%u.%c", attrib, "xyzw"[chan]);
1253 #if DEBUG_STORE
1254 lp_build_printf(gallivm, "output %d : %d ",
1255 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context),
1256 attrib, 0),
1257 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context),
1258 chan, 0));
1259 lp_build_print_value(gallivm, "val = ", out);
1260 {
1261 LLVMValueRef iv =
1262 LLVMBuildBitCast(builder, out, lp_build_int_vec_type(gallivm, soa_type), "");
1263
1264 lp_build_print_value(gallivm, " ival = ", iv);
1265 }
1266 #endif
1267 soa[chan] = out;
1268 }
1269 else {
1270 soa[chan] = 0;
1271 }
1272 }
1273
1274
1275 if (soa_type.length == TGSI_NUM_CHANNELS) {
1276 lp_build_transpose_aos(gallivm, soa_type, soa, aos);
1277 } else {
1278 lp_build_transpose_aos(gallivm, soa_type, soa, soa);
1279
1280 for (i = 0; i < soa_type.length; ++i) {
1281 aos[i] = lp_build_extract_range(gallivm,
1282 soa[i % TGSI_NUM_CHANNELS],
1283 (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS,
1284 TGSI_NUM_CHANNELS);
1285 }
1286 }
1287
1288 store_aos_array(gallivm,
1289 soa_type,
1290 io, indices,
1291 aos,
1292 attrib,
1293 num_outputs,
1294 clipmask,
1295 need_edgeflag);
1296 }
1297 #if DEBUG_STORE
1298 lp_build_printf(gallivm, " # storing end\n");
1299 #endif
1300 }
1301
1302
1303 /**
1304 * Stores original vertex positions in clip coordinates
1305 */
1306 static void
1307 store_clip(struct gallivm_state *gallivm,
1308 const struct lp_type vs_type,
1309 LLVMValueRef io_ptr,
1310 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
1311 int idx)
1312 {
1313 LLVMBuilderRef builder = gallivm->builder;
1314 LLVMValueRef soa[4];
1315 LLVMValueRef aos[LP_MAX_VECTOR_LENGTH];
1316 LLVMValueRef indices[2];
1317 LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32];
1318 LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32];
1319 LLVMValueRef clip_ptrs[LP_MAX_VECTOR_WIDTH / 32];
1320 LLVMTypeRef clip_ptr_type =
1321 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context),
1322 4), 0);
1323 int i, j;
1324
1325 indices[0] =
1326 indices[1] = lp_build_const_int32(gallivm, 0);
1327
1328 for (i = 0; i < vs_type.length; i++) {
1329 inds[i] = lp_build_const_int32(gallivm, i);
1330 io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, "");
1331 }
1332
1333 soa[0] = LLVMBuildLoad(builder, outputs[idx][0], ""); /*x0 x1 .. xn*/
1334 soa[1] = LLVMBuildLoad(builder, outputs[idx][1], ""); /*y0 y1 .. yn*/
1335 soa[2] = LLVMBuildLoad(builder, outputs[idx][2], ""); /*z0 z1 .. zn*/
1336 soa[3] = LLVMBuildLoad(builder, outputs[idx][3], ""); /*w0 w1 .. wn*/
1337
1338 for (i = 0; i < vs_type.length; i++) {
1339 clip_ptrs[i] = draw_jit_header_clip_pos(gallivm, io_ptrs[i]);
1340 }
1341
1342 lp_build_transpose_aos(gallivm, vs_type, soa, soa);
1343 for (i = 0; i < vs_type.length; ++i) {
1344 aos[i] = lp_build_extract_range(gallivm,
1345 soa[i % TGSI_NUM_CHANNELS],
1346 (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS,
1347 TGSI_NUM_CHANNELS);
1348 }
1349
1350 for (j = 0; j < vs_type.length; j++) {
1351 LLVMValueRef clip_ptr;
1352
1353 clip_ptr = LLVMBuildGEP(builder, clip_ptrs[j], indices, 2, "clipo");
1354 clip_ptr = LLVMBuildPointerCast(builder, clip_ptr, clip_ptr_type, "");
1355
1356 /* Unaligned store */
1357 LLVMSetAlignment(LLVMBuildStore(builder, aos[j], clip_ptr), sizeof(float));
1358 }
1359 }
1360
1361
1362 /**
1363 * Transforms the outputs for viewport mapping
1364 */
1365 static void
1366 generate_viewport(struct draw_llvm_variant *variant,
1367 LLVMBuilderRef builder,
1368 struct lp_type vs_type,
1369 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
1370 LLVMValueRef context_ptr)
1371 {
1372 int i;
1373 struct gallivm_state *gallivm = variant->gallivm;
1374 struct lp_type f32_type = vs_type;
1375 const unsigned pos = variant->llvm->draw->vs.position_output;
1376 LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type);
1377 LLVMValueRef out3 = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 .. wn*/
1378 LLVMValueRef const1 = lp_build_const_vec(gallivm, f32_type, 1.0); /*1.0 1.0 1.0 1.0*/
1379 LLVMValueRef vp_ptr = draw_jit_context_viewports(gallivm, context_ptr);
1380
1381 /* We treat pipe_viewport_state as a float array */
1382 const int scale_index_offset = offsetof(struct pipe_viewport_state, scale) / sizeof(float);
1383 const int trans_index_offset = offsetof(struct pipe_viewport_state, translate) / sizeof(float);
1384
1385 /* for 1/w convention*/
1386 out3 = LLVMBuildFDiv(builder, const1, out3, "");
1387 LLVMBuildStore(builder, out3, outputs[pos][3]);
1388
1389 /* Viewport Mapping */
1390 for (i=0; i<3; i++) {
1391 LLVMValueRef out = LLVMBuildLoad(builder, outputs[pos][i], ""); /*x0 x1 .. xn*/
1392 LLVMValueRef scale;
1393 LLVMValueRef trans;
1394 LLVMValueRef scale_i;
1395 LLVMValueRef trans_i;
1396 LLVMValueRef index;
1397
1398 index = lp_build_const_int32(gallivm, i + scale_index_offset);
1399 scale_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, "");
1400
1401 index = lp_build_const_int32(gallivm, i + trans_index_offset);
1402 trans_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, "");
1403
1404 scale = lp_build_broadcast(gallivm, vs_type_llvm,
1405 LLVMBuildLoad(builder, scale_i, "scale"));
1406 trans = lp_build_broadcast(gallivm, vs_type_llvm,
1407 LLVMBuildLoad(builder, trans_i, "trans"));
1408
1409 /* divide by w */
1410 out = LLVMBuildFMul(builder, out, out3, "");
1411 /* mult by scale, add translation */
1412 out = lp_build_fmuladd(builder, out, scale, trans);
1413
1414 /* store transformed outputs */
1415 LLVMBuildStore(builder, out, outputs[pos][i]);
1416 }
1417
1418 }
1419
1420
1421 /**
1422 * Returns clipmask as nxi32 bitmask for the n vertices
1423 */
1424 static LLVMValueRef
1425 generate_clipmask(struct draw_llvm *llvm,
1426 struct gallivm_state *gallivm,
1427 struct lp_type vs_type,
1428 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
1429 struct draw_llvm_variant_key *key,
1430 LLVMValueRef context_ptr,
1431 boolean *have_clipdist)
1432 {
1433 LLVMBuilderRef builder = gallivm->builder;
1434 LLVMValueRef mask; /* stores the <nxi32> clipmasks */
1435 LLVMValueRef test, temp;
1436 LLVMValueRef zero, shift;
1437 LLVMValueRef pos_x, pos_y, pos_z, pos_w;
1438 LLVMValueRef cv_x, cv_y, cv_z, cv_w;
1439 LLVMValueRef plane1, planes, plane_ptr, sum;
1440 struct lp_type f32_type = vs_type;
1441 struct lp_type i32_type = lp_int_type(vs_type);
1442 const unsigned pos = llvm->draw->vs.position_output;
1443 const unsigned cv = llvm->draw->vs.clipvertex_output;
1444 int num_written_clipdistance = llvm->draw->vs.vertex_shader->info.num_written_clipdistance;
1445 boolean have_cd = false;
1446 boolean clip_user = key->clip_user;
1447 unsigned ucp_enable = key->ucp_enable;
1448 unsigned cd[2];
1449
1450 cd[0] = llvm->draw->vs.ccdistance_output[0];
1451 cd[1] = llvm->draw->vs.ccdistance_output[1];
1452
1453 if (cd[0] != pos || cd[1] != pos)
1454 have_cd = true;
1455
1456 if (num_written_clipdistance && !clip_user) {
1457 clip_user = true;
1458 ucp_enable = (1 << num_written_clipdistance) - 1;
1459 }
1460
1461 mask = lp_build_const_int_vec(gallivm, i32_type, 0);
1462 temp = lp_build_const_int_vec(gallivm, i32_type, 0);
1463 zero = lp_build_const_vec(gallivm, f32_type, 0); /* 0.0f 0.0f 0.0f 0.0f */
1464 shift = lp_build_const_int_vec(gallivm, i32_type, 1); /* 1 1 1 1 */
1465
1466 /*
1467 * load clipvertex and position from correct locations.
1468 * if they are the same just load them once.
1469 */
1470 pos_x = LLVMBuildLoad(builder, outputs[pos][0], ""); /*x0 x1 .. xn */
1471 pos_y = LLVMBuildLoad(builder, outputs[pos][1], ""); /*y0 y1 .. yn */
1472 pos_z = LLVMBuildLoad(builder, outputs[pos][2], ""); /*z0 z1 .. zn */
1473 pos_w = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 .. wn */
1474
1475 if (clip_user && cv != pos) {
1476 cv_x = LLVMBuildLoad(builder, outputs[cv][0], ""); /*x0 x1 .. xn */
1477 cv_y = LLVMBuildLoad(builder, outputs[cv][1], ""); /*y0 y1 .. yn */
1478 cv_z = LLVMBuildLoad(builder, outputs[cv][2], ""); /*z0 z1 .. zn */
1479 cv_w = LLVMBuildLoad(builder, outputs[cv][3], ""); /*w0 w1 .. wn */
1480 } else {
1481 cv_x = pos_x;
1482 cv_y = pos_y;
1483 cv_z = pos_z;
1484 cv_w = pos_w;
1485 }
1486
1487 /*
1488 * Be careful with the comparisons and NaNs (using llvm's unordered
1489 * comparisons here).
1490 */
1491 /* Cliptest, for hardwired planes */
1492 /*
1493 * XXX should take guardband into account (currently not in key).
1494 * Otherwise might run the draw pipeline stages for nothing.
1495 */
1496 if (key->clip_xy) {
1497 /* plane 1 */
1498 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_x , pos_w);
1499 temp = shift;
1500 test = LLVMBuildAnd(builder, test, temp, "");
1501 mask = test;
1502
1503 /* plane 2 */
1504 test = LLVMBuildFAdd(builder, pos_x, pos_w, "");
1505 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
1506 temp = LLVMBuildShl(builder, temp, shift, "");
1507 test = LLVMBuildAnd(builder, test, temp, "");
1508 mask = LLVMBuildOr(builder, mask, test, "");
1509
1510 /* plane 3 */
1511 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_y, pos_w);
1512 temp = LLVMBuildShl(builder, temp, shift, "");
1513 test = LLVMBuildAnd(builder, test, temp, "");
1514 mask = LLVMBuildOr(builder, mask, test, "");
1515
1516 /* plane 4 */
1517 test = LLVMBuildFAdd(builder, pos_y, pos_w, "");
1518 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
1519 temp = LLVMBuildShl(builder, temp, shift, "");
1520 test = LLVMBuildAnd(builder, test, temp, "");
1521 mask = LLVMBuildOr(builder, mask, test, "");
1522 }
1523
1524 if (key->clip_z) {
1525 temp = lp_build_const_int_vec(gallivm, i32_type, 16);
1526 if (key->clip_halfz) {
1527 /* plane 5 */
1528 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, pos_z);
1529 test = LLVMBuildAnd(builder, test, temp, "");
1530 mask = LLVMBuildOr(builder, mask, test, "");
1531 }
1532 else {
1533 /* plane 5 */
1534 test = LLVMBuildFAdd(builder, pos_z, pos_w, "");
1535 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
1536 test = LLVMBuildAnd(builder, test, temp, "");
1537 mask = LLVMBuildOr(builder, mask, test, "");
1538 }
1539 /* plane 6 */
1540 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_z, pos_w);
1541 temp = LLVMBuildShl(builder, temp, shift, "");
1542 test = LLVMBuildAnd(builder, test, temp, "");
1543 mask = LLVMBuildOr(builder, mask, test, "");
1544 }
1545
1546 if (clip_user) {
1547 LLVMValueRef planes_ptr = draw_jit_context_planes(gallivm, context_ptr);
1548 LLVMValueRef indices[3];
1549 LLVMValueRef is_nan_or_inf;
1550
1551 /* userclip planes */
1552 while (ucp_enable) {
1553 unsigned plane_idx = ffs(ucp_enable)-1;
1554 ucp_enable &= ~(1 << plane_idx);
1555 plane_idx += 6;
1556
1557 if (have_cd && num_written_clipdistance) {
1558 LLVMValueRef clipdist;
1559 int i;
1560 i = plane_idx - 6;
1561
1562 *have_clipdist = TRUE;
1563 if (i < 4) {
1564 clipdist = LLVMBuildLoad(builder, outputs[cd[0]][i], "");
1565 } else {
1566 clipdist = LLVMBuildLoad(builder, outputs[cd[1]][i-4], "");
1567 }
1568 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, clipdist);
1569 is_nan_or_inf = lp_build_is_inf_or_nan(gallivm, vs_type, clipdist);
1570 test = LLVMBuildOr(builder, test, is_nan_or_inf, "");
1571 temp = lp_build_const_int_vec(gallivm, i32_type, 1LL << plane_idx);
1572 test = LLVMBuildAnd(builder, test, temp, "");
1573 mask = LLVMBuildOr(builder, mask, test, "");
1574 } else {
1575 LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type);
1576 indices[0] = lp_build_const_int32(gallivm, 0);
1577 indices[1] = lp_build_const_int32(gallivm, plane_idx);
1578
1579 indices[2] = lp_build_const_int32(gallivm, 0);
1580 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1581 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_x");
1582 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1583 sum = LLVMBuildFMul(builder, planes, cv_x, "");
1584
1585 indices[2] = lp_build_const_int32(gallivm, 1);
1586 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1587 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_y");
1588 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1589 sum = lp_build_fmuladd(builder, planes, cv_y, sum);
1590
1591 indices[2] = lp_build_const_int32(gallivm, 2);
1592 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1593 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_z");
1594 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1595 sum = lp_build_fmuladd(builder, planes, cv_z, sum);
1596
1597 indices[2] = lp_build_const_int32(gallivm, 3);
1598 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1599 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_w");
1600 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1601 sum = lp_build_fmuladd(builder, planes, cv_w, sum);
1602
1603 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, sum);
1604 temp = lp_build_const_int_vec(gallivm, i32_type, 1LL << plane_idx);
1605 test = LLVMBuildAnd(builder, test, temp, "");
1606 mask = LLVMBuildOr(builder, mask, test, "");
1607 }
1608 }
1609 }
1610 if (key->need_edgeflags) {
1611 /*
1612 * This isn't really part of clipmask but stored the same in vertex
1613 * header later, so do it here.
1614 */
1615 unsigned edge_attr = llvm->draw->vs.edgeflag_output;
1616 LLVMValueRef one = lp_build_const_vec(gallivm, f32_type, 1.0);
1617 LLVMValueRef edgeflag = LLVMBuildLoad(builder, outputs[edge_attr][0], "");
1618 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_EQUAL, one, edgeflag);
1619 temp = lp_build_const_int_vec(gallivm, i32_type,
1620 1LL << DRAW_TOTAL_CLIP_PLANES);
1621 test = LLVMBuildAnd(builder, test, temp, "");
1622 mask = LLVMBuildOr(builder, mask, test, "");
1623 }
1624 return mask;
1625 }
1626
1627
1628 /**
1629 * Returns boolean if any clipping has occurred
1630 * Used zero/one i8 value to represent boolean
1631 */
1632 static LLVMValueRef
1633 clipmask_booli8(struct gallivm_state *gallivm,
1634 const struct lp_type vs_type,
1635 LLVMValueRef clipmask_bool_ptr,
1636 boolean edgeflag_in_clipmask)
1637 {
1638 LLVMBuilderRef builder = gallivm->builder;
1639 LLVMTypeRef int8_type = LLVMInt8TypeInContext(gallivm->context);
1640 LLVMValueRef clipmask_bool = LLVMBuildLoad(builder, clipmask_bool_ptr, "");
1641 LLVMValueRef ret;
1642 struct lp_build_context bldivec;
1643
1644 lp_build_context_init(&bldivec, gallivm, lp_int_type(vs_type));
1645
1646 /*
1647 * We need to invert the edgeflag bit from the clipmask here
1648 * (because the result is really if we want to run the pipeline or not
1649 * and we (may) need it if edgeflag was 0).
1650 */
1651 if (edgeflag_in_clipmask) {
1652 LLVMValueRef edge = lp_build_const_int_vec(gallivm, bldivec.type,
1653 1LL << DRAW_TOTAL_CLIP_PLANES);
1654 clipmask_bool = LLVMBuildXor(builder, clipmask_bool, edge, "");
1655 }
1656
1657 /*
1658 * XXX: probably should mask off bits from the mask which come from
1659 * vertices which were beyond the count (i.e. indices_valid for
1660 * linear fetches, for elts ones we don't have the correct mask
1661 * right now). Otherwise might run the pipeline for nothing,
1662 * though everything should still work.
1663 */
1664 ret = lp_build_any_true_range(&bldivec, vs_type.length, clipmask_bool);
1665 ret = LLVMBuildZExt(builder, ret, int8_type, "");
1666 return ret;
1667 }
1668
1669 static LLVMValueRef
1670 draw_gs_llvm_fetch_input(const struct lp_build_gs_iface *gs_iface,
1671 struct lp_build_context * bld,
1672 boolean is_vindex_indirect,
1673 LLVMValueRef vertex_index,
1674 boolean is_aindex_indirect,
1675 LLVMValueRef attrib_index,
1676 LLVMValueRef swizzle_index)
1677 {
1678 const struct draw_gs_llvm_iface *gs = draw_gs_llvm_iface(gs_iface);
1679 struct gallivm_state *gallivm = bld->gallivm;
1680 LLVMBuilderRef builder = gallivm->builder;
1681 LLVMValueRef indices[3];
1682 LLVMValueRef res;
1683 struct lp_type type = bld->type;
1684
1685 if (is_vindex_indirect || is_aindex_indirect) {
1686 int i;
1687 res = bld->zero;
1688 for (i = 0; i < type.length; ++i) {
1689 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
1690 LLVMValueRef vert_chan_index = vertex_index;
1691 LLVMValueRef attr_chan_index = attrib_index;
1692 LLVMValueRef channel_vec, value;
1693
1694 if (is_vindex_indirect) {
1695 vert_chan_index = LLVMBuildExtractElement(builder,
1696 vertex_index, idx, "");
1697 }
1698 if (is_aindex_indirect) {
1699 attr_chan_index = LLVMBuildExtractElement(builder,
1700 attrib_index, idx, "");
1701 }
1702
1703 indices[0] = vert_chan_index;
1704 indices[1] = attr_chan_index;
1705 indices[2] = swizzle_index;
1706
1707 channel_vec = LLVMBuildGEP(builder, gs->input, indices, 3, "");
1708 channel_vec = LLVMBuildLoad(builder, channel_vec, "");
1709 value = LLVMBuildExtractElement(builder, channel_vec, idx, "");
1710
1711 res = LLVMBuildInsertElement(builder, res, value, idx, "");
1712 }
1713 } else {
1714 indices[0] = vertex_index;
1715 indices[1] = attrib_index;
1716 indices[2] = swizzle_index;
1717
1718 res = LLVMBuildGEP(builder, gs->input, indices, 3, "");
1719 res = LLVMBuildLoad(builder, res, "");
1720 }
1721
1722 return res;
1723 }
1724
1725 static void
1726 draw_gs_llvm_emit_vertex(const struct lp_build_gs_iface *gs_base,
1727 struct lp_build_context * bld,
1728 LLVMValueRef (*outputs)[4],
1729 LLVMValueRef emitted_vertices_vec,
1730 LLVMValueRef stream_id)
1731 {
1732 const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base);
1733 struct draw_gs_llvm_variant *variant = gs_iface->variant;
1734 struct gallivm_state *gallivm = variant->gallivm;
1735 LLVMBuilderRef builder = gallivm->builder;
1736 struct lp_type gs_type = bld->type;
1737 LLVMValueRef clipmask = lp_build_const_int_vec(gallivm,
1738 lp_int_type(gs_type), 0);
1739 LLVMValueRef indices[LP_MAX_VECTOR_LENGTH];
1740 LLVMValueRef next_prim_offset =
1741 lp_build_const_int32(gallivm, variant->shader->base.primitive_boundary);
1742 LLVMValueRef io = variant->io_ptr;
1743 unsigned i;
1744 const struct tgsi_shader_info *gs_info = &variant->shader->base.info;
1745
1746 for (i = 0; i < gs_type.length; ++i) {
1747 LLVMValueRef ind = lp_build_const_int32(gallivm, i);
1748 LLVMValueRef currently_emitted =
1749 LLVMBuildExtractElement(builder, emitted_vertices_vec, ind, "");
1750 indices[i] = LLVMBuildMul(builder, ind, next_prim_offset, "");
1751 indices[i] = LLVMBuildAdd(builder, indices[i], currently_emitted, "");
1752 }
1753
1754 LLVMValueRef stream_idx = LLVMBuildExtractElement(builder, stream_id, lp_build_const_int32(gallivm, 0), "");
1755 LLVMValueRef cnd = LLVMBuildICmp(builder, LLVMIntULT, stream_idx, lp_build_const_int32(gallivm, variant->shader->base.num_vertex_streams), "");
1756 struct lp_build_if_state if_ctx;
1757 lp_build_if(&if_ctx, gallivm, cnd);
1758 io = lp_build_pointer_get(builder, io, LLVMBuildExtractElement(builder, stream_id, lp_build_const_int32(gallivm, 0), ""));
1759
1760 convert_to_aos(gallivm, io, indices,
1761 outputs, clipmask,
1762 gs_info->num_outputs, gs_type,
1763 FALSE);
1764 lp_build_endif(&if_ctx);
1765 }
1766
1767 static void
1768 draw_gs_llvm_end_primitive(const struct lp_build_gs_iface *gs_base,
1769 struct lp_build_context * bld,
1770 LLVMValueRef total_emitted_vertices_vec_ptr,
1771 LLVMValueRef verts_per_prim_vec,
1772 LLVMValueRef emitted_prims_vec,
1773 LLVMValueRef mask_vec)
1774 {
1775 const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base);
1776 struct draw_gs_llvm_variant *variant = gs_iface->variant;
1777 struct gallivm_state *gallivm = variant->gallivm;
1778 LLVMBuilderRef builder = gallivm->builder;
1779 LLVMValueRef prim_lengts_ptr =
1780 draw_gs_jit_prim_lengths(variant->gallivm, variant->context_ptr);
1781 unsigned i;
1782
1783 for (i = 0; i < bld->type.length; ++i) {
1784 LLVMValueRef ind = lp_build_const_int32(gallivm, i);
1785 LLVMValueRef prims_emitted =
1786 LLVMBuildExtractElement(builder, emitted_prims_vec, ind, "");
1787 LLVMValueRef store_ptr;
1788 LLVMValueRef num_vertices =
1789 LLVMBuildExtractElement(builder, verts_per_prim_vec, ind, "");
1790
1791 store_ptr = LLVMBuildGEP(builder, prim_lengts_ptr, &prims_emitted, 1, "");
1792 store_ptr = LLVMBuildLoad(builder, store_ptr, "");
1793 store_ptr = LLVMBuildGEP(builder, store_ptr, &ind, 1, "");
1794 LLVMBuildStore(builder, num_vertices, store_ptr);
1795 }
1796 }
1797
1798 static void
1799 draw_gs_llvm_epilogue(const struct lp_build_gs_iface *gs_base,
1800 LLVMValueRef total_emitted_vertices_vec,
1801 LLVMValueRef emitted_prims_vec, unsigned stream)
1802 {
1803 const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base);
1804 struct draw_gs_llvm_variant *variant = gs_iface->variant;
1805 struct gallivm_state *gallivm = variant->gallivm;
1806 LLVMBuilderRef builder = gallivm->builder;
1807 LLVMValueRef emitted_verts_ptr =
1808 draw_gs_jit_emitted_vertices(gallivm, variant->context_ptr);
1809 LLVMValueRef emitted_prims_ptr =
1810 draw_gs_jit_emitted_prims(gallivm, variant->context_ptr);
1811 LLVMValueRef stream_val = lp_build_const_int32(gallivm, stream);
1812
1813 emitted_verts_ptr = LLVMBuildGEP(builder, emitted_verts_ptr, &stream_val, 1, "");
1814 emitted_prims_ptr = LLVMBuildGEP(builder, emitted_prims_ptr, &stream_val, 1, "");
1815
1816 LLVMBuildStore(builder, total_emitted_vertices_vec, emitted_verts_ptr);
1817 LLVMBuildStore(builder, emitted_prims_vec, emitted_prims_ptr);
1818 }
1819
1820 static void
1821 draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *variant)
1822 {
1823 struct gallivm_state *gallivm = variant->gallivm;
1824 LLVMContextRef context = gallivm->context;
1825 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context);
1826 LLVMTypeRef arg_types[12];
1827 unsigned num_arg_types = ARRAY_SIZE(arg_types);
1828 LLVMTypeRef func_type;
1829 LLVMValueRef context_ptr;
1830 LLVMBasicBlockRef block;
1831 LLVMBuilderRef builder;
1832 char func_name[64];
1833 struct lp_type vs_type;
1834 LLVMValueRef count, fetch_elts, start_or_maxelt;
1835 LLVMValueRef vertex_id_offset;
1836 LLVMValueRef stride, step, io_itr;
1837 LLVMValueRef ind_vec, start_vec, have_elts, fetch_max, tmp;
1838 LLVMValueRef io_ptr, vbuffers_ptr, vb_ptr;
1839 LLVMValueRef vb_stride[PIPE_MAX_ATTRIBS];
1840 LLVMValueRef map_ptr[PIPE_MAX_ATTRIBS];
1841 LLVMValueRef buffer_size_adj[PIPE_MAX_ATTRIBS];
1842 LLVMValueRef instance_index[PIPE_MAX_ATTRIBS];
1843 LLVMValueRef fake_buf_ptr, fake_buf;
1844
1845 struct draw_context *draw = llvm->draw;
1846 const struct tgsi_shader_info *vs_info = &draw->vs.vertex_shader->info;
1847 unsigned i, j;
1848 struct lp_build_context bld, blduivec;
1849 struct lp_build_loop_state lp_loop;
1850 struct lp_build_if_state if_ctx;
1851 const int vector_length = lp_native_vector_width / 32;
1852 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
1853 struct lp_build_sampler_soa *sampler = 0;
1854 struct lp_build_image_soa *image = NULL;
1855 LLVMValueRef ret, clipmask_bool_ptr;
1856 struct draw_llvm_variant_key *key = &variant->key;
1857 /* If geometry shader is present we need to skip both the viewport
1858 * transformation and clipping otherwise the inputs to the geometry
1859 * shader will be incorrect.
1860 * The code can't handle vp transform when vs writes vp index neither
1861 * (though this would be fixable here, but couldn't just broadcast
1862 * the values).
1863 */
1864 const boolean bypass_viewport = key->has_gs_or_tes || key->bypass_viewport ||
1865 vs_info->writes_viewport_index;
1866 const boolean enable_cliptest = !key->has_gs_or_tes && (key->clip_xy ||
1867 key->clip_z ||
1868 key->clip_user ||
1869 key->need_edgeflags);
1870 LLVMValueRef variant_func;
1871 const unsigned pos = draw->vs.position_output;
1872 const unsigned cv = draw->vs.clipvertex_output;
1873 boolean have_clipdist = FALSE;
1874 struct lp_bld_tgsi_system_values system_values;
1875
1876 memset(&system_values, 0, sizeof(system_values));
1877 memset(&outputs, 0, sizeof(outputs));
1878 snprintf(func_name, sizeof(func_name), "draw_llvm_vs_variant%u",
1879 variant->shader->variants_cached);
1880
1881 i = 0;
1882 arg_types[i++] = get_context_ptr_type(variant); /* context */
1883 arg_types[i++] = get_vertex_header_ptr_type(variant); /* vertex_header */
1884 arg_types[i++] = get_buffer_ptr_type(variant); /* vbuffers */
1885 arg_types[i++] = int32_type; /* count */
1886 arg_types[i++] = int32_type; /* start/fetch_elt_max */
1887 arg_types[i++] = int32_type; /* stride */
1888 arg_types[i++] = get_vb_ptr_type(variant); /* pipe_vertex_buffer's */
1889 arg_types[i++] = int32_type; /* instance_id */
1890 arg_types[i++] = int32_type; /* vertex_id_offset */
1891 arg_types[i++] = int32_type; /* start_instance */
1892 arg_types[i++] = LLVMPointerType(int32_type, 0); /* fetch_elts */
1893 arg_types[i++] = int32_type; /* draw_id */
1894
1895 func_type = LLVMFunctionType(LLVMInt8TypeInContext(context),
1896 arg_types, num_arg_types, 0);
1897
1898 variant_func = LLVMAddFunction(gallivm->module, func_name, func_type);
1899 variant->function = variant_func;
1900
1901 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv);
1902 for (i = 0; i < num_arg_types; ++i)
1903 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
1904 lp_add_function_attr(variant_func, i + 1, LP_FUNC_ATTR_NOALIAS);
1905
1906 context_ptr = LLVMGetParam(variant_func, 0);
1907 io_ptr = LLVMGetParam(variant_func, 1);
1908 vbuffers_ptr = LLVMGetParam(variant_func, 2);
1909 count = LLVMGetParam(variant_func, 3);
1910 /*
1911 * XXX: the maxelt part is unused. Not really useful, since we cannot
1912 * get index buffer overflows due to vsplit (which provides its own
1913 * elts buffer, with a different size than what's passed in here).
1914 */
1915 start_or_maxelt = LLVMGetParam(variant_func, 4);
1916 /*
1917 * XXX: stride is actually unused. The stride we use is strictly calculated
1918 * from the number of outputs (including the draw_extra outputs).
1919 * Should probably fix some day (we need a new vs just because of extra
1920 * outputs which the generated vs won't touch).
1921 */
1922 stride = LLVMGetParam(variant_func, 5);
1923 vb_ptr = LLVMGetParam(variant_func, 6);
1924 system_values.instance_id = LLVMGetParam(variant_func, 7);
1925 vertex_id_offset = LLVMGetParam(variant_func, 8);
1926 system_values.base_instance = LLVMGetParam(variant_func, 9);
1927 fetch_elts = LLVMGetParam(variant_func, 10);
1928 system_values.draw_id = LLVMGetParam(variant_func, 11);
1929
1930 lp_build_name(context_ptr, "context");
1931 lp_build_name(io_ptr, "io");
1932 lp_build_name(vbuffers_ptr, "vbuffers");
1933 lp_build_name(count, "count");
1934 lp_build_name(start_or_maxelt, "start_or_maxelt");
1935 lp_build_name(stride, "stride");
1936 lp_build_name(vb_ptr, "vb");
1937 lp_build_name(system_values.instance_id, "instance_id");
1938 lp_build_name(vertex_id_offset, "vertex_id_offset");
1939 lp_build_name(system_values.base_instance, "start_instance");
1940 lp_build_name(fetch_elts, "fetch_elts");
1941 lp_build_name(system_values.draw_id, "draw_id");
1942
1943 /*
1944 * Function body
1945 */
1946
1947 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry");
1948 builder = gallivm->builder;
1949 LLVMPositionBuilderAtEnd(builder, block);
1950
1951 memset(&vs_type, 0, sizeof vs_type);
1952 vs_type.floating = TRUE; /* floating point values */
1953 vs_type.sign = TRUE; /* values are signed */
1954 vs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */
1955 vs_type.width = 32; /* 32-bit float */
1956 vs_type.length = vector_length;
1957
1958 lp_build_context_init(&bld, gallivm, lp_type_uint(32));
1959 lp_build_context_init(&blduivec, gallivm, lp_uint_type(vs_type));
1960
1961 /* hold temporary "bool" clipmask */
1962 clipmask_bool_ptr = lp_build_alloca(gallivm, blduivec.vec_type, "");
1963
1964 fake_buf = lp_build_alloca_undef(gallivm,
1965 LLVMVectorType(LLVMInt64TypeInContext(context), 4), "");
1966 fake_buf = LLVMBuildBitCast(builder, fake_buf,
1967 LLVMPointerType(LLVMInt8TypeInContext(context), 0), "");
1968 fake_buf_ptr = LLVMBuildGEP(builder, fake_buf, &bld.zero, 1, "");
1969
1970 /* code generated texture sampling */
1971 sampler = draw_llvm_sampler_soa_create(draw_llvm_variant_key_samplers(key));
1972
1973 image = draw_llvm_image_soa_create(draw_llvm_variant_key_images(key));
1974
1975 step = lp_build_const_int32(gallivm, vector_length);
1976
1977 ind_vec = blduivec.undef;
1978 for (i = 0; i < vs_type.length; i++) {
1979 LLVMValueRef index = lp_build_const_int32(gallivm, i);
1980 ind_vec = LLVMBuildInsertElement(builder, ind_vec, index, index, "");
1981 }
1982
1983 have_elts = LLVMBuildICmp(builder, LLVMIntNE,
1984 LLVMConstPointerNull(arg_types[10]), fetch_elts, "");
1985
1986 fetch_max = LLVMBuildSub(builder, count, bld.one, "fetch_max");
1987 fetch_max = lp_build_broadcast_scalar(&blduivec, fetch_max);
1988 /*
1989 * Only needed for non-indexed path.
1990 */
1991 start_vec = lp_build_broadcast_scalar(&blduivec, start_or_maxelt);
1992
1993 /*
1994 * Pre-calculate everything which is constant per shader invocation.
1995 */
1996 for (j = 0; j < key->nr_vertex_elements; ++j) {
1997 LLVMValueRef vb_buffer_offset, buffer_size, temp_ptr;
1998 LLVMValueRef vb_info, vbuffer_ptr, buf_offset, ofbit;
1999 struct pipe_vertex_element *velem = &key->vertex_element[j];
2000 LLVMValueRef vb_index =
2001 lp_build_const_int32(gallivm, velem->vertex_buffer_index);
2002 LLVMValueRef bsize = lp_build_const_int32(gallivm,
2003 util_format_get_blocksize(velem->src_format));
2004 LLVMValueRef src_offset = lp_build_const_int32(gallivm,
2005 velem->src_offset);
2006 struct lp_build_if_state if_ctx;
2007
2008 if (velem->src_format != PIPE_FORMAT_NONE) {
2009 vbuffer_ptr = LLVMBuildGEP(builder, vbuffers_ptr, &vb_index, 1, "");
2010 vb_info = LLVMBuildGEP(builder, vb_ptr, &vb_index, 1, "");
2011 vb_stride[j] = draw_jit_vbuffer_stride(gallivm, vb_info);
2012 vb_stride[j] = LLVMBuildZExt(gallivm->builder, vb_stride[j],
2013 LLVMInt32TypeInContext(context), "");
2014 vb_buffer_offset = draw_jit_vbuffer_offset(gallivm, vb_info);
2015 map_ptr[j] = draw_jit_dvbuffer_map(gallivm, vbuffer_ptr);
2016 buffer_size = draw_jit_dvbuffer_size(gallivm, vbuffer_ptr);
2017
2018 ofbit = NULL;
2019 /*
2020 * We'll set buffer_size_adj to zero if we have of, so it will
2021 * always overflow later automatically without having to keep ofbit.
2022 * Overflows (with normal wraparound) doing the actual offset
2023 * calculation should be ok, just not for the buffer size calc.
2024 * It would also be possible to detect such overflows and return
2025 * zeros if that happens, but this would be more complex.
2026 */
2027 buf_offset = lp_build_add(&bld, vb_buffer_offset, src_offset);
2028 tmp = lp_build_sub(&bld, bsize, bld.one);
2029 buffer_size_adj[j] = lp_build_usub_overflow(gallivm, buffer_size, tmp,
2030 &ofbit);
2031 buffer_size_adj[j] = lp_build_usub_overflow(gallivm, buffer_size_adj[j],
2032 buf_offset, &ofbit);
2033
2034 /*
2035 * We can't easily set fake vertex buffers outside the generated code.
2036 * Hence, set fake vertex buffers here instead basically, so fetch
2037 * code can always fetch using offset 0, eliminating all control flow
2038 * inside the main loop.
2039 * (Alternatively, could have control flow per vector skipping fetch
2040 * if ofbit is true.)
2041 */
2042 if (velem->instance_divisor) {
2043 /*
2044 * Index is equal to the start instance plus the number of current
2045 * instance divided by the divisor. In this case we compute it as:
2046 * index = start_instance + (instance_id / divisor).
2047 * Note we could actually do the fetch here, outside the loop -
2048 * it's all constant, hopefully llvm recognizes this.
2049 */
2050 LLVMValueRef current_instance;
2051 current_instance = LLVMBuildUDiv(builder, system_values.instance_id,
2052 lp_build_const_int32(gallivm,
2053 velem->instance_divisor),
2054 "instance_divisor");
2055 instance_index[j] = lp_build_uadd_overflow(gallivm, system_values.base_instance,
2056 current_instance, &ofbit);
2057 }
2058
2059 buffer_size_adj[j] = LLVMBuildSelect(builder, ofbit, bld.zero,
2060 buffer_size_adj[j], "");
2061
2062 temp_ptr = lp_build_alloca_undef(gallivm,
2063 LLVMPointerType(LLVMInt8TypeInContext(context), 0), "");
2064
2065 lp_build_if(&if_ctx, gallivm, ofbit);
2066 {
2067 LLVMBuildStore(builder, fake_buf_ptr, temp_ptr);
2068 }
2069 lp_build_else(&if_ctx);
2070 {
2071 map_ptr[j] = LLVMBuildGEP(builder, map_ptr[j], &buf_offset, 1, "");
2072 LLVMBuildStore(builder, map_ptr[j], temp_ptr);
2073 }
2074 lp_build_endif(&if_ctx);
2075 map_ptr[j] = LLVMBuildLoad(builder, temp_ptr, "map_ptr");
2076
2077 if (0) {
2078 lp_build_printf(gallivm, "velem %d, vbuf index = %u, vb_stride = %u\n",
2079 lp_build_const_int32(gallivm, j),
2080 vb_index, vb_stride[j]);
2081 lp_build_printf(gallivm,
2082 " vb_buffer_offset = %u, src_offset = %u, buf_offset = %u\n",
2083 vb_buffer_offset, src_offset, buf_offset);
2084 lp_build_printf(gallivm, " buffer size = %u, blocksize = %u\n",
2085 buffer_size, bsize);
2086 lp_build_printf(gallivm, " instance_id = %u\n", system_values.instance_id);
2087 }
2088 }
2089 }
2090
2091 lp_build_loop_begin(&lp_loop, gallivm, bld.zero);
2092 {
2093 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
2094 LLVMValueRef io;
2095 LLVMValueRef clipmask; /* holds the clipmask value */
2096 LLVMValueRef true_index_array, index_store;
2097 const LLVMValueRef (*ptr_aos)[TGSI_NUM_CHANNELS];
2098
2099 io_itr = lp_loop.counter;
2100
2101 io = LLVMBuildGEP(builder, io_ptr, &io_itr, 1, "");
2102 #if DEBUG_STORE
2103 lp_build_printf(gallivm, " --- io %d = %p, loop counter %d\n",
2104 io_itr, io, lp_loop.counter);
2105 #endif
2106
2107 true_index_array = lp_build_broadcast_scalar(&blduivec, lp_loop.counter);
2108 true_index_array = LLVMBuildAdd(builder, true_index_array, ind_vec, "");
2109
2110 LLVMValueRef exec_mask = lp_build_cmp(&blduivec, PIPE_FUNC_LEQUAL, true_index_array, fetch_max);
2111 /*
2112 * Limit indices to fetch_max, otherwise might try to access indices
2113 * beyond index buffer (or rather vsplit elt buffer) size.
2114 * Could probably safely (?) skip this for non-indexed draws and
2115 * simplify things minimally (by removing it could combine the ind_vec
2116 * and start_vec adds). I think the only effect for non-indexed draws will
2117 * be that for the invalid elements they will be all fetched from the
2118 * same location as the last valid one, but noone should really care.
2119 */
2120 true_index_array = lp_build_min(&blduivec, true_index_array, fetch_max);
2121
2122 index_store = lp_build_alloca_undef(gallivm, blduivec.vec_type, "index_store");
2123
2124 lp_build_if(&if_ctx, gallivm, have_elts);
2125 {
2126 /*
2127 * Note: you'd expect some comparison/clamp against fetch_elt_max
2128 * here.
2129 * There used to be one here but it was incorrect: overflow was
2130 * detected if index > fetch_elt_max - but the correct condition
2131 * would be index >= fetch_elt_max (since this is just size of elts
2132 * buffer / element size).
2133 * Using the correct condition however will cause failures - due to
2134 * vsplit/vcache code which rebases indices. So, as an example, if
2135 * fetch_elt_max is just 1 and fetch_count 2, vsplit cache will
2136 * replace all invalid indices with 0 - which in case of elt_bias
2137 * not being zero will get a different fetch index than the valid
2138 * index 0. So, just rely on vsplit code preventing out-of-bounds
2139 * fetches. This is also why it's safe to do elts fetch even if there
2140 * was no index buffer bound - the real buffer is never seen here, at
2141 * least not if there are index buffer overflows...
2142 */
2143
2144 /*
2145 * XXX should not have to do this, as scale can be handled
2146 * natively by loads (hits asserts though).
2147 */
2148 tmp = lp_build_shl_imm(&blduivec, true_index_array, 2);
2149 fetch_elts = LLVMBuildBitCast(builder, fetch_elts,
2150 LLVMPointerType(LLVMInt8TypeInContext(context),
2151 0), "");
2152 tmp = lp_build_gather(gallivm, vs_type.length,
2153 32, bld.type, TRUE,
2154 fetch_elts, tmp, FALSE);
2155 LLVMBuildStore(builder, tmp, index_store);
2156 }
2157 lp_build_else(&if_ctx);
2158 {
2159 tmp = LLVMBuildAdd(builder, true_index_array, start_vec, "");
2160 LLVMBuildStore(builder, tmp, index_store);
2161 }
2162 lp_build_endif(&if_ctx);
2163
2164 true_index_array = LLVMBuildLoad(builder, index_store, "");
2165
2166 for (j = 0; j < key->nr_vertex_elements; ++j) {
2167 struct pipe_vertex_element *velem = &key->vertex_element[j];
2168 const struct util_format_description *format_desc =
2169 util_format_description(velem->src_format);
2170
2171 if (format_desc->format == PIPE_FORMAT_NONE) {
2172 for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
2173 inputs[j][i] = lp_build_zero(gallivm, vs_type);
2174 }
2175 }
2176 else if (velem->instance_divisor) {
2177 fetch_instanced(gallivm, format_desc, vs_type,
2178 vb_stride[j], map_ptr[j],
2179 buffer_size_adj[j],
2180 inputs[j], instance_index[j]);
2181 }
2182 else {
2183 fetch_vector(gallivm, format_desc, vs_type,
2184 vb_stride[j], map_ptr[j],
2185 buffer_size_adj[j],
2186 inputs[j], true_index_array);
2187 }
2188 }
2189
2190 struct lp_build_mask_context mask;
2191
2192 lp_build_mask_begin(&mask, gallivm, vs_type, exec_mask);
2193 /* In the paths with elts vertex id has to be unaffected by the
2194 * index bias and because indices inside our elements array have
2195 * already had index bias applied we need to subtract it here to
2196 * get back to the original index.
2197 * in the linear paths vertex id has to be unaffected by the
2198 * original start index and because we abuse the 'start' variable
2199 * to either represent the actual start index or the index at which
2200 * the primitive was split (we split rendering into chunks of at
2201 * most 4095-vertices) we need to back out the original start
2202 * index out of our vertex id here.
2203 * for ARB_shader_draw_parameters, base_vertex should be 0 for non-indexed draws.
2204 */
2205 LLVMValueRef base_vertex = lp_build_select(&bld, have_elts, vertex_id_offset, lp_build_const_int32(gallivm, 0));;
2206 system_values.basevertex = lp_build_broadcast_scalar(&blduivec, base_vertex);
2207 system_values.vertex_id = true_index_array;
2208 system_values.vertex_id_nobase = LLVMBuildSub(builder, true_index_array,
2209 lp_build_broadcast_scalar(&blduivec, vertex_id_offset), "");
2210
2211 ptr_aos = (const LLVMValueRef (*)[TGSI_NUM_CHANNELS]) inputs;
2212 generate_vs(variant,
2213 builder,
2214 vs_type,
2215 outputs,
2216 ptr_aos,
2217 &system_values,
2218 context_ptr,
2219 sampler,
2220 image,
2221 key->clamp_vertex_color,
2222 &mask);
2223
2224 lp_build_mask_end(&mask);
2225 if (pos != -1 && cv != -1) {
2226 /* store original positions in clip before further manipulation */
2227 store_clip(gallivm, vs_type, io, outputs, pos);
2228
2229 /* do cliptest */
2230 if (enable_cliptest) {
2231 LLVMValueRef temp = LLVMBuildLoad(builder, clipmask_bool_ptr, "");
2232 /* allocate clipmask, assign it integer type */
2233 clipmask = generate_clipmask(llvm,
2234 gallivm,
2235 vs_type,
2236 outputs,
2237 key,
2238 context_ptr, &have_clipdist);
2239 temp = LLVMBuildOr(builder, clipmask, temp, "");
2240 /* store temporary clipping boolean value */
2241 LLVMBuildStore(builder, temp, clipmask_bool_ptr);
2242 }
2243 else {
2244 clipmask = blduivec.zero;
2245 }
2246
2247 /* do viewport mapping */
2248 if (!bypass_viewport) {
2249 generate_viewport(variant, builder, vs_type, outputs, context_ptr);
2250 }
2251 }
2252 else {
2253 clipmask = blduivec.zero;
2254 }
2255
2256 /* store clipmask in vertex header,
2257 * original positions in clip
2258 * and transformed positions in data
2259 */
2260 convert_to_aos(gallivm, io, NULL, outputs, clipmask,
2261 vs_info->num_outputs, vs_type,
2262 enable_cliptest && key->need_edgeflags);
2263 }
2264 lp_build_loop_end_cond(&lp_loop, count, step, LLVMIntUGE);
2265
2266 sampler->destroy(sampler);
2267 image->destroy(image);
2268
2269 /* return clipping boolean value for function */
2270 ret = clipmask_booli8(gallivm, vs_type, clipmask_bool_ptr,
2271 enable_cliptest && key->need_edgeflags);
2272
2273 LLVMBuildRet(builder, ret);
2274
2275 gallivm_verify_function(gallivm, variant_func);
2276 }
2277
2278
2279 struct draw_llvm_variant_key *
2280 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store)
2281 {
2282 unsigned i;
2283 struct draw_llvm_variant_key *key;
2284 struct draw_sampler_static_state *draw_sampler;
2285 struct draw_image_static_state *draw_image;
2286
2287 key = (struct draw_llvm_variant_key *)store;
2288
2289 memset(key, 0, offsetof(struct draw_llvm_variant_key, vertex_element[0]));
2290
2291 key->clamp_vertex_color = llvm->draw->rasterizer->clamp_vertex_color; /**/
2292
2293 /* will have to rig this up properly later */
2294 key->clip_xy = llvm->draw->clip_xy;
2295 key->clip_z = llvm->draw->clip_z;
2296 key->clip_user = llvm->draw->clip_user;
2297 key->bypass_viewport = llvm->draw->bypass_viewport;
2298 key->clip_halfz = llvm->draw->rasterizer->clip_halfz;
2299 /* XXX assumes edgeflag output not at 0 */
2300 key->need_edgeflags = (llvm->draw->vs.edgeflag_output ? TRUE : FALSE);
2301 key->ucp_enable = llvm->draw->rasterizer->clip_plane_enable;
2302 key->has_gs_or_tes = llvm->draw->gs.geometry_shader != NULL || llvm->draw->tes.tess_eval_shader != NULL;
2303 key->num_outputs = draw_total_vs_outputs(llvm->draw);
2304
2305 /* All variants of this shader will have the same value for
2306 * nr_samplers. Not yet trying to compact away holes in the
2307 * sampler array.
2308 */
2309 key->nr_samplers = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER] + 1;
2310 if (llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) {
2311 key->nr_sampler_views =
2312 llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1;
2313 }
2314 else {
2315 key->nr_sampler_views = key->nr_samplers;
2316 }
2317
2318 key->nr_images = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_IMAGE] + 1;
2319
2320 /* Presumably all variants of the shader should have the same
2321 * number of vertex elements - ie the number of shader inputs.
2322 * NOTE: we NEED to store the needed number of needed inputs
2323 * here, not the number of provided elements to match keysize
2324 * (and the offset of sampler state in the key).
2325 * If we have excess number of vertex elements, this is valid,
2326 * but the excess ones don't matter.
2327 * If we don't have enough vertex elements (which looks not really
2328 * valid but we'll handle it gracefully) fill out missing ones with
2329 * zero (we'll recognize these later by PIPE_FORMAT_NONE).
2330 */
2331 key->nr_vertex_elements =
2332 llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_INPUT] + 1;
2333
2334 if (llvm->draw->pt.nr_vertex_elements < key->nr_vertex_elements) {
2335 debug_printf("draw: vs with %d inputs but only have %d vertex elements\n",
2336 key->nr_vertex_elements, llvm->draw->pt.nr_vertex_elements);
2337 memset(key->vertex_element, 0,
2338 sizeof(struct pipe_vertex_element) * key->nr_vertex_elements);
2339 }
2340 memcpy(key->vertex_element,
2341 llvm->draw->pt.vertex_element,
2342 sizeof(struct pipe_vertex_element) *
2343 MIN2(key->nr_vertex_elements, llvm->draw->pt.nr_vertex_elements));
2344
2345 draw_sampler = draw_llvm_variant_key_samplers(key);
2346 memset(draw_sampler, 0,
2347 MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler);
2348
2349 for (i = 0 ; i < key->nr_samplers; i++) {
2350 lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state,
2351 llvm->draw->samplers[PIPE_SHADER_VERTEX][i]);
2352 }
2353 for (i = 0 ; i < key->nr_sampler_views; i++) {
2354 lp_sampler_static_texture_state(&draw_sampler[i].texture_state,
2355 llvm->draw->sampler_views[PIPE_SHADER_VERTEX][i]);
2356 }
2357
2358 draw_image = draw_llvm_variant_key_images(key);
2359 memset(draw_image, 0,
2360 key->nr_images * sizeof *draw_image);
2361 for (i = 0; i < key->nr_images; i++) {
2362 lp_sampler_static_texture_state_image(&draw_image[i].image_state,
2363 llvm->draw->images[PIPE_SHADER_VERTEX][i]);
2364 }
2365 return key;
2366 }
2367
2368
2369 void
2370 draw_llvm_dump_variant_key(struct draw_llvm_variant_key *key)
2371 {
2372 unsigned i;
2373 struct draw_sampler_static_state *sampler = draw_llvm_variant_key_samplers(key);
2374 struct draw_image_static_state *image = draw_llvm_variant_key_images(key);
2375 debug_printf("clamp_vertex_color = %u\n", key->clamp_vertex_color);
2376 debug_printf("clip_xy = %u\n", key->clip_xy);
2377 debug_printf("clip_z = %u\n", key->clip_z);
2378 debug_printf("clip_user = %u\n", key->clip_user);
2379 debug_printf("bypass_viewport = %u\n", key->bypass_viewport);
2380 debug_printf("clip_halfz = %u\n", key->clip_halfz);
2381 debug_printf("need_edgeflags = %u\n", key->need_edgeflags);
2382 debug_printf("has_gs_or_tes = %u\n", key->has_gs_or_tes);
2383 debug_printf("ucp_enable = %u\n", key->ucp_enable);
2384
2385 for (i = 0 ; i < key->nr_vertex_elements; i++) {
2386 debug_printf("vertex_element[%i].src_offset = %u\n", i, key->vertex_element[i].src_offset);
2387 debug_printf("vertex_element[%i].instance_divisor = %u\n", i, key->vertex_element[i].instance_divisor);
2388 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i, key->vertex_element[i].vertex_buffer_index);
2389 debug_printf("vertex_element[%i].src_format = %s\n", i, util_format_name(key->vertex_element[i].src_format));
2390 }
2391
2392 for (i = 0 ; i < key->nr_sampler_views; i++) {
2393 debug_printf("sampler[%i].src_format = %s\n", i, util_format_name(sampler[i].texture_state.format));
2394 }
2395
2396 for (i = 0 ; i < key->nr_images; i++)
2397 debug_printf("images[%i].format = %s\n", i, util_format_name(image[i].image_state.format));
2398 }
2399
2400
2401 void
2402 draw_llvm_set_mapped_texture(struct draw_context *draw,
2403 enum pipe_shader_type shader_stage,
2404 unsigned sview_idx,
2405 uint32_t width, uint32_t height, uint32_t depth,
2406 uint32_t first_level, uint32_t last_level,
2407 const void *base_ptr,
2408 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
2409 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
2410 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])
2411 {
2412 unsigned j;
2413 struct draw_jit_texture *jit_tex;
2414
2415 switch (shader_stage) {
2416 case PIPE_SHADER_VERTEX:
2417 assert(sview_idx < ARRAY_SIZE(draw->llvm->jit_context.textures));
2418 jit_tex = &draw->llvm->jit_context.textures[sview_idx];
2419 break;
2420 case PIPE_SHADER_GEOMETRY:
2421 assert(sview_idx < ARRAY_SIZE(draw->llvm->gs_jit_context.textures));
2422 jit_tex = &draw->llvm->gs_jit_context.textures[sview_idx];
2423 break;
2424 case PIPE_SHADER_TESS_CTRL:
2425 assert(sview_idx < ARRAY_SIZE(draw->llvm->tcs_jit_context.textures));
2426 jit_tex = &draw->llvm->tcs_jit_context.textures[sview_idx];
2427 break;
2428 case PIPE_SHADER_TESS_EVAL:
2429 assert(sview_idx < ARRAY_SIZE(draw->llvm->tes_jit_context.textures));
2430 jit_tex = &draw->llvm->tes_jit_context.textures[sview_idx];
2431 break;
2432 default:
2433 assert(0);
2434 return;
2435 }
2436
2437 jit_tex->width = width;
2438 jit_tex->height = height;
2439 jit_tex->depth = depth;
2440 jit_tex->first_level = first_level;
2441 jit_tex->last_level = last_level;
2442 jit_tex->base = base_ptr;
2443
2444 for (j = first_level; j <= last_level; j++) {
2445 jit_tex->mip_offsets[j] = mip_offsets[j];
2446 jit_tex->row_stride[j] = row_stride[j];
2447 jit_tex->img_stride[j] = img_stride[j];
2448 }
2449 }
2450
2451 void
2452 draw_llvm_set_mapped_image(struct draw_context *draw,
2453 enum pipe_shader_type shader_stage,
2454 unsigned idx,
2455 uint32_t width, uint32_t height, uint32_t depth,
2456 const void *base_ptr,
2457 uint32_t row_stride,
2458 uint32_t img_stride)
2459 {
2460 struct draw_jit_image *jit_image;
2461
2462 switch (shader_stage) {
2463 case PIPE_SHADER_VERTEX:
2464 assert(idx < ARRAY_SIZE(draw->llvm->jit_context.images));
2465 jit_image = &draw->llvm->jit_context.images[idx];
2466 break;
2467 case PIPE_SHADER_GEOMETRY:
2468 assert(idx < ARRAY_SIZE(draw->llvm->gs_jit_context.images));
2469 jit_image = &draw->llvm->gs_jit_context.images[idx];
2470 break;
2471 case PIPE_SHADER_TESS_CTRL:
2472 assert(idx < ARRAY_SIZE(draw->llvm->tcs_jit_context.images));
2473 jit_image = &draw->llvm->tcs_jit_context.images[idx];
2474 break;
2475 case PIPE_SHADER_TESS_EVAL:
2476 assert(idx < ARRAY_SIZE(draw->llvm->tes_jit_context.images));
2477 jit_image = &draw->llvm->tes_jit_context.images[idx];
2478 break;
2479 default:
2480 assert(0);
2481 return;
2482 }
2483
2484 jit_image->width = width;
2485 jit_image->height = height;
2486 jit_image->depth = depth;
2487 jit_image->base = base_ptr;
2488
2489 jit_image->row_stride = row_stride;
2490 jit_image->img_stride = img_stride;
2491 }
2492
2493
2494 void
2495 draw_llvm_set_sampler_state(struct draw_context *draw,
2496 enum pipe_shader_type shader_type)
2497 {
2498 unsigned i;
2499
2500 switch (shader_type) {
2501 case PIPE_SHADER_VERTEX:
2502 for (i = 0; i < draw->num_samplers[PIPE_SHADER_VERTEX]; i++) {
2503 struct draw_jit_sampler *jit_sam = &draw->llvm->jit_context.samplers[i];
2504
2505 if (draw->samplers[PIPE_SHADER_VERTEX][i]) {
2506 const struct pipe_sampler_state *s
2507 = draw->samplers[PIPE_SHADER_VERTEX][i];
2508 jit_sam->min_lod = s->min_lod;
2509 jit_sam->max_lod = s->max_lod;
2510 jit_sam->lod_bias = s->lod_bias;
2511 COPY_4V(jit_sam->border_color, s->border_color.f);
2512 }
2513 }
2514 break;
2515 case PIPE_SHADER_GEOMETRY:
2516 for (i = 0; i < draw->num_samplers[PIPE_SHADER_GEOMETRY]; i++) {
2517 struct draw_jit_sampler *jit_sam = &draw->llvm->gs_jit_context.samplers[i];
2518
2519 if (draw->samplers[PIPE_SHADER_GEOMETRY][i]) {
2520 const struct pipe_sampler_state *s
2521 = draw->samplers[PIPE_SHADER_GEOMETRY][i];
2522 jit_sam->min_lod = s->min_lod;
2523 jit_sam->max_lod = s->max_lod;
2524 jit_sam->lod_bias = s->lod_bias;
2525 COPY_4V(jit_sam->border_color, s->border_color.f);
2526 }
2527 }
2528 break;
2529 case PIPE_SHADER_TESS_CTRL:
2530 for (i = 0; i < draw->num_samplers[PIPE_SHADER_TESS_CTRL]; i++) {
2531 struct draw_jit_sampler *jit_sam = &draw->llvm->tcs_jit_context.samplers[i];
2532
2533 if (draw->samplers[PIPE_SHADER_TESS_CTRL][i]) {
2534 const struct pipe_sampler_state *s
2535 = draw->samplers[PIPE_SHADER_TESS_CTRL][i];
2536 jit_sam->min_lod = s->min_lod;
2537 jit_sam->max_lod = s->max_lod;
2538 jit_sam->lod_bias = s->lod_bias;
2539 COPY_4V(jit_sam->border_color, s->border_color.f);
2540 }
2541 }
2542 break;
2543 case PIPE_SHADER_TESS_EVAL:
2544 for (i = 0; i < draw->num_samplers[PIPE_SHADER_TESS_EVAL]; i++) {
2545 struct draw_jit_sampler *jit_sam = &draw->llvm->tes_jit_context.samplers[i];
2546
2547 if (draw->samplers[PIPE_SHADER_TESS_EVAL][i]) {
2548 const struct pipe_sampler_state *s
2549 = draw->samplers[PIPE_SHADER_TESS_EVAL][i];
2550 jit_sam->min_lod = s->min_lod;
2551 jit_sam->max_lod = s->max_lod;
2552 jit_sam->lod_bias = s->lod_bias;
2553 COPY_4V(jit_sam->border_color, s->border_color.f);
2554 }
2555 }
2556 break;
2557 default:
2558 assert(0);
2559 break;
2560 }
2561 }
2562
2563
2564 void
2565 draw_llvm_destroy_variant(struct draw_llvm_variant *variant)
2566 {
2567 struct draw_llvm *llvm = variant->llvm;
2568
2569 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
2570 debug_printf("Deleting VS variant: %u vs variants,\t%u total variants\n",
2571 variant->shader->variants_cached, llvm->nr_variants);
2572 }
2573
2574 gallivm_destroy(variant->gallivm);
2575
2576 remove_from_list(&variant->list_item_local);
2577 variant->shader->variants_cached--;
2578 remove_from_list(&variant->list_item_global);
2579 llvm->nr_variants--;
2580 FREE(variant);
2581 }
2582
2583
2584 /**
2585 * Create LLVM types for various structures.
2586 */
2587 static void
2588 create_gs_jit_types(struct draw_gs_llvm_variant *var)
2589 {
2590 struct gallivm_state *gallivm = var->gallivm;
2591 LLVMTypeRef texture_type, sampler_type, image_type, context_type;
2592
2593 texture_type = create_jit_texture_type(gallivm, "texture");
2594 sampler_type = create_jit_sampler_type(gallivm, "sampler");
2595 image_type = create_jit_image_type(gallivm, "image");
2596
2597 context_type = create_gs_jit_context_type(gallivm,
2598 var->shader->base.vector_length,
2599 texture_type, sampler_type,
2600 image_type,
2601 "draw_gs_jit_context");
2602 var->context_ptr_type = LLVMPointerType(context_type, 0);
2603
2604 var->input_array_type = create_gs_jit_input_type(gallivm);
2605 }
2606
2607 static LLVMTypeRef
2608 get_gs_context_ptr_type(struct draw_gs_llvm_variant *variant)
2609 {
2610 if (!variant->context_ptr_type)
2611 create_gs_jit_types(variant);
2612 return variant->context_ptr_type;
2613 }
2614
2615 static LLVMValueRef
2616 generate_mask_value(struct draw_gs_llvm_variant *variant,
2617 struct lp_type gs_type)
2618 {
2619 struct gallivm_state *gallivm = variant->gallivm;
2620 LLVMBuilderRef builder = gallivm->builder;
2621 struct lp_type mask_type = lp_int_type(gs_type);
2622 LLVMValueRef num_prims;
2623 LLVMValueRef mask_val = lp_build_const_vec(gallivm, mask_type, 0);
2624 unsigned i;
2625
2626 num_prims = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, mask_type),
2627 variant->num_prims);
2628 for (i = 0; i < gs_type.length; i++) {
2629 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
2630 mask_val = LLVMBuildInsertElement(builder, mask_val, idx, idx, "");
2631 }
2632 mask_val = lp_build_compare(gallivm, mask_type,
2633 PIPE_FUNC_GREATER, num_prims, mask_val);
2634
2635 return mask_val;
2636 }
2637
2638 static void
2639 draw_gs_llvm_generate(struct draw_llvm *llvm,
2640 struct draw_gs_llvm_variant *variant)
2641 {
2642 struct gallivm_state *gallivm = variant->gallivm;
2643 LLVMContextRef context = gallivm->context;
2644 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context);
2645 LLVMTypeRef arg_types[7];
2646 LLVMTypeRef func_type;
2647 LLVMValueRef variant_func;
2648 LLVMValueRef context_ptr;
2649 LLVMValueRef prim_id_ptr;
2650 LLVMBasicBlockRef block;
2651 LLVMBuilderRef builder;
2652 LLVMValueRef io_ptr, input_array, num_prims, mask_val;
2653 struct lp_build_sampler_soa *sampler = 0;
2654 struct lp_build_image_soa *image = NULL;
2655 struct lp_build_context bld;
2656 struct lp_bld_tgsi_system_values system_values;
2657 char func_name[64];
2658 struct lp_type gs_type;
2659 unsigned i;
2660 struct draw_gs_llvm_iface gs_iface;
2661 const struct tgsi_token *tokens = variant->shader->base.state.tokens;
2662 LLVMValueRef consts_ptr, num_consts_ptr;
2663 LLVMValueRef ssbos_ptr, num_ssbos_ptr;
2664 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
2665 struct lp_build_mask_context mask;
2666 const struct tgsi_shader_info *gs_info = &variant->shader->base.info;
2667 unsigned vector_length = variant->shader->base.vector_length;
2668
2669 memset(&system_values, 0, sizeof(system_values));
2670 memset(&outputs, 0, sizeof(outputs));
2671
2672 snprintf(func_name, sizeof(func_name), "draw_llvm_gs_variant%u",
2673 variant->shader->variants_cached);
2674
2675 assert(variant->vertex_header_ptr_type);
2676
2677 arg_types[0] = get_gs_context_ptr_type(variant); /* context */
2678 arg_types[1] = variant->input_array_type; /* input */
2679 arg_types[2] = LLVMPointerType(variant->vertex_header_ptr_type, 0); /* vertex_header */
2680 arg_types[3] = int32_type; /* num_prims */
2681 arg_types[4] = int32_type; /* instance_id */
2682 arg_types[5] = LLVMPointerType(
2683 LLVMVectorType(int32_type, vector_length), 0); /* prim_id_ptr */
2684 arg_types[6] = int32_type;
2685
2686 func_type = LLVMFunctionType(int32_type, arg_types, ARRAY_SIZE(arg_types), 0);
2687
2688 variant_func = LLVMAddFunction(gallivm->module, func_name, func_type);
2689
2690 variant->function = variant_func;
2691
2692 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv);
2693
2694 for (i = 0; i < ARRAY_SIZE(arg_types); ++i)
2695 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
2696 lp_add_function_attr(variant_func, i + 1, LP_FUNC_ATTR_NOALIAS);
2697
2698 context_ptr = LLVMGetParam(variant_func, 0);
2699 input_array = LLVMGetParam(variant_func, 1);
2700 io_ptr = LLVMGetParam(variant_func, 2);
2701 num_prims = LLVMGetParam(variant_func, 3);
2702 system_values.instance_id = LLVMGetParam(variant_func, 4);
2703 prim_id_ptr = LLVMGetParam(variant_func, 5);
2704 system_values.invocation_id = LLVMGetParam(variant_func, 6);
2705
2706 lp_build_name(context_ptr, "context");
2707 lp_build_name(input_array, "input");
2708 lp_build_name(io_ptr, "io");
2709 lp_build_name(num_prims, "num_prims");
2710 lp_build_name(system_values.instance_id, "instance_id");
2711 lp_build_name(prim_id_ptr, "prim_id_ptr");
2712 lp_build_name(system_values.invocation_id, "invocation_id");
2713
2714 variant->context_ptr = context_ptr;
2715 variant->io_ptr = io_ptr;
2716 variant->num_prims = num_prims;
2717
2718 gs_iface.base.fetch_input = draw_gs_llvm_fetch_input;
2719 gs_iface.base.emit_vertex = draw_gs_llvm_emit_vertex;
2720 gs_iface.base.end_primitive = draw_gs_llvm_end_primitive;
2721 gs_iface.base.gs_epilogue = draw_gs_llvm_epilogue;
2722 gs_iface.input = input_array;
2723 gs_iface.variant = variant;
2724
2725 /*
2726 * Function body
2727 */
2728
2729 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry");
2730 builder = gallivm->builder;
2731 LLVMPositionBuilderAtEnd(builder, block);
2732
2733 lp_build_context_init(&bld, gallivm, lp_type_int(32));
2734
2735 memset(&gs_type, 0, sizeof gs_type);
2736 gs_type.floating = TRUE; /* floating point values */
2737 gs_type.sign = TRUE; /* values are signed */
2738 gs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */
2739 gs_type.width = 32; /* 32-bit float */
2740 gs_type.length = vector_length;
2741
2742 consts_ptr = draw_gs_jit_context_constants(variant->gallivm, context_ptr);
2743 num_consts_ptr =
2744 draw_gs_jit_context_num_constants(variant->gallivm, context_ptr);
2745
2746 ssbos_ptr = draw_gs_jit_context_ssbos(variant->gallivm, context_ptr);
2747 num_ssbos_ptr =
2748 draw_gs_jit_context_num_ssbos(variant->gallivm, context_ptr);
2749
2750 /* code generated texture sampling */
2751 sampler = draw_llvm_sampler_soa_create(variant->key.samplers);
2752 image = draw_llvm_image_soa_create(draw_gs_llvm_variant_key_images(&variant->key));
2753 mask_val = generate_mask_value(variant, gs_type);
2754 lp_build_mask_begin(&mask, gallivm, gs_type, mask_val);
2755
2756 if (gs_info->uses_primid) {
2757 system_values.prim_id = LLVMBuildLoad(builder, prim_id_ptr, "prim_id");
2758 }
2759
2760 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
2761 if (llvm->draw->gs.geometry_shader->state.type == PIPE_SHADER_IR_TGSI)
2762 tgsi_dump(tokens, 0);
2763 else
2764 nir_print_shader(llvm->draw->gs.geometry_shader->state.ir.nir, stderr);
2765 draw_gs_llvm_dump_variant_key(&variant->key);
2766 }
2767
2768 struct lp_build_tgsi_params params;
2769 memset(&params, 0, sizeof(params));
2770
2771 params.type = gs_type;
2772 params.mask = &mask;
2773 params.consts_ptr = consts_ptr;
2774 params.const_sizes_ptr = num_consts_ptr;
2775 params.system_values = &system_values;
2776 params.context_ptr = context_ptr;
2777 params.sampler = sampler;
2778 params.info = &llvm->draw->gs.geometry_shader->info;
2779 params.gs_iface = (const struct lp_build_gs_iface *)&gs_iface;
2780 params.ssbo_ptr = ssbos_ptr;
2781 params.ssbo_sizes_ptr = num_ssbos_ptr;
2782 params.image = image;
2783
2784 if (llvm->draw->gs.geometry_shader->state.type == PIPE_SHADER_IR_TGSI)
2785 lp_build_tgsi_soa(variant->gallivm,
2786 tokens,
2787 &params,
2788 outputs);
2789 else
2790 lp_build_nir_soa(variant->gallivm,
2791 llvm->draw->gs.geometry_shader->state.ir.nir,
2792 &params,
2793 outputs);
2794
2795 sampler->destroy(sampler);
2796 image->destroy(image);
2797
2798 lp_build_mask_end(&mask);
2799
2800 LLVMBuildRet(builder, lp_build_zero(gallivm, lp_type_uint(32)));
2801
2802 gallivm_verify_function(gallivm, variant_func);
2803 }
2804
2805
2806 struct draw_gs_llvm_variant *
2807 draw_gs_llvm_create_variant(struct draw_llvm *llvm,
2808 unsigned num_outputs,
2809 const struct draw_gs_llvm_variant_key *key)
2810 {
2811 struct draw_gs_llvm_variant *variant;
2812 struct llvm_geometry_shader *shader =
2813 llvm_geometry_shader(llvm->draw->gs.geometry_shader);
2814 LLVMTypeRef vertex_header;
2815 char module_name[64];
2816
2817 variant = MALLOC(sizeof *variant +
2818 shader->variant_key_size -
2819 sizeof variant->key);
2820 if (!variant)
2821 return NULL;
2822
2823 variant->llvm = llvm;
2824 variant->shader = shader;
2825
2826 snprintf(module_name, sizeof(module_name), "draw_llvm_gs_variant%u",
2827 variant->shader->variants_cached);
2828
2829 variant->gallivm = gallivm_create(module_name, llvm->context);
2830
2831 create_gs_jit_types(variant);
2832
2833 memcpy(&variant->key, key, shader->variant_key_size);
2834
2835 vertex_header = create_jit_vertex_header(variant->gallivm, num_outputs);
2836
2837 variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0);
2838
2839 draw_gs_llvm_generate(llvm, variant);
2840
2841 gallivm_compile_module(variant->gallivm);
2842
2843 variant->jit_func = (draw_gs_jit_func)
2844 gallivm_jit_function(variant->gallivm, variant->function);
2845
2846 gallivm_free_ir(variant->gallivm);
2847
2848 variant->list_item_global.base = variant;
2849 variant->list_item_local.base = variant;
2850 /*variant->no = */shader->variants_created++;
2851 variant->list_item_global.base = variant;
2852
2853 return variant;
2854 }
2855
2856 void
2857 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant *variant)
2858 {
2859 struct draw_llvm *llvm = variant->llvm;
2860
2861 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
2862 debug_printf("Deleting GS variant: %u gs variants,\t%u total variants\n",
2863 variant->shader->variants_cached, llvm->nr_gs_variants);
2864 }
2865
2866 gallivm_destroy(variant->gallivm);
2867
2868 remove_from_list(&variant->list_item_local);
2869 variant->shader->variants_cached--;
2870 remove_from_list(&variant->list_item_global);
2871 llvm->nr_gs_variants--;
2872 FREE(variant);
2873 }
2874
2875 struct draw_gs_llvm_variant_key *
2876 draw_gs_llvm_make_variant_key(struct draw_llvm *llvm, char *store)
2877 {
2878 unsigned i;
2879 struct draw_gs_llvm_variant_key *key;
2880 struct draw_sampler_static_state *draw_sampler;
2881 struct draw_image_static_state *draw_image;
2882
2883 key = (struct draw_gs_llvm_variant_key *)store;
2884
2885 memset(key, 0, offsetof(struct draw_gs_llvm_variant_key, samplers[0]));
2886
2887 key->num_outputs = draw_total_gs_outputs(llvm->draw);
2888
2889 /* All variants of this shader will have the same value for
2890 * nr_samplers. Not yet trying to compact away holes in the
2891 * sampler array.
2892 */
2893 key->nr_samplers = llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER] + 1;
2894 if (llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) {
2895 key->nr_sampler_views =
2896 llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1;
2897 }
2898 else {
2899 key->nr_sampler_views = key->nr_samplers;
2900 }
2901
2902 key->nr_images = llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_IMAGE] + 1;
2903
2904 draw_sampler = key->samplers;
2905
2906 memset(draw_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler);
2907
2908 for (i = 0 ; i < key->nr_samplers; i++) {
2909 lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state,
2910 llvm->draw->samplers[PIPE_SHADER_GEOMETRY][i]);
2911 }
2912 for (i = 0 ; i < key->nr_sampler_views; i++) {
2913 lp_sampler_static_texture_state(&draw_sampler[i].texture_state,
2914 llvm->draw->sampler_views[PIPE_SHADER_GEOMETRY][i]);
2915 }
2916
2917 draw_image = draw_gs_llvm_variant_key_images(key);
2918 memset(draw_image, 0,
2919 key->nr_images * sizeof *draw_image);
2920 for (i = 0; i < key->nr_images; i++) {
2921 lp_sampler_static_texture_state_image(&draw_image[i].image_state,
2922 llvm->draw->images[PIPE_SHADER_GEOMETRY][i]);
2923 }
2924 return key;
2925 }
2926
2927 void
2928 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key *key)
2929 {
2930 unsigned i;
2931 struct draw_sampler_static_state *sampler = key->samplers;
2932 struct draw_image_static_state *image = draw_gs_llvm_variant_key_images(key);
2933 for (i = 0 ; i < key->nr_sampler_views; i++) {
2934 debug_printf("sampler[%i].src_format = %s\n", i,
2935 util_format_name(sampler[i].texture_state.format));
2936 }
2937
2938 for (i = 0 ; i < key->nr_images; i++)
2939 debug_printf("images[%i].format = %s\n", i, util_format_name(image[i].image_state.format));
2940
2941 }
2942
2943 static void
2944 create_tcs_jit_types(struct draw_tcs_llvm_variant *var)
2945 {
2946 struct gallivm_state *gallivm = var->gallivm;
2947 LLVMTypeRef texture_type, sampler_type, image_type, context_type;
2948
2949 texture_type = create_jit_texture_type(gallivm, "texture");
2950 sampler_type = create_jit_sampler_type(gallivm, "sampler");
2951 image_type = create_jit_image_type(gallivm, "image");
2952
2953 context_type = create_tcs_jit_context_type(gallivm,
2954 0,
2955 texture_type, sampler_type,
2956 image_type,
2957 "draw_tcs_jit_context");
2958 var->input_array_type = create_tcs_jit_input_type(gallivm);
2959 var->output_array_type = create_tcs_jit_output_type(gallivm);
2960 var->context_ptr_type = LLVMPointerType(context_type, 0);
2961 }
2962
2963 static LLVMTypeRef
2964 get_tcs_context_ptr_type(struct draw_tcs_llvm_variant *variant)
2965 {
2966 if (!variant->context_ptr_type)
2967 create_tcs_jit_types(variant);
2968 return variant->context_ptr_type;
2969 }
2970
2971 static LLVMValueRef
2972 draw_tcs_llvm_emit_fetch_input(const struct lp_build_tcs_iface *tes_iface,
2973 struct lp_build_context *bld,
2974 boolean is_vindex_indirect,
2975 LLVMValueRef vertex_index,
2976 boolean is_aindex_indirect,
2977 LLVMValueRef attrib_index,
2978 LLVMValueRef swizzle_index)
2979 {
2980 const struct draw_tcs_llvm_iface *tcs = draw_tcs_llvm_iface(tes_iface);
2981 struct gallivm_state *gallivm = bld->gallivm;
2982 LLVMBuilderRef builder = gallivm->builder;
2983 LLVMValueRef indices[3];
2984 LLVMValueRef res;
2985 struct lp_type type = bld->type;
2986
2987 if (is_vindex_indirect || is_aindex_indirect) {
2988 int i;
2989
2990 res = bld->zero;
2991 for (i = 0; i < type.length; ++i) {
2992 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
2993 LLVMValueRef vert_chan_index = vertex_index;
2994 LLVMValueRef attr_chan_index = attrib_index;
2995 LLVMValueRef channel_vec;
2996
2997 if (is_vindex_indirect) {
2998 vert_chan_index = LLVMBuildExtractElement(builder,
2999 vertex_index, idx, "");
3000 }
3001 if (is_aindex_indirect) {
3002 attr_chan_index = LLVMBuildExtractElement(builder,
3003 attrib_index, idx, "");
3004 }
3005
3006 indices[0] = vert_chan_index;
3007 indices[1] = attr_chan_index;
3008 indices[2] = swizzle_index;
3009
3010 channel_vec = LLVMBuildGEP(builder, tcs->input, indices, 3, "");
3011 channel_vec = LLVMBuildLoad(builder, channel_vec, "");
3012
3013 res = LLVMBuildInsertElement(builder, res, channel_vec, idx, "");
3014 }
3015 } else {
3016 indices[0] = vertex_index;
3017 indices[1] = attrib_index;
3018 indices[2] = swizzle_index;
3019
3020 res = LLVMBuildGEP(builder, tcs->input, indices, 3, "");
3021 res = LLVMBuildLoad(builder, res, "");
3022 res = lp_build_broadcast_scalar(bld, res);
3023 }
3024 return res;
3025 }
3026
3027 static LLVMValueRef
3028 draw_tcs_llvm_emit_fetch_output(const struct lp_build_tcs_iface *tes_iface,
3029 struct lp_build_context *bld,
3030 boolean is_vindex_indirect,
3031 LLVMValueRef vertex_index,
3032 boolean is_aindex_indirect,
3033 LLVMValueRef attrib_index,
3034 LLVMValueRef swizzle_index,
3035 uint32_t name)
3036 {
3037 const struct draw_tcs_llvm_iface *tcs = draw_tcs_llvm_iface(tes_iface);
3038 struct gallivm_state *gallivm = bld->gallivm;
3039 LLVMBuilderRef builder = gallivm->builder;
3040 LLVMValueRef indices[3];
3041 LLVMValueRef res;
3042 struct lp_type type = bld->type;
3043
3044 if (is_vindex_indirect || is_aindex_indirect) {
3045 int i;
3046
3047 res = bld->zero;
3048 for (i = 0; i < type.length; ++i) {
3049 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
3050 LLVMValueRef vert_chan_index = vertex_index;
3051 LLVMValueRef attr_chan_index = attrib_index;
3052 LLVMValueRef channel_vec;
3053
3054 if (is_vindex_indirect) {
3055 vert_chan_index = LLVMBuildExtractElement(builder,
3056 vertex_index, idx, "");
3057 }
3058 if (is_aindex_indirect) {
3059 attr_chan_index = LLVMBuildExtractElement(builder,
3060 attrib_index, idx, "");
3061 }
3062
3063 indices[0] = vert_chan_index;
3064 indices[1] = attr_chan_index;
3065 indices[2] = swizzle_index;
3066
3067 channel_vec = LLVMBuildGEP(builder, tcs->output, indices, 3, "");
3068 channel_vec = LLVMBuildLoad(builder, channel_vec, "");
3069
3070 res = LLVMBuildInsertElement(builder, res, channel_vec, idx, "");
3071 }
3072 } else {
3073 indices[0] = vertex_index ? vertex_index : lp_build_const_int32(gallivm, 0);
3074 indices[1] = attrib_index;
3075 indices[2] = swizzle_index;
3076
3077 res = LLVMBuildGEP(builder, tcs->output, indices, 3, "");
3078 res = LLVMBuildLoad(builder, res, "");
3079 res = lp_build_broadcast_scalar(bld, res);
3080 }
3081 return res;
3082 }
3083
3084 static void
3085 draw_tcs_llvm_emit_store_output(const struct lp_build_tcs_iface *tes_iface,
3086 struct lp_build_context *bld,
3087 unsigned name,
3088 boolean is_vindex_indirect,
3089 LLVMValueRef vertex_index,
3090 boolean is_aindex_indirect,
3091 LLVMValueRef attrib_index,
3092 LLVMValueRef swizzle_index,
3093 LLVMValueRef value,
3094 LLVMValueRef mask_vec)
3095 {
3096 const struct draw_tcs_llvm_iface *tcs = draw_tcs_llvm_iface(tes_iface);
3097 struct gallivm_state *gallivm = bld->gallivm;
3098 LLVMBuilderRef builder = gallivm->builder;
3099 LLVMValueRef indices[3];
3100 LLVMValueRef res;
3101 struct lp_type type = bld->type;
3102
3103 if (is_vindex_indirect || is_aindex_indirect) {
3104 int i;
3105
3106 for (i = 0; i < type.length; ++i) {
3107 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
3108 LLVMValueRef vert_chan_index = vertex_index ? vertex_index : lp_build_const_int32(gallivm, 0);
3109 LLVMValueRef attr_chan_index = attrib_index;
3110 LLVMValueRef channel_vec;
3111
3112 if (is_vindex_indirect) {
3113 vert_chan_index = LLVMBuildExtractElement(builder,
3114 vertex_index, idx, "");
3115 }
3116 if (is_aindex_indirect) {
3117 attr_chan_index = LLVMBuildExtractElement(builder,
3118 attrib_index, idx, "");
3119 }
3120
3121 indices[0] = vert_chan_index;
3122 indices[1] = attr_chan_index;
3123 indices[2] = swizzle_index;
3124
3125 channel_vec = LLVMBuildGEP(builder, tcs->output, indices, 3, "");
3126
3127 res = LLVMBuildExtractElement(builder, value, idx, "");
3128
3129 struct lp_build_if_state ifthen;
3130 LLVMValueRef cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, mask_vec, lp_build_const_int_vec(gallivm, bld->type, 0), "");
3131 cond = LLVMBuildExtractElement(gallivm->builder, cond, idx, "");
3132 lp_build_if(&ifthen, gallivm, cond);
3133 LLVMBuildStore(builder, res, channel_vec);
3134 lp_build_endif(&ifthen);
3135 }
3136 } else {
3137 indices[0] = vertex_index ? vertex_index : lp_build_const_int32(gallivm, 0);
3138 indices[1] = attrib_index;
3139 indices[2] = swizzle_index;
3140
3141 res = LLVMBuildGEP(builder, tcs->output, indices, 3, "");
3142 for (unsigned i = 0; i < type.length; ++i) {
3143 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
3144 LLVMValueRef val = LLVMBuildExtractElement(builder, value, idx, "");
3145
3146 struct lp_build_if_state ifthen;
3147 LLVMValueRef cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, mask_vec, lp_build_const_int_vec(gallivm, bld->type, 0), "");
3148 cond = LLVMBuildExtractElement(gallivm->builder, cond, idx, "");
3149 lp_build_if(&ifthen, gallivm, cond);
3150 LLVMBuildStore(builder, val, res);
3151 lp_build_endif(&ifthen);
3152 }
3153 }
3154 }
3155
3156
3157 static LLVMValueRef
3158 generate_tcs_mask_value(struct draw_tcs_llvm_variant *variant,
3159 struct lp_type tcs_type, LLVMValueRef limit, LLVMValueRef loop_counter)
3160 {
3161 struct gallivm_state *gallivm = variant->gallivm;
3162 LLVMBuilderRef builder = gallivm->builder;
3163 struct lp_type mask_type = lp_int_type(tcs_type);
3164 LLVMValueRef num_vecs;
3165 LLVMValueRef mask_val = lp_build_const_vec(gallivm, mask_type, 0);
3166 unsigned i;
3167
3168 num_vecs = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, mask_type), limit);
3169 for (i = 0; i < tcs_type.length; i++) {
3170 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
3171 mask_val = LLVMBuildInsertElement(builder, mask_val, LLVMBuildAdd(builder, loop_counter, idx, ""), idx, "");
3172 }
3173 mask_val = lp_build_compare(gallivm, mask_type,
3174 PIPE_FUNC_GREATER, num_vecs, mask_val);
3175
3176 return mask_val;
3177 }
3178
3179 static void
3180 draw_tcs_llvm_generate(struct draw_llvm *llvm,
3181 struct draw_tcs_llvm_variant *variant)
3182 {
3183 struct gallivm_state *gallivm = variant->gallivm;
3184 LLVMContextRef context = gallivm->context;
3185 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context);
3186 LLVMTypeRef arg_types[6];
3187 LLVMTypeRef func_type, coro_func_type;
3188 LLVMValueRef variant_func, variant_coro;
3189 LLVMValueRef context_ptr;
3190 LLVMValueRef input_array, output_array, prim_id, patch_vertices_in;
3191 LLVMValueRef mask_val;
3192 LLVMBasicBlockRef block;
3193 LLVMBuilderRef builder;
3194 struct lp_build_context bld, bldvec;
3195 struct lp_build_sampler_soa *sampler = 0;
3196 struct lp_build_image_soa *image = NULL;
3197 struct lp_bld_tgsi_system_values system_values;
3198 char func_name[64], func_name_coro[64];
3199 unsigned i;
3200 struct draw_tcs_llvm_iface tcs_iface;
3201 struct lp_build_mask_context mask;
3202 LLVMValueRef consts_ptr, num_consts_ptr;
3203 LLVMValueRef ssbos_ptr, num_ssbos_ptr;
3204 struct lp_type tcs_type;
3205 unsigned vector_length = variant->shader->base.vector_length;
3206
3207 memset(&system_values, 0, sizeof(system_values));
3208
3209 snprintf(func_name, sizeof(func_name), "draw_llvm_tcs_variant%u",
3210 variant->shader->variants_cached);
3211
3212 snprintf(func_name_coro, sizeof(func_name_coro), "draw_llvm_tcs_coro_variant%u",
3213 variant->shader->variants_cached);
3214
3215 arg_types[0] = get_tcs_context_ptr_type(variant); /* context */
3216 arg_types[1] = variant->input_array_type; /* input */
3217 arg_types[2] = variant->output_array_type;
3218 arg_types[3] = int32_type;
3219 arg_types[4] = int32_type;
3220 arg_types[5] = int32_type; /* coroutine only */
3221
3222 func_type = LLVMFunctionType(int32_type, arg_types, ARRAY_SIZE(arg_types) - 1, 0);
3223
3224 coro_func_type = LLVMFunctionType(LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0), arg_types, ARRAY_SIZE(arg_types), 0);
3225
3226 variant_func = LLVMAddFunction(gallivm->module, func_name, func_type);
3227
3228 variant_coro = LLVMAddFunction(gallivm->module, func_name_coro, coro_func_type);
3229
3230 variant->function = variant_func;
3231 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv);
3232
3233 LLVMSetFunctionCallConv(variant_coro, LLVMCCallConv);
3234
3235 for (i = 0; i < ARRAY_SIZE(arg_types); ++i) {
3236 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) {
3237 lp_add_function_attr(variant_coro, i + 1, LP_FUNC_ATTR_NOALIAS);
3238 lp_add_function_attr(variant_func, i + 1, LP_FUNC_ATTR_NOALIAS);
3239 }
3240 }
3241
3242 context_ptr = LLVMGetParam(variant_func, 0);
3243 input_array = LLVMGetParam(variant_func, 1);
3244 output_array = LLVMGetParam(variant_func, 2);
3245 prim_id = LLVMGetParam(variant_func, 3);
3246 patch_vertices_in = LLVMGetParam(variant_func, 4);
3247
3248 lp_build_name(context_ptr, "context");
3249 lp_build_name(input_array, "input");
3250 lp_build_name(output_array, "output");
3251 lp_build_name(prim_id, "prim_id");
3252 lp_build_name(patch_vertices_in, "patch_vertices_in");
3253
3254 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry");
3255 builder = gallivm->builder;
3256 LLVMPositionBuilderAtEnd(builder, block);
3257
3258 lp_build_context_init(&bld, gallivm, lp_type_int(32));
3259
3260 memset(&tcs_type, 0, sizeof tcs_type);
3261 tcs_type.floating = TRUE; /* floating point values */
3262 tcs_type.sign = TRUE; /* values are signed */
3263 tcs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */
3264 tcs_type.width = 32; /* 32-bit float */
3265 tcs_type.length = vector_length;
3266
3267 lp_build_context_init(&bldvec, variant->gallivm, lp_int_type(tcs_type));
3268
3269 LLVMValueRef count = lp_build_const_int32(gallivm, variant->shader->base.vertices_out);
3270 LLVMValueRef step = lp_build_const_int32(gallivm, vector_length);
3271
3272 struct lp_build_loop_state loop_state[2];
3273 LLVMValueRef num_inner_loop;
3274 unsigned count_align = util_align_npot(variant->shader->base.vertices_out, tcs_type.length);
3275 num_inner_loop = lp_build_const_int32(gallivm, count_align / tcs_type.length);
3276 LLVMTypeRef hdl_ptr_type = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0);
3277 LLVMValueRef coro_hdls = LLVMBuildArrayAlloca(gallivm->builder, hdl_ptr_type, num_inner_loop, "coro_hdls");
3278 unsigned end_coroutine = INT_MAX;
3279 lp_build_loop_begin(&loop_state[1], gallivm,
3280 lp_build_const_int32(gallivm, 0)); /* coroutine reentry loop */
3281 lp_build_loop_begin(&loop_state[0], gallivm,
3282 lp_build_const_int32(gallivm, 0)); /* inner loop */
3283 {
3284 LLVMValueRef args[6];
3285 args[0] = context_ptr;
3286 args[1] = input_array;
3287 args[2] = output_array;
3288 args[3] = prim_id;
3289 args[4] = patch_vertices_in;
3290 args[5] = loop_state[0].counter;
3291 LLVMValueRef coro_entry = LLVMBuildGEP(builder, coro_hdls, &loop_state[0].counter, 1, "");
3292 LLVMValueRef coro_hdl = LLVMBuildLoad(builder, coro_entry, "coro_hdl");
3293
3294 struct lp_build_if_state ifstate;
3295 LLVMValueRef cmp = LLVMBuildICmp(builder, LLVMIntEQ, loop_state[1].counter,
3296 lp_build_const_int32(gallivm, 0), "");
3297 /* first time here - call the coroutine function entry point */
3298 lp_build_if(&ifstate, gallivm, cmp);
3299 LLVMValueRef coro_ret = LLVMBuildCall(builder, variant_coro, args, 6, "");
3300 LLVMBuildStore(builder, coro_ret, coro_entry);
3301 lp_build_else(&ifstate);
3302 /* subsequent calls for this invocation - check if done. */
3303 LLVMValueRef coro_done = lp_build_coro_done(gallivm, coro_hdl);
3304 struct lp_build_if_state ifstate2;
3305 lp_build_if(&ifstate2, gallivm, coro_done);
3306 /* if done destroy and force loop exit */
3307 lp_build_coro_destroy(gallivm, coro_hdl);
3308 lp_build_loop_force_set_counter(&loop_state[1], lp_build_const_int32(gallivm, end_coroutine - 1));
3309 lp_build_else(&ifstate2);
3310 /* otherwise resume the coroutine */
3311 lp_build_coro_resume(gallivm, coro_hdl);
3312 lp_build_endif(&ifstate2);
3313 lp_build_endif(&ifstate);
3314 lp_build_loop_force_reload_counter(&loop_state[1]);
3315 }
3316 lp_build_loop_end_cond(&loop_state[0],
3317 num_inner_loop,
3318 NULL, LLVMIntUGE);
3319 lp_build_loop_end_cond(&loop_state[1],
3320 lp_build_const_int32(gallivm, end_coroutine),
3321 NULL, LLVMIntEQ);
3322 LLVMBuildRet(builder, lp_build_zero(gallivm, lp_type_uint(32)));
3323
3324 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_coro, "entry");
3325 LLVMPositionBuilderAtEnd(builder, block);
3326
3327 context_ptr = LLVMGetParam(variant_coro, 0);
3328 input_array = LLVMGetParam(variant_coro, 1);
3329 output_array = LLVMGetParam(variant_coro, 2);
3330 prim_id = LLVMGetParam(variant_coro, 3);
3331 patch_vertices_in = LLVMGetParam(variant_coro, 4);
3332
3333 consts_ptr = draw_tcs_jit_context_constants(variant->gallivm, context_ptr);
3334 num_consts_ptr =
3335 draw_tcs_jit_context_num_constants(variant->gallivm, context_ptr);
3336
3337 ssbos_ptr = draw_tcs_jit_context_ssbos(variant->gallivm, context_ptr);
3338 num_ssbos_ptr =
3339 draw_tcs_jit_context_num_ssbos(variant->gallivm, context_ptr);
3340 sampler = draw_llvm_sampler_soa_create(variant->key.samplers);
3341 image = draw_llvm_image_soa_create(draw_tcs_llvm_variant_key_images(&variant->key));
3342
3343 LLVMValueRef counter = LLVMGetParam(variant_coro, 5);
3344 LLVMValueRef invocvec = LLVMGetUndef(LLVMVectorType(int32_type, vector_length));
3345 for (i = 0; i < vector_length; i++) {
3346 LLVMValueRef idx = LLVMBuildAdd(builder, LLVMBuildMul(builder, counter, step, ""), lp_build_const_int32(gallivm, i), "");
3347 invocvec = LLVMBuildInsertElement(builder, invocvec, idx, idx, "");
3348 }
3349
3350 system_values.invocation_id = invocvec;
3351 system_values.prim_id = lp_build_broadcast_scalar(&bldvec, prim_id);
3352 system_values.vertices_in = lp_build_broadcast_scalar(&bldvec, patch_vertices_in);
3353 tcs_iface.input = input_array;
3354 tcs_iface.output = output_array;
3355 tcs_iface.base.emit_fetch_input = draw_tcs_llvm_emit_fetch_input;
3356 tcs_iface.base.emit_fetch_output = draw_tcs_llvm_emit_fetch_output;
3357 tcs_iface.base.emit_store_output = draw_tcs_llvm_emit_store_output;
3358
3359
3360 {
3361 LLVMValueRef coro_id = lp_build_coro_id(gallivm);
3362 LLVMValueRef coro_hdl = lp_build_coro_begin_alloc_mem(gallivm, coro_id);
3363
3364 mask_val = generate_tcs_mask_value(variant, tcs_type, count, LLVMBuildMul(builder, counter, step, ""));
3365 lp_build_mask_begin(&mask, gallivm, tcs_type, mask_val);
3366
3367 struct lp_build_coro_suspend_info coro_info;
3368
3369 LLVMBasicBlockRef sus_block = LLVMAppendBasicBlockInContext(gallivm->context, variant_coro, "suspend");
3370 LLVMBasicBlockRef clean_block = LLVMAppendBasicBlockInContext(gallivm->context, variant_coro, "cleanup");
3371
3372 coro_info.suspend = sus_block;
3373 coro_info.cleanup = clean_block;
3374
3375 struct lp_build_tgsi_params params;
3376 memset(&params, 0, sizeof(params));
3377
3378 params.type = tcs_type;
3379 params.mask = &mask;
3380 params.consts_ptr = consts_ptr;
3381 params.const_sizes_ptr = num_consts_ptr;
3382 params.system_values = &system_values;
3383 params.context_ptr = context_ptr;
3384 params.sampler = sampler;
3385 params.info = &llvm->draw->tcs.tess_ctrl_shader->info;
3386 params.ssbo_ptr = ssbos_ptr;
3387 params.ssbo_sizes_ptr = num_ssbos_ptr;
3388 params.image = image;
3389 params.coro = &coro_info;
3390 params.tcs_iface = &tcs_iface.base;
3391
3392 lp_build_nir_soa(variant->gallivm,
3393 llvm->draw->tcs.tess_ctrl_shader->state.ir.nir,
3394 &params, NULL);
3395
3396 lp_build_mask_end(&mask);
3397
3398 lp_build_coro_suspend_switch(gallivm, &coro_info, NULL, true);
3399 LLVMPositionBuilderAtEnd(builder, clean_block);
3400
3401 lp_build_coro_free_mem(gallivm, coro_id, coro_hdl);
3402
3403 LLVMBuildBr(builder, sus_block);
3404 LLVMPositionBuilderAtEnd(builder, sus_block);
3405
3406 lp_build_coro_end(gallivm, coro_hdl);
3407 LLVMBuildRet(builder, coro_hdl);
3408 }
3409
3410 sampler->destroy(sampler);
3411 image->destroy(image);
3412 gallivm_verify_function(gallivm, variant_func);
3413 gallivm_verify_function(gallivm, variant_coro);
3414 }
3415
3416 struct draw_tcs_llvm_variant *
3417 draw_tcs_llvm_create_variant(struct draw_llvm *llvm,
3418 unsigned num_outputs,
3419 const struct draw_tcs_llvm_variant_key *key)
3420 {
3421 struct draw_tcs_llvm_variant *variant;
3422 struct llvm_tess_ctrl_shader *shader = llvm_tess_ctrl_shader(llvm->draw->tcs.tess_ctrl_shader);
3423 char module_name[64];
3424
3425 variant = MALLOC(sizeof *variant +
3426 shader->variant_key_size - sizeof variant->key);
3427 if (!variant)
3428 return NULL;
3429
3430 variant->llvm = llvm;
3431 variant->shader = shader;
3432
3433 snprintf(module_name, sizeof(module_name), "draw_llvm_tcs_variant%u",
3434 variant->shader->variants_cached);
3435
3436 variant->gallivm = gallivm_create(module_name, llvm->context);
3437
3438 create_tcs_jit_types(variant);
3439
3440 memcpy(&variant->key, key, shader->variant_key_size);
3441
3442 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
3443 nir_print_shader(llvm->draw->tcs.tess_ctrl_shader->state.ir.nir, stderr);
3444 draw_tcs_llvm_dump_variant_key(&variant->key);
3445 }
3446
3447 draw_tcs_llvm_generate(llvm, variant);
3448
3449 gallivm_compile_module(variant->gallivm);
3450
3451 variant->jit_func = (draw_tcs_jit_func)
3452 gallivm_jit_function(variant->gallivm, variant->function);
3453
3454 gallivm_free_ir(variant->gallivm);
3455
3456 variant->list_item_global.base = variant;
3457 variant->list_item_local.base = variant;
3458 /*variant->no = */shader->variants_created++;
3459 variant->list_item_global.base = variant;
3460
3461 return variant;
3462 }
3463
3464 void
3465 draw_tcs_llvm_destroy_variant(struct draw_tcs_llvm_variant *variant)
3466 {
3467 struct draw_llvm *llvm = variant->llvm;
3468
3469 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
3470 debug_printf("Deleting TCS variant: %u tcs variants,\t%u total variants\n",
3471 variant->shader->variants_cached, llvm->nr_tcs_variants);
3472 }
3473
3474 gallivm_destroy(variant->gallivm);
3475
3476 remove_from_list(&variant->list_item_local);
3477 variant->shader->variants_cached--;
3478 remove_from_list(&variant->list_item_global);
3479 llvm->nr_tcs_variants--;
3480 FREE(variant);
3481 }
3482
3483 struct draw_tcs_llvm_variant_key *
3484 draw_tcs_llvm_make_variant_key(struct draw_llvm *llvm, char *store)
3485 {
3486 unsigned i;
3487 struct draw_tcs_llvm_variant_key *key;
3488 struct draw_sampler_static_state *draw_sampler;
3489 struct draw_image_static_state *draw_image;
3490
3491 key = (struct draw_tcs_llvm_variant_key *)store;
3492
3493 memset(key, 0, offsetof(struct draw_tcs_llvm_variant_key, samplers[0]));
3494
3495 /* All variants of this shader will have the same value for
3496 * nr_samplers. Not yet trying to compact away holes in the
3497 * sampler array.
3498 */
3499 key->nr_samplers = llvm->draw->tcs.tess_ctrl_shader->info.file_max[TGSI_FILE_SAMPLER] + 1;
3500 if (llvm->draw->tcs.tess_ctrl_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) {
3501 key->nr_sampler_views =
3502 llvm->draw->tcs.tess_ctrl_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1;
3503 }
3504 else {
3505 key->nr_sampler_views = key->nr_samplers;
3506 }
3507
3508 key->nr_images = llvm->draw->tcs.tess_ctrl_shader->info.file_max[TGSI_FILE_IMAGE] + 1;
3509
3510 draw_sampler = key->samplers;
3511
3512 memset(draw_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler);
3513
3514 for (i = 0 ; i < key->nr_samplers; i++) {
3515 lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state,
3516 llvm->draw->samplers[PIPE_SHADER_TESS_CTRL][i]);
3517 }
3518 for (i = 0 ; i < key->nr_sampler_views; i++) {
3519 lp_sampler_static_texture_state(&draw_sampler[i].texture_state,
3520 llvm->draw->sampler_views[PIPE_SHADER_TESS_CTRL][i]);
3521 }
3522
3523 draw_image = draw_tcs_llvm_variant_key_images(key);
3524 memset(draw_image, 0,
3525 key->nr_images * sizeof *draw_image);
3526 for (i = 0; i < key->nr_images; i++) {
3527 lp_sampler_static_texture_state_image(&draw_image[i].image_state,
3528 llvm->draw->images[PIPE_SHADER_TESS_CTRL][i]);
3529 }
3530 return key;
3531 }
3532
3533 void
3534 draw_tcs_llvm_dump_variant_key(struct draw_tcs_llvm_variant_key *key)
3535 {
3536 unsigned i;
3537 struct draw_sampler_static_state *sampler = key->samplers;
3538 struct draw_image_static_state *image = draw_tcs_llvm_variant_key_images(key);
3539 for (i = 0 ; i < key->nr_sampler_views; i++) {
3540 debug_printf("sampler[%i].src_format = %s\n", i,
3541 util_format_name(sampler[i].texture_state.format));
3542 }
3543
3544 for (i = 0 ; i < key->nr_images; i++)
3545 debug_printf("images[%i].format = %s\n", i, util_format_name(image[i].image_state.format));
3546
3547 }
3548
3549 static void
3550 create_tes_jit_types(struct draw_tes_llvm_variant *var)
3551 {
3552 struct gallivm_state *gallivm = var->gallivm;
3553 LLVMTypeRef texture_type, sampler_type, image_type, context_type;
3554
3555 texture_type = create_jit_texture_type(gallivm, "texture");
3556 sampler_type = create_jit_sampler_type(gallivm, "sampler");
3557 image_type = create_jit_image_type(gallivm, "image");
3558
3559 context_type = create_tes_jit_context_type(gallivm,
3560 0,
3561 texture_type, sampler_type,
3562 image_type,
3563 "draw_tes_jit_context");
3564 var->context_ptr_type = LLVMPointerType(context_type, 0);
3565
3566 var->input_array_type = create_tes_jit_input_type(gallivm);
3567 }
3568
3569 static LLVMTypeRef
3570 get_tes_context_ptr_type(struct draw_tes_llvm_variant *variant)
3571 {
3572 if (!variant->context_ptr_type)
3573 create_tes_jit_types(variant);
3574 return variant->context_ptr_type;
3575 }
3576
3577 static LLVMValueRef
3578 generate_tes_mask_value(struct draw_tes_llvm_variant *variant,
3579 struct lp_type tes_type, LLVMValueRef limit, LLVMValueRef loop_counter)
3580 {
3581 struct gallivm_state *gallivm = variant->gallivm;
3582 LLVMBuilderRef builder = gallivm->builder;
3583 struct lp_type mask_type = lp_int_type(tes_type);
3584 LLVMValueRef num_prims;
3585 LLVMValueRef mask_val = lp_build_const_vec(gallivm, mask_type, 0);
3586 unsigned i;
3587
3588 num_prims = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, mask_type), limit);
3589 for (i = 0; i < tes_type.length; i++) {
3590 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
3591 mask_val = LLVMBuildInsertElement(builder, mask_val, LLVMBuildAdd(builder, loop_counter, idx, ""), idx, "");
3592 }
3593 mask_val = lp_build_compare(gallivm, mask_type,
3594 PIPE_FUNC_GREATER, num_prims, mask_val);
3595
3596 return mask_val;
3597 }
3598
3599 static LLVMValueRef
3600 draw_tes_llvm_fetch_vertex_input(const struct lp_build_tes_iface *tes_iface,
3601 struct lp_build_context *bld,
3602 boolean is_vindex_indirect,
3603 LLVMValueRef vertex_index,
3604 boolean is_aindex_indirect,
3605 LLVMValueRef attrib_index,
3606 LLVMValueRef swizzle_index)
3607 {
3608 const struct draw_tes_llvm_iface *tes = draw_tes_llvm_iface(tes_iface);
3609 struct gallivm_state *gallivm = bld->gallivm;
3610 LLVMBuilderRef builder = gallivm->builder;
3611 LLVMValueRef indices[3];
3612 LLVMValueRef res;
3613 struct lp_type type = bld->type;
3614
3615 if (is_vindex_indirect || is_aindex_indirect) {
3616 int i;
3617
3618 res = bld->zero;
3619
3620 for (i = 0; i < type.length; ++i) {
3621 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
3622 LLVMValueRef vert_chan_index = vertex_index;
3623 LLVMValueRef attr_chan_index = attrib_index;
3624 LLVMValueRef channel_vec;
3625
3626 if (is_vindex_indirect) {
3627 vert_chan_index = LLVMBuildExtractElement(builder,
3628 vertex_index, idx, "");
3629 }
3630 if (is_aindex_indirect) {
3631 attr_chan_index = LLVMBuildExtractElement(builder,
3632 attrib_index, idx, "");
3633 }
3634
3635 indices[0] = vert_chan_index;
3636 indices[1] = attr_chan_index;
3637 indices[2] = swizzle_index;
3638
3639 channel_vec = LLVMBuildGEP(builder, tes->input, indices, 3, "");
3640 channel_vec = LLVMBuildLoad(builder, channel_vec, "");
3641
3642 res = LLVMBuildInsertElement(builder, res, channel_vec, idx, "");
3643 }
3644 } else {
3645 indices[0] = vertex_index;
3646 indices[1] = attrib_index;
3647 indices[2] = swizzle_index;
3648
3649 res = LLVMBuildGEP(builder, tes->input, indices, 3, "");
3650 res = LLVMBuildLoad(builder, res, "");
3651 res = lp_build_broadcast_scalar(bld, res);
3652 }
3653 return res;
3654 }
3655
3656 static LLVMValueRef
3657 draw_tes_llvm_fetch_patch_input(const struct lp_build_tes_iface *tes_iface,
3658 struct lp_build_context *bld,
3659 boolean is_aindex_indirect,
3660 LLVMValueRef attrib_index,
3661 LLVMValueRef swizzle_index)
3662 {
3663 const struct draw_tes_llvm_iface *tes = draw_tes_llvm_iface(tes_iface);
3664 struct gallivm_state *gallivm = bld->gallivm;
3665 LLVMBuilderRef builder = gallivm->builder;
3666 LLVMValueRef indices[3];
3667 LLVMValueRef res;
3668 struct lp_type type = bld->type;
3669
3670 if (is_aindex_indirect) {
3671 int i;
3672
3673 res = bld->zero;
3674
3675 for (i = 0; i < type.length; ++i) {
3676 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
3677 LLVMValueRef attr_chan_index = attrib_index;
3678 LLVMValueRef channel_vec;
3679
3680 if (is_aindex_indirect) {
3681 attr_chan_index = LLVMBuildExtractElement(builder,
3682 attrib_index, idx, "");
3683 }
3684
3685 indices[0] = lp_build_const_int32(gallivm, 0);
3686 indices[1] = attr_chan_index;
3687 indices[2] = swizzle_index;
3688
3689 channel_vec = LLVMBuildGEP(builder, tes->input, indices, 3, "");
3690 channel_vec = LLVMBuildLoad(builder, channel_vec, "");
3691
3692 res = LLVMBuildInsertElement(builder, res, channel_vec, idx, "");
3693 }
3694 } else {
3695 indices[0] = lp_build_const_int32(gallivm, 0);
3696 indices[1] = attrib_index;
3697 indices[2] = swizzle_index;
3698
3699 res = LLVMBuildGEP(builder, tes->input, indices, 3, "");
3700 res = LLVMBuildLoad(builder, res, "");
3701 res = lp_build_broadcast_scalar(bld, res);
3702 }
3703 return res;
3704 }
3705
3706 static void
3707 draw_tes_llvm_generate(struct draw_llvm *llvm,
3708 struct draw_tes_llvm_variant *variant)
3709 {
3710 struct gallivm_state *gallivm = variant->gallivm;
3711 LLVMContextRef context = gallivm->context;
3712 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context);
3713 LLVMTypeRef flt_type = LLVMFloatTypeInContext(context);
3714 LLVMTypeRef arg_types[10];
3715 LLVMTypeRef func_type;
3716 LLVMValueRef variant_func;
3717 LLVMValueRef context_ptr;
3718 LLVMValueRef tess_coord[2], io_ptr, input_array, num_tess_coord;
3719 LLVMValueRef tess_inner, tess_outer, prim_id, patch_vertices_in;
3720 LLVMBasicBlockRef block;
3721 LLVMBuilderRef builder;
3722 LLVMValueRef mask_val;
3723 struct lp_build_context bld, bldvec;
3724 struct lp_build_sampler_soa *sampler = 0;
3725 struct lp_build_image_soa *image = NULL;
3726 struct lp_bld_tgsi_system_values system_values;
3727 char func_name[64];
3728 unsigned i;
3729 struct draw_tes_llvm_iface tes_iface;
3730 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
3731 struct lp_build_mask_context mask;
3732 LLVMValueRef consts_ptr, num_consts_ptr;
3733 LLVMValueRef ssbos_ptr, num_ssbos_ptr;
3734 LLVMValueRef step;
3735 struct lp_type tes_type;
3736 unsigned vector_length = variant->shader->base.vector_length;
3737
3738 memset(&system_values, 0, sizeof(system_values));
3739 memset(&outputs, 0, sizeof(outputs));
3740
3741 snprintf(func_name, sizeof(func_name), "draw_llvm_tes_variant%u",
3742 variant->shader->variants_cached);
3743
3744 arg_types[0] = get_tes_context_ptr_type(variant); /* context */
3745 arg_types[1] = variant->input_array_type; /* input */
3746 arg_types[2] = variant->vertex_header_ptr_type;
3747 arg_types[3] = int32_type;
3748 arg_types[4] = int32_type;
3749 arg_types[5] = LLVMPointerType(flt_type, 0);
3750 arg_types[6] = LLVMPointerType(flt_type, 0);
3751 arg_types[7] = LLVMPointerType(LLVMArrayType(flt_type, 4), 0);
3752 arg_types[8] = LLVMPointerType(LLVMArrayType(flt_type, 2), 0);
3753 arg_types[9] = int32_type;
3754
3755 func_type = LLVMFunctionType(int32_type, arg_types, ARRAY_SIZE(arg_types), 0);
3756 variant_func = LLVMAddFunction(gallivm->module, func_name, func_type);
3757
3758 variant->function = variant_func;
3759 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv);
3760
3761 for (i = 0; i < ARRAY_SIZE(arg_types); ++i)
3762 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
3763 lp_add_function_attr(variant_func, i + 1, LP_FUNC_ATTR_NOALIAS);
3764
3765 context_ptr = LLVMGetParam(variant_func, 0);
3766 input_array = LLVMGetParam(variant_func, 1);
3767 io_ptr = LLVMGetParam(variant_func, 2);
3768 prim_id = LLVMGetParam(variant_func, 3);
3769 num_tess_coord = LLVMGetParam(variant_func, 4);
3770 tess_coord[0] = LLVMGetParam(variant_func, 5);
3771 tess_coord[1] = LLVMGetParam(variant_func, 6);
3772 tess_outer = LLVMGetParam(variant_func, 7);
3773 tess_inner = LLVMGetParam(variant_func, 8);
3774 patch_vertices_in = LLVMGetParam(variant_func, 9);
3775
3776 lp_build_name(context_ptr, "context");
3777 lp_build_name(input_array, "input");
3778 lp_build_name(io_ptr, "io");
3779 lp_build_name(prim_id, "prim_id");
3780 lp_build_name(num_tess_coord, "num_tess_coord");
3781 lp_build_name(tess_coord[0], "tess_coord[0]");
3782 lp_build_name(tess_coord[1], "tess_coord[1]");
3783 lp_build_name(tess_outer, "tess_outer");
3784 lp_build_name(tess_inner, "tess_inner");
3785 lp_build_name(patch_vertices_in, "patch_vertices_in");
3786
3787 tes_iface.base.fetch_vertex_input = draw_tes_llvm_fetch_vertex_input;
3788 tes_iface.base.fetch_patch_input = draw_tes_llvm_fetch_patch_input;
3789 tes_iface.input = input_array;
3790 tes_iface.variant = variant;
3791
3792 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry");
3793 builder = gallivm->builder;
3794 LLVMPositionBuilderAtEnd(builder, block);
3795
3796 lp_build_context_init(&bld, gallivm, lp_type_int(32));
3797
3798 memset(&tes_type, 0, sizeof tes_type);
3799 tes_type.floating = TRUE; /* floating point values */
3800 tes_type.sign = TRUE; /* values are signed */
3801 tes_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */
3802 tes_type.width = 32; /* 32-bit float */
3803 tes_type.length = vector_length;
3804
3805 lp_build_context_init(&bldvec, variant->gallivm, lp_int_type(tes_type));
3806 consts_ptr = draw_tes_jit_context_constants(variant->gallivm, context_ptr);
3807 num_consts_ptr =
3808 draw_tes_jit_context_num_constants(variant->gallivm, context_ptr);
3809
3810 ssbos_ptr = draw_tes_jit_context_ssbos(variant->gallivm, context_ptr);
3811 num_ssbos_ptr =
3812 draw_tes_jit_context_num_ssbos(variant->gallivm, context_ptr);
3813 sampler = draw_llvm_sampler_soa_create(variant->key.samplers);
3814 image = draw_llvm_image_soa_create(draw_tes_llvm_variant_key_images(&variant->key));
3815 step = lp_build_const_int32(gallivm, vector_length);
3816
3817 system_values.tess_outer = LLVMBuildLoad(builder, tess_outer, "");
3818 system_values.tess_inner = LLVMBuildLoad(builder, tess_inner, "");
3819
3820 system_values.prim_id = lp_build_broadcast_scalar(&bldvec, prim_id);
3821
3822 system_values.vertices_in = lp_build_broadcast_scalar(&bldvec, patch_vertices_in);
3823 struct lp_build_loop_state lp_loop;
3824 lp_build_loop_begin(&lp_loop, gallivm, bld.zero);
3825 {
3826 LLVMValueRef io;
3827
3828 io = LLVMBuildGEP(builder, io_ptr, &lp_loop.counter, 1, "");
3829 mask_val = generate_tes_mask_value(variant, tes_type, num_tess_coord, lp_loop.counter);
3830 lp_build_mask_begin(&mask, gallivm, tes_type, mask_val);
3831
3832 system_values.tess_coord = LLVMGetUndef(LLVMArrayType(LLVMVectorType(flt_type, vector_length), 3));
3833 for (i = 0; i < 3; i++) {
3834 LLVMValueRef tess_coord_chan = LLVMGetUndef(LLVMVectorType(flt_type, vector_length));
3835 for (unsigned j = 0; j < vector_length; j++) {
3836 LLVMValueRef idx = LLVMBuildAdd(builder, lp_loop.counter, lp_build_const_int32(gallivm, j), "");
3837 LLVMValueRef tc_val;
3838 if (i == 2) {
3839 if (variant->shader->base.prim_mode == PIPE_PRIM_TRIANGLES) {
3840 tc_val = lp_build_const_float(gallivm, 1.0);
3841 tc_val = LLVMBuildFSub(builder, tc_val, lp_build_pointer_get(builder, tess_coord[0], idx), "");
3842 tc_val = LLVMBuildFSub(builder, tc_val, lp_build_pointer_get(builder, tess_coord[1], idx), "");
3843 } else
3844 tc_val = lp_build_const_float(gallivm, 0.0);
3845 } else
3846 tc_val = lp_build_pointer_get(builder, tess_coord[i], idx);
3847
3848 tess_coord_chan = LLVMBuildInsertElement(builder, tess_coord_chan, tc_val, lp_build_const_int32(gallivm, j), "");
3849 }
3850 system_values.tess_coord = LLVMBuildInsertValue(builder, system_values.tess_coord, tess_coord_chan, i, "");
3851 }
3852
3853 struct lp_build_tgsi_params params;
3854 memset(&params, 0, sizeof(params));
3855
3856 params.type = tes_type;
3857 params.mask = &mask;
3858 params.consts_ptr = consts_ptr;
3859 params.const_sizes_ptr = num_consts_ptr;
3860 params.system_values = &system_values;
3861 params.context_ptr = context_ptr;
3862 params.sampler = sampler;
3863 params.info = &llvm->draw->tes.tess_eval_shader->info;
3864 params.ssbo_ptr = ssbos_ptr;
3865 params.ssbo_sizes_ptr = num_ssbos_ptr;
3866 params.image = image;
3867 params.tes_iface = &tes_iface.base;
3868
3869 lp_build_nir_soa(variant->gallivm,
3870 llvm->draw->tes.tess_eval_shader->state.ir.nir,
3871 &params,
3872 outputs);
3873
3874 lp_build_mask_end(&mask);
3875 LLVMValueRef clipmask = lp_build_const_int_vec(gallivm,
3876 lp_int_type(tes_type), 0);
3877
3878 convert_to_aos(gallivm, io, NULL, outputs, clipmask,
3879 params.info->num_outputs, tes_type, FALSE);
3880 }
3881 lp_build_loop_end_cond(&lp_loop, num_tess_coord, step, LLVMIntUGE);
3882 sampler->destroy(sampler);
3883 image->destroy(image);
3884
3885 LLVMBuildRet(builder, lp_build_zero(gallivm, lp_type_uint(32)));
3886 gallivm_verify_function(gallivm, variant_func);
3887 }
3888
3889 struct draw_tes_llvm_variant *
3890 draw_tes_llvm_create_variant(struct draw_llvm *llvm,
3891 unsigned num_outputs,
3892 const struct draw_tes_llvm_variant_key *key)
3893 {
3894 struct draw_tes_llvm_variant *variant;
3895 struct llvm_tess_eval_shader *shader = llvm_tess_eval_shader(llvm->draw->tes.tess_eval_shader);
3896 LLVMTypeRef vertex_header;
3897 char module_name[64];
3898
3899 variant = MALLOC(sizeof *variant +
3900 shader->variant_key_size - sizeof variant->key);
3901 if (!variant)
3902 return NULL;
3903
3904 variant->llvm = llvm;
3905 variant->shader = shader;
3906
3907 snprintf(module_name, sizeof(module_name), "draw_llvm_tes_variant%u",
3908 variant->shader->variants_cached);
3909
3910 variant->gallivm = gallivm_create(module_name, llvm->context);
3911
3912 create_tes_jit_types(variant);
3913
3914 memcpy(&variant->key, key, shader->variant_key_size);
3915
3916 vertex_header = create_jit_vertex_header(variant->gallivm, num_outputs);
3917
3918 variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0);
3919
3920 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
3921 nir_print_shader(llvm->draw->tes.tess_eval_shader->state.ir.nir, stderr);
3922 draw_tes_llvm_dump_variant_key(&variant->key);
3923 }
3924
3925 draw_tes_llvm_generate(llvm, variant);
3926
3927 gallivm_compile_module(variant->gallivm);
3928
3929 variant->jit_func = (draw_tes_jit_func)
3930 gallivm_jit_function(variant->gallivm, variant->function);
3931
3932 gallivm_free_ir(variant->gallivm);
3933
3934 variant->list_item_global.base = variant;
3935 variant->list_item_local.base = variant;
3936 /*variant->no = */shader->variants_created++;
3937 variant->list_item_global.base = variant;
3938
3939 return variant;
3940 }
3941
3942 void
3943 draw_tes_llvm_destroy_variant(struct draw_tes_llvm_variant *variant)
3944 {
3945 struct draw_llvm *llvm = variant->llvm;
3946
3947 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
3948 debug_printf("Deleting TES variant: %u tes variants,\t%u total variants\n",
3949 variant->shader->variants_cached, llvm->nr_tes_variants);
3950 }
3951
3952 gallivm_destroy(variant->gallivm);
3953
3954 remove_from_list(&variant->list_item_local);
3955 variant->shader->variants_cached--;
3956 remove_from_list(&variant->list_item_global);
3957 llvm->nr_tes_variants--;
3958 FREE(variant);
3959 }
3960
3961 struct draw_tes_llvm_variant_key *
3962 draw_tes_llvm_make_variant_key(struct draw_llvm *llvm, char *store)
3963 {
3964 unsigned i;
3965 struct draw_tes_llvm_variant_key *key;
3966 struct draw_sampler_static_state *draw_sampler;
3967 struct draw_image_static_state *draw_image;
3968
3969 key = (struct draw_tes_llvm_variant_key *)store;
3970
3971 memset(key, 0, offsetof(struct draw_tes_llvm_variant_key, samplers[0]));
3972
3973 /* All variants of this shader will have the same value for
3974 * nr_samplers. Not yet trying to compact away holes in the
3975 * sampler array.
3976 */
3977 key->nr_samplers = llvm->draw->tes.tess_eval_shader->info.file_max[TGSI_FILE_SAMPLER] + 1;
3978 if (llvm->draw->tes.tess_eval_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) {
3979 key->nr_sampler_views =
3980 llvm->draw->tes.tess_eval_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1;
3981 }
3982 else {
3983 key->nr_sampler_views = key->nr_samplers;
3984 }
3985
3986 key->nr_images = llvm->draw->tes.tess_eval_shader->info.file_max[TGSI_FILE_IMAGE] + 1;
3987
3988 draw_sampler = key->samplers;
3989
3990 memset(draw_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler);
3991
3992 for (i = 0 ; i < key->nr_samplers; i++) {
3993 lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state,
3994 llvm->draw->samplers[PIPE_SHADER_TESS_EVAL][i]);
3995 }
3996 for (i = 0 ; i < key->nr_sampler_views; i++) {
3997 lp_sampler_static_texture_state(&draw_sampler[i].texture_state,
3998 llvm->draw->sampler_views[PIPE_SHADER_TESS_EVAL][i]);
3999 }
4000
4001 draw_image = draw_tes_llvm_variant_key_images(key);
4002 memset(draw_image, 0,
4003 key->nr_images * sizeof *draw_image);
4004 for (i = 0; i < key->nr_images; i++) {
4005 lp_sampler_static_texture_state_image(&draw_image[i].image_state,
4006 llvm->draw->images[PIPE_SHADER_TESS_EVAL][i]);
4007 }
4008 return key;
4009 }
4010
4011 void
4012 draw_tes_llvm_dump_variant_key(struct draw_tes_llvm_variant_key *key)
4013 {
4014 unsigned i;
4015 struct draw_sampler_static_state *sampler = key->samplers;
4016 struct draw_image_static_state *image = draw_tes_llvm_variant_key_images(key);
4017 for (i = 0 ; i < key->nr_sampler_views; i++) {
4018 debug_printf("sampler[%i].src_format = %s\n", i,
4019 util_format_name(sampler[i].texture_state.format));
4020 }
4021
4022 for (i = 0 ; i < key->nr_images; i++)
4023 debug_printf("images[%i].format = %s\n", i, util_format_name(image[i].image_state.format));
4024
4025 }