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