1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "draw_llvm.h"
30 #include "draw_context.h"
34 #include "gallivm/lp_bld_arit.h"
35 #include "gallivm/lp_bld_logic.h"
36 #include "gallivm/lp_bld_const.h"
37 #include "gallivm/lp_bld_swizzle.h"
38 #include "gallivm/lp_bld_struct.h"
39 #include "gallivm/lp_bld_type.h"
40 #include "gallivm/lp_bld_flow.h"
41 #include "gallivm/lp_bld_debug.h"
42 #include "gallivm/lp_bld_tgsi.h"
43 #include "gallivm/lp_bld_printf.h"
44 #include "gallivm/lp_bld_intr.h"
45 #include "gallivm/lp_bld_init.h"
46 #include "gallivm/lp_bld_type.h"
47 #include "gallivm/lp_bld_pack.h"
48 #include "gallivm/lp_bld_format.h"
50 #include "tgsi/tgsi_exec.h"
51 #include "tgsi/tgsi_dump.h"
53 #include "util/u_math.h"
54 #include "util/u_pointer.h"
55 #include "util/u_string.h"
56 #include "util/u_simple_list.h"
63 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
,
68 * Create LLVM type for struct draw_jit_texture
71 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
73 LLVMTargetDataRef target
= gallivm
->target
;
74 LLVMTypeRef texture_type
;
75 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
76 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
78 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
79 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
80 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
81 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
82 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
83 elem_types
[DRAW_JIT_TEXTURE_BASE
] =
84 LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
85 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
86 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
87 elem_types
[DRAW_JIT_TEXTURE_MIP_OFFSETS
] =
88 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
90 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
91 Elements(elem_types
), 0);
93 #if HAVE_LLVM < 0x0300
94 LLVMAddTypeName(gallivm
->module
, struct_name
, texture_type
);
96 /* Make sure the target's struct layout cache doesn't return
99 LLVMInvalidateStructLayout(gallivm
->target
, texture_type
);
102 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
103 target
, texture_type
,
104 DRAW_JIT_TEXTURE_WIDTH
);
105 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
106 target
, texture_type
,
107 DRAW_JIT_TEXTURE_HEIGHT
);
108 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
109 target
, texture_type
,
110 DRAW_JIT_TEXTURE_DEPTH
);
111 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
112 target
, texture_type
,
113 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
114 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
115 target
, texture_type
,
116 DRAW_JIT_TEXTURE_LAST_LEVEL
);
117 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, base
,
118 target
, texture_type
,
119 DRAW_JIT_TEXTURE_BASE
);
120 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
121 target
, texture_type
,
122 DRAW_JIT_TEXTURE_ROW_STRIDE
);
123 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
124 target
, texture_type
,
125 DRAW_JIT_TEXTURE_IMG_STRIDE
);
126 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, mip_offsets
,
127 target
, texture_type
,
128 DRAW_JIT_TEXTURE_MIP_OFFSETS
);
130 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
137 * Create LLVM type for struct draw_jit_sampler
140 create_jit_sampler_type(struct gallivm_state
*gallivm
, const char *struct_name
)
142 LLVMTargetDataRef target
= gallivm
->target
;
143 LLVMTypeRef sampler_type
;
144 LLVMTypeRef elem_types
[DRAW_JIT_SAMPLER_NUM_FIELDS
];
146 elem_types
[DRAW_JIT_SAMPLER_MIN_LOD
] =
147 elem_types
[DRAW_JIT_SAMPLER_MAX_LOD
] =
148 elem_types
[DRAW_JIT_SAMPLER_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
149 elem_types
[DRAW_JIT_SAMPLER_BORDER_COLOR
] =
150 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
152 sampler_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
153 Elements(elem_types
), 0);
155 #if HAVE_LLVM < 0x0300
156 LLVMAddTypeName(gallivm
->module
, struct_name
, sampler_type
);
158 /* Make sure the target's struct layout cache doesn't return
159 * stale/invalid data.
161 LLVMInvalidateStructLayout(gallivm
->target
, sampler_type
);
164 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, min_lod
,
165 target
, sampler_type
,
166 DRAW_JIT_SAMPLER_MIN_LOD
);
167 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, max_lod
,
168 target
, sampler_type
,
169 DRAW_JIT_SAMPLER_MAX_LOD
);
170 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, lod_bias
,
171 target
, sampler_type
,
172 DRAW_JIT_SAMPLER_LOD_BIAS
);
173 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler
, border_color
,
174 target
, sampler_type
,
175 DRAW_JIT_SAMPLER_BORDER_COLOR
);
177 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler
, target
, sampler_type
);
184 * Create LLVM type for struct draw_jit_context
187 create_jit_context_type(struct gallivm_state
*gallivm
,
188 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
189 const char *struct_name
)
191 LLVMTargetDataRef target
= gallivm
->target
;
192 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
193 LLVMTypeRef elem_types
[5];
194 LLVMTypeRef context_type
;
196 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* vs_constants */
197 LP_MAX_TGSI_CONST_BUFFERS
);
198 elem_types
[1] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
199 DRAW_TOTAL_CLIP_PLANES
), 0);
200 elem_types
[2] = LLVMPointerType(float_type
, 0); /* viewport */
201 elem_types
[3] = LLVMArrayType(texture_type
,
202 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
203 elem_types
[4] = LLVMArrayType(sampler_type
,
204 PIPE_MAX_SAMPLERS
); /* samplers */
205 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
206 Elements(elem_types
), 0);
207 #if HAVE_LLVM < 0x0300
208 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
210 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
213 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
214 target
, context_type
, 0);
215 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
216 target
, context_type
, 1);
217 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, viewport
,
218 target
, context_type
, 2);
219 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
220 target
, context_type
,
221 DRAW_JIT_CTX_TEXTURES
);
222 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, samplers
,
223 target
, context_type
,
224 DRAW_JIT_CTX_SAMPLERS
);
225 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
226 target
, context_type
);
233 * Create LLVM type for struct draw_gs_jit_context
236 create_gs_jit_context_type(struct gallivm_state
*gallivm
,
237 unsigned vector_length
,
238 LLVMTypeRef texture_type
, LLVMTypeRef sampler_type
,
239 const char *struct_name
)
241 LLVMTargetDataRef target
= gallivm
->target
;
242 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
243 LLVMTypeRef int_type
= LLVMInt32TypeInContext(gallivm
->context
);
244 LLVMTypeRef elem_types
[DRAW_GS_JIT_CTX_NUM_FIELDS
];
245 LLVMTypeRef context_type
;
247 elem_types
[0] = LLVMArrayType(LLVMPointerType(float_type
, 0), /* constants */
248 LP_MAX_TGSI_CONST_BUFFERS
);
249 elem_types
[1] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
250 DRAW_TOTAL_CLIP_PLANES
), 0);
251 elem_types
[2] = LLVMPointerType(float_type
, 0); /* viewport */
253 elem_types
[3] = LLVMArrayType(texture_type
,
254 PIPE_MAX_SHADER_SAMPLER_VIEWS
); /* textures */
255 elem_types
[4] = LLVMArrayType(sampler_type
,
256 PIPE_MAX_SAMPLERS
); /* samplers */
258 elem_types
[5] = LLVMPointerType(LLVMPointerType(int_type
, 0), 0);
259 elem_types
[6] = LLVMPointerType(LLVMVectorType(int_type
,
261 elem_types
[7] = LLVMPointerType(LLVMVectorType(int_type
,
264 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
265 Elements(elem_types
), 0);
266 #if HAVE_LLVM < 0x0300
267 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
269 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
272 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, constants
,
273 target
, context_type
, DRAW_GS_JIT_CTX_CONSTANTS
);
274 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, planes
,
275 target
, context_type
, DRAW_GS_JIT_CTX_PLANES
);
276 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, viewport
,
277 target
, context_type
, DRAW_GS_JIT_CTX_VIEWPORT
);
278 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, textures
,
279 target
, context_type
,
280 DRAW_GS_JIT_CTX_TEXTURES
);
281 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, samplers
,
282 target
, context_type
,
283 DRAW_GS_JIT_CTX_SAMPLERS
);
284 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, prim_lengths
,
285 target
, context_type
,
286 DRAW_GS_JIT_CTX_PRIM_LENGTHS
);
287 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_vertices
,
288 target
, context_type
,
289 DRAW_GS_JIT_CTX_EMITTED_VERTICES
);
290 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context
, emitted_prims
,
291 target
, context_type
,
292 DRAW_GS_JIT_CTX_EMITTED_PRIMS
);
293 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context
,
294 target
, context_type
);
301 create_gs_jit_input_type(struct gallivm_state
*gallivm
)
303 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
304 LLVMTypeRef input_array
;
306 input_array
= LLVMVectorType(float_type
, TGSI_NUM_CHANNELS
); /* num primitives */
307 input_array
= LLVMArrayType(input_array
, TGSI_NUM_CHANNELS
); /* num channels */
308 input_array
= LLVMArrayType(input_array
, PIPE_MAX_SHADER_INPUTS
); /* num attrs per vertex */
309 input_array
= LLVMPointerType(input_array
, 0); /* num vertices per prim */
315 * Create LLVM type for struct pipe_vertex_buffer
318 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
, const char *struct_name
)
320 LLVMTargetDataRef target
= gallivm
->target
;
321 LLVMTypeRef elem_types
[4];
325 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
327 elem_types
[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0); /* vs_constants */
329 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
330 Elements(elem_types
), 0);
331 #if HAVE_LLVM < 0x0300
332 LLVMAddTypeName(gallivm
->module
, struct_name
, vb_type
);
334 LLVMInvalidateStructLayout(gallivm
->target
, vb_type
);
337 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
339 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
342 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
349 * Create LLVM type for struct vertex_header;
352 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
354 LLVMTargetDataRef target
= gallivm
->target
;
355 LLVMTypeRef elem_types
[4];
356 LLVMTypeRef vertex_header
;
357 char struct_name
[24];
359 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
361 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
362 elem_types
[DRAW_JIT_VERTEX_CLIP
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
363 elem_types
[DRAW_JIT_VERTEX_PRE_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
364 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
366 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
367 Elements(elem_types
), 0);
368 #if HAVE_LLVM < 0x0300
369 LLVMAddTypeName(gallivm
->module
, struct_name
, vertex_header
);
371 LLVMInvalidateStructLayout(gallivm
->target
, vertex_header
);
374 /* these are bit-fields and we can't take address of them
375 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
376 target, vertex_header,
377 DRAW_JIT_VERTEX_CLIPMASK);
378 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
379 target, vertex_header,
380 DRAW_JIT_VERTEX_EDGEFLAG);
381 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
382 target, vertex_header,
383 DRAW_JIT_VERTEX_PAD);
384 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
385 target, vertex_header,
386 DRAW_JIT_VERTEX_VERTEX_ID);
388 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
389 target
, vertex_header
,
390 DRAW_JIT_VERTEX_CLIP
);
391 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, pre_clip_pos
,
392 target
, vertex_header
,
393 DRAW_JIT_VERTEX_PRE_CLIP_POS
);
394 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
395 target
, vertex_header
,
396 DRAW_JIT_VERTEX_DATA
);
398 assert(LLVMABISizeOfType(target
, vertex_header
) ==
399 offsetof(struct vertex_header
, data
[data_elems
]));
401 return vertex_header
;
406 * Create LLVM types for various structures.
409 create_jit_types(struct draw_llvm_variant
*variant
)
411 struct gallivm_state
*gallivm
= variant
->gallivm
;
412 LLVMTypeRef texture_type
, sampler_type
, context_type
, buffer_type
, vb_type
;
414 texture_type
= create_jit_texture_type(gallivm
, "texture");
415 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
417 context_type
= create_jit_context_type(gallivm
, texture_type
, sampler_type
,
419 variant
->context_ptr_type
= LLVMPointerType(context_type
, 0);
421 buffer_type
= LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
422 variant
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
424 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
425 variant
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
430 get_context_ptr_type(struct draw_llvm_variant
*variant
)
432 if (!variant
->context_ptr_type
)
433 create_jit_types(variant
);
434 return variant
->context_ptr_type
;
439 get_buffer_ptr_type(struct draw_llvm_variant
*variant
)
441 if (!variant
->buffer_ptr_type
)
442 create_jit_types(variant
);
443 return variant
->buffer_ptr_type
;
448 get_vb_ptr_type(struct draw_llvm_variant
*variant
)
450 if (!variant
->vb_ptr_type
)
451 create_jit_types(variant
);
452 return variant
->vb_ptr_type
;
456 get_vertex_header_ptr_type(struct draw_llvm_variant
*variant
)
458 if (!variant
->vertex_header_ptr_type
)
459 create_jit_types(variant
);
460 return variant
->vertex_header_ptr_type
;
465 * Create per-context LLVM info.
468 draw_llvm_create(struct draw_context
*draw
)
470 struct draw_llvm
*llvm
;
472 llvm
= CALLOC_STRUCT( draw_llvm
);
480 llvm
->nr_variants
= 0;
481 make_empty_list(&llvm
->vs_variants_list
);
483 llvm
->nr_gs_variants
= 0;
484 make_empty_list(&llvm
->gs_variants_list
);
491 * Free per-context LLVM info.
494 draw_llvm_destroy(struct draw_llvm
*llvm
)
496 /* XXX free other draw_llvm data? */
502 * Create LLVM-generated code for a vertex shader.
504 struct draw_llvm_variant
*
505 draw_llvm_create_variant(struct draw_llvm
*llvm
,
507 const struct draw_llvm_variant_key
*key
)
509 struct draw_llvm_variant
*variant
;
510 struct llvm_vertex_shader
*shader
=
511 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
512 LLVMTypeRef vertex_header
;
514 variant
= MALLOC(sizeof *variant
+
515 shader
->variant_key_size
-
516 sizeof variant
->key
);
520 variant
->llvm
= llvm
;
522 variant
->gallivm
= gallivm_create();
524 create_jit_types(variant
);
526 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
528 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_inputs
);
530 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
532 draw_llvm_generate(llvm
, variant
, FALSE
); /* linear */
533 draw_llvm_generate(llvm
, variant
, TRUE
); /* elts */
535 gallivm_compile_module(variant
->gallivm
);
537 variant
->jit_func
= (draw_jit_vert_func
)
538 gallivm_jit_function(variant
->gallivm
, variant
->function
);
540 variant
->jit_func_elts
= (draw_jit_vert_func_elts
)
541 gallivm_jit_function(variant
->gallivm
, variant
->function_elts
);
543 variant
->shader
= shader
;
544 variant
->list_item_global
.base
= variant
;
545 variant
->list_item_local
.base
= variant
;
546 /*variant->no = */shader
->variants_created
++;
547 variant
->list_item_global
.base
= variant
;
554 generate_vs(struct draw_llvm_variant
*variant
,
555 LLVMBuilderRef builder
,
556 struct lp_type vs_type
,
557 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
558 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
559 const struct lp_bld_tgsi_system_values
*system_values
,
560 LLVMValueRef context_ptr
,
561 struct lp_build_sampler_soa
*draw_sampler
,
562 boolean clamp_vertex_color
)
564 struct draw_llvm
*llvm
= variant
->llvm
;
565 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
566 LLVMValueRef consts_ptr
= draw_jit_context_vs_constants(variant
->gallivm
, context_ptr
);
567 struct lp_build_sampler_soa
*sampler
= 0;
569 if (gallivm_debug
& (GALLIVM_DEBUG_TGSI
| GALLIVM_DEBUG_IR
)) {
570 tgsi_dump(tokens
, 0);
571 draw_llvm_dump_variant_key(&variant
->key
);
574 if (llvm
->draw
->num_sampler_views
&& llvm
->draw
->num_samplers
)
575 sampler
= draw_sampler
;
577 lp_build_tgsi_soa(variant
->gallivm
,
580 NULL
/*struct lp_build_mask_context *mask*/,
587 &llvm
->draw
->vs
.vertex_shader
->info
,
592 unsigned chan
, attrib
;
593 struct lp_build_context bld
;
594 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
595 lp_build_context_init(&bld
, variant
->gallivm
, vs_type
);
597 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
598 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
599 if (outputs
[attrib
][chan
]) {
600 switch (info
->output_semantic_name
[attrib
]) {
601 case TGSI_SEMANTIC_COLOR
:
602 case TGSI_SEMANTIC_BCOLOR
:
603 if (clamp_vertex_color
) {
604 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
605 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
606 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
609 case TGSI_SEMANTIC_FOG
:
610 if (chan
== 1 || chan
== 2)
611 LLVMBuildStore(builder
, bld
.zero
, outputs
[attrib
][chan
]);
613 LLVMBuildStore(builder
, bld
.one
, outputs
[attrib
][chan
]);
624 generate_fetch(struct gallivm_state
*gallivm
,
625 LLVMValueRef vbuffers_ptr
,
627 struct pipe_vertex_element
*velem
,
630 LLVMValueRef instance_id
)
632 const struct util_format_description
*format_desc
= util_format_description(velem
->src_format
);
633 LLVMValueRef zero
= LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
));
634 LLVMBuilderRef builder
= gallivm
->builder
;
635 LLVMValueRef indices
=
636 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
637 velem
->vertex_buffer_index
, 0);
638 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
640 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
641 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
644 if (velem
->instance_divisor
) {
645 /* array index = instance_id / instance_divisor */
646 index
= LLVMBuildUDiv(builder
, instance_id
,
647 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
651 stride
= LLVMBuildMul(builder
, vb_stride
, index
, "");
653 vbuffer_ptr
= LLVMBuildLoad(builder
, vbuffer_ptr
, "vbuffer");
655 stride
= LLVMBuildAdd(builder
, stride
,
658 stride
= LLVMBuildAdd(builder
, stride
,
659 lp_build_const_int32(gallivm
, velem
->src_offset
),
662 /* lp_build_printf(gallivm, "vbuf index = %d, stride is %d\n", indices, stride);*/
663 vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffer_ptr
, &stride
, 1, "");
665 *res
= lp_build_fetch_rgba_aos(gallivm
,
667 lp_float32_vec4_type(),
673 convert_to_soa(struct gallivm_state
*gallivm
,
674 LLVMValueRef (*src_aos
)[LP_MAX_VECTOR_WIDTH
/ 32],
675 LLVMValueRef (*dst_soa
)[TGSI_NUM_CHANNELS
],
676 unsigned num_attribs
, const struct lp_type soa_type
)
679 struct lp_type aos_channel_type
= soa_type
;
681 debug_assert(TGSI_NUM_CHANNELS
== 4);
682 debug_assert((soa_type
.length
% TGSI_NUM_CHANNELS
) == 0);
684 aos_channel_type
.length
>>= 1;
686 for (i
= 0; i
< num_attribs
; ++i
) {
687 LLVMValueRef aos_channels
[TGSI_NUM_CHANNELS
];
688 unsigned pixels_per_channel
= soa_type
.length
/ TGSI_NUM_CHANNELS
;
690 for (j
= 0; j
< TGSI_NUM_CHANNELS
; ++j
) {
691 LLVMValueRef channel
[LP_MAX_VECTOR_LENGTH
] = { 0 };
693 assert(pixels_per_channel
<= LP_MAX_VECTOR_LENGTH
);
695 for (k
= 0; k
< pixels_per_channel
; ++k
) {
696 channel
[k
] = src_aos
[i
][j
+ TGSI_NUM_CHANNELS
* k
];
699 aos_channels
[j
] = lp_build_concat(gallivm
, channel
, aos_channel_type
, pixels_per_channel
);
702 lp_build_transpose_aos(gallivm
, soa_type
, aos_channels
, dst_soa
[i
]);
708 store_aos(struct gallivm_state
*gallivm
,
713 LLVMTypeRef data_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, lp_float32_vec4_type()), 0);
714 LLVMBuilderRef builder
= gallivm
->builder
;
715 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
716 LLVMValueRef indices
[3];
718 indices
[0] = lp_build_const_int32(gallivm
, 0);
720 indices
[2] = lp_build_const_int32(gallivm
, 0);
722 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
723 data_ptr
= LLVMBuildPointerCast(builder
, data_ptr
, data_ptr_type
, "");
726 lp_build_printf(gallivm
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
729 /* Unaligned store due to the vertex header */
730 lp_set_store_alignment(LLVMBuildStore(builder
, value
, data_ptr
), sizeof(float));
734 * Adjust the mask to architecture endianess. The mask will the store in struct:
736 * struct vertex_header {
737 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
738 * unsigned edgeflag:1;
739 * unsigned have_clipdist:1;
740 * unsigned vertex_id:16;
744 * On little-endian machine nothing needs to done, however on bit-endian machine
745 * the mask's fields need to be adjusted with the algorithm:
747 * uint32_t reverse (uint32_t x)
749 * return (x >> 16) | // vertex_id
750 * ((x & 0x3fff) << 18) | // clipmask
751 * ((x & 0x4000) << 3) | // have_clipdist
752 * ((x & 0x8000) << 1); // edgeflag
756 adjust_mask(struct gallivm_state
*gallivm
,
759 #ifdef PIPE_ARCH_BIG_ENDIAN
760 LLVMBuilderRef builder
= gallivm
->builder
;
761 LLVMValueRef vertex_id
;
762 LLVMValueRef clipmask
;
763 LLVMValueRef have_clipdist
;
764 LLVMValueRef edgeflag
;
766 vertex_id
= LLVMBuildLShr(builder
, mask
, lp_build_const_int32(gallivm
, 16), "");
767 clipmask
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x3fff), "");
768 clipmask
= LLVMBuildShl(builder
, clipmask
, lp_build_const_int32(gallivm
, 18), "");
769 have_clipdist
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x4000), "");
770 have_clipdist
= LLVMBuildShl(builder
, have_clipdist
, lp_build_const_int32(gallivm
, 3), "");
771 edgeflag
= LLVMBuildAnd(builder
, mask
, lp_build_const_int32(gallivm
, 0x8000), "");
772 edgeflag
= LLVMBuildShl(builder
, edgeflag
, lp_build_const_int32(gallivm
, 1), "");
774 mask
= LLVMBuildOr(builder
, vertex_id
, clipmask
, "");
775 mask
= LLVMBuildOr(builder
, mask
, have_clipdist
, "");
776 mask
= LLVMBuildOr(builder
, mask
, edgeflag
, "");
782 store_aos_array(struct gallivm_state
*gallivm
,
783 struct lp_type soa_type
,
785 LLVMValueRef
*indices
,
789 LLVMValueRef clipmask
,
790 boolean have_clipdist
)
792 LLVMBuilderRef builder
= gallivm
->builder
;
793 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
794 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
795 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
796 int vector_length
= soa_type
.length
;
799 debug_assert(TGSI_NUM_CHANNELS
== 4);
801 for (i
= 0; i
< vector_length
; i
++) {
803 inds
[i
] = indices
[i
];
805 inds
[i
] = lp_build_const_int32(gallivm
, i
);
807 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
811 /* store vertex header for each of the n vertices */
812 LLVMValueRef val
, cliptmp
;
813 int vertex_id_pad_edgeflag
;
815 /* If this assertion fails, it means we need to update the bit twidding
816 * code here. See struct vertex_header in draw_private.h.
818 assert(DRAW_TOTAL_CLIP_PLANES
==14);
819 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
820 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
822 vertex_id_pad_edgeflag
|= 1 << (DRAW_TOTAL_CLIP_PLANES
+1);
823 val
= lp_build_const_int_vec(gallivm
, lp_int_type(soa_type
), vertex_id_pad_edgeflag
);
824 /* OR with the clipmask */
825 cliptmp
= LLVMBuildOr(builder
, val
, clipmask
, "");
826 for (i
= 0; i
< vector_length
; i
++) {
827 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptrs
[i
]);
828 val
= LLVMBuildExtractElement(builder
, cliptmp
, inds
[i
], "");
829 val
= adjust_mask(gallivm
, val
);
830 LLVMBuildStore(builder
, val
, id_ptr
);
832 lp_build_printf(gallivm
, "io = %p, index %d, clipmask = %x\n",
833 io_ptrs
[i
], inds
[i
], val
);
838 /* store for each of the n vertices */
839 for (i
= 0; i
< vector_length
; i
++) {
840 store_aos(gallivm
, io_ptrs
[i
], attr_index
, aos
[i
]);
846 convert_to_aos(struct gallivm_state
*gallivm
,
848 LLVMValueRef
*indices
,
849 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
850 LLVMValueRef clipmask
,
852 struct lp_type soa_type
,
853 boolean have_clipdist
)
855 LLVMBuilderRef builder
= gallivm
->builder
;
856 unsigned chan
, attrib
, i
;
859 lp_build_printf(gallivm
, " # storing begin\n");
861 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
862 LLVMValueRef soa
[TGSI_NUM_CHANNELS
];
863 LLVMValueRef aos
[LP_MAX_VECTOR_WIDTH
/ 32];
864 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
865 if (outputs
[attrib
][chan
]) {
866 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
867 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
869 lp_build_printf(gallivm
, "output %d : %d ",
870 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
872 LLVMConstInt(LLVMInt32TypeInContext(gallivm
->context
),
874 lp_build_print_value(gallivm
, "val = ", out
);
884 if (soa_type
.length
== TGSI_NUM_CHANNELS
) {
885 lp_build_transpose_aos(gallivm
, soa_type
, soa
, aos
);
887 lp_build_transpose_aos(gallivm
, soa_type
, soa
, soa
);
889 for (i
= 0; i
< soa_type
.length
; ++i
) {
890 aos
[i
] = lp_build_extract_range(gallivm
,
891 soa
[i
% TGSI_NUM_CHANNELS
],
892 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
897 store_aos_array(gallivm
,
903 clipmask
, have_clipdist
);
906 lp_build_printf(gallivm
, " # storing end\n");
912 * Stores original vertex positions in clip coordinates
915 store_clip(struct gallivm_state
*gallivm
,
916 const struct lp_type vs_type
,
918 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
919 boolean pre_clip_pos
, int idx
)
921 LLVMBuilderRef builder
= gallivm
->builder
;
923 LLVMValueRef aos
[LP_MAX_VECTOR_LENGTH
];
924 LLVMValueRef indices
[2];
925 LLVMValueRef io_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
926 LLVMValueRef inds
[LP_MAX_VECTOR_WIDTH
/ 32];
927 LLVMValueRef clip_ptrs
[LP_MAX_VECTOR_WIDTH
/ 32];
931 indices
[1] = lp_build_const_int32(gallivm
, 0);
933 for (i
= 0; i
< vs_type
.length
; i
++) {
934 inds
[i
] = lp_build_const_int32(gallivm
, i
);
935 io_ptrs
[i
] = LLVMBuildGEP(builder
, io_ptr
, &inds
[i
], 1, "");
938 soa
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 .. xn*/
939 soa
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 .. yn*/
940 soa
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 .. zn*/
941 soa
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 .. wn*/
944 for (i
= 0; i
< vs_type
.length
; i
++) {
945 clip_ptrs
[i
] = draw_jit_header_clip(gallivm
, io_ptrs
[i
]);
948 for (i
= 0; i
< vs_type
.length
; i
++) {
949 clip_ptrs
[i
] = draw_jit_header_pre_clip_pos(gallivm
, io_ptrs
[i
]);
953 lp_build_transpose_aos(gallivm
, vs_type
, soa
, soa
);
954 for (i
= 0; i
< vs_type
.length
; ++i
) {
955 aos
[i
] = lp_build_extract_range(gallivm
,
956 soa
[i
% TGSI_NUM_CHANNELS
],
957 (i
/ TGSI_NUM_CHANNELS
) * TGSI_NUM_CHANNELS
,
961 for (j
= 0; j
< vs_type
.length
; j
++) {
962 LLVMTypeRef clip_ptr_type
= LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4), 0);
963 LLVMValueRef clip_ptr
;
965 clip_ptr
= LLVMBuildGEP(builder
, clip_ptrs
[j
], indices
, 2, "clipo");
966 clip_ptr
= LLVMBuildPointerCast(builder
, clip_ptr
, clip_ptr_type
, "");
968 /* Unaligned store */
969 lp_set_store_alignment(LLVMBuildStore(builder
, aos
[j
], clip_ptr
), sizeof(float));
975 * Transforms the outputs for viewport mapping
978 generate_viewport(struct draw_llvm_variant
*variant
,
979 LLVMBuilderRef builder
,
980 struct lp_type vs_type
,
981 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
982 LLVMValueRef context_ptr
)
985 struct gallivm_state
*gallivm
= variant
->gallivm
;
986 struct lp_type f32_type
= vs_type
;
987 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
988 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[0][3], ""); /*w0 w1 .. wn*/
989 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
990 LLVMValueRef vp_ptr
= draw_jit_context_viewport(gallivm
, context_ptr
);
992 /* for 1/w convention*/
993 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
994 LLVMBuildStore(builder
, out3
, outputs
[0][3]);
996 /* Viewport Mapping */
997 for (i
=0; i
<3; i
++) {
998 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[0][i
], ""); /*x0 x1 .. xn*/
1001 LLVMValueRef scale_i
;
1002 LLVMValueRef trans_i
;
1005 index
= lp_build_const_int32(gallivm
, i
);
1006 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1008 index
= lp_build_const_int32(gallivm
, i
+4);
1009 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1011 scale
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1012 LLVMBuildLoad(builder
, scale_i
, "scale"));
1013 trans
= lp_build_broadcast(gallivm
, vs_type_llvm
,
1014 LLVMBuildLoad(builder
, trans_i
, "trans"));
1017 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1019 out
= LLVMBuildFMul(builder
, out
, scale
, "");
1020 /* add translation */
1021 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
1023 /* store transformed outputs */
1024 LLVMBuildStore(builder
, out
, outputs
[0][i
]);
1031 * Returns clipmask as nxi32 bitmask for the n vertices
1034 generate_clipmask(struct draw_llvm
*llvm
,
1035 struct gallivm_state
*gallivm
,
1036 struct lp_type vs_type
,
1037 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1042 unsigned ucp_enable
,
1043 LLVMValueRef context_ptr
,
1044 boolean
*have_clipdist
)
1046 LLVMBuilderRef builder
= gallivm
->builder
;
1047 LLVMValueRef mask
; /* stores the <nxi32> clipmasks */
1048 LLVMValueRef test
, temp
;
1049 LLVMValueRef zero
, shift
;
1050 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1051 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1052 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1053 struct lp_type f32_type
= vs_type
;
1054 struct lp_type i32_type
= lp_int_type(vs_type
);
1055 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1056 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1057 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1058 bool have_cd
= false;
1061 cd
[0] = draw_current_shader_clipdistance_output(llvm
->draw
, 0);
1062 cd
[1] = draw_current_shader_clipdistance_output(llvm
->draw
, 1);
1064 if (cd
[0] != pos
|| cd
[1] != pos
)
1067 mask
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1068 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 0);
1069 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1070 shift
= lp_build_const_int_vec(gallivm
, i32_type
, 1); /* 1 1 1 1 */
1073 * load clipvertex and position from correct locations.
1074 * if they are the same just load them once.
1076 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 .. xn */
1077 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 .. yn */
1078 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 .. zn */
1079 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 .. wn */
1081 if (clip_user
&& cv
!= pos
) {
1082 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 .. xn */
1083 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 .. yn */
1084 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 .. zn */
1085 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 .. wn */
1093 /* Cliptest, for hardwired planes */
1096 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1098 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1102 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1103 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1104 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1105 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1106 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1109 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1110 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1111 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1112 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1115 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1116 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1117 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1118 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1119 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1123 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 16);
1126 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1127 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1128 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1132 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1133 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1134 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1135 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1138 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1139 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1140 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1141 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1145 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1146 LLVMValueRef indices
[3];
1148 /* userclip planes */
1149 while (ucp_enable
) {
1150 unsigned plane_idx
= ffs(ucp_enable
)-1;
1151 ucp_enable
&= ~(1 << plane_idx
);
1154 if (have_cd
&& num_written_clipdistance
) {
1155 LLVMValueRef clipdist
;
1159 *have_clipdist
= TRUE
;
1161 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1163 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1165 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1166 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1167 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1168 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1170 LLVMTypeRef vs_type_llvm
= lp_build_vec_type(gallivm
, vs_type
);
1171 indices
[0] = lp_build_const_int32(gallivm
, 0);
1172 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1174 indices
[2] = lp_build_const_int32(gallivm
, 0);
1175 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1176 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1177 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1178 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1180 indices
[2] = lp_build_const_int32(gallivm
, 1);
1181 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1182 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1183 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1184 test
= LLVMBuildFMul(builder
, planes
, cv_y
, "");
1185 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1187 indices
[2] = lp_build_const_int32(gallivm
, 2);
1188 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1189 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1190 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1191 test
= LLVMBuildFMul(builder
, planes
, cv_z
, "");
1192 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1194 indices
[2] = lp_build_const_int32(gallivm
, 3);
1195 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1196 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1197 planes
= lp_build_broadcast(gallivm
, vs_type_llvm
, plane1
);
1198 test
= LLVMBuildFMul(builder
, planes
, cv_w
, "");
1199 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1201 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1202 temp
= lp_build_const_int_vec(gallivm
, i32_type
, 1 << plane_idx
);
1203 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1204 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1213 * Returns boolean if any clipping has occurred
1214 * Used zero/non-zero i32 value to represent boolean
1217 clipmask_booli32(struct gallivm_state
*gallivm
,
1218 const struct lp_type vs_type
,
1219 LLVMValueRef clipmask_bool_ptr
)
1221 LLVMBuilderRef builder
= gallivm
->builder
;
1222 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
1223 LLVMValueRef clipmask_bool
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1224 LLVMValueRef ret
= LLVMConstNull(int32_type
);
1229 * Can do this with log2(vector length) pack instructions and one extract
1230 * (as we don't actually need a or) with sse2 which would be way better.
1232 for (i
=0; i
< vs_type
.length
; i
++) {
1233 temp
= LLVMBuildExtractElement(builder
, clipmask_bool
,
1234 lp_build_const_int32(gallivm
, i
) , "");
1235 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1241 draw_gs_llvm_emit_vertex(struct lp_build_tgsi_context
* bld_base
,
1242 LLVMValueRef (*outputs
)[4],
1243 LLVMValueRef emitted_vertices_vec
,
1246 struct draw_gs_llvm_variant
*variant
=
1247 (struct draw_gs_llvm_variant
*)user_data
;
1248 struct gallivm_state
*gallivm
= variant
->gallivm
;
1249 LLVMBuilderRef builder
= gallivm
->builder
;
1250 struct lp_type gs_type
= bld_base
->base
.type
;
1251 LLVMValueRef clipmask
= lp_build_const_int_vec(gallivm
,
1252 lp_int_type(gs_type
), 0);
1253 LLVMValueRef indices
[LP_MAX_VECTOR_LENGTH
];
1254 LLVMValueRef max_output_vertices
=
1255 lp_build_const_int32(gallivm
, variant
->shader
->base
.max_output_vertices
);
1256 LLVMValueRef io
= variant
->io_ptr
;
1258 const struct tgsi_shader_info
*gs_info
= &variant
->shader
->base
.info
;
1260 for (i
= 0; i
< gs_type
.length
; ++i
) {
1261 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1262 LLVMValueRef currently_emitted
=
1263 LLVMBuildExtractElement(builder
, emitted_vertices_vec
, ind
, "");
1264 indices
[i
] = LLVMBuildMul(builder
, ind
, max_output_vertices
, "");
1265 indices
[i
] = LLVMBuildAdd(builder
, indices
[i
], currently_emitted
, "");
1268 convert_to_aos(gallivm
, io
, indices
,
1270 gs_info
->num_outputs
, gs_type
,
1275 draw_gs_llvm_end_primitive(struct lp_build_tgsi_context
* bld_base
,
1276 LLVMValueRef verts_per_prim_vec
,
1277 LLVMValueRef emitted_prims_vec
,
1280 struct draw_gs_llvm_variant
*variant
=
1281 (struct draw_gs_llvm_variant
*)user_data
;
1282 struct gallivm_state
*gallivm
= variant
->gallivm
;
1283 LLVMBuilderRef builder
= gallivm
->builder
;
1284 LLVMValueRef prim_lengts_ptr
=
1285 draw_gs_jit_prim_lengths(variant
->gallivm
, variant
->context_ptr
);
1288 for (i
= 0; i
< bld_base
->base
.type
.length
; ++i
) {
1289 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1290 LLVMValueRef prims_emitted
=
1291 LLVMBuildExtractElement(builder
, emitted_prims_vec
, ind
, "");
1292 LLVMValueRef store_ptr
;
1293 LLVMValueRef num_vertices
=
1294 LLVMBuildExtractElement(builder
, verts_per_prim_vec
, ind
, "");
1296 store_ptr
= LLVMBuildGEP(builder
, prim_lengts_ptr
, &prims_emitted
, 1, "");
1297 store_ptr
= LLVMBuildLoad(builder
, store_ptr
, "");
1298 store_ptr
= LLVMBuildGEP(builder
, store_ptr
, &ind
, 1, "");
1299 LLVMBuildStore(builder
, num_vertices
, store_ptr
);
1304 draw_gs_llvm_epilogue(struct lp_build_tgsi_context
* bld_base
,
1305 LLVMValueRef total_emitted_vertices_vec
,
1306 LLVMValueRef emitted_prims_vec
,
1309 struct draw_gs_llvm_variant
*variant
=
1310 (struct draw_gs_llvm_variant
*)user_data
;
1311 struct gallivm_state
*gallivm
= variant
->gallivm
;
1312 LLVMBuilderRef builder
= gallivm
->builder
;
1313 LLVMValueRef emitted_verts_ptr
=
1314 draw_gs_jit_emitted_vertices(gallivm
, variant
->context_ptr
);
1315 LLVMValueRef emitted_prims_ptr
=
1316 draw_gs_jit_emitted_prims(gallivm
, variant
->context_ptr
);
1317 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1319 emitted_verts_ptr
= LLVMBuildGEP(builder
, emitted_verts_ptr
, &zero
, 0, "");
1320 emitted_prims_ptr
= LLVMBuildGEP(builder
, emitted_prims_ptr
, &zero
, 0, "");
1322 LLVMBuildStore(builder
, total_emitted_vertices_vec
, emitted_verts_ptr
);
1323 LLVMBuildStore(builder
, emitted_prims_vec
, emitted_prims_ptr
);
1327 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1330 struct gallivm_state
*gallivm
= variant
->gallivm
;
1331 LLVMContextRef context
= gallivm
->context
;
1332 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1333 LLVMTypeRef arg_types
[8];
1334 LLVMTypeRef func_type
;
1335 LLVMValueRef context_ptr
;
1336 LLVMBasicBlockRef block
;
1337 LLVMBuilderRef builder
;
1338 struct lp_type vs_type
;
1339 LLVMValueRef end
, start
;
1340 LLVMValueRef count
, fetch_elts
, fetch_count
;
1341 LLVMValueRef stride
, step
, io_itr
;
1342 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1343 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1344 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1345 struct draw_context
*draw
= llvm
->draw
;
1346 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1348 struct lp_build_context bld
;
1349 struct lp_build_loop_state lp_loop
;
1350 const int vector_length
= lp_native_vector_width
/ 32;
1351 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1352 LLVMValueRef fetch_max
;
1353 struct lp_build_sampler_soa
*sampler
= 0;
1354 LLVMValueRef ret
, clipmask_bool_ptr
;
1355 const struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
1356 /* If geometry shader is present we need to skip both the viewport
1357 * transformation and clipping otherwise the inputs to the geometry
1358 * shader will be incorrect.
1360 const boolean bypass_viewport
= gs
|| variant
->key
.bypass_viewport
;
1361 const boolean enable_cliptest
= !gs
&& (variant
->key
.clip_xy
||
1362 variant
->key
.clip_z
||
1363 variant
->key
.clip_user
);
1364 LLVMValueRef variant_func
;
1365 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1366 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1367 boolean have_clipdist
= FALSE
;
1368 struct lp_bld_tgsi_system_values system_values
;
1370 memset(&system_values
, 0, sizeof(system_values
));
1372 arg_types
[0] = get_context_ptr_type(variant
); /* context */
1373 arg_types
[1] = get_vertex_header_ptr_type(variant
); /* vertex_header */
1374 arg_types
[2] = get_buffer_ptr_type(variant
); /* vbuffers */
1376 arg_types
[3] = LLVMPointerType(int32_type
, 0);/* fetch_elts * */
1378 arg_types
[3] = int32_type
; /* start */
1379 arg_types
[4] = int32_type
; /* fetch_count / count */
1380 arg_types
[5] = int32_type
; /* stride */
1381 arg_types
[6] = get_vb_ptr_type(variant
); /* pipe_vertex_buffer's */
1382 arg_types
[7] = int32_type
; /* instance_id */
1384 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
1386 variant_func
= LLVMAddFunction(gallivm
->module
,
1387 elts
? "draw_llvm_shader_elts" : "draw_llvm_shader",
1391 variant
->function_elts
= variant_func
;
1393 variant
->function
= variant_func
;
1395 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1396 for (i
= 0; i
< Elements(arg_types
); ++i
)
1397 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1398 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1399 LLVMNoAliasAttribute
);
1401 context_ptr
= LLVMGetParam(variant_func
, 0);
1402 io_ptr
= LLVMGetParam(variant_func
, 1);
1403 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1404 stride
= LLVMGetParam(variant_func
, 5);
1405 vb_ptr
= LLVMGetParam(variant_func
, 6);
1406 system_values
.instance_id
= LLVMGetParam(variant_func
, 7);
1408 lp_build_name(context_ptr
, "context");
1409 lp_build_name(io_ptr
, "io");
1410 lp_build_name(vbuffers_ptr
, "vbuffers");
1411 lp_build_name(stride
, "stride");
1412 lp_build_name(vb_ptr
, "vb");
1413 lp_build_name(system_values
.instance_id
, "instance_id");
1416 fetch_elts
= LLVMGetParam(variant_func
, 3);
1417 fetch_count
= LLVMGetParam(variant_func
, 4);
1418 lp_build_name(fetch_elts
, "fetch_elts");
1419 lp_build_name(fetch_count
, "fetch_count");
1420 start
= count
= NULL
;
1423 start
= LLVMGetParam(variant_func
, 3);
1424 count
= LLVMGetParam(variant_func
, 4);
1425 lp_build_name(start
, "start");
1426 lp_build_name(count
, "count");
1427 fetch_elts
= fetch_count
= NULL
;
1434 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1435 builder
= gallivm
->builder
;
1436 LLVMPositionBuilderAtEnd(builder
, block
);
1438 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1440 memset(&vs_type
, 0, sizeof vs_type
);
1441 vs_type
.floating
= TRUE
; /* floating point values */
1442 vs_type
.sign
= TRUE
; /* values are signed */
1443 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1444 vs_type
.width
= 32; /* 32-bit float */
1445 vs_type
.length
= vector_length
;
1447 /* hold temporary "bool" clipmask */
1448 clipmask_bool_ptr
= lp_build_alloca(gallivm
, lp_build_int_vec_type(gallivm
, vs_type
), "");
1449 LLVMBuildStore(builder
, lp_build_zero(gallivm
, lp_int_type(vs_type
)), clipmask_bool_ptr
);
1451 /* code generated texture sampling */
1452 sampler
= draw_llvm_sampler_soa_create(
1453 draw_llvm_variant_key_samplers(&variant
->key
),
1461 end
= lp_build_add(&bld
, start
, count
);
1464 step
= lp_build_const_int32(gallivm
, vector_length
);
1466 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1468 lp_build_loop_begin(&lp_loop
, gallivm
, start
);
1470 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1471 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][LP_MAX_VECTOR_WIDTH
/ 32] = { { 0 } };
1473 LLVMValueRef clipmask
; /* holds the clipmask value */
1474 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1477 io_itr
= lp_loop
.counter
;
1479 io_itr
= LLVMBuildSub(builder
, lp_loop
.counter
, start
, "");
1481 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1483 lp_build_printf(gallivm
, " --- io %d = %p, loop counter %d\n",
1484 io_itr
, io
, lp_loop
.counter
);
1486 system_values
.vertex_id
= lp_build_zero(gallivm
, lp_type_uint_vec(32, 32*vector_length
));
1487 for (i
= 0; i
< vector_length
; ++i
) {
1488 LLVMValueRef true_index
=
1489 LLVMBuildAdd(builder
,
1491 lp_build_const_int32(gallivm
, i
), "");
1493 /* make sure we're not out of bounds which can happen
1494 * if fetch_count % 4 != 0, because on the last iteration
1495 * a few of the 4 vertex fetches will be out of bounds */
1496 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1499 LLVMValueRef fetch_ptr
;
1500 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1501 &true_index
, 1, "");
1502 true_index
= LLVMBuildLoad(builder
, fetch_ptr
, "fetch_elt");
1505 system_values
.vertex_id
= LLVMBuildInsertElement(gallivm
->builder
,
1506 system_values
.vertex_id
, true_index
,
1507 lp_build_const_int32(gallivm
, i
), "");
1508 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1509 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1510 LLVMValueRef vb_index
=
1511 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1512 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1513 generate_fetch(gallivm
, vbuffers_ptr
,
1514 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1515 system_values
.instance_id
);
1518 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1519 draw
->pt
.nr_vertex_elements
, vs_type
);
1521 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1522 generate_vs(variant
,
1530 variant
->key
.clamp_vertex_color
);
1532 if (pos
!= -1 && cv
!= -1) {
1533 /* store original positions in clip before further manipulation */
1534 store_clip(gallivm
, vs_type
, io
, outputs
, 0, cv
);
1535 store_clip(gallivm
, vs_type
, io
, outputs
, 1, pos
);
1538 if (enable_cliptest
) {
1539 LLVMValueRef temp
= LLVMBuildLoad(builder
, clipmask_bool_ptr
, "");
1540 /* allocate clipmask, assign it integer type */
1541 clipmask
= generate_clipmask(llvm
,
1545 variant
->key
.clip_xy
,
1546 variant
->key
.clip_z
,
1547 variant
->key
.clip_user
,
1548 variant
->key
.clip_halfz
,
1549 variant
->key
.ucp_enable
,
1550 context_ptr
, &have_clipdist
);
1551 temp
= LLVMBuildOr(builder
, clipmask
, temp
, "");
1552 /* store temporary clipping boolean value */
1553 LLVMBuildStore(builder
, temp
, clipmask_bool_ptr
);
1556 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1559 /* do viewport mapping */
1560 if (!bypass_viewport
) {
1561 generate_viewport(variant
, builder
, vs_type
, outputs
, context_ptr
);
1565 clipmask
= lp_build_const_int_vec(gallivm
, lp_int_type(vs_type
), 0);
1568 /* store clipmask in vertex header,
1569 * original positions in clip
1570 * and transformed positions in data
1572 convert_to_aos(gallivm
, io
, NULL
, outputs
, clipmask
,
1573 vs_info
->num_outputs
, vs_type
,
1577 lp_build_loop_end_cond(&lp_loop
, end
, step
, LLVMIntUGE
);
1579 sampler
->destroy(sampler
);
1581 /* return clipping boolean value for function */
1582 ret
= clipmask_booli32(gallivm
, vs_type
, clipmask_bool_ptr
);
1584 LLVMBuildRet(builder
, ret
);
1586 gallivm_verify_function(gallivm
, variant_func
);
1590 struct draw_llvm_variant_key
*
1591 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1594 struct draw_llvm_variant_key
*key
;
1595 struct draw_sampler_static_state
*draw_sampler
;
1597 key
= (struct draw_llvm_variant_key
*)store
;
1599 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1601 /* Presumably all variants of the shader should have the same
1602 * number of vertex elements - ie the number of shader inputs.
1603 * NOTE: we NEED to store the needed number of needed inputs
1604 * here, not the number of provided elements to match keysize
1605 * (and the offset of sampler state in the key).
1607 key
->nr_vertex_elements
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
1608 assert(key
->nr_vertex_elements
<= llvm
->draw
->pt
.nr_vertex_elements
);
1610 /* will have to rig this up properly later */
1611 key
->clip_xy
= llvm
->draw
->clip_xy
;
1612 key
->clip_z
= llvm
->draw
->clip_z
;
1613 key
->clip_user
= llvm
->draw
->clip_user
;
1614 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1615 key
->clip_halfz
= !llvm
->draw
->rasterizer
->gl_rasterization_rules
;
1616 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1617 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1618 key
->has_gs
= llvm
->draw
->gs
.geometry_shader
!= NULL
;
1621 /* All variants of this shader will have the same value for
1622 * nr_samplers. Not yet trying to compact away holes in the
1625 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1626 if (llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
1627 key
->nr_sampler_views
=
1628 llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
1631 key
->nr_sampler_views
= key
->nr_samplers
;
1634 draw_sampler
= draw_llvm_variant_key_samplers(key
);
1636 memcpy(key
->vertex_element
,
1637 llvm
->draw
->pt
.vertex_element
,
1638 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1640 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
1642 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1643 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
1644 llvm
->draw
->samplers
[PIPE_SHADER_VERTEX
][i
]);
1646 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1647 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
1648 llvm
->draw
->sampler_views
[PIPE_SHADER_VERTEX
][i
]);
1656 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
)
1659 struct draw_sampler_static_state
*sampler
= draw_llvm_variant_key_samplers(key
);
1661 debug_printf("clamp_vertex_color = %u\n", key
->clamp_vertex_color
);
1662 debug_printf("clip_xy = %u\n", key
->clip_xy
);
1663 debug_printf("clip_z = %u\n", key
->clip_z
);
1664 debug_printf("clip_user = %u\n", key
->clip_user
);
1665 debug_printf("bypass_viewport = %u\n", key
->bypass_viewport
);
1666 debug_printf("clip_halfz = %u\n", key
->clip_halfz
);
1667 debug_printf("need_edgeflags = %u\n", key
->need_edgeflags
);
1668 debug_printf("has_gs = %u\n", key
->has_gs
);
1669 debug_printf("ucp_enable = %u\n", key
->ucp_enable
);
1671 for (i
= 0 ; i
< key
->nr_vertex_elements
; i
++) {
1672 debug_printf("vertex_element[%i].src_offset = %u\n", i
, key
->vertex_element
[i
].src_offset
);
1673 debug_printf("vertex_element[%i].instance_divisor = %u\n", i
, key
->vertex_element
[i
].instance_divisor
);
1674 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i
, key
->vertex_element
[i
].vertex_buffer_index
);
1675 debug_printf("vertex_element[%i].src_format = %s\n", i
, util_format_name(key
->vertex_element
[i
].src_format
));
1678 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
1679 debug_printf("sampler[%i].src_format = %s\n", i
, util_format_name(sampler
[i
].texture_state
.format
));
1685 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1686 unsigned shader_stage
,
1688 uint32_t width
, uint32_t height
, uint32_t depth
,
1689 uint32_t first_level
, uint32_t last_level
,
1690 const void *base_ptr
,
1691 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1692 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1693 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
])
1696 struct draw_jit_texture
*jit_tex
;
1698 assert(shader_stage
== PIPE_SHADER_VERTEX
||
1699 shader_stage
== PIPE_SHADER_GEOMETRY
);
1701 if (shader_stage
== PIPE_SHADER_VERTEX
) {
1702 assert(sview_idx
< Elements(draw
->llvm
->jit_context
.textures
));
1704 jit_tex
= &draw
->llvm
->jit_context
.textures
[sview_idx
];
1705 } else if (shader_stage
== PIPE_SHADER_GEOMETRY
) {
1706 assert(sview_idx
< Elements(draw
->llvm
->gs_jit_context
.textures
));
1708 jit_tex
= &draw
->llvm
->gs_jit_context
.textures
[sview_idx
];
1711 jit_tex
->width
= width
;
1712 jit_tex
->height
= height
;
1713 jit_tex
->depth
= depth
;
1714 jit_tex
->first_level
= first_level
;
1715 jit_tex
->last_level
= last_level
;
1716 jit_tex
->base
= base_ptr
;
1718 for (j
= first_level
; j
<= last_level
; j
++) {
1719 jit_tex
->mip_offsets
[j
] = mip_offsets
[j
];
1720 jit_tex
->row_stride
[j
] = row_stride
[j
];
1721 jit_tex
->img_stride
[j
] = img_stride
[j
];
1727 draw_llvm_set_sampler_state(struct draw_context
*draw
,
1728 unsigned shader_type
)
1732 if (shader_type
== PIPE_SHADER_VERTEX
) {
1733 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_VERTEX
]; i
++) {
1734 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->jit_context
.samplers
[i
];
1736 if (draw
->samplers
[i
]) {
1737 const struct pipe_sampler_state
*s
1738 = draw
->samplers
[PIPE_SHADER_VERTEX
][i
];
1739 jit_sam
->min_lod
= s
->min_lod
;
1740 jit_sam
->max_lod
= s
->max_lod
;
1741 jit_sam
->lod_bias
= s
->lod_bias
;
1742 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1745 } else if (shader_type
== PIPE_SHADER_GEOMETRY
) {
1746 for (i
= 0; i
< draw
->num_samplers
[PIPE_SHADER_GEOMETRY
]; i
++) {
1747 struct draw_jit_sampler
*jit_sam
= &draw
->llvm
->gs_jit_context
.samplers
[i
];
1749 if (draw
->samplers
[i
]) {
1750 const struct pipe_sampler_state
*s
1751 = draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
];
1752 jit_sam
->min_lod
= s
->min_lod
;
1753 jit_sam
->max_lod
= s
->max_lod
;
1754 jit_sam
->lod_bias
= s
->lod_bias
;
1755 COPY_4V(jit_sam
->border_color
, s
->border_color
.f
);
1763 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
1765 struct draw_llvm
*llvm
= variant
->llvm
;
1767 if (variant
->function_elts
) {
1768 gallivm_free_function(variant
->gallivm
,
1769 variant
->function_elts
, variant
->jit_func_elts
);
1772 if (variant
->function
) {
1773 gallivm_free_function(variant
->gallivm
,
1774 variant
->function
, variant
->jit_func
);
1777 gallivm_destroy(variant
->gallivm
);
1779 remove_from_list(&variant
->list_item_local
);
1780 variant
->shader
->variants_cached
--;
1781 remove_from_list(&variant
->list_item_global
);
1782 llvm
->nr_variants
--;
1788 * Create LLVM types for various structures.
1791 create_gs_jit_types(struct draw_gs_llvm_variant
*var
)
1793 struct gallivm_state
*gallivm
= var
->gallivm
;
1794 LLVMTypeRef texture_type
, sampler_type
, context_type
;
1796 texture_type
= create_jit_texture_type(gallivm
, "texture");
1797 sampler_type
= create_jit_sampler_type(gallivm
, "sampler");
1799 context_type
= create_gs_jit_context_type(gallivm
,
1800 var
->shader
->base
.vector_length
,
1801 texture_type
, sampler_type
,
1802 "draw_gs_jit_context");
1803 var
->context_ptr_type
= LLVMPointerType(context_type
, 0);
1805 var
->input_array_type
= create_gs_jit_input_type(gallivm
);
1809 get_gs_context_ptr_type(struct draw_gs_llvm_variant
*variant
)
1811 if (!variant
->context_ptr_type
)
1812 create_gs_jit_types(variant
);
1813 return variant
->context_ptr_type
;
1817 generate_mask_value(struct draw_gs_llvm_variant
*variant
,
1818 struct lp_type gs_type
)
1820 struct gallivm_state
*gallivm
= variant
->gallivm
;
1821 LLVMBuilderRef builder
= gallivm
->builder
;
1822 LLVMValueRef bits
[16];
1823 struct lp_type mask_type
= lp_int_type(gs_type
);
1824 struct lp_type mask_elem_type
= lp_elem_type(mask_type
);
1825 LLVMValueRef mask_val
= lp_build_const_vec(gallivm
,
1830 assert(gs_type
.length
<= Elements(bits
));
1832 for (i
= gs_type
.length
; i
>= 1; --i
) {
1834 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1835 bits
[idx
] = lp_build_compare(gallivm
,
1836 mask_elem_type
, PIPE_FUNC_GEQUAL
,
1837 variant
->num_prims
, ind
);
1839 for (i
= 0; i
< gs_type
.length
; ++i
) {
1840 LLVMValueRef ind
= lp_build_const_int32(gallivm
, i
);
1841 mask_val
= LLVMBuildInsertElement(builder
, mask_val
, bits
[i
], ind
, "");
1843 mask_val
= lp_build_compare(gallivm
,
1844 mask_type
, PIPE_FUNC_NOTEQUAL
,
1846 lp_build_const_int_vec(gallivm
, mask_type
, 0));
1852 draw_gs_llvm_generate(struct draw_llvm
*llvm
,
1853 struct draw_gs_llvm_variant
*variant
)
1855 struct gallivm_state
*gallivm
= variant
->gallivm
;
1856 LLVMContextRef context
= gallivm
->context
;
1857 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1858 LLVMTypeRef arg_types
[5];
1859 LLVMTypeRef func_type
;
1860 LLVMValueRef variant_func
;
1861 LLVMValueRef context_ptr
;
1862 LLVMBasicBlockRef block
;
1863 LLVMBuilderRef builder
;
1864 LLVMValueRef io_ptr
, input_array
, num_prims
, mask_val
;
1865 struct lp_build_sampler_soa
*sampler
= 0;
1866 struct lp_build_context bld
;
1867 struct lp_bld_tgsi_system_values system_values
;
1868 struct lp_type gs_type
;
1870 struct lp_build_tgsi_gs_iface gs_iface
;
1871 const struct tgsi_token
*tokens
= variant
->shader
->base
.state
.tokens
;
1872 LLVMValueRef consts_ptr
;
1873 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1874 struct lp_build_mask_context mask
;
1876 memset(&system_values
, 0, sizeof(system_values
));
1878 assert(variant
->vertex_header_ptr_type
);
1880 arg_types
[0] = get_gs_context_ptr_type(variant
); /* context */
1881 arg_types
[1] = variant
->input_array_type
; /* input */
1882 arg_types
[2] = variant
->vertex_header_ptr_type
; /* vertex_header */
1883 arg_types
[3] = int32_type
; /* num_prims */
1884 arg_types
[4] = int32_type
; /* instance_id */
1886 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
1888 variant_func
= LLVMAddFunction(gallivm
->module
, "draw_geometry_shader",
1890 variant
->function
= variant_func
;
1892 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1894 for (i
= 0; i
< Elements(arg_types
); ++i
)
1895 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1896 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1897 LLVMNoAliasAttribute
);
1899 context_ptr
= LLVMGetParam(variant_func
, 0);
1900 input_array
= LLVMGetParam(variant_func
, 1);
1901 io_ptr
= LLVMGetParam(variant_func
, 2);
1902 num_prims
= LLVMGetParam(variant_func
, 3);
1903 system_values
.instance_id
= LLVMGetParam(variant_func
, 4);
1905 lp_build_name(context_ptr
, "context");
1906 lp_build_name(input_array
, "input");
1907 lp_build_name(io_ptr
, "io");
1908 lp_build_name(io_ptr
, "num_prims");
1909 lp_build_name(system_values
.instance_id
, "instance_id");
1911 variant
->context_ptr
= context_ptr
;
1912 variant
->io_ptr
= io_ptr
;
1913 variant
->num_prims
= num_prims
;
1915 gs_iface
.input
= input_array
;
1916 gs_iface
.emit_vertex
= draw_gs_llvm_emit_vertex
;
1917 gs_iface
.end_primitive
= draw_gs_llvm_end_primitive
;
1918 gs_iface
.gs_epilogue
= draw_gs_llvm_epilogue
;
1919 gs_iface
.user_data
= variant
;
1925 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1926 builder
= gallivm
->builder
;
1927 LLVMPositionBuilderAtEnd(builder
, block
);
1929 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1931 memset(&gs_type
, 0, sizeof gs_type
);
1932 gs_type
.floating
= TRUE
; /* floating point values */
1933 gs_type
.sign
= TRUE
; /* values are signed */
1934 gs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
1935 gs_type
.width
= 32; /* 32-bit float */
1936 gs_type
.length
= variant
->shader
->base
.vector_length
;
1938 consts_ptr
= draw_gs_jit_context_constants(variant
->gallivm
, context_ptr
);
1940 /* code generated texture sampling */
1941 sampler
= draw_llvm_sampler_soa_create(variant
->key
.samplers
,
1944 mask_val
= generate_mask_value(variant
, gs_type
);
1945 lp_build_mask_begin(&mask
, gallivm
, gs_type
, mask_val
);
1947 lp_build_tgsi_soa(variant
->gallivm
,
1957 &llvm
->draw
->gs
.geometry_shader
->info
,
1960 sampler
->destroy(sampler
);
1962 lp_build_mask_end(&mask
);
1964 LLVMBuildRet(builder
, lp_build_zero(gallivm
, lp_type_uint(32)));
1966 gallivm_verify_function(gallivm
, variant_func
);
1970 struct draw_gs_llvm_variant
*
1971 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
1972 unsigned num_outputs
,
1973 const struct draw_gs_llvm_variant_key
*key
)
1975 struct draw_gs_llvm_variant
*variant
;
1976 struct llvm_geometry_shader
*shader
=
1977 llvm_geometry_shader(llvm
->draw
->gs
.geometry_shader
);
1978 LLVMTypeRef vertex_header
;
1980 variant
= MALLOC(sizeof *variant
+
1981 shader
->variant_key_size
-
1982 sizeof variant
->key
);
1983 if (variant
== NULL
)
1986 variant
->llvm
= llvm
;
1987 variant
->shader
= shader
;
1989 variant
->gallivm
= gallivm_create();
1991 create_gs_jit_types(variant
);
1993 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
1995 vertex_header
= create_jit_vertex_header(variant
->gallivm
, num_outputs
);
1997 variant
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
1999 draw_gs_llvm_generate(llvm
, variant
);
2001 gallivm_compile_module(variant
->gallivm
);
2003 variant
->jit_func
= (draw_gs_jit_func
)
2004 gallivm_jit_function(variant
->gallivm
, variant
->function
);
2006 variant
->list_item_global
.base
= variant
;
2007 variant
->list_item_local
.base
= variant
;
2008 /*variant->no = */shader
->variants_created
++;
2009 variant
->list_item_global
.base
= variant
;
2015 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
)
2017 struct draw_llvm
*llvm
= variant
->llvm
;
2019 if (variant
->function
) {
2020 gallivm_free_function(variant
->gallivm
,
2021 variant
->function
, variant
->jit_func
);
2024 gallivm_destroy(variant
->gallivm
);
2026 remove_from_list(&variant
->list_item_local
);
2027 variant
->shader
->variants_cached
--;
2028 remove_from_list(&variant
->list_item_global
);
2029 llvm
->nr_gs_variants
--;
2033 struct draw_gs_llvm_variant_key
*
2034 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
2037 struct draw_gs_llvm_variant_key
*key
;
2038 struct draw_sampler_static_state
*draw_sampler
;
2040 key
= (struct draw_gs_llvm_variant_key
*)store
;
2042 /* All variants of this shader will have the same value for
2043 * nr_samplers. Not yet trying to compact away holes in the
2046 key
->nr_samplers
= llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
2047 if (llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
2048 key
->nr_sampler_views
=
2049 llvm
->draw
->gs
.geometry_shader
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
2052 key
->nr_sampler_views
= key
->nr_samplers
;
2055 draw_sampler
= key
->samplers
;
2057 memset(draw_sampler
, 0, MAX2(key
->nr_samplers
, key
->nr_sampler_views
) * sizeof *draw_sampler
);
2059 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
2060 lp_sampler_static_sampler_state(&draw_sampler
[i
].sampler_state
,
2061 llvm
->draw
->samplers
[PIPE_SHADER_GEOMETRY
][i
]);
2063 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2064 lp_sampler_static_texture_state(&draw_sampler
[i
].texture_state
,
2065 llvm
->draw
->sampler_views
[PIPE_SHADER_GEOMETRY
][i
]);
2072 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
)
2075 struct draw_sampler_static_state
*sampler
= key
->samplers
;
2077 for (i
= 0 ; i
< key
->nr_sampler_views
; i
++) {
2078 debug_printf("sampler[%i].src_format = %s\n", i
,
2079 util_format_name(sampler
[i
].texture_state
.format
));