draw/gs: track emitted prims + verts per stream.
[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_swizzle.h"
41 #include "gallivm/lp_bld_struct.h"
42 #include "gallivm/lp_bld_type.h"
43 #include "gallivm/lp_bld_flow.h"
44 #include "gallivm/lp_bld_debug.h"
45 #include "gallivm/lp_bld_tgsi.h"
46 #include "gallivm/lp_bld_nir.h"
47 #include "gallivm/lp_bld_printf.h"
48 #include "gallivm/lp_bld_intr.h"
49 #include "gallivm/lp_bld_init.h"
50 #include "gallivm/lp_bld_type.h"
51 #include "gallivm/lp_bld_pack.h"
52 #include "gallivm/lp_bld_format.h"
53
54 #include "tgsi/tgsi_exec.h"
55 #include "tgsi/tgsi_dump.h"
56
57 #include "util/u_math.h"
58 #include "util/u_pointer.h"
59 #include "util/u_string.h"
60 #include "util/simple_list.h"
61
62
63 #define DEBUG_STORE 0
64
65
66 static void
67 draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *var);
68
69
70 struct draw_gs_llvm_iface {
71 struct lp_build_gs_iface base;
72
73 struct draw_gs_llvm_variant *variant;
74 LLVMValueRef input;
75 };
76
77 static inline const struct draw_gs_llvm_iface *
78 draw_gs_llvm_iface(const struct lp_build_gs_iface *iface)
79 {
80 return (const struct draw_gs_llvm_iface *)iface;
81 }
82
83 /**
84 * Create LLVM type for draw_vertex_buffer.
85 */
86 static LLVMTypeRef
87 create_jit_dvbuffer_type(struct gallivm_state *gallivm,
88 const char *struct_name)
89 {
90 LLVMTargetDataRef target = gallivm->target;
91 LLVMTypeRef dvbuffer_type;
92 LLVMTypeRef elem_types[DRAW_JIT_DVBUFFER_NUM_FIELDS];
93 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
94
95 elem_types[DRAW_JIT_DVBUFFER_MAP] =
96 LLVMPointerType(LLVMIntTypeInContext(gallivm->context, 8), 0);
97 elem_types[DRAW_JIT_DVBUFFER_SIZE] = int32_type;
98
99 dvbuffer_type = LLVMStructTypeInContext(gallivm->context, elem_types,
100 ARRAY_SIZE(elem_types), 0);
101
102 (void) target; /* silence unused var warning for non-debug build */
103 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer, map,
104 target, dvbuffer_type,
105 DRAW_JIT_DVBUFFER_MAP);
106 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer, size,
107 target, dvbuffer_type,
108 DRAW_JIT_DVBUFFER_SIZE);
109
110 return dvbuffer_type;
111 }
112
113 /**
114 * Create LLVM type for struct draw_jit_texture
115 */
116 static LLVMTypeRef
117 create_jit_texture_type(struct gallivm_state *gallivm, const char *struct_name)
118 {
119 LLVMTargetDataRef target = gallivm->target;
120 LLVMTypeRef texture_type;
121 LLVMTypeRef elem_types[DRAW_JIT_TEXTURE_NUM_FIELDS];
122 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
123
124 elem_types[DRAW_JIT_TEXTURE_WIDTH] =
125 elem_types[DRAW_JIT_TEXTURE_HEIGHT] =
126 elem_types[DRAW_JIT_TEXTURE_DEPTH] =
127 elem_types[DRAW_JIT_TEXTURE_FIRST_LEVEL] =
128 elem_types[DRAW_JIT_TEXTURE_LAST_LEVEL] = int32_type;
129 elem_types[DRAW_JIT_TEXTURE_BASE] =
130 LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0);
131 elem_types[DRAW_JIT_TEXTURE_ROW_STRIDE] =
132 elem_types[DRAW_JIT_TEXTURE_IMG_STRIDE] =
133 elem_types[DRAW_JIT_TEXTURE_MIP_OFFSETS] =
134 LLVMArrayType(int32_type, PIPE_MAX_TEXTURE_LEVELS);
135
136 texture_type = LLVMStructTypeInContext(gallivm->context, elem_types,
137 ARRAY_SIZE(elem_types), 0);
138
139 (void) target; /* silence unused var warning for non-debug build */
140 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, width,
141 target, texture_type,
142 DRAW_JIT_TEXTURE_WIDTH);
143 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, height,
144 target, texture_type,
145 DRAW_JIT_TEXTURE_HEIGHT);
146 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, depth,
147 target, texture_type,
148 DRAW_JIT_TEXTURE_DEPTH);
149 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, base,
150 target, texture_type,
151 DRAW_JIT_TEXTURE_BASE);
152 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, row_stride,
153 target, texture_type,
154 DRAW_JIT_TEXTURE_ROW_STRIDE);
155 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, img_stride,
156 target, texture_type,
157 DRAW_JIT_TEXTURE_IMG_STRIDE);
158 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, first_level,
159 target, texture_type,
160 DRAW_JIT_TEXTURE_FIRST_LEVEL);
161 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, last_level,
162 target, texture_type,
163 DRAW_JIT_TEXTURE_LAST_LEVEL);
164 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, mip_offsets,
165 target, texture_type,
166 DRAW_JIT_TEXTURE_MIP_OFFSETS);
167
168 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture, target, texture_type);
169
170 return texture_type;
171 }
172
173
174 /**
175 * Create LLVM type for struct draw_jit_sampler
176 */
177 static LLVMTypeRef
178 create_jit_sampler_type(struct gallivm_state *gallivm, const char *struct_name)
179 {
180 LLVMTargetDataRef target = gallivm->target;
181 LLVMTypeRef sampler_type;
182 LLVMTypeRef elem_types[DRAW_JIT_SAMPLER_NUM_FIELDS];
183
184 elem_types[DRAW_JIT_SAMPLER_MIN_LOD] =
185 elem_types[DRAW_JIT_SAMPLER_MAX_LOD] =
186 elem_types[DRAW_JIT_SAMPLER_LOD_BIAS] = LLVMFloatTypeInContext(gallivm->context);
187 elem_types[DRAW_JIT_SAMPLER_BORDER_COLOR] =
188 LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4);
189
190 sampler_type = LLVMStructTypeInContext(gallivm->context, elem_types,
191 ARRAY_SIZE(elem_types), 0);
192
193 (void) target; /* silence unused var warning for non-debug build */
194 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, min_lod,
195 target, sampler_type,
196 DRAW_JIT_SAMPLER_MIN_LOD);
197 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, max_lod,
198 target, sampler_type,
199 DRAW_JIT_SAMPLER_MAX_LOD);
200 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, lod_bias,
201 target, sampler_type,
202 DRAW_JIT_SAMPLER_LOD_BIAS);
203 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, border_color,
204 target, sampler_type,
205 DRAW_JIT_SAMPLER_BORDER_COLOR);
206
207 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler, target, sampler_type);
208
209 return sampler_type;
210 }
211
212 /**
213 * Create LLVM type for struct draw_jit_texture
214 */
215 static LLVMTypeRef
216 create_jit_image_type(struct gallivm_state *gallivm, const char *struct_name)
217 {
218 LLVMTargetDataRef target = gallivm->target;
219 LLVMTypeRef image_type;
220 LLVMTypeRef elem_types[DRAW_JIT_IMAGE_NUM_FIELDS];
221 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
222
223 elem_types[DRAW_JIT_IMAGE_WIDTH] =
224 elem_types[DRAW_JIT_IMAGE_HEIGHT] =
225 elem_types[DRAW_JIT_IMAGE_DEPTH] =
226 elem_types[DRAW_JIT_IMAGE_ROW_STRIDE] =
227 elem_types[DRAW_JIT_IMAGE_IMG_STRIDE] = int32_type;
228 elem_types[DRAW_JIT_IMAGE_BASE] =
229 LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0);
230
231 image_type = LLVMStructTypeInContext(gallivm->context, elem_types,
232 ARRAY_SIZE(elem_types), 0);
233
234 (void) target; /* silence unused var warning for non-debug build */
235 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, width,
236 target, image_type,
237 DRAW_JIT_IMAGE_WIDTH);
238 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, height,
239 target, image_type,
240 DRAW_JIT_IMAGE_HEIGHT);
241 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, depth,
242 target, image_type,
243 DRAW_JIT_IMAGE_DEPTH);
244 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, base,
245 target, image_type,
246 DRAW_JIT_IMAGE_BASE);
247 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, row_stride,
248 target, image_type,
249 DRAW_JIT_IMAGE_ROW_STRIDE);
250 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, img_stride,
251 target, image_type,
252 DRAW_JIT_IMAGE_IMG_STRIDE);
253
254 LP_CHECK_STRUCT_SIZE(struct draw_jit_image, target, image_type);
255
256 return image_type;
257 }
258
259 /**
260 * Create LLVM type for struct draw_jit_context
261 */
262 static LLVMTypeRef
263 create_jit_context_type(struct gallivm_state *gallivm,
264 LLVMTypeRef texture_type, LLVMTypeRef sampler_type,
265 LLVMTypeRef image_type,
266 const char *struct_name)
267 {
268 LLVMTargetDataRef target = gallivm->target;
269 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
270 LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context);
271 LLVMTypeRef elem_types[DRAW_JIT_CTX_NUM_FIELDS];
272 LLVMTypeRef context_type;
273
274 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* vs_constants */
275 LP_MAX_TGSI_CONST_BUFFERS);
276 elem_types[1] = LLVMArrayType(int_type, /* num_vs_constants */
277 LP_MAX_TGSI_CONST_BUFFERS);
278 elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4),
279 DRAW_TOTAL_CLIP_PLANES), 0);
280 elem_types[3] = LLVMPointerType(float_type, 0); /* viewports */
281 elem_types[4] = LLVMArrayType(texture_type,
282 PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */
283 elem_types[5] = LLVMArrayType(sampler_type,
284 PIPE_MAX_SAMPLERS); /* samplers */
285 elem_types[6] = LLVMArrayType(image_type,
286 PIPE_MAX_SHADER_IMAGES); /* images */
287 elem_types[7] = LLVMArrayType(LLVMPointerType(int_type, 0), /* vs_ssbo */
288 LP_MAX_TGSI_SHADER_BUFFERS);
289 elem_types[8] = LLVMArrayType(int_type, /* num_vs_ssbos */
290 LP_MAX_TGSI_SHADER_BUFFERS);
291 context_type = LLVMStructTypeInContext(gallivm->context, elem_types,
292 ARRAY_SIZE(elem_types), 0);
293
294 (void) target; /* silence unused var warning for non-debug build */
295 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, vs_constants,
296 target, context_type, DRAW_JIT_CTX_CONSTANTS);
297 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, num_vs_constants,
298 target, context_type, DRAW_JIT_CTX_NUM_CONSTANTS);
299 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, planes,
300 target, context_type, DRAW_JIT_CTX_PLANES);
301 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, viewports,
302 target, context_type, DRAW_JIT_CTX_VIEWPORT);
303 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, textures,
304 target, context_type,
305 DRAW_JIT_CTX_TEXTURES);
306 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, samplers,
307 target, context_type,
308 DRAW_JIT_CTX_SAMPLERS);
309 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, images,
310 target, context_type, DRAW_JIT_CTX_IMAGES);
311 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, vs_ssbos,
312 target, context_type, DRAW_JIT_CTX_SSBOS);
313 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, num_vs_ssbos,
314 target, context_type, DRAW_JIT_CTX_NUM_SSBOS);
315 LP_CHECK_STRUCT_SIZE(struct draw_jit_context,
316 target, context_type);
317
318 return context_type;
319 }
320
321
322 /**
323 * Create LLVM type for struct draw_gs_jit_context
324 */
325 static LLVMTypeRef
326 create_gs_jit_context_type(struct gallivm_state *gallivm,
327 unsigned vector_length,
328 LLVMTypeRef texture_type, LLVMTypeRef sampler_type,
329 LLVMTypeRef image_type,
330 const char *struct_name)
331 {
332 LLVMTargetDataRef target = gallivm->target;
333 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
334 LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context);
335 LLVMTypeRef elem_types[DRAW_GS_JIT_CTX_NUM_FIELDS];
336 LLVMTypeRef context_type;
337
338 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* constants */
339 LP_MAX_TGSI_CONST_BUFFERS);
340 elem_types[1] = LLVMArrayType(int_type, /* num_constants */
341 LP_MAX_TGSI_CONST_BUFFERS);
342 elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4),
343 DRAW_TOTAL_CLIP_PLANES), 0);
344 elem_types[3] = LLVMPointerType(float_type, 0); /* viewports */
345
346 elem_types[4] = LLVMArrayType(texture_type,
347 PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */
348 elem_types[5] = LLVMArrayType(sampler_type,
349 PIPE_MAX_SAMPLERS); /* samplers */
350 elem_types[6] = LLVMArrayType(image_type,
351 PIPE_MAX_SHADER_IMAGES); /* images */
352 elem_types[7] = LLVMPointerType(LLVMPointerType(int_type, 0), 0);
353 elem_types[8] = LLVMPointerType(LLVMVectorType(int_type,
354 vector_length), 0);
355 elem_types[9] = LLVMPointerType(LLVMVectorType(int_type,
356 vector_length), 0);
357
358 elem_types[10] = LLVMArrayType(LLVMPointerType(int_type, 0), /* ssbos */
359 LP_MAX_TGSI_SHADER_BUFFERS);
360 elem_types[11] = LLVMArrayType(int_type, /* num_ssbos */
361 LP_MAX_TGSI_SHADER_BUFFERS);
362
363 context_type = LLVMStructTypeInContext(gallivm->context, elem_types,
364 ARRAY_SIZE(elem_types), 0);
365
366 (void) target; /* silence unused var warning for non-debug build */
367 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, constants,
368 target, context_type, DRAW_GS_JIT_CTX_CONSTANTS);
369 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, num_constants,
370 target, context_type, DRAW_GS_JIT_CTX_NUM_CONSTANTS);
371 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, planes,
372 target, context_type, DRAW_GS_JIT_CTX_PLANES);
373 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, viewports,
374 target, context_type, DRAW_GS_JIT_CTX_VIEWPORT);
375 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, textures,
376 target, context_type,
377 DRAW_GS_JIT_CTX_TEXTURES);
378 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, samplers,
379 target, context_type,
380 DRAW_GS_JIT_CTX_SAMPLERS);
381 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, prim_lengths,
382 target, context_type,
383 DRAW_GS_JIT_CTX_PRIM_LENGTHS);
384 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, emitted_vertices,
385 target, context_type,
386 DRAW_GS_JIT_CTX_EMITTED_VERTICES);
387 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, emitted_prims,
388 target, context_type,
389 DRAW_GS_JIT_CTX_EMITTED_PRIMS);
390 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, ssbos,
391 target, context_type, DRAW_GS_JIT_CTX_SSBOS);
392 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, num_ssbos,
393 target, context_type, DRAW_GS_JIT_CTX_NUM_SSBOS);
394 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, images,
395 target, context_type, DRAW_GS_JIT_CTX_IMAGES);
396 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context,
397 target, context_type);
398
399 return context_type;
400 }
401
402
403 static LLVMTypeRef
404 create_gs_jit_input_type(struct gallivm_state *gallivm)
405 {
406 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
407 LLVMTypeRef input_array;
408
409 input_array = LLVMVectorType(float_type, TGSI_NUM_CHANNELS); /* num primitives */
410 input_array = LLVMArrayType(input_array, TGSI_NUM_CHANNELS); /* num channels */
411 input_array = LLVMArrayType(input_array, PIPE_MAX_SHADER_INPUTS); /* num attrs per vertex */
412 input_array = LLVMPointerType(input_array, 0); /* num vertices per prim */
413
414 return input_array;
415 }
416
417 /**
418 * Create LLVM type for struct pipe_vertex_buffer
419 */
420 static LLVMTypeRef
421 create_jit_vertex_buffer_type(struct gallivm_state *gallivm,
422 const char *struct_name)
423 {
424 LLVMTargetDataRef target = gallivm->target;
425 LLVMTypeRef elem_types[4];
426 LLVMTypeRef vb_type;
427
428 elem_types[0] = LLVMInt16TypeInContext(gallivm->context);
429 elem_types[1] = LLVMInt8TypeInContext(gallivm->context);
430 elem_types[2] = LLVMInt32TypeInContext(gallivm->context);
431 elem_types[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0);
432
433 vb_type = LLVMStructTypeInContext(gallivm->context, elem_types,
434 ARRAY_SIZE(elem_types), 0);
435
436 (void) target; /* silence unused var warning for non-debug build */
437 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, stride,
438 target, vb_type, 0);
439 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, is_user_buffer,
440 target, vb_type, 1);
441 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, buffer_offset,
442 target, vb_type, 2);
443 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, buffer.resource,
444 target, vb_type, 3);
445
446 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer, target, vb_type);
447
448 return vb_type;
449 }
450
451
452 /**
453 * Create LLVM type for struct vertex_header;
454 */
455 static LLVMTypeRef
456 create_jit_vertex_header(struct gallivm_state *gallivm, int data_elems)
457 {
458 LLVMTargetDataRef target = gallivm->target;
459 LLVMTypeRef elem_types[3];
460 LLVMTypeRef vertex_header;
461 char struct_name[24];
462
463 snprintf(struct_name, 23, "vertex_header%d", data_elems);
464
465 elem_types[DRAW_JIT_VERTEX_VERTEX_ID] = LLVMIntTypeInContext(gallivm->context, 32);
466 elem_types[DRAW_JIT_VERTEX_CLIP_POS] = LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4);
467 elem_types[DRAW_JIT_VERTEX_DATA] = LLVMArrayType(elem_types[1], data_elems);
468
469 vertex_header = LLVMStructTypeInContext(gallivm->context, elem_types,
470 ARRAY_SIZE(elem_types), 0);
471
472 /* these are bit-fields and we can't take address of them
473 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
474 target, vertex_header,
475 DRAW_JIT_VERTEX_CLIPMASK);
476 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
477 target, vertex_header,
478 DRAW_JIT_VERTEX_EDGEFLAG);
479 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
480 target, vertex_header,
481 DRAW_JIT_VERTEX_PAD);
482 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
483 target, vertex_header,
484 DRAW_JIT_VERTEX_VERTEX_ID);
485 */
486 (void) target; /* silence unused var warning for non-debug build */
487 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clip_pos,
488 target, vertex_header,
489 DRAW_JIT_VERTEX_CLIP_POS);
490 LP_CHECK_MEMBER_OFFSET(struct vertex_header, data,
491 target, vertex_header,
492 DRAW_JIT_VERTEX_DATA);
493
494 assert(LLVMABISizeOfType(target, vertex_header) ==
495 offsetof(struct vertex_header, data[data_elems]));
496
497 return vertex_header;
498 }
499
500
501 /**
502 * Create LLVM types for various structures.
503 */
504 static void
505 create_jit_types(struct draw_llvm_variant *variant)
506 {
507 struct gallivm_state *gallivm = variant->gallivm;
508 LLVMTypeRef texture_type, sampler_type, context_type, buffer_type,
509 vb_type, image_type;
510
511 texture_type = create_jit_texture_type(gallivm, "texture");
512 sampler_type = create_jit_sampler_type(gallivm, "sampler");
513 image_type = create_jit_image_type(gallivm, "image");
514
515 context_type = create_jit_context_type(gallivm, texture_type, sampler_type,
516 image_type,
517 "draw_jit_context");
518 variant->context_ptr_type = LLVMPointerType(context_type, 0);
519
520 buffer_type = create_jit_dvbuffer_type(gallivm, "draw_vertex_buffer");
521 variant->buffer_ptr_type = LLVMPointerType(buffer_type, 0);
522
523 vb_type = create_jit_vertex_buffer_type(gallivm, "pipe_vertex_buffer");
524 variant->vb_ptr_type = LLVMPointerType(vb_type, 0);
525 }
526
527
528 static LLVMTypeRef
529 get_context_ptr_type(struct draw_llvm_variant *variant)
530 {
531 if (!variant->context_ptr_type)
532 create_jit_types(variant);
533 return variant->context_ptr_type;
534 }
535
536
537 static LLVMTypeRef
538 get_buffer_ptr_type(struct draw_llvm_variant *variant)
539 {
540 if (!variant->buffer_ptr_type)
541 create_jit_types(variant);
542 return variant->buffer_ptr_type;
543 }
544
545
546 static LLVMTypeRef
547 get_vb_ptr_type(struct draw_llvm_variant *variant)
548 {
549 if (!variant->vb_ptr_type)
550 create_jit_types(variant);
551 return variant->vb_ptr_type;
552 }
553
554 static LLVMTypeRef
555 get_vertex_header_ptr_type(struct draw_llvm_variant *variant)
556 {
557 if (!variant->vertex_header_ptr_type)
558 create_jit_types(variant);
559 return variant->vertex_header_ptr_type;
560 }
561
562
563 /**
564 * Create per-context LLVM info.
565 */
566 struct draw_llvm *
567 draw_llvm_create(struct draw_context *draw, LLVMContextRef context)
568 {
569 struct draw_llvm *llvm;
570
571 if (!lp_build_init())
572 return NULL;
573
574 llvm = CALLOC_STRUCT( draw_llvm );
575 if (!llvm)
576 return NULL;
577
578 llvm->draw = draw;
579
580 llvm->context = context;
581 if (!llvm->context) {
582 llvm->context = LLVMContextCreate();
583 llvm->context_owned = true;
584 }
585 if (!llvm->context)
586 goto fail;
587
588 llvm->nr_variants = 0;
589 make_empty_list(&llvm->vs_variants_list);
590
591 llvm->nr_gs_variants = 0;
592 make_empty_list(&llvm->gs_variants_list);
593
594 return llvm;
595
596 fail:
597 draw_llvm_destroy(llvm);
598 return NULL;
599 }
600
601
602 /**
603 * Free per-context LLVM info.
604 */
605 void
606 draw_llvm_destroy(struct draw_llvm *llvm)
607 {
608 if (llvm->context_owned)
609 LLVMContextDispose(llvm->context);
610 llvm->context = NULL;
611
612 /* XXX free other draw_llvm data? */
613 FREE(llvm);
614 }
615
616
617 /**
618 * Create LLVM-generated code for a vertex shader.
619 */
620 struct draw_llvm_variant *
621 draw_llvm_create_variant(struct draw_llvm *llvm,
622 unsigned num_inputs,
623 const struct draw_llvm_variant_key *key)
624 {
625 struct draw_llvm_variant *variant;
626 struct llvm_vertex_shader *shader =
627 llvm_vertex_shader(llvm->draw->vs.vertex_shader);
628 LLVMTypeRef vertex_header;
629 char module_name[64];
630
631 variant = MALLOC(sizeof *variant +
632 shader->variant_key_size -
633 sizeof variant->key);
634 if (!variant)
635 return NULL;
636
637 variant->llvm = llvm;
638 variant->shader = shader;
639
640 snprintf(module_name, sizeof(module_name), "draw_llvm_vs_variant%u",
641 variant->shader->variants_cached);
642
643 variant->gallivm = gallivm_create(module_name, llvm->context);
644
645 create_jit_types(variant);
646
647 memcpy(&variant->key, key, shader->variant_key_size);
648
649 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
650 if (llvm->draw->vs.vertex_shader->state.type == PIPE_SHADER_IR_TGSI)
651 tgsi_dump(llvm->draw->vs.vertex_shader->state.tokens, 0);
652 else
653 nir_print_shader(llvm->draw->vs.vertex_shader->state.ir.nir, stderr);
654 draw_llvm_dump_variant_key(&variant->key);
655 }
656
657 vertex_header = create_jit_vertex_header(variant->gallivm, num_inputs);
658
659 variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0);
660
661 draw_llvm_generate(llvm, variant);
662
663 gallivm_compile_module(variant->gallivm);
664
665 variant->jit_func = (draw_jit_vert_func)
666 gallivm_jit_function(variant->gallivm, variant->function);
667
668 gallivm_free_ir(variant->gallivm);
669
670 variant->list_item_global.base = variant;
671 variant->list_item_local.base = variant;
672 /*variant->no = */shader->variants_created++;
673 variant->list_item_global.base = variant;
674
675 return variant;
676 }
677
678
679 static void
680 generate_vs(struct draw_llvm_variant *variant,
681 LLVMBuilderRef builder,
682 struct lp_type vs_type,
683 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
684 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS],
685 const struct lp_bld_tgsi_system_values *system_values,
686 LLVMValueRef context_ptr,
687 const struct lp_build_sampler_soa *draw_sampler,
688 const struct lp_build_image_soa *draw_image,
689 boolean clamp_vertex_color,
690 struct lp_build_mask_context *bld_mask)
691 {
692 struct draw_llvm *llvm = variant->llvm;
693 const struct tgsi_token *tokens = llvm->draw->vs.vertex_shader->state.tokens;
694 LLVMValueRef consts_ptr =
695 draw_jit_context_vs_constants(variant->gallivm, context_ptr);
696 LLVMValueRef num_consts_ptr =
697 draw_jit_context_num_vs_constants(variant->gallivm, context_ptr);
698 LLVMValueRef ssbos_ptr =
699 draw_jit_context_vs_ssbos(variant->gallivm, context_ptr);
700 LLVMValueRef num_ssbos_ptr =
701 draw_jit_context_num_vs_ssbos(variant->gallivm, context_ptr);
702
703 struct lp_build_tgsi_params params;
704 memset(&params, 0, sizeof(params));
705
706 params.type = vs_type;
707 params.mask = bld_mask;
708 params.consts_ptr = consts_ptr;
709 params.const_sizes_ptr = num_consts_ptr;
710 params.system_values = system_values;
711 params.inputs = inputs;
712 params.context_ptr = context_ptr;
713 params.sampler = draw_sampler;
714 params.info = &llvm->draw->vs.vertex_shader->info;
715 params.ssbo_ptr = ssbos_ptr;
716 params.ssbo_sizes_ptr = num_ssbos_ptr;
717 params.image = draw_image;
718
719 if (llvm->draw->vs.vertex_shader->state.ir.nir &&
720 llvm->draw->vs.vertex_shader->state.type == PIPE_SHADER_IR_NIR)
721 lp_build_nir_soa(variant->gallivm,
722 llvm->draw->vs.vertex_shader->state.ir.nir,
723 &params,
724 outputs);
725 else
726 lp_build_tgsi_soa(variant->gallivm,
727 tokens,
728 &params,
729 outputs);
730
731 {
732 LLVMValueRef out;
733 unsigned chan, attrib;
734 struct lp_build_context bld;
735 struct tgsi_shader_info* info = &llvm->draw->vs.vertex_shader->info;
736 lp_build_context_init(&bld, variant->gallivm, vs_type);
737
738 for (attrib = 0; attrib < info->num_outputs; ++attrib) {
739 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
740 if (outputs[attrib][chan]) {
741 switch (info->output_semantic_name[attrib]) {
742 case TGSI_SEMANTIC_COLOR:
743 case TGSI_SEMANTIC_BCOLOR:
744 if (clamp_vertex_color) {
745 out = LLVMBuildLoad(builder, outputs[attrib][chan], "");
746 out = lp_build_clamp(&bld, out, bld.zero, bld.one);
747 LLVMBuildStore(builder, out, outputs[attrib][chan]);
748 }
749 break;
750 }
751 }
752 }
753 }
754 }
755 }
756
757
758 static void
759 fetch_instanced(struct gallivm_state *gallivm,
760 const struct util_format_description *format_desc,
761 struct lp_type vs_type,
762 LLVMValueRef vb_stride,
763 LLVMValueRef map_ptr,
764 LLVMValueRef buffer_size_adj,
765 LLVMValueRef *inputs,
766 LLVMValueRef index)
767 {
768 LLVMTypeRef i32_t = LLVMInt32TypeInContext(gallivm->context);
769 LLVMTypeRef aosf_t, aosi_t;
770 LLVMValueRef zero = LLVMConstNull(i32_t);
771 LLVMBuilderRef builder = gallivm->builder;
772 LLVMValueRef stride, buffer_overflowed, aos, index_valid;
773 unsigned i;
774
775 aosf_t = lp_build_vec_type(gallivm, lp_float32_vec4_type());
776 aosi_t = lp_build_vec_type(gallivm, lp_int32_vec4_type());
777
778 /* This mul can overflow. Wraparound is ok. */
779 stride = LLVMBuildMul(builder, vb_stride, index, "");
780
781 buffer_overflowed = LLVMBuildICmp(builder, LLVMIntUGE,
782 stride, buffer_size_adj,
783 "buffer_overflowed");
784
785 if (0) {
786 lp_build_print_value(gallivm, " instance index = ", index);
787 lp_build_print_value(gallivm, " buffer overflowed = ", buffer_overflowed);
788 }
789
790 index_valid = LLVMBuildNot(builder, buffer_overflowed, "");
791 index_valid = LLVMBuildSExt(builder, index_valid, i32_t, "");
792 stride = LLVMBuildAnd(builder, stride, index_valid, "");
793
794 aos = lp_build_fetch_rgba_aos(gallivm,
795 format_desc,
796 lp_float32_vec4_type(),
797 FALSE,
798 map_ptr,
799 stride, zero, zero,
800 NULL);
801
802 index_valid = lp_build_broadcast(gallivm, aosi_t, index_valid);
803 aos = LLVMBuildBitCast(builder, aos, aosi_t, "");
804 aos = LLVMBuildAnd(builder, aos, index_valid, "");
805 aos = LLVMBuildBitCast(builder, aos, aosf_t, "");
806
807 for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
808 LLVMValueRef index = lp_build_const_int32(gallivm, i);
809 inputs[i] = lp_build_extract_broadcast(gallivm,
810 lp_float32_vec4_type(),
811 vs_type, aos, index);
812 }
813 }
814
815
816 static void
817 fetch_vector(struct gallivm_state *gallivm,
818 const struct util_format_description *format_desc,
819 struct lp_type vs_type,
820 LLVMValueRef vb_stride,
821 LLVMValueRef map_ptr,
822 LLVMValueRef buffer_size_adj,
823 LLVMValueRef *inputs,
824 LLVMValueRef indices)
825 {
826 LLVMBuilderRef builder = gallivm->builder;
827 struct lp_build_context blduivec;
828 struct lp_type fetch_type = vs_type;
829 LLVMValueRef offset, valid_mask;
830 unsigned i;
831
832 lp_build_context_init(&blduivec, gallivm, lp_uint_type(vs_type));
833
834 vb_stride = lp_build_broadcast_scalar(&blduivec, vb_stride);
835 buffer_size_adj = lp_build_broadcast_scalar(&blduivec, buffer_size_adj);
836
837 /* This mul can overflow. Wraparound is ok. */
838 offset = lp_build_mul(&blduivec, vb_stride, indices);
839
840 valid_mask = lp_build_compare(gallivm, blduivec.type,
841 PIPE_FUNC_LESS, offset, buffer_size_adj);
842
843 /* not valid elements use offset 0 */
844 offset = LLVMBuildAnd(builder, offset, valid_mask, "");
845
846 if (0) {
847 lp_build_print_value(gallivm, " indices = ", indices);
848 lp_build_print_value(gallivm, " offsets = ", offset);
849 lp_build_print_value(gallivm, " valid_mask = ", valid_mask);
850 }
851
852 /*
853 * Unlike fetch_instanced, use SoA fetch instead of multiple AoS fetches.
854 * This should always produce better code.
855 */
856
857 /* The type handling is annoying here... */
858 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB &&
859 format_desc->channel[0].pure_integer) {
860 if (format_desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
861 fetch_type = lp_type_int_vec(vs_type.width, vs_type.width * vs_type.length);
862 }
863 else if (format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED) {
864 fetch_type = lp_type_uint_vec(vs_type.width, vs_type.width * vs_type.length);
865 }
866 }
867
868 lp_build_fetch_rgba_soa(gallivm, format_desc,
869 fetch_type, FALSE, map_ptr, offset,
870 blduivec.zero, blduivec.zero,
871 NULL, inputs);
872
873 for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
874 inputs[i] = LLVMBuildBitCast(builder, inputs[i],
875 lp_build_vec_type(gallivm, vs_type), "");
876 }
877
878 /* out-of-bound fetches return all zeros */
879 for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
880 inputs[i] = LLVMBuildBitCast(builder, inputs[i], blduivec.vec_type, "");
881 inputs[i] = LLVMBuildAnd(builder, inputs[i], valid_mask, "");
882 inputs[i] = LLVMBuildBitCast(builder, inputs[i],
883 lp_build_vec_type(gallivm, vs_type), "");
884 }
885 }
886
887
888 static void
889 store_aos(struct gallivm_state *gallivm,
890 LLVMValueRef io_ptr,
891 LLVMValueRef index,
892 LLVMValueRef value)
893 {
894 LLVMTypeRef data_ptr_type = LLVMPointerType(lp_build_vec_type(gallivm, lp_float32_vec4_type()), 0);
895 LLVMBuilderRef builder = gallivm->builder;
896 LLVMValueRef data_ptr = draw_jit_header_data(gallivm, io_ptr);
897 LLVMValueRef indices[3];
898
899 indices[0] = lp_build_const_int32(gallivm, 0);
900 indices[1] = index;
901 indices[2] = lp_build_const_int32(gallivm, 0);
902
903 data_ptr = LLVMBuildGEP(builder, data_ptr, indices, 3, "");
904 data_ptr = LLVMBuildPointerCast(builder, data_ptr, data_ptr_type, "");
905
906 #if DEBUG_STORE
907 lp_build_printf(gallivm, " ---- %p storing attribute %d (io = %p)\n", data_ptr, index, io_ptr);
908 #endif
909
910 /* Unaligned store due to the vertex header */
911 LLVMSetAlignment(LLVMBuildStore(builder, value, data_ptr), sizeof(float));
912 }
913
914 /**
915 * Adjust the mask to architecture endianess. The mask will the store in struct:
916 *
917 * struct vertex_header {
918 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
919 * unsigned edgeflag:1;
920 * unsigned pad:1;
921 * unsigned vertex_id:16;
922 * [...]
923 * }
924 *
925 * On little-endian machine nothing needs to done, however on bit-endian machine
926 * the mask's fields need to be adjusted with the algorithm:
927 *
928 * uint32_t reverse (uint32_t x)
929 * {
930 * return (x >> 16) | // vertex_id
931 * ((x & 0x3fff) << 18) | // clipmask
932 * ((x & 0x4000) << 3) | // pad
933 * ((x & 0x8000) << 1); // edgeflag
934 * }
935 */
936 static LLVMValueRef
937 adjust_mask(struct gallivm_state *gallivm,
938 LLVMValueRef mask)
939 {
940 #if UTIL_ARCH_BIG_ENDIAN
941 LLVMBuilderRef builder = gallivm->builder;
942 LLVMValueRef vertex_id;
943 LLVMValueRef clipmask;
944 LLVMValueRef pad;
945 LLVMValueRef edgeflag;
946
947 vertex_id = LLVMBuildLShr(builder, mask, lp_build_const_int32(gallivm, 16), "");
948 clipmask = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x3fff), "");
949 clipmask = LLVMBuildShl(builder, clipmask, lp_build_const_int32(gallivm, 18), "");
950 if (0) {
951 pad = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x4000), "");
952 pad = LLVMBuildShl(builder, pad, lp_build_const_int32(gallivm, 3), "");
953 }
954 edgeflag = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x8000), "");
955 edgeflag = LLVMBuildShl(builder, edgeflag, lp_build_const_int32(gallivm, 1), "");
956
957 mask = LLVMBuildOr(builder, vertex_id, clipmask, "");
958 if (0) {
959 mask = LLVMBuildOr(builder, mask, pad, "");
960 }
961 mask = LLVMBuildOr(builder, mask, edgeflag, "");
962 #endif
963 return mask;
964 }
965
966 static void
967 store_aos_array(struct gallivm_state *gallivm,
968 struct lp_type soa_type,
969 LLVMValueRef io_ptr,
970 LLVMValueRef *indices,
971 LLVMValueRef* aos,
972 int attrib,
973 int num_outputs,
974 LLVMValueRef clipmask,
975 boolean need_edgeflag)
976 {
977 LLVMBuilderRef builder = gallivm->builder;
978 LLVMValueRef attr_index = lp_build_const_int32(gallivm, attrib);
979 LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32];
980 LLVMValueRef linear_inds[LP_MAX_VECTOR_WIDTH / 32];
981 LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32];
982 int vector_length = soa_type.length;
983 int i;
984
985 debug_assert(TGSI_NUM_CHANNELS == 4);
986
987 for (i = 0; i < vector_length; i++) {
988 linear_inds[i] = lp_build_const_int32(gallivm, i);
989 if (indices) {
990 inds[i] = indices[i];
991 } else {
992 inds[i] = linear_inds[i];
993 }
994 io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, "");
995 }
996
997 if (attrib == 0) {
998 /* store vertex header for each of the n vertices */
999 LLVMValueRef val, cliptmp;
1000 int vertex_id_pad_edgeflag;
1001
1002 /* If this assertion fails, it means we need to update the bit twidding
1003 * code here. See struct vertex_header in draw_private.h.
1004 */
1005 assert(DRAW_TOTAL_CLIP_PLANES==14);
1006 /* initialize vertex id:16 = 0xffff, pad:1 = 0, edgeflag:1 = 1 */
1007 if (!need_edgeflag) {
1008 vertex_id_pad_edgeflag = (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES);
1009 }
1010 else {
1011 vertex_id_pad_edgeflag = (0xffff << 16);
1012 }
1013 val = lp_build_const_int_vec(gallivm, lp_int_type(soa_type),
1014 vertex_id_pad_edgeflag);
1015 /* OR with the clipmask */
1016 cliptmp = LLVMBuildOr(builder, val, clipmask, "");
1017 for (i = 0; i < vector_length; i++) {
1018 LLVMValueRef id_ptr = draw_jit_header_id(gallivm, io_ptrs[i]);
1019 val = LLVMBuildExtractElement(builder, cliptmp, linear_inds[i], "");
1020 val = adjust_mask(gallivm, val);
1021 #if DEBUG_STORE
1022 lp_build_printf(gallivm, "io = %p, index %d, clipmask = %x\n",
1023 io_ptrs[i], inds[i], val);
1024 #endif
1025 LLVMBuildStore(builder, val, id_ptr);
1026 }
1027 }
1028
1029 /* store for each of the n vertices */
1030 for (i = 0; i < vector_length; i++) {
1031 store_aos(gallivm, io_ptrs[i], attr_index, aos[i]);
1032 }
1033 }
1034
1035
1036 static void
1037 convert_to_aos(struct gallivm_state *gallivm,
1038 LLVMValueRef io,
1039 LLVMValueRef *indices,
1040 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
1041 LLVMValueRef clipmask,
1042 int num_outputs,
1043 struct lp_type soa_type,
1044 boolean need_edgeflag)
1045 {
1046 LLVMBuilderRef builder = gallivm->builder;
1047 unsigned chan, attrib, i;
1048
1049 #if DEBUG_STORE
1050 lp_build_printf(gallivm, " # storing begin\n");
1051 #endif
1052 for (attrib = 0; attrib < num_outputs; ++attrib) {
1053 LLVMValueRef soa[TGSI_NUM_CHANNELS];
1054 LLVMValueRef aos[LP_MAX_VECTOR_WIDTH / 32];
1055 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
1056 if (outputs[attrib][chan]) {
1057 LLVMValueRef out = LLVMBuildLoad(builder, outputs[attrib][chan], "");
1058 lp_build_name(out, "output%u.%c", attrib, "xyzw"[chan]);
1059 #if DEBUG_STORE
1060 lp_build_printf(gallivm, "output %d : %d ",
1061 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context),
1062 attrib, 0),
1063 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context),
1064 chan, 0));
1065 lp_build_print_value(gallivm, "val = ", out);
1066 {
1067 LLVMValueRef iv =
1068 LLVMBuildBitCast(builder, out, lp_build_int_vec_type(gallivm, soa_type), "");
1069
1070 lp_build_print_value(gallivm, " ival = ", iv);
1071 }
1072 #endif
1073 soa[chan] = out;
1074 }
1075 else {
1076 soa[chan] = 0;
1077 }
1078 }
1079
1080
1081 if (soa_type.length == TGSI_NUM_CHANNELS) {
1082 lp_build_transpose_aos(gallivm, soa_type, soa, aos);
1083 } else {
1084 lp_build_transpose_aos(gallivm, soa_type, soa, soa);
1085
1086 for (i = 0; i < soa_type.length; ++i) {
1087 aos[i] = lp_build_extract_range(gallivm,
1088 soa[i % TGSI_NUM_CHANNELS],
1089 (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS,
1090 TGSI_NUM_CHANNELS);
1091 }
1092 }
1093
1094 store_aos_array(gallivm,
1095 soa_type,
1096 io, indices,
1097 aos,
1098 attrib,
1099 num_outputs,
1100 clipmask,
1101 need_edgeflag);
1102 }
1103 #if DEBUG_STORE
1104 lp_build_printf(gallivm, " # storing end\n");
1105 #endif
1106 }
1107
1108
1109 /**
1110 * Stores original vertex positions in clip coordinates
1111 */
1112 static void
1113 store_clip(struct gallivm_state *gallivm,
1114 const struct lp_type vs_type,
1115 LLVMValueRef io_ptr,
1116 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
1117 int idx)
1118 {
1119 LLVMBuilderRef builder = gallivm->builder;
1120 LLVMValueRef soa[4];
1121 LLVMValueRef aos[LP_MAX_VECTOR_LENGTH];
1122 LLVMValueRef indices[2];
1123 LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32];
1124 LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32];
1125 LLVMValueRef clip_ptrs[LP_MAX_VECTOR_WIDTH / 32];
1126 LLVMTypeRef clip_ptr_type =
1127 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context),
1128 4), 0);
1129 int i, j;
1130
1131 indices[0] =
1132 indices[1] = lp_build_const_int32(gallivm, 0);
1133
1134 for (i = 0; i < vs_type.length; i++) {
1135 inds[i] = lp_build_const_int32(gallivm, i);
1136 io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, "");
1137 }
1138
1139 soa[0] = LLVMBuildLoad(builder, outputs[idx][0], ""); /*x0 x1 .. xn*/
1140 soa[1] = LLVMBuildLoad(builder, outputs[idx][1], ""); /*y0 y1 .. yn*/
1141 soa[2] = LLVMBuildLoad(builder, outputs[idx][2], ""); /*z0 z1 .. zn*/
1142 soa[3] = LLVMBuildLoad(builder, outputs[idx][3], ""); /*w0 w1 .. wn*/
1143
1144 for (i = 0; i < vs_type.length; i++) {
1145 clip_ptrs[i] = draw_jit_header_clip_pos(gallivm, io_ptrs[i]);
1146 }
1147
1148 lp_build_transpose_aos(gallivm, vs_type, soa, soa);
1149 for (i = 0; i < vs_type.length; ++i) {
1150 aos[i] = lp_build_extract_range(gallivm,
1151 soa[i % TGSI_NUM_CHANNELS],
1152 (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS,
1153 TGSI_NUM_CHANNELS);
1154 }
1155
1156 for (j = 0; j < vs_type.length; j++) {
1157 LLVMValueRef clip_ptr;
1158
1159 clip_ptr = LLVMBuildGEP(builder, clip_ptrs[j], indices, 2, "clipo");
1160 clip_ptr = LLVMBuildPointerCast(builder, clip_ptr, clip_ptr_type, "");
1161
1162 /* Unaligned store */
1163 LLVMSetAlignment(LLVMBuildStore(builder, aos[j], clip_ptr), sizeof(float));
1164 }
1165 }
1166
1167
1168 /**
1169 * Transforms the outputs for viewport mapping
1170 */
1171 static void
1172 generate_viewport(struct draw_llvm_variant *variant,
1173 LLVMBuilderRef builder,
1174 struct lp_type vs_type,
1175 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
1176 LLVMValueRef context_ptr)
1177 {
1178 int i;
1179 struct gallivm_state *gallivm = variant->gallivm;
1180 struct lp_type f32_type = vs_type;
1181 const unsigned pos = variant->llvm->draw->vs.position_output;
1182 LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type);
1183 LLVMValueRef out3 = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 .. wn*/
1184 LLVMValueRef const1 = lp_build_const_vec(gallivm, f32_type, 1.0); /*1.0 1.0 1.0 1.0*/
1185 LLVMValueRef vp_ptr = draw_jit_context_viewports(gallivm, context_ptr);
1186
1187 /* We treat pipe_viewport_state as a float array */
1188 const int scale_index_offset = offsetof(struct pipe_viewport_state, scale) / sizeof(float);
1189 const int trans_index_offset = offsetof(struct pipe_viewport_state, translate) / sizeof(float);
1190
1191 /* for 1/w convention*/
1192 out3 = LLVMBuildFDiv(builder, const1, out3, "");
1193 LLVMBuildStore(builder, out3, outputs[pos][3]);
1194
1195 /* Viewport Mapping */
1196 for (i=0; i<3; i++) {
1197 LLVMValueRef out = LLVMBuildLoad(builder, outputs[pos][i], ""); /*x0 x1 .. xn*/
1198 LLVMValueRef scale;
1199 LLVMValueRef trans;
1200 LLVMValueRef scale_i;
1201 LLVMValueRef trans_i;
1202 LLVMValueRef index;
1203
1204 index = lp_build_const_int32(gallivm, i + scale_index_offset);
1205 scale_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, "");
1206
1207 index = lp_build_const_int32(gallivm, i + trans_index_offset);
1208 trans_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, "");
1209
1210 scale = lp_build_broadcast(gallivm, vs_type_llvm,
1211 LLVMBuildLoad(builder, scale_i, "scale"));
1212 trans = lp_build_broadcast(gallivm, vs_type_llvm,
1213 LLVMBuildLoad(builder, trans_i, "trans"));
1214
1215 /* divide by w */
1216 out = LLVMBuildFMul(builder, out, out3, "");
1217 /* mult by scale, add translation */
1218 out = lp_build_fmuladd(builder, out, scale, trans);
1219
1220 /* store transformed outputs */
1221 LLVMBuildStore(builder, out, outputs[pos][i]);
1222 }
1223
1224 }
1225
1226
1227 /**
1228 * Returns clipmask as nxi32 bitmask for the n vertices
1229 */
1230 static LLVMValueRef
1231 generate_clipmask(struct draw_llvm *llvm,
1232 struct gallivm_state *gallivm,
1233 struct lp_type vs_type,
1234 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
1235 struct draw_llvm_variant_key *key,
1236 LLVMValueRef context_ptr,
1237 boolean *have_clipdist)
1238 {
1239 LLVMBuilderRef builder = gallivm->builder;
1240 LLVMValueRef mask; /* stores the <nxi32> clipmasks */
1241 LLVMValueRef test, temp;
1242 LLVMValueRef zero, shift;
1243 LLVMValueRef pos_x, pos_y, pos_z, pos_w;
1244 LLVMValueRef cv_x, cv_y, cv_z, cv_w;
1245 LLVMValueRef plane1, planes, plane_ptr, sum;
1246 struct lp_type f32_type = vs_type;
1247 struct lp_type i32_type = lp_int_type(vs_type);
1248 const unsigned pos = llvm->draw->vs.position_output;
1249 const unsigned cv = llvm->draw->vs.clipvertex_output;
1250 int num_written_clipdistance = llvm->draw->vs.vertex_shader->info.num_written_clipdistance;
1251 boolean have_cd = false;
1252 boolean clip_user = key->clip_user;
1253 unsigned ucp_enable = key->ucp_enable;
1254 unsigned cd[2];
1255
1256 cd[0] = llvm->draw->vs.ccdistance_output[0];
1257 cd[1] = llvm->draw->vs.ccdistance_output[1];
1258
1259 if (cd[0] != pos || cd[1] != pos)
1260 have_cd = true;
1261
1262 if (num_written_clipdistance && !clip_user) {
1263 clip_user = true;
1264 ucp_enable = (1 << num_written_clipdistance) - 1;
1265 }
1266
1267 mask = lp_build_const_int_vec(gallivm, i32_type, 0);
1268 temp = lp_build_const_int_vec(gallivm, i32_type, 0);
1269 zero = lp_build_const_vec(gallivm, f32_type, 0); /* 0.0f 0.0f 0.0f 0.0f */
1270 shift = lp_build_const_int_vec(gallivm, i32_type, 1); /* 1 1 1 1 */
1271
1272 /*
1273 * load clipvertex and position from correct locations.
1274 * if they are the same just load them once.
1275 */
1276 pos_x = LLVMBuildLoad(builder, outputs[pos][0], ""); /*x0 x1 .. xn */
1277 pos_y = LLVMBuildLoad(builder, outputs[pos][1], ""); /*y0 y1 .. yn */
1278 pos_z = LLVMBuildLoad(builder, outputs[pos][2], ""); /*z0 z1 .. zn */
1279 pos_w = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 .. wn */
1280
1281 if (clip_user && cv != pos) {
1282 cv_x = LLVMBuildLoad(builder, outputs[cv][0], ""); /*x0 x1 .. xn */
1283 cv_y = LLVMBuildLoad(builder, outputs[cv][1], ""); /*y0 y1 .. yn */
1284 cv_z = LLVMBuildLoad(builder, outputs[cv][2], ""); /*z0 z1 .. zn */
1285 cv_w = LLVMBuildLoad(builder, outputs[cv][3], ""); /*w0 w1 .. wn */
1286 } else {
1287 cv_x = pos_x;
1288 cv_y = pos_y;
1289 cv_z = pos_z;
1290 cv_w = pos_w;
1291 }
1292
1293 /*
1294 * Be careful with the comparisons and NaNs (using llvm's unordered
1295 * comparisons here).
1296 */
1297 /* Cliptest, for hardwired planes */
1298 /*
1299 * XXX should take guardband into account (currently not in key).
1300 * Otherwise might run the draw pipeline stages for nothing.
1301 */
1302 if (key->clip_xy) {
1303 /* plane 1 */
1304 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_x , pos_w);
1305 temp = shift;
1306 test = LLVMBuildAnd(builder, test, temp, "");
1307 mask = test;
1308
1309 /* plane 2 */
1310 test = LLVMBuildFAdd(builder, pos_x, pos_w, "");
1311 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
1312 temp = LLVMBuildShl(builder, temp, shift, "");
1313 test = LLVMBuildAnd(builder, test, temp, "");
1314 mask = LLVMBuildOr(builder, mask, test, "");
1315
1316 /* plane 3 */
1317 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_y, pos_w);
1318 temp = LLVMBuildShl(builder, temp, shift, "");
1319 test = LLVMBuildAnd(builder, test, temp, "");
1320 mask = LLVMBuildOr(builder, mask, test, "");
1321
1322 /* plane 4 */
1323 test = LLVMBuildFAdd(builder, pos_y, pos_w, "");
1324 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
1325 temp = LLVMBuildShl(builder, temp, shift, "");
1326 test = LLVMBuildAnd(builder, test, temp, "");
1327 mask = LLVMBuildOr(builder, mask, test, "");
1328 }
1329
1330 if (key->clip_z) {
1331 temp = lp_build_const_int_vec(gallivm, i32_type, 16);
1332 if (key->clip_halfz) {
1333 /* plane 5 */
1334 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, pos_z);
1335 test = LLVMBuildAnd(builder, test, temp, "");
1336 mask = LLVMBuildOr(builder, mask, test, "");
1337 }
1338 else {
1339 /* plane 5 */
1340 test = LLVMBuildFAdd(builder, pos_z, pos_w, "");
1341 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
1342 test = LLVMBuildAnd(builder, test, temp, "");
1343 mask = LLVMBuildOr(builder, mask, test, "");
1344 }
1345 /* plane 6 */
1346 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_z, pos_w);
1347 temp = LLVMBuildShl(builder, temp, shift, "");
1348 test = LLVMBuildAnd(builder, test, temp, "");
1349 mask = LLVMBuildOr(builder, mask, test, "");
1350 }
1351
1352 if (clip_user) {
1353 LLVMValueRef planes_ptr = draw_jit_context_planes(gallivm, context_ptr);
1354 LLVMValueRef indices[3];
1355 LLVMValueRef is_nan_or_inf;
1356
1357 /* userclip planes */
1358 while (ucp_enable) {
1359 unsigned plane_idx = ffs(ucp_enable)-1;
1360 ucp_enable &= ~(1 << plane_idx);
1361 plane_idx += 6;
1362
1363 if (have_cd && num_written_clipdistance) {
1364 LLVMValueRef clipdist;
1365 int i;
1366 i = plane_idx - 6;
1367
1368 *have_clipdist = TRUE;
1369 if (i < 4) {
1370 clipdist = LLVMBuildLoad(builder, outputs[cd[0]][i], "");
1371 } else {
1372 clipdist = LLVMBuildLoad(builder, outputs[cd[1]][i-4], "");
1373 }
1374 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, clipdist);
1375 is_nan_or_inf = lp_build_is_inf_or_nan(gallivm, vs_type, clipdist);
1376 test = LLVMBuildOr(builder, test, is_nan_or_inf, "");
1377 temp = lp_build_const_int_vec(gallivm, i32_type, 1LL << plane_idx);
1378 test = LLVMBuildAnd(builder, test, temp, "");
1379 mask = LLVMBuildOr(builder, mask, test, "");
1380 } else {
1381 LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type);
1382 indices[0] = lp_build_const_int32(gallivm, 0);
1383 indices[1] = lp_build_const_int32(gallivm, plane_idx);
1384
1385 indices[2] = lp_build_const_int32(gallivm, 0);
1386 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1387 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_x");
1388 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1389 sum = LLVMBuildFMul(builder, planes, cv_x, "");
1390
1391 indices[2] = lp_build_const_int32(gallivm, 1);
1392 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1393 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_y");
1394 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1395 sum = lp_build_fmuladd(builder, planes, cv_y, sum);
1396
1397 indices[2] = lp_build_const_int32(gallivm, 2);
1398 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1399 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_z");
1400 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1401 sum = lp_build_fmuladd(builder, planes, cv_z, sum);
1402
1403 indices[2] = lp_build_const_int32(gallivm, 3);
1404 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1405 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_w");
1406 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1407 sum = lp_build_fmuladd(builder, planes, cv_w, sum);
1408
1409 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, sum);
1410 temp = lp_build_const_int_vec(gallivm, i32_type, 1LL << plane_idx);
1411 test = LLVMBuildAnd(builder, test, temp, "");
1412 mask = LLVMBuildOr(builder, mask, test, "");
1413 }
1414 }
1415 }
1416 if (key->need_edgeflags) {
1417 /*
1418 * This isn't really part of clipmask but stored the same in vertex
1419 * header later, so do it here.
1420 */
1421 unsigned edge_attr = llvm->draw->vs.edgeflag_output;
1422 LLVMValueRef one = lp_build_const_vec(gallivm, f32_type, 1.0);
1423 LLVMValueRef edgeflag = LLVMBuildLoad(builder, outputs[edge_attr][0], "");
1424 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_EQUAL, one, edgeflag);
1425 temp = lp_build_const_int_vec(gallivm, i32_type,
1426 1LL << DRAW_TOTAL_CLIP_PLANES);
1427 test = LLVMBuildAnd(builder, test, temp, "");
1428 mask = LLVMBuildOr(builder, mask, test, "");
1429 }
1430 return mask;
1431 }
1432
1433
1434 /**
1435 * Returns boolean if any clipping has occurred
1436 * Used zero/one i8 value to represent boolean
1437 */
1438 static LLVMValueRef
1439 clipmask_booli8(struct gallivm_state *gallivm,
1440 const struct lp_type vs_type,
1441 LLVMValueRef clipmask_bool_ptr,
1442 boolean edgeflag_in_clipmask)
1443 {
1444 LLVMBuilderRef builder = gallivm->builder;
1445 LLVMTypeRef int8_type = LLVMInt8TypeInContext(gallivm->context);
1446 LLVMValueRef clipmask_bool = LLVMBuildLoad(builder, clipmask_bool_ptr, "");
1447 LLVMValueRef ret;
1448 struct lp_build_context bldivec;
1449
1450 lp_build_context_init(&bldivec, gallivm, lp_int_type(vs_type));
1451
1452 /*
1453 * We need to invert the edgeflag bit from the clipmask here
1454 * (because the result is really if we want to run the pipeline or not
1455 * and we (may) need it if edgeflag was 0).
1456 */
1457 if (edgeflag_in_clipmask) {
1458 LLVMValueRef edge = lp_build_const_int_vec(gallivm, bldivec.type,
1459 1LL << DRAW_TOTAL_CLIP_PLANES);
1460 clipmask_bool = LLVMBuildXor(builder, clipmask_bool, edge, "");
1461 }
1462
1463 /*
1464 * XXX: probably should mask off bits from the mask which come from
1465 * vertices which were beyond the count (i.e. indices_valid for
1466 * linear fetches, for elts ones we don't have the correct mask
1467 * right now). Otherwise might run the pipeline for nothing,
1468 * though everything should still work.
1469 */
1470 ret = lp_build_any_true_range(&bldivec, vs_type.length, clipmask_bool);
1471 ret = LLVMBuildZExt(builder, ret, int8_type, "");
1472 return ret;
1473 }
1474
1475 static LLVMValueRef
1476 draw_gs_llvm_fetch_input(const struct lp_build_gs_iface *gs_iface,
1477 struct lp_build_context * bld,
1478 boolean is_vindex_indirect,
1479 LLVMValueRef vertex_index,
1480 boolean is_aindex_indirect,
1481 LLVMValueRef attrib_index,
1482 LLVMValueRef swizzle_index)
1483 {
1484 const struct draw_gs_llvm_iface *gs = draw_gs_llvm_iface(gs_iface);
1485 struct gallivm_state *gallivm = bld->gallivm;
1486 LLVMBuilderRef builder = gallivm->builder;
1487 LLVMValueRef indices[3];
1488 LLVMValueRef res;
1489 struct lp_type type = bld->type;
1490
1491 if (is_vindex_indirect || is_aindex_indirect) {
1492 int i;
1493 res = bld->zero;
1494 for (i = 0; i < type.length; ++i) {
1495 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
1496 LLVMValueRef vert_chan_index = vertex_index;
1497 LLVMValueRef attr_chan_index = attrib_index;
1498 LLVMValueRef channel_vec, value;
1499
1500 if (is_vindex_indirect) {
1501 vert_chan_index = LLVMBuildExtractElement(builder,
1502 vertex_index, idx, "");
1503 }
1504 if (is_aindex_indirect) {
1505 attr_chan_index = LLVMBuildExtractElement(builder,
1506 attrib_index, idx, "");
1507 }
1508
1509 indices[0] = vert_chan_index;
1510 indices[1] = attr_chan_index;
1511 indices[2] = swizzle_index;
1512
1513 channel_vec = LLVMBuildGEP(builder, gs->input, indices, 3, "");
1514 channel_vec = LLVMBuildLoad(builder, channel_vec, "");
1515 value = LLVMBuildExtractElement(builder, channel_vec, idx, "");
1516
1517 res = LLVMBuildInsertElement(builder, res, value, idx, "");
1518 }
1519 } else {
1520 indices[0] = vertex_index;
1521 indices[1] = attrib_index;
1522 indices[2] = swizzle_index;
1523
1524 res = LLVMBuildGEP(builder, gs->input, indices, 3, "");
1525 res = LLVMBuildLoad(builder, res, "");
1526 }
1527
1528 return res;
1529 }
1530
1531 static void
1532 draw_gs_llvm_emit_vertex(const struct lp_build_gs_iface *gs_base,
1533 struct lp_build_context * bld,
1534 LLVMValueRef (*outputs)[4],
1535 LLVMValueRef emitted_vertices_vec,
1536 LLVMValueRef stream_id)
1537 {
1538 const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base);
1539 struct draw_gs_llvm_variant *variant = gs_iface->variant;
1540 struct gallivm_state *gallivm = variant->gallivm;
1541 LLVMBuilderRef builder = gallivm->builder;
1542 struct lp_type gs_type = bld->type;
1543 LLVMValueRef clipmask = lp_build_const_int_vec(gallivm,
1544 lp_int_type(gs_type), 0);
1545 LLVMValueRef indices[LP_MAX_VECTOR_LENGTH];
1546 LLVMValueRef next_prim_offset =
1547 lp_build_const_int32(gallivm, variant->shader->base.primitive_boundary);
1548 LLVMValueRef io = variant->io_ptr;
1549 unsigned i;
1550 const struct tgsi_shader_info *gs_info = &variant->shader->base.info;
1551
1552 for (i = 0; i < gs_type.length; ++i) {
1553 LLVMValueRef ind = lp_build_const_int32(gallivm, i);
1554 LLVMValueRef currently_emitted =
1555 LLVMBuildExtractElement(builder, emitted_vertices_vec, ind, "");
1556 indices[i] = LLVMBuildMul(builder, ind, next_prim_offset, "");
1557 indices[i] = LLVMBuildAdd(builder, indices[i], currently_emitted, "");
1558 }
1559
1560 io = lp_build_pointer_get(builder, io, LLVMBuildExtractElement(builder, stream_id, lp_build_const_int32(gallivm, 0), ""));
1561
1562 convert_to_aos(gallivm, io, indices,
1563 outputs, clipmask,
1564 gs_info->num_outputs, gs_type,
1565 FALSE);
1566 }
1567
1568 static void
1569 draw_gs_llvm_end_primitive(const struct lp_build_gs_iface *gs_base,
1570 struct lp_build_context * bld,
1571 LLVMValueRef total_emitted_vertices_vec_ptr,
1572 LLVMValueRef verts_per_prim_vec,
1573 LLVMValueRef emitted_prims_vec,
1574 LLVMValueRef mask_vec)
1575 {
1576 const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base);
1577 struct draw_gs_llvm_variant *variant = gs_iface->variant;
1578 struct gallivm_state *gallivm = variant->gallivm;
1579 LLVMBuilderRef builder = gallivm->builder;
1580 LLVMValueRef prim_lengts_ptr =
1581 draw_gs_jit_prim_lengths(variant->gallivm, variant->context_ptr);
1582 unsigned i;
1583
1584 for (i = 0; i < bld->type.length; ++i) {
1585 LLVMValueRef ind = lp_build_const_int32(gallivm, i);
1586 LLVMValueRef prims_emitted =
1587 LLVMBuildExtractElement(builder, emitted_prims_vec, ind, "");
1588 LLVMValueRef store_ptr;
1589 LLVMValueRef num_vertices =
1590 LLVMBuildExtractElement(builder, verts_per_prim_vec, ind, "");
1591
1592 store_ptr = LLVMBuildGEP(builder, prim_lengts_ptr, &prims_emitted, 1, "");
1593 store_ptr = LLVMBuildLoad(builder, store_ptr, "");
1594 store_ptr = LLVMBuildGEP(builder, store_ptr, &ind, 1, "");
1595 LLVMBuildStore(builder, num_vertices, store_ptr);
1596 }
1597 }
1598
1599 static void
1600 draw_gs_llvm_epilogue(const struct lp_build_gs_iface *gs_base,
1601 LLVMValueRef total_emitted_vertices_vec,
1602 LLVMValueRef emitted_prims_vec, unsigned stream)
1603 {
1604 const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base);
1605 struct draw_gs_llvm_variant *variant = gs_iface->variant;
1606 struct gallivm_state *gallivm = variant->gallivm;
1607 LLVMBuilderRef builder = gallivm->builder;
1608 LLVMValueRef emitted_verts_ptr =
1609 draw_gs_jit_emitted_vertices(gallivm, variant->context_ptr);
1610 LLVMValueRef emitted_prims_ptr =
1611 draw_gs_jit_emitted_prims(gallivm, variant->context_ptr);
1612 LLVMValueRef stream_val = lp_build_const_int32(gallivm, stream);
1613
1614 emitted_verts_ptr = LLVMBuildGEP(builder, emitted_verts_ptr, &stream_val, 1, "");
1615 emitted_prims_ptr = LLVMBuildGEP(builder, emitted_prims_ptr, &stream_val, 1, "");
1616
1617 LLVMBuildStore(builder, total_emitted_vertices_vec, emitted_verts_ptr);
1618 LLVMBuildStore(builder, emitted_prims_vec, emitted_prims_ptr);
1619 }
1620
1621 static void
1622 draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *variant)
1623 {
1624 struct gallivm_state *gallivm = variant->gallivm;
1625 LLVMContextRef context = gallivm->context;
1626 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context);
1627 LLVMTypeRef arg_types[12];
1628 unsigned num_arg_types = ARRAY_SIZE(arg_types);
1629 LLVMTypeRef func_type;
1630 LLVMValueRef context_ptr;
1631 LLVMBasicBlockRef block;
1632 LLVMBuilderRef builder;
1633 char func_name[64];
1634 struct lp_type vs_type;
1635 LLVMValueRef count, fetch_elts, start_or_maxelt;
1636 LLVMValueRef vertex_id_offset;
1637 LLVMValueRef stride, step, io_itr;
1638 LLVMValueRef ind_vec, start_vec, have_elts, fetch_max, tmp;
1639 LLVMValueRef io_ptr, vbuffers_ptr, vb_ptr;
1640 LLVMValueRef vb_stride[PIPE_MAX_ATTRIBS];
1641 LLVMValueRef map_ptr[PIPE_MAX_ATTRIBS];
1642 LLVMValueRef buffer_size_adj[PIPE_MAX_ATTRIBS];
1643 LLVMValueRef instance_index[PIPE_MAX_ATTRIBS];
1644 LLVMValueRef fake_buf_ptr, fake_buf;
1645
1646 struct draw_context *draw = llvm->draw;
1647 const struct tgsi_shader_info *vs_info = &draw->vs.vertex_shader->info;
1648 unsigned i, j;
1649 struct lp_build_context bld, blduivec;
1650 struct lp_build_loop_state lp_loop;
1651 struct lp_build_if_state if_ctx;
1652 const int vector_length = lp_native_vector_width / 32;
1653 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
1654 struct lp_build_sampler_soa *sampler = 0;
1655 struct lp_build_image_soa *image = NULL;
1656 LLVMValueRef ret, clipmask_bool_ptr;
1657 struct draw_llvm_variant_key *key = &variant->key;
1658 /* If geometry shader is present we need to skip both the viewport
1659 * transformation and clipping otherwise the inputs to the geometry
1660 * shader will be incorrect.
1661 * The code can't handle vp transform when vs writes vp index neither
1662 * (though this would be fixable here, but couldn't just broadcast
1663 * the values).
1664 */
1665 const boolean bypass_viewport = key->has_gs || key->bypass_viewport ||
1666 vs_info->writes_viewport_index;
1667 const boolean enable_cliptest = !key->has_gs && (key->clip_xy ||
1668 key->clip_z ||
1669 key->clip_user ||
1670 key->need_edgeflags);
1671 LLVMValueRef variant_func;
1672 const unsigned pos = draw->vs.position_output;
1673 const unsigned cv = draw->vs.clipvertex_output;
1674 boolean have_clipdist = FALSE;
1675 struct lp_bld_tgsi_system_values system_values;
1676
1677 memset(&system_values, 0, sizeof(system_values));
1678 memset(&outputs, 0, sizeof(outputs));
1679 snprintf(func_name, sizeof(func_name), "draw_llvm_vs_variant%u",
1680 variant->shader->variants_cached);
1681
1682 i = 0;
1683 arg_types[i++] = get_context_ptr_type(variant); /* context */
1684 arg_types[i++] = get_vertex_header_ptr_type(variant); /* vertex_header */
1685 arg_types[i++] = get_buffer_ptr_type(variant); /* vbuffers */
1686 arg_types[i++] = int32_type; /* count */
1687 arg_types[i++] = int32_type; /* start/fetch_elt_max */
1688 arg_types[i++] = int32_type; /* stride */
1689 arg_types[i++] = get_vb_ptr_type(variant); /* pipe_vertex_buffer's */
1690 arg_types[i++] = int32_type; /* instance_id */
1691 arg_types[i++] = int32_type; /* vertex_id_offset */
1692 arg_types[i++] = int32_type; /* start_instance */
1693 arg_types[i++] = LLVMPointerType(int32_type, 0); /* fetch_elts */
1694 arg_types[i++] = int32_type; /* draw_id */
1695
1696 func_type = LLVMFunctionType(LLVMInt8TypeInContext(context),
1697 arg_types, num_arg_types, 0);
1698
1699 variant_func = LLVMAddFunction(gallivm->module, func_name, func_type);
1700 variant->function = variant_func;
1701
1702 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv);
1703 for (i = 0; i < num_arg_types; ++i)
1704 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
1705 lp_add_function_attr(variant_func, i + 1, LP_FUNC_ATTR_NOALIAS);
1706
1707 context_ptr = LLVMGetParam(variant_func, 0);
1708 io_ptr = LLVMGetParam(variant_func, 1);
1709 vbuffers_ptr = LLVMGetParam(variant_func, 2);
1710 count = LLVMGetParam(variant_func, 3);
1711 /*
1712 * XXX: the maxelt part is unused. Not really useful, since we cannot
1713 * get index buffer overflows due to vsplit (which provides its own
1714 * elts buffer, with a different size than what's passed in here).
1715 */
1716 start_or_maxelt = LLVMGetParam(variant_func, 4);
1717 /*
1718 * XXX: stride is actually unused. The stride we use is strictly calculated
1719 * from the number of outputs (including the draw_extra outputs).
1720 * Should probably fix some day (we need a new vs just because of extra
1721 * outputs which the generated vs won't touch).
1722 */
1723 stride = LLVMGetParam(variant_func, 5);
1724 vb_ptr = LLVMGetParam(variant_func, 6);
1725 system_values.instance_id = LLVMGetParam(variant_func, 7);
1726 vertex_id_offset = LLVMGetParam(variant_func, 8);
1727 system_values.base_instance = LLVMGetParam(variant_func, 9);
1728 fetch_elts = LLVMGetParam(variant_func, 10);
1729 system_values.draw_id = LLVMGetParam(variant_func, 11);
1730
1731 lp_build_name(context_ptr, "context");
1732 lp_build_name(io_ptr, "io");
1733 lp_build_name(vbuffers_ptr, "vbuffers");
1734 lp_build_name(count, "count");
1735 lp_build_name(start_or_maxelt, "start_or_maxelt");
1736 lp_build_name(stride, "stride");
1737 lp_build_name(vb_ptr, "vb");
1738 lp_build_name(system_values.instance_id, "instance_id");
1739 lp_build_name(vertex_id_offset, "vertex_id_offset");
1740 lp_build_name(system_values.base_instance, "start_instance");
1741 lp_build_name(fetch_elts, "fetch_elts");
1742 lp_build_name(system_values.draw_id, "draw_id");
1743
1744 /*
1745 * Function body
1746 */
1747
1748 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry");
1749 builder = gallivm->builder;
1750 LLVMPositionBuilderAtEnd(builder, block);
1751
1752 memset(&vs_type, 0, sizeof vs_type);
1753 vs_type.floating = TRUE; /* floating point values */
1754 vs_type.sign = TRUE; /* values are signed */
1755 vs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */
1756 vs_type.width = 32; /* 32-bit float */
1757 vs_type.length = vector_length;
1758
1759 lp_build_context_init(&bld, gallivm, lp_type_uint(32));
1760 lp_build_context_init(&blduivec, gallivm, lp_uint_type(vs_type));
1761
1762 /* hold temporary "bool" clipmask */
1763 clipmask_bool_ptr = lp_build_alloca(gallivm, blduivec.vec_type, "");
1764
1765 fake_buf = lp_build_alloca_undef(gallivm,
1766 LLVMVectorType(LLVMInt64TypeInContext(context), 4), "");
1767 fake_buf = LLVMBuildBitCast(builder, fake_buf,
1768 LLVMPointerType(LLVMInt8TypeInContext(context), 0), "");
1769 fake_buf_ptr = LLVMBuildGEP(builder, fake_buf, &bld.zero, 1, "");
1770
1771 /* code generated texture sampling */
1772 sampler = draw_llvm_sampler_soa_create(draw_llvm_variant_key_samplers(key));
1773
1774 image = draw_llvm_image_soa_create(draw_llvm_variant_key_images(key));
1775
1776 step = lp_build_const_int32(gallivm, vector_length);
1777
1778 ind_vec = blduivec.undef;
1779 for (i = 0; i < vs_type.length; i++) {
1780 LLVMValueRef index = lp_build_const_int32(gallivm, i);
1781 ind_vec = LLVMBuildInsertElement(builder, ind_vec, index, index, "");
1782 }
1783
1784 have_elts = LLVMBuildICmp(builder, LLVMIntNE,
1785 LLVMConstPointerNull(arg_types[10]), fetch_elts, "");
1786
1787 fetch_max = LLVMBuildSub(builder, count, bld.one, "fetch_max");
1788 fetch_max = lp_build_broadcast_scalar(&blduivec, fetch_max);
1789 /*
1790 * Only needed for non-indexed path.
1791 */
1792 start_vec = lp_build_broadcast_scalar(&blduivec, start_or_maxelt);
1793
1794 /*
1795 * Pre-calculate everything which is constant per shader invocation.
1796 */
1797 for (j = 0; j < key->nr_vertex_elements; ++j) {
1798 LLVMValueRef vb_buffer_offset, buffer_size, temp_ptr;
1799 LLVMValueRef vb_info, vbuffer_ptr, buf_offset, ofbit;
1800 struct pipe_vertex_element *velem = &key->vertex_element[j];
1801 LLVMValueRef vb_index =
1802 lp_build_const_int32(gallivm, velem->vertex_buffer_index);
1803 LLVMValueRef bsize = lp_build_const_int32(gallivm,
1804 util_format_get_blocksize(velem->src_format));
1805 LLVMValueRef src_offset = lp_build_const_int32(gallivm,
1806 velem->src_offset);
1807 struct lp_build_if_state if_ctx;
1808
1809 if (velem->src_format != PIPE_FORMAT_NONE) {
1810 vbuffer_ptr = LLVMBuildGEP(builder, vbuffers_ptr, &vb_index, 1, "");
1811 vb_info = LLVMBuildGEP(builder, vb_ptr, &vb_index, 1, "");
1812 vb_stride[j] = draw_jit_vbuffer_stride(gallivm, vb_info);
1813 vb_stride[j] = LLVMBuildZExt(gallivm->builder, vb_stride[j],
1814 LLVMInt32TypeInContext(context), "");
1815 vb_buffer_offset = draw_jit_vbuffer_offset(gallivm, vb_info);
1816 map_ptr[j] = draw_jit_dvbuffer_map(gallivm, vbuffer_ptr);
1817 buffer_size = draw_jit_dvbuffer_size(gallivm, vbuffer_ptr);
1818
1819 ofbit = NULL;
1820 /*
1821 * We'll set buffer_size_adj to zero if we have of, so it will
1822 * always overflow later automatically without having to keep ofbit.
1823 * Overflows (with normal wraparound) doing the actual offset
1824 * calculation should be ok, just not for the buffer size calc.
1825 * It would also be possible to detect such overflows and return
1826 * zeros if that happens, but this would be more complex.
1827 */
1828 buf_offset = lp_build_add(&bld, vb_buffer_offset, src_offset);
1829 tmp = lp_build_sub(&bld, bsize, bld.one);
1830 buffer_size_adj[j] = lp_build_usub_overflow(gallivm, buffer_size, tmp,
1831 &ofbit);
1832 buffer_size_adj[j] = lp_build_usub_overflow(gallivm, buffer_size_adj[j],
1833 buf_offset, &ofbit);
1834
1835 /*
1836 * We can't easily set fake vertex buffers outside the generated code.
1837 * Hence, set fake vertex buffers here instead basically, so fetch
1838 * code can always fetch using offset 0, eliminating all control flow
1839 * inside the main loop.
1840 * (Alternatively, could have control flow per vector skipping fetch
1841 * if ofbit is true.)
1842 */
1843 if (velem->instance_divisor) {
1844 /*
1845 * Index is equal to the start instance plus the number of current
1846 * instance divided by the divisor. In this case we compute it as:
1847 * index = start_instance + (instance_id / divisor).
1848 * Note we could actually do the fetch here, outside the loop -
1849 * it's all constant, hopefully llvm recognizes this.
1850 */
1851 LLVMValueRef current_instance;
1852 current_instance = LLVMBuildUDiv(builder, system_values.instance_id,
1853 lp_build_const_int32(gallivm,
1854 velem->instance_divisor),
1855 "instance_divisor");
1856 instance_index[j] = lp_build_uadd_overflow(gallivm, system_values.base_instance,
1857 current_instance, &ofbit);
1858 }
1859
1860 buffer_size_adj[j] = LLVMBuildSelect(builder, ofbit, bld.zero,
1861 buffer_size_adj[j], "");
1862
1863 temp_ptr = lp_build_alloca_undef(gallivm,
1864 LLVMPointerType(LLVMInt8TypeInContext(context), 0), "");
1865
1866 lp_build_if(&if_ctx, gallivm, ofbit);
1867 {
1868 LLVMBuildStore(builder, fake_buf_ptr, temp_ptr);
1869 }
1870 lp_build_else(&if_ctx);
1871 {
1872 map_ptr[j] = LLVMBuildGEP(builder, map_ptr[j], &buf_offset, 1, "");
1873 LLVMBuildStore(builder, map_ptr[j], temp_ptr);
1874 }
1875 lp_build_endif(&if_ctx);
1876 map_ptr[j] = LLVMBuildLoad(builder, temp_ptr, "map_ptr");
1877
1878 if (0) {
1879 lp_build_printf(gallivm, "velem %d, vbuf index = %u, vb_stride = %u\n",
1880 lp_build_const_int32(gallivm, j),
1881 vb_index, vb_stride[j]);
1882 lp_build_printf(gallivm,
1883 " vb_buffer_offset = %u, src_offset = %u, buf_offset = %u\n",
1884 vb_buffer_offset, src_offset, buf_offset);
1885 lp_build_printf(gallivm, " buffer size = %u, blocksize = %u\n",
1886 buffer_size, bsize);
1887 lp_build_printf(gallivm, " instance_id = %u\n", system_values.instance_id);
1888 }
1889 }
1890 }
1891
1892 lp_build_loop_begin(&lp_loop, gallivm, bld.zero);
1893 {
1894 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
1895 LLVMValueRef io;
1896 LLVMValueRef clipmask; /* holds the clipmask value */
1897 LLVMValueRef true_index_array, index_store;
1898 const LLVMValueRef (*ptr_aos)[TGSI_NUM_CHANNELS];
1899
1900 io_itr = lp_loop.counter;
1901
1902 io = LLVMBuildGEP(builder, io_ptr, &io_itr, 1, "");
1903 #if DEBUG_STORE
1904 lp_build_printf(gallivm, " --- io %d = %p, loop counter %d\n",
1905 io_itr, io, lp_loop.counter);
1906 #endif
1907
1908 true_index_array = lp_build_broadcast_scalar(&blduivec, lp_loop.counter);
1909 true_index_array = LLVMBuildAdd(builder, true_index_array, ind_vec, "");
1910
1911 LLVMValueRef exec_mask = lp_build_cmp(&blduivec, PIPE_FUNC_LEQUAL, true_index_array, fetch_max);
1912 /*
1913 * Limit indices to fetch_max, otherwise might try to access indices
1914 * beyond index buffer (or rather vsplit elt buffer) size.
1915 * Could probably safely (?) skip this for non-indexed draws and
1916 * simplify things minimally (by removing it could combine the ind_vec
1917 * and start_vec adds). I think the only effect for non-indexed draws will
1918 * be that for the invalid elements they will be all fetched from the
1919 * same location as the last valid one, but noone should really care.
1920 */
1921 true_index_array = lp_build_min(&blduivec, true_index_array, fetch_max);
1922
1923 index_store = lp_build_alloca_undef(gallivm, blduivec.vec_type, "index_store");
1924
1925 lp_build_if(&if_ctx, gallivm, have_elts);
1926 {
1927 /*
1928 * Note: you'd expect some comparison/clamp against fetch_elt_max
1929 * here.
1930 * There used to be one here but it was incorrect: overflow was
1931 * detected if index > fetch_elt_max - but the correct condition
1932 * would be index >= fetch_elt_max (since this is just size of elts
1933 * buffer / element size).
1934 * Using the correct condition however will cause failures - due to
1935 * vsplit/vcache code which rebases indices. So, as an example, if
1936 * fetch_elt_max is just 1 and fetch_count 2, vsplit cache will
1937 * replace all invalid indices with 0 - which in case of elt_bias
1938 * not being zero will get a different fetch index than the valid
1939 * index 0. So, just rely on vsplit code preventing out-of-bounds
1940 * fetches. This is also why it's safe to do elts fetch even if there
1941 * was no index buffer bound - the real buffer is never seen here, at
1942 * least not if there are index buffer overflows...
1943 */
1944
1945 /*
1946 * XXX should not have to do this, as scale can be handled
1947 * natively by loads (hits asserts though).
1948 */
1949 tmp = lp_build_shl_imm(&blduivec, true_index_array, 2);
1950 fetch_elts = LLVMBuildBitCast(builder, fetch_elts,
1951 LLVMPointerType(LLVMInt8TypeInContext(context),
1952 0), "");
1953 tmp = lp_build_gather(gallivm, vs_type.length,
1954 32, bld.type, TRUE,
1955 fetch_elts, tmp, FALSE);
1956 LLVMBuildStore(builder, tmp, index_store);
1957 }
1958 lp_build_else(&if_ctx);
1959 {
1960 tmp = LLVMBuildAdd(builder, true_index_array, start_vec, "");
1961 LLVMBuildStore(builder, tmp, index_store);
1962 }
1963 lp_build_endif(&if_ctx);
1964
1965 true_index_array = LLVMBuildLoad(builder, index_store, "");
1966
1967 for (j = 0; j < key->nr_vertex_elements; ++j) {
1968 struct pipe_vertex_element *velem = &key->vertex_element[j];
1969 const struct util_format_description *format_desc =
1970 util_format_description(velem->src_format);
1971
1972 if (format_desc->format == PIPE_FORMAT_NONE) {
1973 for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
1974 inputs[j][i] = lp_build_zero(gallivm, vs_type);
1975 }
1976 }
1977 else if (velem->instance_divisor) {
1978 fetch_instanced(gallivm, format_desc, vs_type,
1979 vb_stride[j], map_ptr[j],
1980 buffer_size_adj[j],
1981 inputs[j], instance_index[j]);
1982 }
1983 else {
1984 fetch_vector(gallivm, format_desc, vs_type,
1985 vb_stride[j], map_ptr[j],
1986 buffer_size_adj[j],
1987 inputs[j], true_index_array);
1988 }
1989 }
1990
1991 struct lp_build_mask_context mask;
1992
1993 lp_build_mask_begin(&mask, gallivm, vs_type, exec_mask);
1994 /* In the paths with elts vertex id has to be unaffected by the
1995 * index bias and because indices inside our elements array have
1996 * already had index bias applied we need to subtract it here to
1997 * get back to the original index.
1998 * in the linear paths vertex id has to be unaffected by the
1999 * original start index and because we abuse the 'start' variable
2000 * to either represent the actual start index or the index at which
2001 * the primitive was split (we split rendering into chunks of at
2002 * most 4095-vertices) we need to back out the original start
2003 * index out of our vertex id here.
2004 * for ARB_shader_draw_parameters, base_vertex should be 0 for non-indexed draws.
2005 */
2006 LLVMValueRef base_vertex = lp_build_select(&bld, have_elts, vertex_id_offset, lp_build_const_int32(gallivm, 0));;
2007 system_values.basevertex = lp_build_broadcast_scalar(&blduivec, base_vertex);
2008 system_values.vertex_id = true_index_array;
2009 system_values.vertex_id_nobase = LLVMBuildSub(builder, true_index_array,
2010 lp_build_broadcast_scalar(&blduivec, vertex_id_offset), "");
2011
2012 ptr_aos = (const LLVMValueRef (*)[TGSI_NUM_CHANNELS]) inputs;
2013 generate_vs(variant,
2014 builder,
2015 vs_type,
2016 outputs,
2017 ptr_aos,
2018 &system_values,
2019 context_ptr,
2020 sampler,
2021 image,
2022 key->clamp_vertex_color,
2023 &mask);
2024
2025 lp_build_mask_end(&mask);
2026 if (pos != -1 && cv != -1) {
2027 /* store original positions in clip before further manipulation */
2028 store_clip(gallivm, vs_type, io, outputs, pos);
2029
2030 /* do cliptest */
2031 if (enable_cliptest) {
2032 LLVMValueRef temp = LLVMBuildLoad(builder, clipmask_bool_ptr, "");
2033 /* allocate clipmask, assign it integer type */
2034 clipmask = generate_clipmask(llvm,
2035 gallivm,
2036 vs_type,
2037 outputs,
2038 key,
2039 context_ptr, &have_clipdist);
2040 temp = LLVMBuildOr(builder, clipmask, temp, "");
2041 /* store temporary clipping boolean value */
2042 LLVMBuildStore(builder, temp, clipmask_bool_ptr);
2043 }
2044 else {
2045 clipmask = blduivec.zero;
2046 }
2047
2048 /* do viewport mapping */
2049 if (!bypass_viewport) {
2050 generate_viewport(variant, builder, vs_type, outputs, context_ptr);
2051 }
2052 }
2053 else {
2054 clipmask = blduivec.zero;
2055 }
2056
2057 /* store clipmask in vertex header,
2058 * original positions in clip
2059 * and transformed positions in data
2060 */
2061 convert_to_aos(gallivm, io, NULL, outputs, clipmask,
2062 vs_info->num_outputs, vs_type,
2063 enable_cliptest && key->need_edgeflags);
2064 }
2065 lp_build_loop_end_cond(&lp_loop, count, step, LLVMIntUGE);
2066
2067 sampler->destroy(sampler);
2068 image->destroy(image);
2069
2070 /* return clipping boolean value for function */
2071 ret = clipmask_booli8(gallivm, vs_type, clipmask_bool_ptr,
2072 enable_cliptest && key->need_edgeflags);
2073
2074 LLVMBuildRet(builder, ret);
2075
2076 gallivm_verify_function(gallivm, variant_func);
2077 }
2078
2079
2080 struct draw_llvm_variant_key *
2081 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store)
2082 {
2083 unsigned i;
2084 struct draw_llvm_variant_key *key;
2085 struct draw_sampler_static_state *draw_sampler;
2086 struct draw_image_static_state *draw_image;
2087
2088 key = (struct draw_llvm_variant_key *)store;
2089
2090 memset(key, 0, offsetof(struct draw_llvm_variant_key, vertex_element[0]));
2091
2092 key->clamp_vertex_color = llvm->draw->rasterizer->clamp_vertex_color; /**/
2093
2094 /* will have to rig this up properly later */
2095 key->clip_xy = llvm->draw->clip_xy;
2096 key->clip_z = llvm->draw->clip_z;
2097 key->clip_user = llvm->draw->clip_user;
2098 key->bypass_viewport = llvm->draw->bypass_viewport;
2099 key->clip_halfz = llvm->draw->rasterizer->clip_halfz;
2100 /* XXX assumes edgeflag output not at 0 */
2101 key->need_edgeflags = (llvm->draw->vs.edgeflag_output ? TRUE : FALSE);
2102 key->ucp_enable = llvm->draw->rasterizer->clip_plane_enable;
2103 key->has_gs = llvm->draw->gs.geometry_shader != NULL;
2104 key->num_outputs = draw_total_vs_outputs(llvm->draw);
2105
2106 /* All variants of this shader will have the same value for
2107 * nr_samplers. Not yet trying to compact away holes in the
2108 * sampler array.
2109 */
2110 key->nr_samplers = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER] + 1;
2111 if (llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) {
2112 key->nr_sampler_views =
2113 llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1;
2114 }
2115 else {
2116 key->nr_sampler_views = key->nr_samplers;
2117 }
2118
2119 key->nr_images = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_IMAGE] + 1;
2120
2121 /* Presumably all variants of the shader should have the same
2122 * number of vertex elements - ie the number of shader inputs.
2123 * NOTE: we NEED to store the needed number of needed inputs
2124 * here, not the number of provided elements to match keysize
2125 * (and the offset of sampler state in the key).
2126 * If we have excess number of vertex elements, this is valid,
2127 * but the excess ones don't matter.
2128 * If we don't have enough vertex elements (which looks not really
2129 * valid but we'll handle it gracefully) fill out missing ones with
2130 * zero (we'll recognize these later by PIPE_FORMAT_NONE).
2131 */
2132 key->nr_vertex_elements =
2133 llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_INPUT] + 1;
2134
2135 if (llvm->draw->pt.nr_vertex_elements < key->nr_vertex_elements) {
2136 debug_printf("draw: vs with %d inputs but only have %d vertex elements\n",
2137 key->nr_vertex_elements, llvm->draw->pt.nr_vertex_elements);
2138 memset(key->vertex_element, 0,
2139 sizeof(struct pipe_vertex_element) * key->nr_vertex_elements);
2140 }
2141 memcpy(key->vertex_element,
2142 llvm->draw->pt.vertex_element,
2143 sizeof(struct pipe_vertex_element) *
2144 MIN2(key->nr_vertex_elements, llvm->draw->pt.nr_vertex_elements));
2145
2146 draw_sampler = draw_llvm_variant_key_samplers(key);
2147 memset(draw_sampler, 0,
2148 MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler);
2149
2150 for (i = 0 ; i < key->nr_samplers; i++) {
2151 lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state,
2152 llvm->draw->samplers[PIPE_SHADER_VERTEX][i]);
2153 }
2154 for (i = 0 ; i < key->nr_sampler_views; i++) {
2155 lp_sampler_static_texture_state(&draw_sampler[i].texture_state,
2156 llvm->draw->sampler_views[PIPE_SHADER_VERTEX][i]);
2157 }
2158
2159 draw_image = draw_llvm_variant_key_images(key);
2160 memset(draw_image, 0,
2161 key->nr_images * sizeof *draw_image);
2162 for (i = 0; i < key->nr_images; i++) {
2163 lp_sampler_static_texture_state_image(&draw_image[i].image_state,
2164 llvm->draw->images[PIPE_SHADER_VERTEX][i]);
2165 }
2166 return key;
2167 }
2168
2169
2170 void
2171 draw_llvm_dump_variant_key(struct draw_llvm_variant_key *key)
2172 {
2173 unsigned i;
2174 struct draw_sampler_static_state *sampler = draw_llvm_variant_key_samplers(key);
2175 struct draw_image_static_state *image = draw_llvm_variant_key_images(key);
2176 debug_printf("clamp_vertex_color = %u\n", key->clamp_vertex_color);
2177 debug_printf("clip_xy = %u\n", key->clip_xy);
2178 debug_printf("clip_z = %u\n", key->clip_z);
2179 debug_printf("clip_user = %u\n", key->clip_user);
2180 debug_printf("bypass_viewport = %u\n", key->bypass_viewport);
2181 debug_printf("clip_halfz = %u\n", key->clip_halfz);
2182 debug_printf("need_edgeflags = %u\n", key->need_edgeflags);
2183 debug_printf("has_gs = %u\n", key->has_gs);
2184 debug_printf("ucp_enable = %u\n", key->ucp_enable);
2185
2186 for (i = 0 ; i < key->nr_vertex_elements; i++) {
2187 debug_printf("vertex_element[%i].src_offset = %u\n", i, key->vertex_element[i].src_offset);
2188 debug_printf("vertex_element[%i].instance_divisor = %u\n", i, key->vertex_element[i].instance_divisor);
2189 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i, key->vertex_element[i].vertex_buffer_index);
2190 debug_printf("vertex_element[%i].src_format = %s\n", i, util_format_name(key->vertex_element[i].src_format));
2191 }
2192
2193 for (i = 0 ; i < key->nr_sampler_views; i++) {
2194 debug_printf("sampler[%i].src_format = %s\n", i, util_format_name(sampler[i].texture_state.format));
2195 }
2196
2197 for (i = 0 ; i < key->nr_images; i++)
2198 debug_printf("images[%i].format = %s\n", i, util_format_name(image[i].image_state.format));
2199 }
2200
2201
2202 void
2203 draw_llvm_set_mapped_texture(struct draw_context *draw,
2204 enum pipe_shader_type shader_stage,
2205 unsigned sview_idx,
2206 uint32_t width, uint32_t height, uint32_t depth,
2207 uint32_t first_level, uint32_t last_level,
2208 const void *base_ptr,
2209 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
2210 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
2211 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])
2212 {
2213 unsigned j;
2214 struct draw_jit_texture *jit_tex;
2215
2216 assert(shader_stage == PIPE_SHADER_VERTEX ||
2217 shader_stage == PIPE_SHADER_GEOMETRY);
2218
2219 if (shader_stage == PIPE_SHADER_VERTEX) {
2220 assert(sview_idx < ARRAY_SIZE(draw->llvm->jit_context.textures));
2221
2222 jit_tex = &draw->llvm->jit_context.textures[sview_idx];
2223 } else if (shader_stage == PIPE_SHADER_GEOMETRY) {
2224 assert(sview_idx < ARRAY_SIZE(draw->llvm->gs_jit_context.textures));
2225
2226 jit_tex = &draw->llvm->gs_jit_context.textures[sview_idx];
2227 } else {
2228 assert(0);
2229 return;
2230 }
2231
2232 jit_tex->width = width;
2233 jit_tex->height = height;
2234 jit_tex->depth = depth;
2235 jit_tex->first_level = first_level;
2236 jit_tex->last_level = last_level;
2237 jit_tex->base = base_ptr;
2238
2239 for (j = first_level; j <= last_level; j++) {
2240 jit_tex->mip_offsets[j] = mip_offsets[j];
2241 jit_tex->row_stride[j] = row_stride[j];
2242 jit_tex->img_stride[j] = img_stride[j];
2243 }
2244 }
2245
2246 void
2247 draw_llvm_set_mapped_image(struct draw_context *draw,
2248 enum pipe_shader_type shader_stage,
2249 unsigned idx,
2250 uint32_t width, uint32_t height, uint32_t depth,
2251 const void *base_ptr,
2252 uint32_t row_stride,
2253 uint32_t img_stride)
2254 {
2255 struct draw_jit_image *jit_image;
2256
2257 assert(shader_stage == PIPE_SHADER_VERTEX ||
2258 shader_stage == PIPE_SHADER_GEOMETRY);
2259
2260 if (shader_stage == PIPE_SHADER_VERTEX) {
2261 assert(idx < ARRAY_SIZE(draw->llvm->jit_context.images));
2262
2263 jit_image = &draw->llvm->jit_context.images[idx];
2264 } else if (shader_stage == PIPE_SHADER_GEOMETRY) {
2265 assert(idx < ARRAY_SIZE(draw->llvm->gs_jit_context.images));
2266
2267 jit_image = &draw->llvm->gs_jit_context.images[idx];
2268 } else {
2269 assert(0);
2270 return;
2271 }
2272
2273 jit_image->width = width;
2274 jit_image->height = height;
2275 jit_image->depth = depth;
2276 jit_image->base = base_ptr;
2277
2278 jit_image->row_stride = row_stride;
2279 jit_image->img_stride = img_stride;
2280 }
2281
2282
2283 void
2284 draw_llvm_set_sampler_state(struct draw_context *draw,
2285 enum pipe_shader_type shader_type)
2286 {
2287 unsigned i;
2288
2289 if (shader_type == PIPE_SHADER_VERTEX) {
2290 for (i = 0; i < draw->num_samplers[PIPE_SHADER_VERTEX]; i++) {
2291 struct draw_jit_sampler *jit_sam = &draw->llvm->jit_context.samplers[i];
2292
2293 if (draw->samplers[PIPE_SHADER_VERTEX][i]) {
2294 const struct pipe_sampler_state *s
2295 = draw->samplers[PIPE_SHADER_VERTEX][i];
2296 jit_sam->min_lod = s->min_lod;
2297 jit_sam->max_lod = s->max_lod;
2298 jit_sam->lod_bias = s->lod_bias;
2299 COPY_4V(jit_sam->border_color, s->border_color.f);
2300 }
2301 }
2302 } else if (shader_type == PIPE_SHADER_GEOMETRY) {
2303 for (i = 0; i < draw->num_samplers[PIPE_SHADER_GEOMETRY]; i++) {
2304 struct draw_jit_sampler *jit_sam = &draw->llvm->gs_jit_context.samplers[i];
2305
2306 if (draw->samplers[PIPE_SHADER_GEOMETRY][i]) {
2307 const struct pipe_sampler_state *s
2308 = draw->samplers[PIPE_SHADER_GEOMETRY][i];
2309 jit_sam->min_lod = s->min_lod;
2310 jit_sam->max_lod = s->max_lod;
2311 jit_sam->lod_bias = s->lod_bias;
2312 COPY_4V(jit_sam->border_color, s->border_color.f);
2313 }
2314 }
2315 }
2316 }
2317
2318
2319 void
2320 draw_llvm_destroy_variant(struct draw_llvm_variant *variant)
2321 {
2322 struct draw_llvm *llvm = variant->llvm;
2323
2324 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
2325 debug_printf("Deleting VS variant: %u vs variants,\t%u total variants\n",
2326 variant->shader->variants_cached, llvm->nr_variants);
2327 }
2328
2329 gallivm_destroy(variant->gallivm);
2330
2331 remove_from_list(&variant->list_item_local);
2332 variant->shader->variants_cached--;
2333 remove_from_list(&variant->list_item_global);
2334 llvm->nr_variants--;
2335 FREE(variant);
2336 }
2337
2338
2339 /**
2340 * Create LLVM types for various structures.
2341 */
2342 static void
2343 create_gs_jit_types(struct draw_gs_llvm_variant *var)
2344 {
2345 struct gallivm_state *gallivm = var->gallivm;
2346 LLVMTypeRef texture_type, sampler_type, image_type, context_type;
2347
2348 texture_type = create_jit_texture_type(gallivm, "texture");
2349 sampler_type = create_jit_sampler_type(gallivm, "sampler");
2350 image_type = create_jit_image_type(gallivm, "image");
2351
2352 context_type = create_gs_jit_context_type(gallivm,
2353 var->shader->base.vector_length,
2354 texture_type, sampler_type,
2355 image_type,
2356 "draw_gs_jit_context");
2357 var->context_ptr_type = LLVMPointerType(context_type, 0);
2358
2359 var->input_array_type = create_gs_jit_input_type(gallivm);
2360 }
2361
2362 static LLVMTypeRef
2363 get_gs_context_ptr_type(struct draw_gs_llvm_variant *variant)
2364 {
2365 if (!variant->context_ptr_type)
2366 create_gs_jit_types(variant);
2367 return variant->context_ptr_type;
2368 }
2369
2370 static LLVMValueRef
2371 generate_mask_value(struct draw_gs_llvm_variant *variant,
2372 struct lp_type gs_type)
2373 {
2374 struct gallivm_state *gallivm = variant->gallivm;
2375 LLVMBuilderRef builder = gallivm->builder;
2376 struct lp_type mask_type = lp_int_type(gs_type);
2377 LLVMValueRef num_prims;
2378 LLVMValueRef mask_val = lp_build_const_vec(gallivm, mask_type, 0);
2379 unsigned i;
2380
2381 num_prims = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, mask_type),
2382 variant->num_prims);
2383 for (i = 0; i < gs_type.length; i++) {
2384 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
2385 mask_val = LLVMBuildInsertElement(builder, mask_val, idx, idx, "");
2386 }
2387 mask_val = lp_build_compare(gallivm, mask_type,
2388 PIPE_FUNC_GREATER, num_prims, mask_val);
2389
2390 return mask_val;
2391 }
2392
2393 static void
2394 draw_gs_llvm_generate(struct draw_llvm *llvm,
2395 struct draw_gs_llvm_variant *variant)
2396 {
2397 struct gallivm_state *gallivm = variant->gallivm;
2398 LLVMContextRef context = gallivm->context;
2399 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context);
2400 LLVMTypeRef arg_types[7];
2401 LLVMTypeRef func_type;
2402 LLVMValueRef variant_func;
2403 LLVMValueRef context_ptr;
2404 LLVMValueRef prim_id_ptr;
2405 LLVMBasicBlockRef block;
2406 LLVMBuilderRef builder;
2407 LLVMValueRef io_ptr, input_array, num_prims, mask_val;
2408 struct lp_build_sampler_soa *sampler = 0;
2409 struct lp_build_image_soa *image = NULL;
2410 struct lp_build_context bld;
2411 struct lp_bld_tgsi_system_values system_values;
2412 char func_name[64];
2413 struct lp_type gs_type;
2414 unsigned i;
2415 struct draw_gs_llvm_iface gs_iface;
2416 const struct tgsi_token *tokens = variant->shader->base.state.tokens;
2417 LLVMValueRef consts_ptr, num_consts_ptr;
2418 LLVMValueRef ssbos_ptr, num_ssbos_ptr;
2419 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
2420 struct lp_build_mask_context mask;
2421 const struct tgsi_shader_info *gs_info = &variant->shader->base.info;
2422 unsigned vector_length = variant->shader->base.vector_length;
2423
2424 memset(&system_values, 0, sizeof(system_values));
2425 memset(&outputs, 0, sizeof(outputs));
2426
2427 snprintf(func_name, sizeof(func_name), "draw_llvm_gs_variant%u",
2428 variant->shader->variants_cached);
2429
2430 assert(variant->vertex_header_ptr_type);
2431
2432 arg_types[0] = get_gs_context_ptr_type(variant); /* context */
2433 arg_types[1] = variant->input_array_type; /* input */
2434 arg_types[2] = LLVMPointerType(variant->vertex_header_ptr_type, 0); /* vertex_header */
2435 arg_types[3] = int32_type; /* num_prims */
2436 arg_types[4] = int32_type; /* instance_id */
2437 arg_types[5] = LLVMPointerType(
2438 LLVMVectorType(int32_type, vector_length), 0); /* prim_id_ptr */
2439 arg_types[6] = int32_type;
2440
2441 func_type = LLVMFunctionType(int32_type, arg_types, ARRAY_SIZE(arg_types), 0);
2442
2443 variant_func = LLVMAddFunction(gallivm->module, func_name, func_type);
2444
2445 variant->function = variant_func;
2446
2447 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv);
2448
2449 for (i = 0; i < ARRAY_SIZE(arg_types); ++i)
2450 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
2451 lp_add_function_attr(variant_func, i + 1, LP_FUNC_ATTR_NOALIAS);
2452
2453 context_ptr = LLVMGetParam(variant_func, 0);
2454 input_array = LLVMGetParam(variant_func, 1);
2455 io_ptr = LLVMGetParam(variant_func, 2);
2456 num_prims = LLVMGetParam(variant_func, 3);
2457 system_values.instance_id = LLVMGetParam(variant_func, 4);
2458 prim_id_ptr = LLVMGetParam(variant_func, 5);
2459 system_values.invocation_id = LLVMGetParam(variant_func, 6);
2460
2461 lp_build_name(context_ptr, "context");
2462 lp_build_name(input_array, "input");
2463 lp_build_name(io_ptr, "io");
2464 lp_build_name(num_prims, "num_prims");
2465 lp_build_name(system_values.instance_id, "instance_id");
2466 lp_build_name(prim_id_ptr, "prim_id_ptr");
2467 lp_build_name(system_values.invocation_id, "invocation_id");
2468
2469 variant->context_ptr = context_ptr;
2470 variant->io_ptr = io_ptr;
2471 variant->num_prims = num_prims;
2472
2473 gs_iface.base.fetch_input = draw_gs_llvm_fetch_input;
2474 gs_iface.base.emit_vertex = draw_gs_llvm_emit_vertex;
2475 gs_iface.base.end_primitive = draw_gs_llvm_end_primitive;
2476 gs_iface.base.gs_epilogue = draw_gs_llvm_epilogue;
2477 gs_iface.input = input_array;
2478 gs_iface.variant = variant;
2479
2480 /*
2481 * Function body
2482 */
2483
2484 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry");
2485 builder = gallivm->builder;
2486 LLVMPositionBuilderAtEnd(builder, block);
2487
2488 lp_build_context_init(&bld, gallivm, lp_type_int(32));
2489
2490 memset(&gs_type, 0, sizeof gs_type);
2491 gs_type.floating = TRUE; /* floating point values */
2492 gs_type.sign = TRUE; /* values are signed */
2493 gs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */
2494 gs_type.width = 32; /* 32-bit float */
2495 gs_type.length = vector_length;
2496
2497 consts_ptr = draw_gs_jit_context_constants(variant->gallivm, context_ptr);
2498 num_consts_ptr =
2499 draw_gs_jit_context_num_constants(variant->gallivm, context_ptr);
2500
2501 ssbos_ptr = draw_gs_jit_context_ssbos(variant->gallivm, context_ptr);
2502 num_ssbos_ptr =
2503 draw_gs_jit_context_num_ssbos(variant->gallivm, context_ptr);
2504
2505 /* code generated texture sampling */
2506 sampler = draw_llvm_sampler_soa_create(variant->key.samplers);
2507 image = draw_llvm_image_soa_create(draw_gs_llvm_variant_key_images(&variant->key));
2508 mask_val = generate_mask_value(variant, gs_type);
2509 lp_build_mask_begin(&mask, gallivm, gs_type, mask_val);
2510
2511 if (gs_info->uses_primid) {
2512 system_values.prim_id = LLVMBuildLoad(builder, prim_id_ptr, "prim_id");
2513 }
2514
2515 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
2516 if (llvm->draw->gs.geometry_shader->state.type == PIPE_SHADER_IR_TGSI)
2517 tgsi_dump(tokens, 0);
2518 else
2519 nir_print_shader(llvm->draw->gs.geometry_shader->state.ir.nir, stderr);
2520 draw_gs_llvm_dump_variant_key(&variant->key);
2521 }
2522
2523 struct lp_build_tgsi_params params;
2524 memset(&params, 0, sizeof(params));
2525
2526 params.type = gs_type;
2527 params.mask = &mask;
2528 params.consts_ptr = consts_ptr;
2529 params.const_sizes_ptr = num_consts_ptr;
2530 params.system_values = &system_values;
2531 params.context_ptr = context_ptr;
2532 params.sampler = sampler;
2533 params.info = &llvm->draw->gs.geometry_shader->info;
2534 params.gs_iface = (const struct lp_build_gs_iface *)&gs_iface;
2535 params.ssbo_ptr = ssbos_ptr;
2536 params.ssbo_sizes_ptr = num_ssbos_ptr;
2537 params.image = image;
2538
2539 if (llvm->draw->gs.geometry_shader->state.type == PIPE_SHADER_IR_TGSI)
2540 lp_build_tgsi_soa(variant->gallivm,
2541 tokens,
2542 &params,
2543 outputs);
2544 else
2545 lp_build_nir_soa(variant->gallivm,
2546 llvm->draw->gs.geometry_shader->state.ir.nir,
2547 &params,
2548 outputs);
2549
2550 sampler->destroy(sampler);
2551 image->destroy(image);
2552
2553 lp_build_mask_end(&mask);
2554
2555 LLVMBuildRet(builder, lp_build_zero(gallivm, lp_type_uint(32)));
2556
2557 gallivm_verify_function(gallivm, variant_func);
2558 }
2559
2560
2561 struct draw_gs_llvm_variant *
2562 draw_gs_llvm_create_variant(struct draw_llvm *llvm,
2563 unsigned num_outputs,
2564 const struct draw_gs_llvm_variant_key *key)
2565 {
2566 struct draw_gs_llvm_variant *variant;
2567 struct llvm_geometry_shader *shader =
2568 llvm_geometry_shader(llvm->draw->gs.geometry_shader);
2569 LLVMTypeRef vertex_header;
2570 char module_name[64];
2571
2572 variant = MALLOC(sizeof *variant +
2573 shader->variant_key_size -
2574 sizeof variant->key);
2575 if (!variant)
2576 return NULL;
2577
2578 variant->llvm = llvm;
2579 variant->shader = shader;
2580
2581 snprintf(module_name, sizeof(module_name), "draw_llvm_gs_variant%u",
2582 variant->shader->variants_cached);
2583
2584 variant->gallivm = gallivm_create(module_name, llvm->context);
2585
2586 create_gs_jit_types(variant);
2587
2588 memcpy(&variant->key, key, shader->variant_key_size);
2589
2590 vertex_header = create_jit_vertex_header(variant->gallivm, num_outputs);
2591
2592 variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0);
2593
2594 draw_gs_llvm_generate(llvm, variant);
2595
2596 gallivm_compile_module(variant->gallivm);
2597
2598 variant->jit_func = (draw_gs_jit_func)
2599 gallivm_jit_function(variant->gallivm, variant->function);
2600
2601 gallivm_free_ir(variant->gallivm);
2602
2603 variant->list_item_global.base = variant;
2604 variant->list_item_local.base = variant;
2605 /*variant->no = */shader->variants_created++;
2606 variant->list_item_global.base = variant;
2607
2608 return variant;
2609 }
2610
2611 void
2612 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant *variant)
2613 {
2614 struct draw_llvm *llvm = variant->llvm;
2615
2616 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
2617 debug_printf("Deleting GS variant: %u gs variants,\t%u total variants\n",
2618 variant->shader->variants_cached, llvm->nr_gs_variants);
2619 }
2620
2621 gallivm_destroy(variant->gallivm);
2622
2623 remove_from_list(&variant->list_item_local);
2624 variant->shader->variants_cached--;
2625 remove_from_list(&variant->list_item_global);
2626 llvm->nr_gs_variants--;
2627 FREE(variant);
2628 }
2629
2630 struct draw_gs_llvm_variant_key *
2631 draw_gs_llvm_make_variant_key(struct draw_llvm *llvm, char *store)
2632 {
2633 unsigned i;
2634 struct draw_gs_llvm_variant_key *key;
2635 struct draw_sampler_static_state *draw_sampler;
2636 struct draw_image_static_state *draw_image;
2637
2638 key = (struct draw_gs_llvm_variant_key *)store;
2639
2640 memset(key, 0, offsetof(struct draw_gs_llvm_variant_key, samplers[0]));
2641
2642 key->num_outputs = draw_total_gs_outputs(llvm->draw);
2643
2644 /* All variants of this shader will have the same value for
2645 * nr_samplers. Not yet trying to compact away holes in the
2646 * sampler array.
2647 */
2648 key->nr_samplers = llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER] + 1;
2649 if (llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) {
2650 key->nr_sampler_views =
2651 llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1;
2652 }
2653 else {
2654 key->nr_sampler_views = key->nr_samplers;
2655 }
2656
2657 key->nr_images = llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_IMAGE] + 1;
2658
2659 draw_sampler = key->samplers;
2660
2661 memset(draw_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler);
2662
2663 for (i = 0 ; i < key->nr_samplers; i++) {
2664 lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state,
2665 llvm->draw->samplers[PIPE_SHADER_GEOMETRY][i]);
2666 }
2667 for (i = 0 ; i < key->nr_sampler_views; i++) {
2668 lp_sampler_static_texture_state(&draw_sampler[i].texture_state,
2669 llvm->draw->sampler_views[PIPE_SHADER_GEOMETRY][i]);
2670 }
2671
2672 draw_image = draw_gs_llvm_variant_key_images(key);
2673 memset(draw_image, 0,
2674 key->nr_images * sizeof *draw_image);
2675 for (i = 0; i < key->nr_images; i++) {
2676 lp_sampler_static_texture_state_image(&draw_image[i].image_state,
2677 llvm->draw->images[PIPE_SHADER_GEOMETRY][i]);
2678 }
2679 return key;
2680 }
2681
2682 void
2683 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key *key)
2684 {
2685 unsigned i;
2686 struct draw_sampler_static_state *sampler = key->samplers;
2687 struct draw_image_static_state *image = draw_gs_llvm_variant_key_images(key);
2688 for (i = 0 ; i < key->nr_sampler_views; i++) {
2689 debug_printf("sampler[%i].src_format = %s\n", i,
2690 util_format_name(sampler[i].texture_state.format));
2691 }
2692
2693 for (i = 0 ; i < key->nr_images; i++)
2694 debug_printf("images[%i].format = %s\n", i, util_format_name(image[i].image_state.format));
2695
2696 }