draw: Dump LLVM shader key.
[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
33 #include "gallivm/lp_bld_arit.h"
34 #include "gallivm/lp_bld_logic.h"
35 #include "gallivm/lp_bld_const.h"
36 #include "gallivm/lp_bld_swizzle.h"
37 #include "gallivm/lp_bld_struct.h"
38 #include "gallivm/lp_bld_type.h"
39 #include "gallivm/lp_bld_flow.h"
40 #include "gallivm/lp_bld_debug.h"
41 #include "gallivm/lp_bld_tgsi.h"
42 #include "gallivm/lp_bld_printf.h"
43 #include "gallivm/lp_bld_intr.h"
44 #include "gallivm/lp_bld_init.h"
45 #include "gallivm/lp_bld_type.h"
46 #include "gallivm/lp_bld_pack.h"
47 #include "gallivm/lp_bld_format.h"
48
49 #include "tgsi/tgsi_exec.h"
50 #include "tgsi/tgsi_dump.h"
51
52 #include "util/u_math.h"
53 #include "util/u_pointer.h"
54 #include "util/u_string.h"
55 #include "util/u_simple_list.h"
56
57
58 #define DEBUG_STORE 0
59
60
61 static void
62 draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *var,
63 boolean elts);
64
65
66 /**
67 * Create LLVM type for struct draw_jit_texture
68 */
69 static LLVMTypeRef
70 create_jit_texture_type(struct gallivm_state *gallivm, const char *struct_name)
71 {
72 LLVMTargetDataRef target = gallivm->target;
73 LLVMTypeRef texture_type;
74 LLVMTypeRef elem_types[DRAW_JIT_TEXTURE_NUM_FIELDS];
75 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
76
77 elem_types[DRAW_JIT_TEXTURE_WIDTH] =
78 elem_types[DRAW_JIT_TEXTURE_HEIGHT] =
79 elem_types[DRAW_JIT_TEXTURE_DEPTH] =
80 elem_types[DRAW_JIT_TEXTURE_FIRST_LEVEL] =
81 elem_types[DRAW_JIT_TEXTURE_LAST_LEVEL] = int32_type;
82 elem_types[DRAW_JIT_TEXTURE_BASE] =
83 LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0);
84 elem_types[DRAW_JIT_TEXTURE_ROW_STRIDE] =
85 elem_types[DRAW_JIT_TEXTURE_IMG_STRIDE] =
86 elem_types[DRAW_JIT_TEXTURE_MIP_OFFSETS] =
87 LLVMArrayType(int32_type, PIPE_MAX_TEXTURE_LEVELS);
88 elem_types[DRAW_JIT_TEXTURE_MIN_LOD] =
89 elem_types[DRAW_JIT_TEXTURE_MAX_LOD] =
90 elem_types[DRAW_JIT_TEXTURE_LOD_BIAS] = LLVMFloatTypeInContext(gallivm->context);
91 elem_types[DRAW_JIT_TEXTURE_BORDER_COLOR] =
92 LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4);
93
94 texture_type = LLVMStructTypeInContext(gallivm->context, elem_types,
95 Elements(elem_types), 0);
96
97 #if HAVE_LLVM < 0x0300
98 LLVMAddTypeName(gallivm->module, struct_name, texture_type);
99
100 /* Make sure the target's struct layout cache doesn't return
101 * stale/invalid data.
102 */
103 LLVMInvalidateStructLayout(gallivm->target, texture_type);
104 #endif
105
106 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, width,
107 target, texture_type,
108 DRAW_JIT_TEXTURE_WIDTH);
109 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, height,
110 target, texture_type,
111 DRAW_JIT_TEXTURE_HEIGHT);
112 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, depth,
113 target, texture_type,
114 DRAW_JIT_TEXTURE_DEPTH);
115 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, first_level,
116 target, texture_type,
117 DRAW_JIT_TEXTURE_FIRST_LEVEL);
118 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, last_level,
119 target, texture_type,
120 DRAW_JIT_TEXTURE_LAST_LEVEL);
121 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, base,
122 target, texture_type,
123 DRAW_JIT_TEXTURE_BASE);
124 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, row_stride,
125 target, texture_type,
126 DRAW_JIT_TEXTURE_ROW_STRIDE);
127 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, img_stride,
128 target, texture_type,
129 DRAW_JIT_TEXTURE_IMG_STRIDE);
130 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, mip_offsets,
131 target, texture_type,
132 DRAW_JIT_TEXTURE_MIP_OFFSETS);
133 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, min_lod,
134 target, texture_type,
135 DRAW_JIT_TEXTURE_MIN_LOD);
136 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, max_lod,
137 target, texture_type,
138 DRAW_JIT_TEXTURE_MAX_LOD);
139 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, lod_bias,
140 target, texture_type,
141 DRAW_JIT_TEXTURE_LOD_BIAS);
142 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, border_color,
143 target, texture_type,
144 DRAW_JIT_TEXTURE_BORDER_COLOR);
145
146 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture, target, texture_type);
147
148 return texture_type;
149 }
150
151
152 /**
153 * Create LLVM type for struct draw_jit_texture
154 */
155 static LLVMTypeRef
156 create_jit_context_type(struct gallivm_state *gallivm,
157 LLVMTypeRef texture_type, const char *struct_name)
158 {
159 LLVMTargetDataRef target = gallivm->target;
160 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
161 LLVMTypeRef elem_types[5];
162 LLVMTypeRef context_type;
163
164 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* vs_constants */
165 LP_MAX_TGSI_CONST_BUFFERS);
166 elem_types[1] = elem_types[0]; /* gs_constants */
167 elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4),
168 DRAW_TOTAL_CLIP_PLANES), 0);
169 elem_types[3] = LLVMPointerType(float_type, 0); /* viewport */
170 elem_types[4] = LLVMArrayType(texture_type,
171 PIPE_MAX_SAMPLERS); /* textures */
172 context_type = LLVMStructTypeInContext(gallivm->context, elem_types,
173 Elements(elem_types), 0);
174 #if HAVE_LLVM < 0x0300
175 LLVMAddTypeName(gallivm->module, struct_name, context_type);
176
177 LLVMInvalidateStructLayout(gallivm->target, context_type);
178 #endif
179
180 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, vs_constants,
181 target, context_type, 0);
182 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, gs_constants,
183 target, context_type, 1);
184 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, planes,
185 target, context_type, 2);
186 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, textures,
187 target, context_type,
188 DRAW_JIT_CTX_TEXTURES);
189 LP_CHECK_STRUCT_SIZE(struct draw_jit_context,
190 target, context_type);
191
192 return context_type;
193 }
194
195
196 /**
197 * Create LLVM type for struct pipe_vertex_buffer
198 */
199 static LLVMTypeRef
200 create_jit_vertex_buffer_type(struct gallivm_state *gallivm, const char *struct_name)
201 {
202 LLVMTargetDataRef target = gallivm->target;
203 LLVMTypeRef elem_types[4];
204 LLVMTypeRef vb_type;
205
206 elem_types[0] =
207 elem_types[1] = LLVMInt32TypeInContext(gallivm->context);
208 elem_types[2] =
209 elem_types[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0); /* vs_constants */
210
211 vb_type = LLVMStructTypeInContext(gallivm->context, elem_types,
212 Elements(elem_types), 0);
213 #if HAVE_LLVM < 0x0300
214 LLVMAddTypeName(gallivm->module, struct_name, vb_type);
215
216 LLVMInvalidateStructLayout(gallivm->target, vb_type);
217 #endif
218
219 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, stride,
220 target, vb_type, 0);
221 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, buffer_offset,
222 target, vb_type, 1);
223
224 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer, target, vb_type);
225
226 return vb_type;
227 }
228
229
230 /**
231 * Create LLVM type for struct vertex_header;
232 */
233 static LLVMTypeRef
234 create_jit_vertex_header(struct gallivm_state *gallivm, int data_elems)
235 {
236 LLVMTargetDataRef target = gallivm->target;
237 LLVMTypeRef elem_types[4];
238 LLVMTypeRef vertex_header;
239 char struct_name[24];
240
241 util_snprintf(struct_name, 23, "vertex_header%d", data_elems);
242
243 elem_types[DRAW_JIT_VERTEX_VERTEX_ID] = LLVMIntTypeInContext(gallivm->context, 32);
244 elem_types[DRAW_JIT_VERTEX_CLIP] = LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4);
245 elem_types[DRAW_JIT_VERTEX_PRE_CLIP_POS] = LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4);
246 elem_types[DRAW_JIT_VERTEX_DATA] = LLVMArrayType(elem_types[1], data_elems);
247
248 vertex_header = LLVMStructTypeInContext(gallivm->context, elem_types,
249 Elements(elem_types), 0);
250 #if HAVE_LLVM < 0x0300
251 LLVMAddTypeName(gallivm->module, struct_name, vertex_header);
252
253 LLVMInvalidateStructLayout(gallivm->target, vertex_header);
254 #endif
255
256 /* these are bit-fields and we can't take address of them
257 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
258 target, vertex_header,
259 DRAW_JIT_VERTEX_CLIPMASK);
260 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
261 target, vertex_header,
262 DRAW_JIT_VERTEX_EDGEFLAG);
263 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
264 target, vertex_header,
265 DRAW_JIT_VERTEX_PAD);
266 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
267 target, vertex_header,
268 DRAW_JIT_VERTEX_VERTEX_ID);
269 */
270 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clip,
271 target, vertex_header,
272 DRAW_JIT_VERTEX_CLIP);
273 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pre_clip_pos,
274 target, vertex_header,
275 DRAW_JIT_VERTEX_PRE_CLIP_POS);
276 LP_CHECK_MEMBER_OFFSET(struct vertex_header, data,
277 target, vertex_header,
278 DRAW_JIT_VERTEX_DATA);
279
280 assert(LLVMABISizeOfType(target, vertex_header) ==
281 offsetof(struct vertex_header, data[data_elems]));
282
283 return vertex_header;
284 }
285
286
287 /**
288 * Create LLVM types for various structures.
289 */
290 static void
291 create_jit_types(struct draw_llvm_variant *variant)
292 {
293 struct gallivm_state *gallivm = variant->gallivm;
294 LLVMTypeRef texture_type, context_type, buffer_type, vb_type;
295
296 texture_type = create_jit_texture_type(gallivm, "texture");
297
298 context_type = create_jit_context_type(gallivm, texture_type, "draw_jit_context");
299 variant->context_ptr_type = LLVMPointerType(context_type, 0);
300
301 buffer_type = LLVMPointerType(LLVMIntTypeInContext(gallivm->context, 8), 0);
302 variant->buffer_ptr_type = LLVMPointerType(buffer_type, 0);
303
304 vb_type = create_jit_vertex_buffer_type(gallivm, "pipe_vertex_buffer");
305 variant->vb_ptr_type = LLVMPointerType(vb_type, 0);
306 }
307
308
309 static LLVMTypeRef
310 get_context_ptr_type(struct draw_llvm_variant *variant)
311 {
312 if (!variant->context_ptr_type)
313 create_jit_types(variant);
314 return variant->context_ptr_type;
315 }
316
317
318 static LLVMTypeRef
319 get_buffer_ptr_type(struct draw_llvm_variant *variant)
320 {
321 if (!variant->buffer_ptr_type)
322 create_jit_types(variant);
323 return variant->buffer_ptr_type;
324 }
325
326
327 static LLVMTypeRef
328 get_vb_ptr_type(struct draw_llvm_variant *variant)
329 {
330 if (!variant->vb_ptr_type)
331 create_jit_types(variant);
332 return variant->vb_ptr_type;
333 }
334
335 static LLVMTypeRef
336 get_vertex_header_ptr_type(struct draw_llvm_variant *variant)
337 {
338 if (!variant->vertex_header_ptr_type)
339 create_jit_types(variant);
340 return variant->vertex_header_ptr_type;
341 }
342
343
344 /**
345 * Create per-context LLVM info.
346 */
347 struct draw_llvm *
348 draw_llvm_create(struct draw_context *draw)
349 {
350 struct draw_llvm *llvm;
351
352 llvm = CALLOC_STRUCT( draw_llvm );
353 if (!llvm)
354 return NULL;
355
356 lp_build_init();
357
358 llvm->draw = draw;
359
360 llvm->nr_variants = 0;
361 make_empty_list(&llvm->vs_variants_list);
362
363 return llvm;
364 }
365
366
367 /**
368 * Free per-context LLVM info.
369 */
370 void
371 draw_llvm_destroy(struct draw_llvm *llvm)
372 {
373 /* XXX free other draw_llvm data? */
374 FREE(llvm);
375 }
376
377
378 /**
379 * Create LLVM-generated code for a vertex shader.
380 */
381 struct draw_llvm_variant *
382 draw_llvm_create_variant(struct draw_llvm *llvm,
383 unsigned num_inputs,
384 const struct draw_llvm_variant_key *key)
385 {
386 struct draw_llvm_variant *variant;
387 struct llvm_vertex_shader *shader =
388 llvm_vertex_shader(llvm->draw->vs.vertex_shader);
389 LLVMTypeRef vertex_header;
390
391 variant = MALLOC(sizeof *variant +
392 shader->variant_key_size -
393 sizeof variant->key);
394 if (variant == NULL)
395 return NULL;
396
397 variant->llvm = llvm;
398
399 variant->gallivm = gallivm_create();
400
401 create_jit_types(variant);
402
403 memcpy(&variant->key, key, shader->variant_key_size);
404
405 vertex_header = create_jit_vertex_header(variant->gallivm, num_inputs);
406
407 variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0);
408
409 draw_llvm_generate(llvm, variant, FALSE); /* linear */
410 draw_llvm_generate(llvm, variant, TRUE); /* elts */
411
412 gallivm_compile_module(variant->gallivm);
413
414 variant->jit_func = (draw_jit_vert_func)
415 gallivm_jit_function(variant->gallivm, variant->function);
416
417 variant->jit_func_elts = (draw_jit_vert_func_elts)
418 gallivm_jit_function(variant->gallivm, variant->function_elts);
419
420 variant->shader = shader;
421 variant->list_item_global.base = variant;
422 variant->list_item_local.base = variant;
423 /*variant->no = */shader->variants_created++;
424 variant->list_item_global.base = variant;
425
426 return variant;
427 }
428
429
430 static void
431 generate_vs(struct draw_llvm_variant *variant,
432 LLVMBuilderRef builder,
433 struct lp_type vs_type,
434 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
435 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS],
436 const struct lp_bld_tgsi_system_values *system_values,
437 LLVMValueRef context_ptr,
438 struct lp_build_sampler_soa *draw_sampler,
439 boolean clamp_vertex_color)
440 {
441 struct draw_llvm *llvm = variant->llvm;
442 const struct tgsi_token *tokens = llvm->draw->vs.vertex_shader->state.tokens;
443 LLVMValueRef consts_ptr = draw_jit_context_vs_constants(variant->gallivm, context_ptr);
444 struct lp_build_sampler_soa *sampler = 0;
445
446 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
447 tgsi_dump(tokens, 0);
448 draw_llvm_dump_variant_key(&variant->key);
449 }
450
451 if (llvm->draw->num_sampler_views && llvm->draw->num_samplers)
452 sampler = draw_sampler;
453
454 lp_build_tgsi_soa(variant->gallivm,
455 tokens,
456 vs_type,
457 NULL /*struct lp_build_mask_context *mask*/,
458 consts_ptr,
459 system_values,
460 NULL /*pos*/,
461 inputs,
462 outputs,
463 sampler,
464 &llvm->draw->vs.vertex_shader->info);
465
466 {
467 LLVMValueRef out;
468 unsigned chan, attrib;
469 struct lp_build_context bld;
470 struct tgsi_shader_info* info = &llvm->draw->vs.vertex_shader->info;
471 lp_build_context_init(&bld, variant->gallivm, vs_type);
472
473 for (attrib = 0; attrib < info->num_outputs; ++attrib) {
474 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
475 if (outputs[attrib][chan]) {
476 switch (info->output_semantic_name[attrib]) {
477 case TGSI_SEMANTIC_COLOR:
478 case TGSI_SEMANTIC_BCOLOR:
479 if (clamp_vertex_color) {
480 out = LLVMBuildLoad(builder, outputs[attrib][chan], "");
481 out = lp_build_clamp(&bld, out, bld.zero, bld.one);
482 LLVMBuildStore(builder, out, outputs[attrib][chan]);
483 }
484 break;
485 case TGSI_SEMANTIC_FOG:
486 if (chan == 1 || chan == 2)
487 LLVMBuildStore(builder, bld.zero, outputs[attrib][chan]);
488 else if (chan == 3)
489 LLVMBuildStore(builder, bld.one, outputs[attrib][chan]);
490 break;
491 }
492 }
493 }
494 }
495 }
496 }
497
498
499 static void
500 generate_fetch(struct gallivm_state *gallivm,
501 LLVMValueRef vbuffers_ptr,
502 LLVMValueRef *res,
503 struct pipe_vertex_element *velem,
504 LLVMValueRef vbuf,
505 LLVMValueRef index,
506 LLVMValueRef instance_id)
507 {
508 const struct util_format_description *format_desc = util_format_description(velem->src_format);
509 LLVMValueRef zero = LLVMConstNull(LLVMInt32TypeInContext(gallivm->context));
510 LLVMBuilderRef builder = gallivm->builder;
511 LLVMValueRef indices =
512 LLVMConstInt(LLVMInt64TypeInContext(gallivm->context),
513 velem->vertex_buffer_index, 0);
514 LLVMValueRef vbuffer_ptr = LLVMBuildGEP(builder, vbuffers_ptr,
515 &indices, 1, "");
516 LLVMValueRef vb_stride = draw_jit_vbuffer_stride(gallivm, vbuf);
517 LLVMValueRef vb_buffer_offset = draw_jit_vbuffer_offset(gallivm, vbuf);
518 LLVMValueRef stride;
519
520 if (velem->instance_divisor) {
521 /* array index = instance_id / instance_divisor */
522 index = LLVMBuildUDiv(builder, instance_id,
523 lp_build_const_int32(gallivm, velem->instance_divisor),
524 "instance_divisor");
525 }
526
527 stride = LLVMBuildMul(builder, vb_stride, index, "");
528
529 vbuffer_ptr = LLVMBuildLoad(builder, vbuffer_ptr, "vbuffer");
530
531 stride = LLVMBuildAdd(builder, stride,
532 vb_buffer_offset,
533 "");
534 stride = LLVMBuildAdd(builder, stride,
535 lp_build_const_int32(gallivm, velem->src_offset),
536 "");
537
538 /* lp_build_printf(gallivm, "vbuf index = %d, stride is %d\n", indices, stride);*/
539 vbuffer_ptr = LLVMBuildGEP(builder, vbuffer_ptr, &stride, 1, "");
540
541 *res = lp_build_fetch_rgba_aos(gallivm,
542 format_desc,
543 lp_float32_vec4_type(),
544 vbuffer_ptr,
545 zero, zero, zero);
546 }
547
548 static void
549 convert_to_soa(struct gallivm_state *gallivm,
550 LLVMValueRef (*src_aos)[LP_MAX_VECTOR_WIDTH / 32],
551 LLVMValueRef (*dst_soa)[TGSI_NUM_CHANNELS],
552 unsigned num_attribs, const struct lp_type soa_type)
553 {
554 unsigned i, j, k;
555 struct lp_type aos_channel_type = soa_type;
556
557 debug_assert(TGSI_NUM_CHANNELS == 4);
558 debug_assert((soa_type.length % TGSI_NUM_CHANNELS) == 0);
559
560 aos_channel_type.length >>= 1;
561
562 for (i = 0; i < num_attribs; ++i) {
563 LLVMValueRef aos_channels[TGSI_NUM_CHANNELS];
564 unsigned pixels_per_channel = soa_type.length / TGSI_NUM_CHANNELS;
565
566 for (j = 0; j < TGSI_NUM_CHANNELS; ++j) {
567 LLVMValueRef channel[LP_MAX_VECTOR_LENGTH] = { 0 };
568
569 assert(pixels_per_channel <= LP_MAX_VECTOR_LENGTH);
570
571 for (k = 0; k < pixels_per_channel; ++k) {
572 channel[k] = src_aos[i][j + TGSI_NUM_CHANNELS * k];
573 }
574
575 aos_channels[j] = lp_build_concat(gallivm, channel, aos_channel_type, pixels_per_channel);
576 }
577
578 lp_build_transpose_aos(gallivm, soa_type, aos_channels, dst_soa[i]);
579 }
580 }
581
582
583 static void
584 store_aos(struct gallivm_state *gallivm,
585 LLVMValueRef io_ptr,
586 LLVMValueRef index,
587 LLVMValueRef value)
588 {
589 LLVMTypeRef data_ptr_type = LLVMPointerType(lp_build_vec_type(gallivm, lp_float32_vec4_type()), 0);
590 LLVMBuilderRef builder = gallivm->builder;
591 LLVMValueRef data_ptr = draw_jit_header_data(gallivm, io_ptr);
592 LLVMValueRef indices[3];
593
594 indices[0] = lp_build_const_int32(gallivm, 0);
595 indices[1] = index;
596 indices[2] = lp_build_const_int32(gallivm, 0);
597
598 #if DEBUG_STORE
599 lp_build_printf(gallivm, " ---- %p storing attribute %d (io = %p)\n", data_ptr, index, io_ptr);
600 #endif
601
602 data_ptr = LLVMBuildGEP(builder, data_ptr, indices, 3, "");
603 data_ptr = LLVMBuildPointerCast(builder, data_ptr, data_ptr_type, "");
604
605 /* Unaligned store due to the vertex header */
606 lp_set_store_alignment(LLVMBuildStore(builder, value, data_ptr), sizeof(float));
607 }
608
609
610 static void
611 store_aos_array(struct gallivm_state *gallivm,
612 struct lp_type soa_type,
613 LLVMValueRef io_ptr,
614 LLVMValueRef* aos,
615 int attrib,
616 int num_outputs,
617 LLVMValueRef clipmask,
618 boolean have_clipdist)
619 {
620 LLVMBuilderRef builder = gallivm->builder;
621 LLVMValueRef attr_index = lp_build_const_int32(gallivm, attrib);
622 LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32];
623 LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32];
624 int vector_length = soa_type.length;
625 int i;
626
627 debug_assert(TGSI_NUM_CHANNELS == 4);
628
629 for (i = 0; i < vector_length; i++) {
630 inds[i] = lp_build_const_int32(gallivm, i);
631 io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, "");
632 }
633
634 if (attrib == 0) {
635 /* store vertex header for each of the n vertices */
636 LLVMValueRef val, cliptmp;
637 int vertex_id_pad_edgeflag;
638
639 /* If this assertion fails, it means we need to update the bit twidding
640 * code here. See struct vertex_header in draw_private.h.
641 */
642 assert(DRAW_TOTAL_CLIP_PLANES==14);
643 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
644 vertex_id_pad_edgeflag = (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES);
645 if (have_clipdist)
646 vertex_id_pad_edgeflag |= 1 << (DRAW_TOTAL_CLIP_PLANES+1);
647 val = lp_build_const_int_vec(gallivm, lp_int_type(soa_type), vertex_id_pad_edgeflag);
648 /* OR with the clipmask */
649 cliptmp = LLVMBuildOr(builder, val, clipmask, "");
650 for (i = 0; i < vector_length; i++) {
651 LLVMValueRef id_ptr = draw_jit_header_id(gallivm, io_ptrs[i]);
652 val = LLVMBuildExtractElement(builder, cliptmp, inds[i], "");
653 LLVMBuildStore(builder, val, id_ptr);
654 #if DEBUG_STORE
655 lp_build_printf(gallivm, "io = %p, index %d\n, clipmask = %x\n",
656 io_ptrs[i], inds[i], val);
657 #endif
658 }
659 }
660
661 /* store for each of the n vertices */
662 for (i = 0; i < vector_length; i++) {
663 store_aos(gallivm, io_ptrs[i], attr_index, aos[i]);
664 }
665 }
666
667
668 static void
669 convert_to_aos(struct gallivm_state *gallivm,
670 LLVMValueRef io,
671 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
672 LLVMValueRef clipmask,
673 int num_outputs,
674 struct lp_type soa_type,
675 boolean have_clipdist)
676 {
677 LLVMBuilderRef builder = gallivm->builder;
678 unsigned chan, attrib, i;
679
680 #if DEBUG_STORE
681 lp_build_printf(gallivm, " # storing begin\n");
682 #endif
683 for (attrib = 0; attrib < num_outputs; ++attrib) {
684 LLVMValueRef soa[TGSI_NUM_CHANNELS];
685 LLVMValueRef aos[LP_MAX_VECTOR_WIDTH / 32];
686 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
687 if (outputs[attrib][chan]) {
688 LLVMValueRef out = LLVMBuildLoad(builder, outputs[attrib][chan], "");
689 lp_build_name(out, "output%u.%c", attrib, "xyzw"[chan]);
690 #if DEBUG_STORE
691 lp_build_printf(gallivm, "output %d : %d ",
692 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context),
693 attrib, 0),
694 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context),
695 chan, 0));
696 lp_build_print_value(gallivm, "val = ", out);
697 #endif
698 soa[chan] = out;
699 }
700 else {
701 soa[chan] = 0;
702 }
703 }
704
705
706 if (soa_type.length == TGSI_NUM_CHANNELS) {
707 lp_build_transpose_aos(gallivm, soa_type, soa, aos);
708 } else {
709 lp_build_transpose_aos(gallivm, soa_type, soa, soa);
710
711 for (i = 0; i < soa_type.length; ++i) {
712 aos[i] = lp_build_extract_range(gallivm,
713 soa[i % TGSI_NUM_CHANNELS],
714 (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS,
715 TGSI_NUM_CHANNELS);
716 }
717 }
718
719 store_aos_array(gallivm,
720 soa_type,
721 io,
722 aos,
723 attrib,
724 num_outputs,
725 clipmask, have_clipdist);
726 }
727 #if DEBUG_STORE
728 lp_build_printf(gallivm, " # storing end\n");
729 #endif
730 }
731
732
733 /**
734 * Stores original vertex positions in clip coordinates
735 */
736 static void
737 store_clip(struct gallivm_state *gallivm,
738 const struct lp_type vs_type,
739 LLVMValueRef io_ptr,
740 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
741 boolean pre_clip_pos, int idx)
742 {
743 LLVMBuilderRef builder = gallivm->builder;
744 LLVMValueRef soa[4];
745 LLVMValueRef aos[LP_MAX_VECTOR_LENGTH];
746 LLVMValueRef indices[2];
747 LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32];
748 LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32];
749 LLVMValueRef clip_ptrs[LP_MAX_VECTOR_WIDTH / 32];
750 int i, j;
751
752 indices[0] =
753 indices[1] = lp_build_const_int32(gallivm, 0);
754
755 for (i = 0; i < vs_type.length; i++) {
756 inds[i] = lp_build_const_int32(gallivm, i);
757 io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, "");
758 }
759
760 soa[0] = LLVMBuildLoad(builder, outputs[idx][0], ""); /*x0 x1 .. xn*/
761 soa[1] = LLVMBuildLoad(builder, outputs[idx][1], ""); /*y0 y1 .. yn*/
762 soa[2] = LLVMBuildLoad(builder, outputs[idx][2], ""); /*z0 z1 .. zn*/
763 soa[3] = LLVMBuildLoad(builder, outputs[idx][3], ""); /*w0 w1 .. wn*/
764
765 if (!pre_clip_pos) {
766 for (i = 0; i < vs_type.length; i++) {
767 clip_ptrs[i] = draw_jit_header_clip(gallivm, io_ptrs[i]);
768 }
769 } else {
770 for (i = 0; i < vs_type.length; i++) {
771 clip_ptrs[i] = draw_jit_header_pre_clip_pos(gallivm, io_ptrs[i]);
772 }
773 }
774
775 lp_build_transpose_aos(gallivm, vs_type, soa, soa);
776 for (i = 0; i < vs_type.length; ++i) {
777 aos[i] = lp_build_extract_range(gallivm,
778 soa[i % TGSI_NUM_CHANNELS],
779 (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS,
780 TGSI_NUM_CHANNELS);
781 }
782
783 for (j = 0; j < vs_type.length; j++) {
784 LLVMTypeRef clip_ptr_type = LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4), 0);
785 LLVMValueRef clip_ptr;
786
787 clip_ptr = LLVMBuildGEP(builder, clip_ptrs[j], indices, 2, "clipo");
788 clip_ptr = LLVMBuildPointerCast(builder, clip_ptr, clip_ptr_type, "");
789
790 /* Unaligned store */
791 lp_set_store_alignment(LLVMBuildStore(builder, aos[j], clip_ptr), sizeof(float));
792 }
793 }
794
795
796 /**
797 * Transforms the outputs for viewport mapping
798 */
799 static void
800 generate_viewport(struct draw_llvm_variant *variant,
801 LLVMBuilderRef builder,
802 struct lp_type vs_type,
803 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
804 LLVMValueRef context_ptr)
805 {
806 int i;
807 struct gallivm_state *gallivm = variant->gallivm;
808 struct lp_type f32_type = vs_type;
809 LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type);
810 LLVMValueRef out3 = LLVMBuildLoad(builder, outputs[0][3], ""); /*w0 w1 .. wn*/
811 LLVMValueRef const1 = lp_build_const_vec(gallivm, f32_type, 1.0); /*1.0 1.0 1.0 1.0*/
812 LLVMValueRef vp_ptr = draw_jit_context_viewport(gallivm, context_ptr);
813
814 /* for 1/w convention*/
815 out3 = LLVMBuildFDiv(builder, const1, out3, "");
816 LLVMBuildStore(builder, out3, outputs[0][3]);
817
818 /* Viewport Mapping */
819 for (i=0; i<3; i++) {
820 LLVMValueRef out = LLVMBuildLoad(builder, outputs[0][i], ""); /*x0 x1 .. xn*/
821 LLVMValueRef scale;
822 LLVMValueRef trans;
823 LLVMValueRef scale_i;
824 LLVMValueRef trans_i;
825 LLVMValueRef index;
826
827 index = lp_build_const_int32(gallivm, i);
828 scale_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, "");
829
830 index = lp_build_const_int32(gallivm, i+4);
831 trans_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, "");
832
833 scale = lp_build_broadcast(gallivm, vs_type_llvm,
834 LLVMBuildLoad(builder, scale_i, "scale"));
835 trans = lp_build_broadcast(gallivm, vs_type_llvm,
836 LLVMBuildLoad(builder, trans_i, "trans"));
837
838 /* divide by w */
839 out = LLVMBuildFMul(builder, out, out3, "");
840 /* mult by scale */
841 out = LLVMBuildFMul(builder, out, scale, "");
842 /* add translation */
843 out = LLVMBuildFAdd(builder, out, trans, "");
844
845 /* store transformed outputs */
846 LLVMBuildStore(builder, out, outputs[0][i]);
847 }
848
849 }
850
851
852 /**
853 * Returns clipmask as nxi32 bitmask for the n vertices
854 */
855 static LLVMValueRef
856 generate_clipmask(struct draw_llvm *llvm,
857 struct gallivm_state *gallivm,
858 struct lp_type vs_type,
859 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
860 boolean clip_xy,
861 boolean clip_z,
862 boolean clip_user,
863 boolean clip_halfz,
864 unsigned ucp_enable,
865 LLVMValueRef context_ptr,
866 boolean *have_clipdist)
867 {
868 LLVMBuilderRef builder = gallivm->builder;
869 LLVMValueRef mask; /* stores the <nxi32> clipmasks */
870 LLVMValueRef test, temp;
871 LLVMValueRef zero, shift;
872 LLVMValueRef pos_x, pos_y, pos_z, pos_w;
873 LLVMValueRef cv_x, cv_y, cv_z, cv_w;
874 LLVMValueRef plane1, planes, plane_ptr, sum;
875 struct lp_type f32_type = vs_type;
876 struct lp_type i32_type = lp_int_type(vs_type);
877 const unsigned pos = draw_current_shader_position_output(llvm->draw);
878 const unsigned cv = draw_current_shader_clipvertex_output(llvm->draw);
879 int num_written_clipdistance = llvm->draw->vs.vertex_shader->info.num_written_clipdistance;
880 bool have_cd = false;
881 unsigned cd[2];
882
883 cd[0] = draw_current_shader_clipdistance_output(llvm->draw, 0);
884 cd[1] = draw_current_shader_clipdistance_output(llvm->draw, 1);
885
886 if (cd[0] != pos || cd[1] != pos)
887 have_cd = true;
888
889 mask = lp_build_const_int_vec(gallivm, i32_type, 0);
890 temp = lp_build_const_int_vec(gallivm, i32_type, 0);
891 zero = lp_build_const_vec(gallivm, f32_type, 0); /* 0.0f 0.0f 0.0f 0.0f */
892 shift = lp_build_const_int_vec(gallivm, i32_type, 1); /* 1 1 1 1 */
893
894 /*
895 * load clipvertex and position from correct locations.
896 * if they are the same just load them once.
897 */
898 pos_x = LLVMBuildLoad(builder, outputs[pos][0], ""); /*x0 x1 .. xn */
899 pos_y = LLVMBuildLoad(builder, outputs[pos][1], ""); /*y0 y1 .. yn */
900 pos_z = LLVMBuildLoad(builder, outputs[pos][2], ""); /*z0 z1 .. zn */
901 pos_w = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 .. wn */
902
903 if (clip_user && cv != pos) {
904 cv_x = LLVMBuildLoad(builder, outputs[cv][0], ""); /*x0 x1 .. xn */
905 cv_y = LLVMBuildLoad(builder, outputs[cv][1], ""); /*y0 y1 .. yn */
906 cv_z = LLVMBuildLoad(builder, outputs[cv][2], ""); /*z0 z1 .. zn */
907 cv_w = LLVMBuildLoad(builder, outputs[cv][3], ""); /*w0 w1 .. wn */
908 } else {
909 cv_x = pos_x;
910 cv_y = pos_y;
911 cv_z = pos_z;
912 cv_w = pos_w;
913 }
914
915 /* Cliptest, for hardwired planes */
916 if (clip_xy) {
917 /* plane 1 */
918 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_x , pos_w);
919 temp = shift;
920 test = LLVMBuildAnd(builder, test, temp, "");
921 mask = test;
922
923 /* plane 2 */
924 test = LLVMBuildFAdd(builder, pos_x, pos_w, "");
925 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
926 temp = LLVMBuildShl(builder, temp, shift, "");
927 test = LLVMBuildAnd(builder, test, temp, "");
928 mask = LLVMBuildOr(builder, mask, test, "");
929
930 /* plane 3 */
931 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_y, pos_w);
932 temp = LLVMBuildShl(builder, temp, shift, "");
933 test = LLVMBuildAnd(builder, test, temp, "");
934 mask = LLVMBuildOr(builder, mask, test, "");
935
936 /* plane 4 */
937 test = LLVMBuildFAdd(builder, pos_y, pos_w, "");
938 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
939 temp = LLVMBuildShl(builder, temp, shift, "");
940 test = LLVMBuildAnd(builder, test, temp, "");
941 mask = LLVMBuildOr(builder, mask, test, "");
942 }
943
944 if (clip_z) {
945 temp = lp_build_const_int_vec(gallivm, i32_type, 16);
946 if (clip_halfz) {
947 /* plane 5 */
948 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, pos_z);
949 test = LLVMBuildAnd(builder, test, temp, "");
950 mask = LLVMBuildOr(builder, mask, test, "");
951 }
952 else {
953 /* plane 5 */
954 test = LLVMBuildFAdd(builder, pos_z, pos_w, "");
955 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
956 test = LLVMBuildAnd(builder, test, temp, "");
957 mask = LLVMBuildOr(builder, mask, test, "");
958 }
959 /* plane 6 */
960 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_z, pos_w);
961 temp = LLVMBuildShl(builder, temp, shift, "");
962 test = LLVMBuildAnd(builder, test, temp, "");
963 mask = LLVMBuildOr(builder, mask, test, "");
964 }
965
966 if (clip_user) {
967 LLVMValueRef planes_ptr = draw_jit_context_planes(gallivm, context_ptr);
968 LLVMValueRef indices[3];
969
970 /* userclip planes */
971 while (ucp_enable) {
972 unsigned plane_idx = ffs(ucp_enable)-1;
973 ucp_enable &= ~(1 << plane_idx);
974 plane_idx += 6;
975
976 if (have_cd && num_written_clipdistance) {
977 LLVMValueRef clipdist;
978 int i;
979 i = plane_idx - 6;
980
981 *have_clipdist = TRUE;
982 if (i < 4) {
983 clipdist = LLVMBuildLoad(builder, outputs[cd[0]][i], "");
984 } else {
985 clipdist = LLVMBuildLoad(builder, outputs[cd[1]][i-4], "");
986 }
987 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, clipdist);
988 temp = lp_build_const_int_vec(gallivm, i32_type, 1 << plane_idx);
989 test = LLVMBuildAnd(builder, test, temp, "");
990 mask = LLVMBuildOr(builder, mask, test, "");
991 } else {
992 LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type);
993 indices[0] = lp_build_const_int32(gallivm, 0);
994 indices[1] = lp_build_const_int32(gallivm, plane_idx);
995
996 indices[2] = lp_build_const_int32(gallivm, 0);
997 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
998 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_x");
999 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1000 sum = LLVMBuildFMul(builder, planes, cv_x, "");
1001
1002 indices[2] = lp_build_const_int32(gallivm, 1);
1003 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1004 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_y");
1005 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1006 test = LLVMBuildFMul(builder, planes, cv_y, "");
1007 sum = LLVMBuildFAdd(builder, sum, test, "");
1008
1009 indices[2] = lp_build_const_int32(gallivm, 2);
1010 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1011 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_z");
1012 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1013 test = LLVMBuildFMul(builder, planes, cv_z, "");
1014 sum = LLVMBuildFAdd(builder, sum, test, "");
1015
1016 indices[2] = lp_build_const_int32(gallivm, 3);
1017 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1018 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_w");
1019 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1020 test = LLVMBuildFMul(builder, planes, cv_w, "");
1021 sum = LLVMBuildFAdd(builder, sum, test, "");
1022
1023 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, sum);
1024 temp = lp_build_const_int_vec(gallivm, i32_type, 1 << plane_idx);
1025 test = LLVMBuildAnd(builder, test, temp, "");
1026 mask = LLVMBuildOr(builder, mask, test, "");
1027 }
1028 }
1029 }
1030 return mask;
1031 }
1032
1033
1034 /**
1035 * Returns boolean if any clipping has occurred
1036 * Used zero/non-zero i32 value to represent boolean
1037 */
1038 static LLVMValueRef
1039 clipmask_booli32(struct gallivm_state *gallivm,
1040 const struct lp_type vs_type,
1041 LLVMValueRef clipmask_bool_ptr)
1042 {
1043 LLVMBuilderRef builder = gallivm->builder;
1044 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
1045 LLVMValueRef clipmask_bool = LLVMBuildLoad(builder, clipmask_bool_ptr, "");
1046 LLVMValueRef ret = LLVMConstNull(int32_type);
1047 LLVMValueRef temp;
1048 int i;
1049
1050 /*
1051 * Can do this with log2(vector length) pack instructions and one extract
1052 * (as we don't actually need a or) with sse2 which would be way better.
1053 */
1054 for (i=0; i < vs_type.length; i++) {
1055 temp = LLVMBuildExtractElement(builder, clipmask_bool,
1056 lp_build_const_int32(gallivm, i) , "");
1057 ret = LLVMBuildOr(builder, ret, temp, "");
1058 }
1059 return ret;
1060 }
1061
1062
1063 static void
1064 draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *variant,
1065 boolean elts)
1066 {
1067 struct gallivm_state *gallivm = variant->gallivm;
1068 LLVMContextRef context = gallivm->context;
1069 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context);
1070 LLVMTypeRef arg_types[8];
1071 LLVMTypeRef func_type;
1072 LLVMValueRef context_ptr;
1073 LLVMBasicBlockRef block;
1074 LLVMBuilderRef builder;
1075 struct lp_type vs_type;
1076 LLVMValueRef end, start;
1077 LLVMValueRef count, fetch_elts, fetch_count;
1078 LLVMValueRef stride, step, io_itr;
1079 LLVMValueRef io_ptr, vbuffers_ptr, vb_ptr;
1080 LLVMValueRef zero = lp_build_const_int32(gallivm, 0);
1081 LLVMValueRef one = lp_build_const_int32(gallivm, 1);
1082 struct draw_context *draw = llvm->draw;
1083 const struct tgsi_shader_info *vs_info = &draw->vs.vertex_shader->info;
1084 unsigned i, j;
1085 struct lp_build_context bld;
1086 struct lp_build_loop_state lp_loop;
1087 const int vector_length = lp_native_vector_width / 32;
1088 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
1089 LLVMValueRef fetch_max;
1090 struct lp_build_sampler_soa *sampler = 0;
1091 LLVMValueRef ret, clipmask_bool_ptr;
1092 const boolean bypass_viewport = variant->key.bypass_viewport;
1093 const boolean enable_cliptest = variant->key.clip_xy ||
1094 variant->key.clip_z ||
1095 variant->key.clip_user;
1096 LLVMValueRef variant_func;
1097 const unsigned pos = draw_current_shader_position_output(llvm->draw);
1098 const unsigned cv = draw_current_shader_clipvertex_output(llvm->draw);
1099 boolean have_clipdist = FALSE;
1100 struct lp_bld_tgsi_system_values system_values;
1101
1102 memset(&system_values, 0, sizeof(system_values));
1103
1104 arg_types[0] = get_context_ptr_type(variant); /* context */
1105 arg_types[1] = get_vertex_header_ptr_type(variant); /* vertex_header */
1106 arg_types[2] = get_buffer_ptr_type(variant); /* vbuffers */
1107 if (elts)
1108 arg_types[3] = LLVMPointerType(int32_type, 0);/* fetch_elts * */
1109 else
1110 arg_types[3] = int32_type; /* start */
1111 arg_types[4] = int32_type; /* fetch_count / count */
1112 arg_types[5] = int32_type; /* stride */
1113 arg_types[6] = get_vb_ptr_type(variant); /* pipe_vertex_buffer's */
1114 arg_types[7] = int32_type; /* instance_id */
1115
1116 func_type = LLVMFunctionType(int32_type, arg_types, Elements(arg_types), 0);
1117
1118 variant_func = LLVMAddFunction(gallivm->module,
1119 elts ? "draw_llvm_shader_elts" : "draw_llvm_shader",
1120 func_type);
1121
1122 if (elts)
1123 variant->function_elts = variant_func;
1124 else
1125 variant->function = variant_func;
1126
1127 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv);
1128 for (i = 0; i < Elements(arg_types); ++i)
1129 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
1130 LLVMAddAttribute(LLVMGetParam(variant_func, i),
1131 LLVMNoAliasAttribute);
1132
1133 context_ptr = LLVMGetParam(variant_func, 0);
1134 io_ptr = LLVMGetParam(variant_func, 1);
1135 vbuffers_ptr = LLVMGetParam(variant_func, 2);
1136 stride = LLVMGetParam(variant_func, 5);
1137 vb_ptr = LLVMGetParam(variant_func, 6);
1138 system_values.instance_id = LLVMGetParam(variant_func, 7);
1139
1140 lp_build_name(context_ptr, "context");
1141 lp_build_name(io_ptr, "io");
1142 lp_build_name(vbuffers_ptr, "vbuffers");
1143 lp_build_name(stride, "stride");
1144 lp_build_name(vb_ptr, "vb");
1145 lp_build_name(system_values.instance_id, "instance_id");
1146
1147 if (elts) {
1148 fetch_elts = LLVMGetParam(variant_func, 3);
1149 fetch_count = LLVMGetParam(variant_func, 4);
1150 lp_build_name(fetch_elts, "fetch_elts");
1151 lp_build_name(fetch_count, "fetch_count");
1152 start = count = NULL;
1153 }
1154 else {
1155 start = LLVMGetParam(variant_func, 3);
1156 count = LLVMGetParam(variant_func, 4);
1157 lp_build_name(start, "start");
1158 lp_build_name(count, "count");
1159 fetch_elts = fetch_count = NULL;
1160 }
1161
1162 /*
1163 * Function body
1164 */
1165
1166 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry");
1167 builder = gallivm->builder;
1168 LLVMPositionBuilderAtEnd(builder, block);
1169
1170 lp_build_context_init(&bld, gallivm, lp_type_int(32));
1171
1172 memset(&vs_type, 0, sizeof vs_type);
1173 vs_type.floating = TRUE; /* floating point values */
1174 vs_type.sign = TRUE; /* values are signed */
1175 vs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */
1176 vs_type.width = 32; /* 32-bit float */
1177 vs_type.length = vector_length;
1178
1179 /* hold temporary "bool" clipmask */
1180 clipmask_bool_ptr = lp_build_alloca(gallivm, lp_build_int_vec_type(gallivm, vs_type), "");
1181 LLVMBuildStore(builder, lp_build_zero(gallivm, lp_int_type(vs_type)), clipmask_bool_ptr);
1182
1183 /* code generated texture sampling */
1184 sampler = draw_llvm_sampler_soa_create(
1185 draw_llvm_variant_key_samplers(&variant->key),
1186 context_ptr);
1187
1188 if (elts) {
1189 start = zero;
1190 end = fetch_count;
1191 }
1192 else {
1193 end = lp_build_add(&bld, start, count);
1194 }
1195
1196 step = lp_build_const_int32(gallivm, vector_length);
1197
1198 fetch_max = LLVMBuildSub(builder, end, one, "fetch_max");
1199
1200 lp_build_loop_begin(&lp_loop, gallivm, start);
1201 {
1202 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
1203 LLVMValueRef aos_attribs[PIPE_MAX_SHADER_INPUTS][LP_MAX_VECTOR_WIDTH / 32] = { { 0 } };
1204 LLVMValueRef io;
1205 LLVMValueRef clipmask; /* holds the clipmask value */
1206 const LLVMValueRef (*ptr_aos)[TGSI_NUM_CHANNELS];
1207
1208 if (elts)
1209 io_itr = lp_loop.counter;
1210 else
1211 io_itr = LLVMBuildSub(builder, lp_loop.counter, start, "");
1212
1213 io = LLVMBuildGEP(builder, io_ptr, &io_itr, 1, "");
1214 #if DEBUG_STORE
1215 lp_build_printf(gallivm, " --- io %d = %p, loop counter %d\n",
1216 io_itr, io, lp_loop.counter);
1217 #endif
1218 system_values.vertex_id = lp_build_zero(gallivm, lp_type_uint_vec(32, 32*vector_length));
1219 for (i = 0; i < vector_length; ++i) {
1220 LLVMValueRef true_index =
1221 LLVMBuildAdd(builder,
1222 lp_loop.counter,
1223 lp_build_const_int32(gallivm, i), "");
1224
1225 /* make sure we're not out of bounds which can happen
1226 * if fetch_count % 4 != 0, because on the last iteration
1227 * a few of the 4 vertex fetches will be out of bounds */
1228 true_index = lp_build_min(&bld, true_index, fetch_max);
1229
1230 if (elts) {
1231 LLVMValueRef fetch_ptr;
1232 fetch_ptr = LLVMBuildGEP(builder, fetch_elts,
1233 &true_index, 1, "");
1234 true_index = LLVMBuildLoad(builder, fetch_ptr, "fetch_elt");
1235 }
1236
1237 system_values.vertex_id = LLVMBuildInsertElement(gallivm->builder,
1238 system_values.vertex_id, true_index,
1239 lp_build_const_int32(gallivm, i), "");
1240 for (j = 0; j < draw->pt.nr_vertex_elements; ++j) {
1241 struct pipe_vertex_element *velem = &draw->pt.vertex_element[j];
1242 LLVMValueRef vb_index =
1243 lp_build_const_int32(gallivm, velem->vertex_buffer_index);
1244 LLVMValueRef vb = LLVMBuildGEP(builder, vb_ptr, &vb_index, 1, "");
1245 generate_fetch(gallivm, vbuffers_ptr,
1246 &aos_attribs[j][i], velem, vb, true_index,
1247 system_values.instance_id);
1248 }
1249 }
1250 convert_to_soa(gallivm, aos_attribs, inputs,
1251 draw->pt.nr_vertex_elements, vs_type);
1252
1253 ptr_aos = (const LLVMValueRef (*)[TGSI_NUM_CHANNELS]) inputs;
1254 generate_vs(variant,
1255 builder,
1256 vs_type,
1257 outputs,
1258 ptr_aos,
1259 &system_values,
1260 context_ptr,
1261 sampler,
1262 variant->key.clamp_vertex_color);
1263
1264 /* store original positions in clip before further manipulation */
1265 store_clip(gallivm, vs_type, io, outputs, 0, cv);
1266 store_clip(gallivm, vs_type, io, outputs, 1, pos);
1267
1268 /* do cliptest */
1269 if (enable_cliptest) {
1270 LLVMValueRef temp = LLVMBuildLoad(builder, clipmask_bool_ptr, "");
1271 /* allocate clipmask, assign it integer type */
1272 clipmask = generate_clipmask(llvm,
1273 gallivm,
1274 vs_type,
1275 outputs,
1276 variant->key.clip_xy,
1277 variant->key.clip_z,
1278 variant->key.clip_user,
1279 variant->key.clip_halfz,
1280 variant->key.ucp_enable,
1281 context_ptr, &have_clipdist);
1282 temp = LLVMBuildOr(builder, clipmask, temp, "");
1283 /* store temporary clipping boolean value */
1284 LLVMBuildStore(builder, temp, clipmask_bool_ptr);
1285 }
1286 else {
1287 clipmask = lp_build_const_int_vec(gallivm, lp_int_type(vs_type), 0);
1288 }
1289
1290 /* do viewport mapping */
1291 if (!bypass_viewport) {
1292 generate_viewport(variant, builder, vs_type, outputs, context_ptr);
1293 }
1294
1295 /* store clipmask in vertex header,
1296 * original positions in clip
1297 * and transformed positions in data
1298 */
1299 convert_to_aos(gallivm, io, outputs, clipmask,
1300 vs_info->num_outputs, vs_type,
1301 have_clipdist);
1302 }
1303
1304 lp_build_loop_end_cond(&lp_loop, end, step, LLVMIntUGE);
1305
1306 sampler->destroy(sampler);
1307
1308 /* return clipping boolean value for function */
1309 ret = clipmask_booli32(gallivm, vs_type, clipmask_bool_ptr);
1310
1311 LLVMBuildRet(builder, ret);
1312
1313 gallivm_verify_function(gallivm, variant_func);
1314 }
1315
1316
1317 struct draw_llvm_variant_key *
1318 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store)
1319 {
1320 unsigned i;
1321 struct draw_llvm_variant_key *key;
1322 struct lp_sampler_static_state *sampler;
1323
1324 key = (struct draw_llvm_variant_key *)store;
1325
1326 key->clamp_vertex_color = llvm->draw->rasterizer->clamp_vertex_color; /**/
1327
1328 /* Presumably all variants of the shader should have the same
1329 * number of vertex elements - ie the number of shader inputs.
1330 */
1331 key->nr_vertex_elements = llvm->draw->pt.nr_vertex_elements;
1332
1333 /* will have to rig this up properly later */
1334 key->clip_xy = llvm->draw->clip_xy;
1335 key->clip_z = llvm->draw->clip_z;
1336 key->clip_user = llvm->draw->clip_user;
1337 key->bypass_viewport = llvm->draw->identity_viewport;
1338 key->clip_halfz = !llvm->draw->rasterizer->gl_rasterization_rules;
1339 key->need_edgeflags = (llvm->draw->vs.edgeflag_output ? TRUE : FALSE);
1340 key->ucp_enable = llvm->draw->rasterizer->clip_plane_enable;
1341 key->pad = 0;
1342
1343 /* All variants of this shader will have the same value for
1344 * nr_samplers. Not yet trying to compact away holes in the
1345 * sampler array.
1346 */
1347 key->nr_samplers = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER] + 1;
1348
1349 sampler = draw_llvm_variant_key_samplers(key);
1350
1351 memcpy(key->vertex_element,
1352 llvm->draw->pt.vertex_element,
1353 sizeof(struct pipe_vertex_element) * key->nr_vertex_elements);
1354
1355 memset(sampler, 0, key->nr_samplers * sizeof *sampler);
1356
1357 for (i = 0 ; i < key->nr_samplers; i++) {
1358 lp_sampler_static_state(&sampler[i],
1359 llvm->draw->sampler_views[PIPE_SHADER_VERTEX][i],
1360 llvm->draw->samplers[PIPE_SHADER_VERTEX][i]);
1361 }
1362
1363 return key;
1364 }
1365
1366
1367 void
1368 draw_llvm_dump_variant_key(struct draw_llvm_variant_key *key)
1369 {
1370 unsigned i;
1371 struct lp_sampler_static_state *sampler = draw_llvm_variant_key_samplers(key);
1372
1373 debug_printf("clamp_vertex_color = %u\n", key->clamp_vertex_color);
1374 debug_printf("clip_xy = %u\n", key->clip_xy);
1375 debug_printf("clip_z = %u\n", key->clip_z);
1376 debug_printf("clip_user = %u\n", key->clip_user);
1377 debug_printf("bypass_viewport = %u\n", key->bypass_viewport);
1378 debug_printf("clip_halfz = %u\n", key->clip_halfz);
1379 debug_printf("need_edgeflags = %u\n", key->need_edgeflags);
1380 debug_printf("ucp_enable = %u\n", key->ucp_enable);
1381
1382 for (i = 0 ; i < key->nr_vertex_elements; i++) {
1383 debug_printf("vertex_element[%i].src_offset = %u\n", i, key->vertex_element[i].src_offset);
1384 debug_printf("vertex_element[%i].instance_divisor = %u\n", i, key->vertex_element[i].instance_divisor);
1385 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i, key->vertex_element[i].vertex_buffer_index);
1386 debug_printf("vertex_element[%i].src_format = %s\n", i, util_format_name(key->vertex_element[i].src_format));
1387 }
1388
1389 for (i = 0 ; i < key->nr_samplers; i++) {
1390 debug_printf("sampler[%i].src_format = %s\n", i, util_format_name(sampler[i].format));
1391 }
1392 }
1393
1394
1395 void
1396 draw_llvm_set_mapped_texture(struct draw_context *draw,
1397 unsigned sampler_idx,
1398 uint32_t width, uint32_t height, uint32_t depth,
1399 uint32_t first_level, uint32_t last_level,
1400 const void *base_ptr,
1401 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
1402 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
1403 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])
1404 {
1405 unsigned j;
1406 struct draw_jit_texture *jit_tex;
1407
1408 assert(sampler_idx < Elements(draw->llvm->jit_context.textures));
1409
1410 jit_tex = &draw->llvm->jit_context.textures[sampler_idx];
1411
1412 jit_tex->width = width;
1413 jit_tex->height = height;
1414 jit_tex->depth = depth;
1415 jit_tex->first_level = first_level;
1416 jit_tex->last_level = last_level;
1417 jit_tex->base = base_ptr;
1418
1419 for (j = first_level; j <= last_level; j++) {
1420 jit_tex->mip_offsets[j] = mip_offsets[j];
1421 jit_tex->row_stride[j] = row_stride[j];
1422 jit_tex->img_stride[j] = img_stride[j];
1423 }
1424 }
1425
1426
1427 void
1428 draw_llvm_set_sampler_state(struct draw_context *draw)
1429 {
1430 unsigned i;
1431
1432 for (i = 0; i < draw->num_samplers[PIPE_SHADER_VERTEX]; i++) {
1433 struct draw_jit_texture *jit_tex = &draw->llvm->jit_context.textures[i];
1434
1435 if (draw->samplers[i]) {
1436 const struct pipe_sampler_state *s
1437 = draw->samplers[PIPE_SHADER_VERTEX][i];
1438 jit_tex->min_lod = s->min_lod;
1439 jit_tex->max_lod = s->max_lod;
1440 jit_tex->lod_bias = s->lod_bias;
1441 COPY_4V(jit_tex->border_color, s->border_color.f);
1442 }
1443 }
1444 }
1445
1446
1447 void
1448 draw_llvm_destroy_variant(struct draw_llvm_variant *variant)
1449 {
1450 struct draw_llvm *llvm = variant->llvm;
1451
1452 if (variant->function_elts) {
1453 gallivm_free_function(variant->gallivm,
1454 variant->function_elts, variant->jit_func_elts);
1455 }
1456
1457 if (variant->function) {
1458 gallivm_free_function(variant->gallivm,
1459 variant->function, variant->jit_func);
1460 }
1461
1462 gallivm_destroy(variant->gallivm);
1463
1464 remove_from_list(&variant->list_item_local);
1465 variant->shader->variants_cached--;
1466 remove_from_list(&variant->list_item_global);
1467 llvm->nr_variants--;
1468 FREE(variant);
1469 }