gallivm,llvmpipe,draw: Support multiple constant buffers.
[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_IR) {
447 tgsi_dump(tokens, 0);
448 }
449
450 if (llvm->draw->num_sampler_views && llvm->draw->num_samplers)
451 sampler = draw_sampler;
452
453 lp_build_tgsi_soa(variant->gallivm,
454 tokens,
455 vs_type,
456 NULL /*struct lp_build_mask_context *mask*/,
457 consts_ptr,
458 system_values,
459 NULL /*pos*/,
460 inputs,
461 outputs,
462 sampler,
463 &llvm->draw->vs.vertex_shader->info);
464
465 {
466 LLVMValueRef out;
467 unsigned chan, attrib;
468 struct lp_build_context bld;
469 struct tgsi_shader_info* info = &llvm->draw->vs.vertex_shader->info;
470 lp_build_context_init(&bld, variant->gallivm, vs_type);
471
472 for (attrib = 0; attrib < info->num_outputs; ++attrib) {
473 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
474 if (outputs[attrib][chan]) {
475 switch (info->output_semantic_name[attrib]) {
476 case TGSI_SEMANTIC_COLOR:
477 case TGSI_SEMANTIC_BCOLOR:
478 if (clamp_vertex_color) {
479 out = LLVMBuildLoad(builder, outputs[attrib][chan], "");
480 out = lp_build_clamp(&bld, out, bld.zero, bld.one);
481 LLVMBuildStore(builder, out, outputs[attrib][chan]);
482 }
483 break;
484 case TGSI_SEMANTIC_FOG:
485 if (chan == 1 || chan == 2)
486 LLVMBuildStore(builder, bld.zero, outputs[attrib][chan]);
487 else if (chan == 3)
488 LLVMBuildStore(builder, bld.one, outputs[attrib][chan]);
489 break;
490 }
491 }
492 }
493 }
494 }
495 }
496
497
498 static void
499 generate_fetch(struct gallivm_state *gallivm,
500 LLVMValueRef vbuffers_ptr,
501 LLVMValueRef *res,
502 struct pipe_vertex_element *velem,
503 LLVMValueRef vbuf,
504 LLVMValueRef index,
505 LLVMValueRef instance_id)
506 {
507 const struct util_format_description *format_desc = util_format_description(velem->src_format);
508 LLVMValueRef zero = LLVMConstNull(LLVMInt32TypeInContext(gallivm->context));
509 LLVMBuilderRef builder = gallivm->builder;
510 LLVMValueRef indices =
511 LLVMConstInt(LLVMInt64TypeInContext(gallivm->context),
512 velem->vertex_buffer_index, 0);
513 LLVMValueRef vbuffer_ptr = LLVMBuildGEP(builder, vbuffers_ptr,
514 &indices, 1, "");
515 LLVMValueRef vb_stride = draw_jit_vbuffer_stride(gallivm, vbuf);
516 LLVMValueRef vb_buffer_offset = draw_jit_vbuffer_offset(gallivm, vbuf);
517 LLVMValueRef stride;
518
519 if (velem->instance_divisor) {
520 /* array index = instance_id / instance_divisor */
521 index = LLVMBuildUDiv(builder, instance_id,
522 lp_build_const_int32(gallivm, velem->instance_divisor),
523 "instance_divisor");
524 }
525
526 stride = LLVMBuildMul(builder, vb_stride, index, "");
527
528 vbuffer_ptr = LLVMBuildLoad(builder, vbuffer_ptr, "vbuffer");
529
530 stride = LLVMBuildAdd(builder, stride,
531 vb_buffer_offset,
532 "");
533 stride = LLVMBuildAdd(builder, stride,
534 lp_build_const_int32(gallivm, velem->src_offset),
535 "");
536
537 /* lp_build_printf(gallivm, "vbuf index = %d, stride is %d\n", indices, stride);*/
538 vbuffer_ptr = LLVMBuildGEP(builder, vbuffer_ptr, &stride, 1, "");
539
540 *res = lp_build_fetch_rgba_aos(gallivm,
541 format_desc,
542 lp_float32_vec4_type(),
543 vbuffer_ptr,
544 zero, zero, zero);
545 }
546
547 static void
548 convert_to_soa(struct gallivm_state *gallivm,
549 LLVMValueRef (*src_aos)[LP_MAX_VECTOR_WIDTH / 32],
550 LLVMValueRef (*dst_soa)[TGSI_NUM_CHANNELS],
551 unsigned num_attribs, const struct lp_type soa_type)
552 {
553 unsigned i, j, k;
554 struct lp_type aos_channel_type = soa_type;
555
556 debug_assert(TGSI_NUM_CHANNELS == 4);
557 debug_assert((soa_type.length % TGSI_NUM_CHANNELS) == 0);
558
559 aos_channel_type.length >>= 1;
560
561 for (i = 0; i < num_attribs; ++i) {
562 LLVMValueRef aos_channels[TGSI_NUM_CHANNELS];
563 unsigned pixels_per_channel = soa_type.length / TGSI_NUM_CHANNELS;
564
565 for (j = 0; j < TGSI_NUM_CHANNELS; ++j) {
566 LLVMValueRef channel[LP_MAX_VECTOR_LENGTH] = { 0 };
567
568 assert(pixels_per_channel <= LP_MAX_VECTOR_LENGTH);
569
570 for (k = 0; k < pixels_per_channel; ++k) {
571 channel[k] = src_aos[i][j + TGSI_NUM_CHANNELS * k];
572 }
573
574 aos_channels[j] = lp_build_concat(gallivm, channel, aos_channel_type, pixels_per_channel);
575 }
576
577 lp_build_transpose_aos(gallivm, soa_type, aos_channels, dst_soa[i]);
578 }
579 }
580
581
582 static void
583 store_aos(struct gallivm_state *gallivm,
584 LLVMValueRef io_ptr,
585 LLVMValueRef index,
586 LLVMValueRef value)
587 {
588 LLVMTypeRef data_ptr_type = LLVMPointerType(lp_build_vec_type(gallivm, lp_float32_vec4_type()), 0);
589 LLVMBuilderRef builder = gallivm->builder;
590 LLVMValueRef data_ptr = draw_jit_header_data(gallivm, io_ptr);
591 LLVMValueRef indices[3];
592
593 indices[0] = lp_build_const_int32(gallivm, 0);
594 indices[1] = index;
595 indices[2] = lp_build_const_int32(gallivm, 0);
596
597 #if DEBUG_STORE
598 lp_build_printf(gallivm, " ---- %p storing attribute %d (io = %p)\n", data_ptr, index, io_ptr);
599 #endif
600
601 data_ptr = LLVMBuildGEP(builder, data_ptr, indices, 3, "");
602 data_ptr = LLVMBuildPointerCast(builder, data_ptr, data_ptr_type, "");
603
604 /* Unaligned store due to the vertex header */
605 lp_set_store_alignment(LLVMBuildStore(builder, value, data_ptr), sizeof(float));
606 }
607
608
609 static void
610 store_aos_array(struct gallivm_state *gallivm,
611 struct lp_type soa_type,
612 LLVMValueRef io_ptr,
613 LLVMValueRef* aos,
614 int attrib,
615 int num_outputs,
616 LLVMValueRef clipmask,
617 boolean have_clipdist)
618 {
619 LLVMBuilderRef builder = gallivm->builder;
620 LLVMValueRef attr_index = lp_build_const_int32(gallivm, attrib);
621 LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32];
622 LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32];
623 int vector_length = soa_type.length;
624 int i;
625
626 debug_assert(TGSI_NUM_CHANNELS == 4);
627
628 for (i = 0; i < vector_length; i++) {
629 inds[i] = lp_build_const_int32(gallivm, i);
630 io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, "");
631 }
632
633 if (attrib == 0) {
634 /* store vertex header for each of the n vertices */
635 LLVMValueRef val, cliptmp;
636 int vertex_id_pad_edgeflag;
637
638 /* If this assertion fails, it means we need to update the bit twidding
639 * code here. See struct vertex_header in draw_private.h.
640 */
641 assert(DRAW_TOTAL_CLIP_PLANES==14);
642 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
643 vertex_id_pad_edgeflag = (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES);
644 if (have_clipdist)
645 vertex_id_pad_edgeflag |= 1 << (DRAW_TOTAL_CLIP_PLANES+1);
646 val = lp_build_const_int_vec(gallivm, lp_int_type(soa_type), vertex_id_pad_edgeflag);
647 /* OR with the clipmask */
648 cliptmp = LLVMBuildOr(builder, val, clipmask, "");
649 for (i = 0; i < vector_length; i++) {
650 LLVMValueRef id_ptr = draw_jit_header_id(gallivm, io_ptrs[i]);
651 val = LLVMBuildExtractElement(builder, cliptmp, inds[i], "");
652 LLVMBuildStore(builder, val, id_ptr);
653 #if DEBUG_STORE
654 lp_build_printf(gallivm, "io = %p, index %d\n, clipmask = %x\n",
655 io_ptrs[i], inds[i], val);
656 #endif
657 }
658 }
659
660 /* store for each of the n vertices */
661 for (i = 0; i < vector_length; i++) {
662 store_aos(gallivm, io_ptrs[i], attr_index, aos[i]);
663 }
664 }
665
666
667 static void
668 convert_to_aos(struct gallivm_state *gallivm,
669 LLVMValueRef io,
670 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
671 LLVMValueRef clipmask,
672 int num_outputs,
673 struct lp_type soa_type,
674 boolean have_clipdist)
675 {
676 LLVMBuilderRef builder = gallivm->builder;
677 unsigned chan, attrib, i;
678
679 #if DEBUG_STORE
680 lp_build_printf(gallivm, " # storing begin\n");
681 #endif
682 for (attrib = 0; attrib < num_outputs; ++attrib) {
683 LLVMValueRef soa[TGSI_NUM_CHANNELS];
684 LLVMValueRef aos[LP_MAX_VECTOR_WIDTH / 32];
685 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
686 if (outputs[attrib][chan]) {
687 LLVMValueRef out = LLVMBuildLoad(builder, outputs[attrib][chan], "");
688 lp_build_name(out, "output%u.%c", attrib, "xyzw"[chan]);
689 #if DEBUG_STORE
690 lp_build_printf(gallivm, "output %d : %d ",
691 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context),
692 attrib, 0),
693 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context),
694 chan, 0));
695 lp_build_print_value(gallivm, "val = ", out);
696 #endif
697 soa[chan] = out;
698 }
699 else {
700 soa[chan] = 0;
701 }
702 }
703
704
705 if (soa_type.length == TGSI_NUM_CHANNELS) {
706 lp_build_transpose_aos(gallivm, soa_type, soa, aos);
707 } else {
708 lp_build_transpose_aos(gallivm, soa_type, soa, soa);
709
710 for (i = 0; i < soa_type.length; ++i) {
711 aos[i] = lp_build_extract_range(gallivm,
712 soa[i % TGSI_NUM_CHANNELS],
713 (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS,
714 TGSI_NUM_CHANNELS);
715 }
716 }
717
718 store_aos_array(gallivm,
719 soa_type,
720 io,
721 aos,
722 attrib,
723 num_outputs,
724 clipmask, have_clipdist);
725 }
726 #if DEBUG_STORE
727 lp_build_printf(gallivm, " # storing end\n");
728 #endif
729 }
730
731
732 /**
733 * Stores original vertex positions in clip coordinates
734 */
735 static void
736 store_clip(struct gallivm_state *gallivm,
737 const struct lp_type vs_type,
738 LLVMValueRef io_ptr,
739 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
740 boolean pre_clip_pos, int idx)
741 {
742 LLVMBuilderRef builder = gallivm->builder;
743 LLVMValueRef soa[4];
744 LLVMValueRef aos[LP_MAX_VECTOR_LENGTH];
745 LLVMValueRef indices[2];
746 LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32];
747 LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32];
748 LLVMValueRef clip_ptrs[LP_MAX_VECTOR_WIDTH / 32];
749 int i, j;
750
751 indices[0] =
752 indices[1] = lp_build_const_int32(gallivm, 0);
753
754 for (i = 0; i < vs_type.length; i++) {
755 inds[i] = lp_build_const_int32(gallivm, i);
756 io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, "");
757 }
758
759 soa[0] = LLVMBuildLoad(builder, outputs[idx][0], ""); /*x0 x1 .. xn*/
760 soa[1] = LLVMBuildLoad(builder, outputs[idx][1], ""); /*y0 y1 .. yn*/
761 soa[2] = LLVMBuildLoad(builder, outputs[idx][2], ""); /*z0 z1 .. zn*/
762 soa[3] = LLVMBuildLoad(builder, outputs[idx][3], ""); /*w0 w1 .. wn*/
763
764 if (!pre_clip_pos) {
765 for (i = 0; i < vs_type.length; i++) {
766 clip_ptrs[i] = draw_jit_header_clip(gallivm, io_ptrs[i]);
767 }
768 } else {
769 for (i = 0; i < vs_type.length; i++) {
770 clip_ptrs[i] = draw_jit_header_pre_clip_pos(gallivm, io_ptrs[i]);
771 }
772 }
773
774 lp_build_transpose_aos(gallivm, vs_type, soa, soa);
775 for (i = 0; i < vs_type.length; ++i) {
776 aos[i] = lp_build_extract_range(gallivm,
777 soa[i % TGSI_NUM_CHANNELS],
778 (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS,
779 TGSI_NUM_CHANNELS);
780 }
781
782 for (j = 0; j < vs_type.length; j++) {
783 LLVMTypeRef clip_ptr_type = LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4), 0);
784 LLVMValueRef clip_ptr;
785
786 clip_ptr = LLVMBuildGEP(builder, clip_ptrs[j], indices, 2, "clipo");
787 clip_ptr = LLVMBuildPointerCast(builder, clip_ptr, clip_ptr_type, "");
788
789 /* Unaligned store */
790 lp_set_store_alignment(LLVMBuildStore(builder, aos[j], clip_ptr), sizeof(float));
791 }
792 }
793
794
795 /**
796 * Transforms the outputs for viewport mapping
797 */
798 static void
799 generate_viewport(struct draw_llvm_variant *variant,
800 LLVMBuilderRef builder,
801 struct lp_type vs_type,
802 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
803 LLVMValueRef context_ptr)
804 {
805 int i;
806 struct gallivm_state *gallivm = variant->gallivm;
807 struct lp_type f32_type = vs_type;
808 LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type);
809 LLVMValueRef out3 = LLVMBuildLoad(builder, outputs[0][3], ""); /*w0 w1 .. wn*/
810 LLVMValueRef const1 = lp_build_const_vec(gallivm, f32_type, 1.0); /*1.0 1.0 1.0 1.0*/
811 LLVMValueRef vp_ptr = draw_jit_context_viewport(gallivm, context_ptr);
812
813 /* for 1/w convention*/
814 out3 = LLVMBuildFDiv(builder, const1, out3, "");
815 LLVMBuildStore(builder, out3, outputs[0][3]);
816
817 /* Viewport Mapping */
818 for (i=0; i<3; i++) {
819 LLVMValueRef out = LLVMBuildLoad(builder, outputs[0][i], ""); /*x0 x1 .. xn*/
820 LLVMValueRef scale;
821 LLVMValueRef trans;
822 LLVMValueRef scale_i;
823 LLVMValueRef trans_i;
824 LLVMValueRef index;
825
826 index = lp_build_const_int32(gallivm, i);
827 scale_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, "");
828
829 index = lp_build_const_int32(gallivm, i+4);
830 trans_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, "");
831
832 scale = lp_build_broadcast(gallivm, vs_type_llvm,
833 LLVMBuildLoad(builder, scale_i, "scale"));
834 trans = lp_build_broadcast(gallivm, vs_type_llvm,
835 LLVMBuildLoad(builder, trans_i, "trans"));
836
837 /* divide by w */
838 out = LLVMBuildFMul(builder, out, out3, "");
839 /* mult by scale */
840 out = LLVMBuildFMul(builder, out, scale, "");
841 /* add translation */
842 out = LLVMBuildFAdd(builder, out, trans, "");
843
844 /* store transformed outputs */
845 LLVMBuildStore(builder, out, outputs[0][i]);
846 }
847
848 }
849
850
851 /**
852 * Returns clipmask as nxi32 bitmask for the n vertices
853 */
854 static LLVMValueRef
855 generate_clipmask(struct draw_llvm *llvm,
856 struct gallivm_state *gallivm,
857 struct lp_type vs_type,
858 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
859 boolean clip_xy,
860 boolean clip_z,
861 boolean clip_user,
862 boolean clip_halfz,
863 unsigned ucp_enable,
864 LLVMValueRef context_ptr,
865 boolean *have_clipdist)
866 {
867 LLVMBuilderRef builder = gallivm->builder;
868 LLVMValueRef mask; /* stores the <nxi32> clipmasks */
869 LLVMValueRef test, temp;
870 LLVMValueRef zero, shift;
871 LLVMValueRef pos_x, pos_y, pos_z, pos_w;
872 LLVMValueRef cv_x, cv_y, cv_z, cv_w;
873 LLVMValueRef plane1, planes, plane_ptr, sum;
874 struct lp_type f32_type = vs_type;
875 struct lp_type i32_type = lp_int_type(vs_type);
876 const unsigned pos = draw_current_shader_position_output(llvm->draw);
877 const unsigned cv = draw_current_shader_clipvertex_output(llvm->draw);
878 int num_written_clipdistance = llvm->draw->vs.vertex_shader->info.num_written_clipdistance;
879 bool have_cd = false;
880 unsigned cd[2];
881
882 cd[0] = draw_current_shader_clipdistance_output(llvm->draw, 0);
883 cd[1] = draw_current_shader_clipdistance_output(llvm->draw, 1);
884
885 if (cd[0] != pos || cd[1] != pos)
886 have_cd = true;
887
888 mask = lp_build_const_int_vec(gallivm, i32_type, 0);
889 temp = lp_build_const_int_vec(gallivm, i32_type, 0);
890 zero = lp_build_const_vec(gallivm, f32_type, 0); /* 0.0f 0.0f 0.0f 0.0f */
891 shift = lp_build_const_int_vec(gallivm, i32_type, 1); /* 1 1 1 1 */
892
893 /*
894 * load clipvertex and position from correct locations.
895 * if they are the same just load them once.
896 */
897 pos_x = LLVMBuildLoad(builder, outputs[pos][0], ""); /*x0 x1 .. xn */
898 pos_y = LLVMBuildLoad(builder, outputs[pos][1], ""); /*y0 y1 .. yn */
899 pos_z = LLVMBuildLoad(builder, outputs[pos][2], ""); /*z0 z1 .. zn */
900 pos_w = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 .. wn */
901
902 if (clip_user && cv != pos) {
903 cv_x = LLVMBuildLoad(builder, outputs[cv][0], ""); /*x0 x1 .. xn */
904 cv_y = LLVMBuildLoad(builder, outputs[cv][1], ""); /*y0 y1 .. yn */
905 cv_z = LLVMBuildLoad(builder, outputs[cv][2], ""); /*z0 z1 .. zn */
906 cv_w = LLVMBuildLoad(builder, outputs[cv][3], ""); /*w0 w1 .. wn */
907 } else {
908 cv_x = pos_x;
909 cv_y = pos_y;
910 cv_z = pos_z;
911 cv_w = pos_w;
912 }
913
914 /* Cliptest, for hardwired planes */
915 if (clip_xy) {
916 /* plane 1 */
917 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_x , pos_w);
918 temp = shift;
919 test = LLVMBuildAnd(builder, test, temp, "");
920 mask = test;
921
922 /* plane 2 */
923 test = LLVMBuildFAdd(builder, pos_x, pos_w, "");
924 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
925 temp = LLVMBuildShl(builder, temp, shift, "");
926 test = LLVMBuildAnd(builder, test, temp, "");
927 mask = LLVMBuildOr(builder, mask, test, "");
928
929 /* plane 3 */
930 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_y, pos_w);
931 temp = LLVMBuildShl(builder, temp, shift, "");
932 test = LLVMBuildAnd(builder, test, temp, "");
933 mask = LLVMBuildOr(builder, mask, test, "");
934
935 /* plane 4 */
936 test = LLVMBuildFAdd(builder, pos_y, pos_w, "");
937 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
938 temp = LLVMBuildShl(builder, temp, shift, "");
939 test = LLVMBuildAnd(builder, test, temp, "");
940 mask = LLVMBuildOr(builder, mask, test, "");
941 }
942
943 if (clip_z) {
944 temp = lp_build_const_int_vec(gallivm, i32_type, 16);
945 if (clip_halfz) {
946 /* plane 5 */
947 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, pos_z);
948 test = LLVMBuildAnd(builder, test, temp, "");
949 mask = LLVMBuildOr(builder, mask, test, "");
950 }
951 else {
952 /* plane 5 */
953 test = LLVMBuildFAdd(builder, pos_z, pos_w, "");
954 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
955 test = LLVMBuildAnd(builder, test, temp, "");
956 mask = LLVMBuildOr(builder, mask, test, "");
957 }
958 /* plane 6 */
959 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_z, pos_w);
960 temp = LLVMBuildShl(builder, temp, shift, "");
961 test = LLVMBuildAnd(builder, test, temp, "");
962 mask = LLVMBuildOr(builder, mask, test, "");
963 }
964
965 if (clip_user) {
966 LLVMValueRef planes_ptr = draw_jit_context_planes(gallivm, context_ptr);
967 LLVMValueRef indices[3];
968
969 /* userclip planes */
970 while (ucp_enable) {
971 unsigned plane_idx = ffs(ucp_enable)-1;
972 ucp_enable &= ~(1 << plane_idx);
973 plane_idx += 6;
974
975 if (have_cd && num_written_clipdistance) {
976 LLVMValueRef clipdist;
977 int i;
978 i = plane_idx - 6;
979
980 *have_clipdist = TRUE;
981 if (i < 4) {
982 clipdist = LLVMBuildLoad(builder, outputs[cd[0]][i], "");
983 } else {
984 clipdist = LLVMBuildLoad(builder, outputs[cd[1]][i-4], "");
985 }
986 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, clipdist);
987 temp = lp_build_const_int_vec(gallivm, i32_type, 1 << plane_idx);
988 test = LLVMBuildAnd(builder, test, temp, "");
989 mask = LLVMBuildOr(builder, mask, test, "");
990 } else {
991 LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type);
992 indices[0] = lp_build_const_int32(gallivm, 0);
993 indices[1] = lp_build_const_int32(gallivm, plane_idx);
994
995 indices[2] = lp_build_const_int32(gallivm, 0);
996 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
997 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_x");
998 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
999 sum = LLVMBuildFMul(builder, planes, cv_x, "");
1000
1001 indices[2] = lp_build_const_int32(gallivm, 1);
1002 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1003 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_y");
1004 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1005 test = LLVMBuildFMul(builder, planes, cv_y, "");
1006 sum = LLVMBuildFAdd(builder, sum, test, "");
1007
1008 indices[2] = lp_build_const_int32(gallivm, 2);
1009 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1010 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_z");
1011 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1012 test = LLVMBuildFMul(builder, planes, cv_z, "");
1013 sum = LLVMBuildFAdd(builder, sum, test, "");
1014
1015 indices[2] = lp_build_const_int32(gallivm, 3);
1016 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1017 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_w");
1018 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1019 test = LLVMBuildFMul(builder, planes, cv_w, "");
1020 sum = LLVMBuildFAdd(builder, sum, test, "");
1021
1022 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, sum);
1023 temp = lp_build_const_int_vec(gallivm, i32_type, 1 << plane_idx);
1024 test = LLVMBuildAnd(builder, test, temp, "");
1025 mask = LLVMBuildOr(builder, mask, test, "");
1026 }
1027 }
1028 }
1029 return mask;
1030 }
1031
1032
1033 /**
1034 * Returns boolean if any clipping has occurred
1035 * Used zero/non-zero i32 value to represent boolean
1036 */
1037 static LLVMValueRef
1038 clipmask_booli32(struct gallivm_state *gallivm,
1039 const struct lp_type vs_type,
1040 LLVMValueRef clipmask_bool_ptr)
1041 {
1042 LLVMBuilderRef builder = gallivm->builder;
1043 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
1044 LLVMValueRef clipmask_bool = LLVMBuildLoad(builder, clipmask_bool_ptr, "");
1045 LLVMValueRef ret = LLVMConstNull(int32_type);
1046 LLVMValueRef temp;
1047 int i;
1048
1049 /*
1050 * Can do this with log2(vector length) pack instructions and one extract
1051 * (as we don't actually need a or) with sse2 which would be way better.
1052 */
1053 for (i=0; i < vs_type.length; i++) {
1054 temp = LLVMBuildExtractElement(builder, clipmask_bool,
1055 lp_build_const_int32(gallivm, i) , "");
1056 ret = LLVMBuildOr(builder, ret, temp, "");
1057 }
1058 return ret;
1059 }
1060
1061
1062 static void
1063 draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *variant,
1064 boolean elts)
1065 {
1066 struct gallivm_state *gallivm = variant->gallivm;
1067 LLVMContextRef context = gallivm->context;
1068 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context);
1069 LLVMTypeRef arg_types[8];
1070 LLVMTypeRef func_type;
1071 LLVMValueRef context_ptr;
1072 LLVMBasicBlockRef block;
1073 LLVMBuilderRef builder;
1074 struct lp_type vs_type;
1075 LLVMValueRef end, start;
1076 LLVMValueRef count, fetch_elts, fetch_count;
1077 LLVMValueRef stride, step, io_itr;
1078 LLVMValueRef io_ptr, vbuffers_ptr, vb_ptr;
1079 LLVMValueRef zero = lp_build_const_int32(gallivm, 0);
1080 LLVMValueRef one = lp_build_const_int32(gallivm, 1);
1081 struct draw_context *draw = llvm->draw;
1082 const struct tgsi_shader_info *vs_info = &draw->vs.vertex_shader->info;
1083 unsigned i, j;
1084 struct lp_build_context bld;
1085 struct lp_build_loop_state lp_loop;
1086 const int vector_length = lp_native_vector_width / 32;
1087 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
1088 LLVMValueRef fetch_max;
1089 struct lp_build_sampler_soa *sampler = 0;
1090 LLVMValueRef ret, clipmask_bool_ptr;
1091 const boolean bypass_viewport = variant->key.bypass_viewport;
1092 const boolean enable_cliptest = variant->key.clip_xy ||
1093 variant->key.clip_z ||
1094 variant->key.clip_user;
1095 LLVMValueRef variant_func;
1096 const unsigned pos = draw_current_shader_position_output(llvm->draw);
1097 const unsigned cv = draw_current_shader_clipvertex_output(llvm->draw);
1098 boolean have_clipdist = FALSE;
1099 struct lp_bld_tgsi_system_values system_values;
1100
1101 memset(&system_values, 0, sizeof(system_values));
1102
1103 arg_types[0] = get_context_ptr_type(variant); /* context */
1104 arg_types[1] = get_vertex_header_ptr_type(variant); /* vertex_header */
1105 arg_types[2] = get_buffer_ptr_type(variant); /* vbuffers */
1106 if (elts)
1107 arg_types[3] = LLVMPointerType(int32_type, 0);/* fetch_elts * */
1108 else
1109 arg_types[3] = int32_type; /* start */
1110 arg_types[4] = int32_type; /* fetch_count / count */
1111 arg_types[5] = int32_type; /* stride */
1112 arg_types[6] = get_vb_ptr_type(variant); /* pipe_vertex_buffer's */
1113 arg_types[7] = int32_type; /* instance_id */
1114
1115 func_type = LLVMFunctionType(int32_type, arg_types, Elements(arg_types), 0);
1116
1117 variant_func = LLVMAddFunction(gallivm->module,
1118 elts ? "draw_llvm_shader_elts" : "draw_llvm_shader",
1119 func_type);
1120
1121 if (elts)
1122 variant->function_elts = variant_func;
1123 else
1124 variant->function = variant_func;
1125
1126 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv);
1127 for (i = 0; i < Elements(arg_types); ++i)
1128 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
1129 LLVMAddAttribute(LLVMGetParam(variant_func, i),
1130 LLVMNoAliasAttribute);
1131
1132 context_ptr = LLVMGetParam(variant_func, 0);
1133 io_ptr = LLVMGetParam(variant_func, 1);
1134 vbuffers_ptr = LLVMGetParam(variant_func, 2);
1135 stride = LLVMGetParam(variant_func, 5);
1136 vb_ptr = LLVMGetParam(variant_func, 6);
1137 system_values.instance_id = LLVMGetParam(variant_func, 7);
1138
1139 lp_build_name(context_ptr, "context");
1140 lp_build_name(io_ptr, "io");
1141 lp_build_name(vbuffers_ptr, "vbuffers");
1142 lp_build_name(stride, "stride");
1143 lp_build_name(vb_ptr, "vb");
1144 lp_build_name(system_values.instance_id, "instance_id");
1145
1146 if (elts) {
1147 fetch_elts = LLVMGetParam(variant_func, 3);
1148 fetch_count = LLVMGetParam(variant_func, 4);
1149 lp_build_name(fetch_elts, "fetch_elts");
1150 lp_build_name(fetch_count, "fetch_count");
1151 start = count = NULL;
1152 }
1153 else {
1154 start = LLVMGetParam(variant_func, 3);
1155 count = LLVMGetParam(variant_func, 4);
1156 lp_build_name(start, "start");
1157 lp_build_name(count, "count");
1158 fetch_elts = fetch_count = NULL;
1159 }
1160
1161 /*
1162 * Function body
1163 */
1164
1165 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry");
1166 builder = gallivm->builder;
1167 LLVMPositionBuilderAtEnd(builder, block);
1168
1169 lp_build_context_init(&bld, gallivm, lp_type_int(32));
1170
1171 memset(&vs_type, 0, sizeof vs_type);
1172 vs_type.floating = TRUE; /* floating point values */
1173 vs_type.sign = TRUE; /* values are signed */
1174 vs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */
1175 vs_type.width = 32; /* 32-bit float */
1176 vs_type.length = vector_length;
1177
1178 /* hold temporary "bool" clipmask */
1179 clipmask_bool_ptr = lp_build_alloca(gallivm, lp_build_int_vec_type(gallivm, vs_type), "");
1180 LLVMBuildStore(builder, lp_build_zero(gallivm, lp_int_type(vs_type)), clipmask_bool_ptr);
1181
1182 /* code generated texture sampling */
1183 sampler = draw_llvm_sampler_soa_create(
1184 draw_llvm_variant_key_samplers(&variant->key),
1185 context_ptr);
1186
1187 if (elts) {
1188 start = zero;
1189 end = fetch_count;
1190 }
1191 else {
1192 end = lp_build_add(&bld, start, count);
1193 }
1194
1195 step = lp_build_const_int32(gallivm, vector_length);
1196
1197 fetch_max = LLVMBuildSub(builder, end, one, "fetch_max");
1198
1199 lp_build_loop_begin(&lp_loop, gallivm, start);
1200 {
1201 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
1202 LLVMValueRef aos_attribs[PIPE_MAX_SHADER_INPUTS][LP_MAX_VECTOR_WIDTH / 32] = { { 0 } };
1203 LLVMValueRef io;
1204 LLVMValueRef clipmask; /* holds the clipmask value */
1205 const LLVMValueRef (*ptr_aos)[TGSI_NUM_CHANNELS];
1206
1207 if (elts)
1208 io_itr = lp_loop.counter;
1209 else
1210 io_itr = LLVMBuildSub(builder, lp_loop.counter, start, "");
1211
1212 io = LLVMBuildGEP(builder, io_ptr, &io_itr, 1, "");
1213 #if DEBUG_STORE
1214 lp_build_printf(gallivm, " --- io %d = %p, loop counter %d\n",
1215 io_itr, io, lp_loop.counter);
1216 #endif
1217 system_values.vertex_id = lp_build_zero(gallivm, lp_type_uint_vec(32, 32*vector_length));
1218 for (i = 0; i < vector_length; ++i) {
1219 LLVMValueRef true_index =
1220 LLVMBuildAdd(builder,
1221 lp_loop.counter,
1222 lp_build_const_int32(gallivm, i), "");
1223
1224 /* make sure we're not out of bounds which can happen
1225 * if fetch_count % 4 != 0, because on the last iteration
1226 * a few of the 4 vertex fetches will be out of bounds */
1227 true_index = lp_build_min(&bld, true_index, fetch_max);
1228
1229 if (elts) {
1230 LLVMValueRef fetch_ptr;
1231 fetch_ptr = LLVMBuildGEP(builder, fetch_elts,
1232 &true_index, 1, "");
1233 true_index = LLVMBuildLoad(builder, fetch_ptr, "fetch_elt");
1234 }
1235
1236 system_values.vertex_id = LLVMBuildInsertElement(gallivm->builder,
1237 system_values.vertex_id, true_index,
1238 lp_build_const_int32(gallivm, i), "");
1239 for (j = 0; j < draw->pt.nr_vertex_elements; ++j) {
1240 struct pipe_vertex_element *velem = &draw->pt.vertex_element[j];
1241 LLVMValueRef vb_index =
1242 lp_build_const_int32(gallivm, velem->vertex_buffer_index);
1243 LLVMValueRef vb = LLVMBuildGEP(builder, vb_ptr, &vb_index, 1, "");
1244 generate_fetch(gallivm, vbuffers_ptr,
1245 &aos_attribs[j][i], velem, vb, true_index,
1246 system_values.instance_id);
1247 }
1248 }
1249 convert_to_soa(gallivm, aos_attribs, inputs,
1250 draw->pt.nr_vertex_elements, vs_type);
1251
1252 ptr_aos = (const LLVMValueRef (*)[TGSI_NUM_CHANNELS]) inputs;
1253 generate_vs(variant,
1254 builder,
1255 vs_type,
1256 outputs,
1257 ptr_aos,
1258 &system_values,
1259 context_ptr,
1260 sampler,
1261 variant->key.clamp_vertex_color);
1262
1263 /* store original positions in clip before further manipulation */
1264 store_clip(gallivm, vs_type, io, outputs, 0, cv);
1265 store_clip(gallivm, vs_type, io, outputs, 1, pos);
1266
1267 /* do cliptest */
1268 if (enable_cliptest) {
1269 LLVMValueRef temp = LLVMBuildLoad(builder, clipmask_bool_ptr, "");
1270 /* allocate clipmask, assign it integer type */
1271 clipmask = generate_clipmask(llvm,
1272 gallivm,
1273 vs_type,
1274 outputs,
1275 variant->key.clip_xy,
1276 variant->key.clip_z,
1277 variant->key.clip_user,
1278 variant->key.clip_halfz,
1279 variant->key.ucp_enable,
1280 context_ptr, &have_clipdist);
1281 temp = LLVMBuildOr(builder, clipmask, temp, "");
1282 /* store temporary clipping boolean value */
1283 LLVMBuildStore(builder, temp, clipmask_bool_ptr);
1284 }
1285 else {
1286 clipmask = lp_build_const_int_vec(gallivm, lp_int_type(vs_type), 0);
1287 }
1288
1289 /* do viewport mapping */
1290 if (!bypass_viewport) {
1291 generate_viewport(variant, builder, vs_type, outputs, context_ptr);
1292 }
1293
1294 /* store clipmask in vertex header,
1295 * original positions in clip
1296 * and transformed positions in data
1297 */
1298 convert_to_aos(gallivm, io, outputs, clipmask,
1299 vs_info->num_outputs, vs_type,
1300 have_clipdist);
1301 }
1302
1303 lp_build_loop_end_cond(&lp_loop, end, step, LLVMIntUGE);
1304
1305 sampler->destroy(sampler);
1306
1307 /* return clipping boolean value for function */
1308 ret = clipmask_booli32(gallivm, vs_type, clipmask_bool_ptr);
1309
1310 LLVMBuildRet(builder, ret);
1311
1312 gallivm_verify_function(gallivm, variant_func);
1313 }
1314
1315
1316 struct draw_llvm_variant_key *
1317 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store)
1318 {
1319 unsigned i;
1320 struct draw_llvm_variant_key *key;
1321 struct lp_sampler_static_state *sampler;
1322
1323 key = (struct draw_llvm_variant_key *)store;
1324
1325 key->clamp_vertex_color = llvm->draw->rasterizer->clamp_vertex_color; /**/
1326
1327 /* Presumably all variants of the shader should have the same
1328 * number of vertex elements - ie the number of shader inputs.
1329 */
1330 key->nr_vertex_elements = llvm->draw->pt.nr_vertex_elements;
1331
1332 /* will have to rig this up properly later */
1333 key->clip_xy = llvm->draw->clip_xy;
1334 key->clip_z = llvm->draw->clip_z;
1335 key->clip_user = llvm->draw->clip_user;
1336 key->bypass_viewport = llvm->draw->identity_viewport;
1337 key->clip_halfz = !llvm->draw->rasterizer->gl_rasterization_rules;
1338 key->need_edgeflags = (llvm->draw->vs.edgeflag_output ? TRUE : FALSE);
1339 key->ucp_enable = llvm->draw->rasterizer->clip_plane_enable;
1340 key->pad = 0;
1341
1342 /* All variants of this shader will have the same value for
1343 * nr_samplers. Not yet trying to compact away holes in the
1344 * sampler array.
1345 */
1346 key->nr_samplers = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER] + 1;
1347
1348 sampler = draw_llvm_variant_key_samplers(key);
1349
1350 memcpy(key->vertex_element,
1351 llvm->draw->pt.vertex_element,
1352 sizeof(struct pipe_vertex_element) * key->nr_vertex_elements);
1353
1354 memset(sampler, 0, key->nr_samplers * sizeof *sampler);
1355
1356 for (i = 0 ; i < key->nr_samplers; i++) {
1357 lp_sampler_static_state(&sampler[i],
1358 llvm->draw->sampler_views[PIPE_SHADER_VERTEX][i],
1359 llvm->draw->samplers[PIPE_SHADER_VERTEX][i]);
1360 }
1361
1362 return key;
1363 }
1364
1365
1366 void
1367 draw_llvm_set_mapped_texture(struct draw_context *draw,
1368 unsigned sampler_idx,
1369 uint32_t width, uint32_t height, uint32_t depth,
1370 uint32_t first_level, uint32_t last_level,
1371 const void *base_ptr,
1372 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
1373 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
1374 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])
1375 {
1376 unsigned j;
1377 struct draw_jit_texture *jit_tex;
1378
1379 assert(sampler_idx < Elements(draw->llvm->jit_context.textures));
1380
1381 jit_tex = &draw->llvm->jit_context.textures[sampler_idx];
1382
1383 jit_tex->width = width;
1384 jit_tex->height = height;
1385 jit_tex->depth = depth;
1386 jit_tex->first_level = first_level;
1387 jit_tex->last_level = last_level;
1388 jit_tex->base = base_ptr;
1389
1390 for (j = first_level; j <= last_level; j++) {
1391 jit_tex->mip_offsets[j] = mip_offsets[j];
1392 jit_tex->row_stride[j] = row_stride[j];
1393 jit_tex->img_stride[j] = img_stride[j];
1394 }
1395 }
1396
1397
1398 void
1399 draw_llvm_set_sampler_state(struct draw_context *draw)
1400 {
1401 unsigned i;
1402
1403 for (i = 0; i < draw->num_samplers[PIPE_SHADER_VERTEX]; i++) {
1404 struct draw_jit_texture *jit_tex = &draw->llvm->jit_context.textures[i];
1405
1406 if (draw->samplers[i]) {
1407 const struct pipe_sampler_state *s
1408 = draw->samplers[PIPE_SHADER_VERTEX][i];
1409 jit_tex->min_lod = s->min_lod;
1410 jit_tex->max_lod = s->max_lod;
1411 jit_tex->lod_bias = s->lod_bias;
1412 COPY_4V(jit_tex->border_color, s->border_color.f);
1413 }
1414 }
1415 }
1416
1417
1418 void
1419 draw_llvm_destroy_variant(struct draw_llvm_variant *variant)
1420 {
1421 struct draw_llvm *llvm = variant->llvm;
1422
1423 if (variant->function_elts) {
1424 gallivm_free_function(variant->gallivm,
1425 variant->function_elts, variant->jit_func_elts);
1426 }
1427
1428 if (variant->function) {
1429 gallivm_free_function(variant->gallivm,
1430 variant->function, variant->jit_func);
1431 }
1432
1433 gallivm_destroy(variant->gallivm);
1434
1435 remove_from_list(&variant->list_item_local);
1436 variant->shader->variants_cached--;
1437 remove_from_list(&variant->list_item_global);
1438 llvm->nr_variants--;
1439 FREE(variant);
1440 }