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