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