1848b8605Smrg/************************************************************************** 2848b8605Smrg * 3848b8605Smrg * Copyright 2010 VMware, Inc. 4848b8605Smrg * All Rights Reserved. 5848b8605Smrg * 6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a 7848b8605Smrg * copy of this software and associated documentation files (the 8848b8605Smrg * "Software"), to deal in the Software without restriction, including 9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish, 10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to 11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to 12848b8605Smrg * the following conditions: 13848b8605Smrg * 14848b8605Smrg * The above copyright notice and this permission notice (including the 15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions 16848b8605Smrg * of the Software. 17848b8605Smrg * 18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21848b8605Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25848b8605Smrg * 26848b8605Smrg **************************************************************************/ 27848b8605Smrg 28848b8605Smrg#include "draw_llvm.h" 29848b8605Smrg 30848b8605Smrg#include "draw_context.h" 31848b8605Smrg#include "draw_vs.h" 32848b8605Smrg#include "draw_gs.h" 33848b8605Smrg 34848b8605Smrg#include "gallivm/lp_bld_arit.h" 35848b8605Smrg#include "gallivm/lp_bld_arit_overflow.h" 36b8e80941Smrg#include "gallivm/lp_bld_bitarit.h" 37b8e80941Smrg#include "gallivm/lp_bld_gather.h" 38848b8605Smrg#include "gallivm/lp_bld_logic.h" 39848b8605Smrg#include "gallivm/lp_bld_const.h" 40848b8605Smrg#include "gallivm/lp_bld_swizzle.h" 41848b8605Smrg#include "gallivm/lp_bld_struct.h" 42848b8605Smrg#include "gallivm/lp_bld_type.h" 43848b8605Smrg#include "gallivm/lp_bld_flow.h" 44848b8605Smrg#include "gallivm/lp_bld_debug.h" 45848b8605Smrg#include "gallivm/lp_bld_tgsi.h" 46848b8605Smrg#include "gallivm/lp_bld_printf.h" 47848b8605Smrg#include "gallivm/lp_bld_intr.h" 48848b8605Smrg#include "gallivm/lp_bld_init.h" 49848b8605Smrg#include "gallivm/lp_bld_type.h" 50848b8605Smrg#include "gallivm/lp_bld_pack.h" 51848b8605Smrg#include "gallivm/lp_bld_format.h" 52848b8605Smrg 53848b8605Smrg#include "tgsi/tgsi_exec.h" 54848b8605Smrg#include "tgsi/tgsi_dump.h" 55848b8605Smrg 56848b8605Smrg#include "util/u_math.h" 57848b8605Smrg#include "util/u_pointer.h" 58848b8605Smrg#include "util/u_string.h" 59b8e80941Smrg#include "util/simple_list.h" 60848b8605Smrg 61848b8605Smrg 62848b8605Smrg#define DEBUG_STORE 0 63848b8605Smrg 64848b8605Smrg 65848b8605Smrgstatic void 66b8e80941Smrgdraw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *var); 67848b8605Smrg 68848b8605Smrg 69848b8605Smrgstruct draw_gs_llvm_iface { 70848b8605Smrg struct lp_build_tgsi_gs_iface base; 71848b8605Smrg 72848b8605Smrg struct draw_gs_llvm_variant *variant; 73848b8605Smrg LLVMValueRef input; 74848b8605Smrg}; 75848b8605Smrg 76b8e80941Smrgstatic inline const struct draw_gs_llvm_iface * 77848b8605Smrgdraw_gs_llvm_iface(const struct lp_build_tgsi_gs_iface *iface) 78848b8605Smrg{ 79848b8605Smrg return (const struct draw_gs_llvm_iface *)iface; 80848b8605Smrg} 81848b8605Smrg 82848b8605Smrg/** 83848b8605Smrg * Create LLVM type for draw_vertex_buffer. 84848b8605Smrg */ 85848b8605Smrgstatic LLVMTypeRef 86848b8605Smrgcreate_jit_dvbuffer_type(struct gallivm_state *gallivm, 87848b8605Smrg const char *struct_name) 88848b8605Smrg{ 89848b8605Smrg LLVMTargetDataRef target = gallivm->target; 90848b8605Smrg LLVMTypeRef dvbuffer_type; 91848b8605Smrg LLVMTypeRef elem_types[DRAW_JIT_DVBUFFER_NUM_FIELDS]; 92848b8605Smrg LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context); 93848b8605Smrg 94848b8605Smrg elem_types[DRAW_JIT_DVBUFFER_MAP] = 95848b8605Smrg LLVMPointerType(LLVMIntTypeInContext(gallivm->context, 8), 0); 96848b8605Smrg elem_types[DRAW_JIT_DVBUFFER_SIZE] = int32_type; 97848b8605Smrg 98848b8605Smrg dvbuffer_type = LLVMStructTypeInContext(gallivm->context, elem_types, 99b8e80941Smrg ARRAY_SIZE(elem_types), 0); 100848b8605Smrg 101b8e80941Smrg (void) target; /* silence unused var warning for non-debug build */ 102848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer, map, 103848b8605Smrg target, dvbuffer_type, 104848b8605Smrg DRAW_JIT_DVBUFFER_MAP); 105848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer, size, 106848b8605Smrg target, dvbuffer_type, 107848b8605Smrg DRAW_JIT_DVBUFFER_SIZE); 108848b8605Smrg 109848b8605Smrg return dvbuffer_type; 110848b8605Smrg} 111848b8605Smrg 112848b8605Smrg/** 113848b8605Smrg * Create LLVM type for struct draw_jit_texture 114848b8605Smrg */ 115848b8605Smrgstatic LLVMTypeRef 116848b8605Smrgcreate_jit_texture_type(struct gallivm_state *gallivm, const char *struct_name) 117848b8605Smrg{ 118848b8605Smrg LLVMTargetDataRef target = gallivm->target; 119848b8605Smrg LLVMTypeRef texture_type; 120848b8605Smrg LLVMTypeRef elem_types[DRAW_JIT_TEXTURE_NUM_FIELDS]; 121848b8605Smrg LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context); 122848b8605Smrg 123848b8605Smrg elem_types[DRAW_JIT_TEXTURE_WIDTH] = 124848b8605Smrg elem_types[DRAW_JIT_TEXTURE_HEIGHT] = 125848b8605Smrg elem_types[DRAW_JIT_TEXTURE_DEPTH] = 126848b8605Smrg elem_types[DRAW_JIT_TEXTURE_FIRST_LEVEL] = 127848b8605Smrg elem_types[DRAW_JIT_TEXTURE_LAST_LEVEL] = int32_type; 128848b8605Smrg elem_types[DRAW_JIT_TEXTURE_BASE] = 129848b8605Smrg LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0); 130848b8605Smrg elem_types[DRAW_JIT_TEXTURE_ROW_STRIDE] = 131848b8605Smrg elem_types[DRAW_JIT_TEXTURE_IMG_STRIDE] = 132848b8605Smrg elem_types[DRAW_JIT_TEXTURE_MIP_OFFSETS] = 133848b8605Smrg LLVMArrayType(int32_type, PIPE_MAX_TEXTURE_LEVELS); 134848b8605Smrg 135848b8605Smrg texture_type = LLVMStructTypeInContext(gallivm->context, elem_types, 136b8e80941Smrg ARRAY_SIZE(elem_types), 0); 137848b8605Smrg 138b8e80941Smrg (void) target; /* silence unused var warning for non-debug build */ 139848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, width, 140848b8605Smrg target, texture_type, 141848b8605Smrg DRAW_JIT_TEXTURE_WIDTH); 142848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, height, 143848b8605Smrg target, texture_type, 144848b8605Smrg DRAW_JIT_TEXTURE_HEIGHT); 145848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, depth, 146848b8605Smrg target, texture_type, 147848b8605Smrg DRAW_JIT_TEXTURE_DEPTH); 148848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, first_level, 149848b8605Smrg target, texture_type, 150848b8605Smrg DRAW_JIT_TEXTURE_FIRST_LEVEL); 151848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, last_level, 152848b8605Smrg target, texture_type, 153848b8605Smrg DRAW_JIT_TEXTURE_LAST_LEVEL); 154848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, base, 155848b8605Smrg target, texture_type, 156848b8605Smrg DRAW_JIT_TEXTURE_BASE); 157848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, row_stride, 158848b8605Smrg target, texture_type, 159848b8605Smrg DRAW_JIT_TEXTURE_ROW_STRIDE); 160848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, img_stride, 161848b8605Smrg target, texture_type, 162848b8605Smrg DRAW_JIT_TEXTURE_IMG_STRIDE); 163848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, mip_offsets, 164848b8605Smrg target, texture_type, 165848b8605Smrg DRAW_JIT_TEXTURE_MIP_OFFSETS); 166848b8605Smrg 167848b8605Smrg LP_CHECK_STRUCT_SIZE(struct draw_jit_texture, target, texture_type); 168848b8605Smrg 169848b8605Smrg return texture_type; 170848b8605Smrg} 171848b8605Smrg 172848b8605Smrg 173848b8605Smrg/** 174848b8605Smrg * Create LLVM type for struct draw_jit_sampler 175848b8605Smrg */ 176848b8605Smrgstatic LLVMTypeRef 177848b8605Smrgcreate_jit_sampler_type(struct gallivm_state *gallivm, const char *struct_name) 178848b8605Smrg{ 179848b8605Smrg LLVMTargetDataRef target = gallivm->target; 180848b8605Smrg LLVMTypeRef sampler_type; 181848b8605Smrg LLVMTypeRef elem_types[DRAW_JIT_SAMPLER_NUM_FIELDS]; 182848b8605Smrg 183848b8605Smrg elem_types[DRAW_JIT_SAMPLER_MIN_LOD] = 184848b8605Smrg elem_types[DRAW_JIT_SAMPLER_MAX_LOD] = 185848b8605Smrg elem_types[DRAW_JIT_SAMPLER_LOD_BIAS] = LLVMFloatTypeInContext(gallivm->context); 186848b8605Smrg elem_types[DRAW_JIT_SAMPLER_BORDER_COLOR] = 187848b8605Smrg LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4); 188848b8605Smrg 189848b8605Smrg sampler_type = LLVMStructTypeInContext(gallivm->context, elem_types, 190b8e80941Smrg ARRAY_SIZE(elem_types), 0); 191848b8605Smrg 192b8e80941Smrg (void) target; /* silence unused var warning for non-debug build */ 193848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, min_lod, 194848b8605Smrg target, sampler_type, 195848b8605Smrg DRAW_JIT_SAMPLER_MIN_LOD); 196848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, max_lod, 197848b8605Smrg target, sampler_type, 198848b8605Smrg DRAW_JIT_SAMPLER_MAX_LOD); 199848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, lod_bias, 200848b8605Smrg target, sampler_type, 201848b8605Smrg DRAW_JIT_SAMPLER_LOD_BIAS); 202848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, border_color, 203848b8605Smrg target, sampler_type, 204848b8605Smrg DRAW_JIT_SAMPLER_BORDER_COLOR); 205848b8605Smrg 206848b8605Smrg LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler, target, sampler_type); 207848b8605Smrg 208848b8605Smrg return sampler_type; 209848b8605Smrg} 210848b8605Smrg 211848b8605Smrg 212848b8605Smrg/** 213848b8605Smrg * Create LLVM type for struct draw_jit_context 214848b8605Smrg */ 215848b8605Smrgstatic LLVMTypeRef 216848b8605Smrgcreate_jit_context_type(struct gallivm_state *gallivm, 217848b8605Smrg LLVMTypeRef texture_type, LLVMTypeRef sampler_type, 218848b8605Smrg const char *struct_name) 219848b8605Smrg{ 220848b8605Smrg LLVMTargetDataRef target = gallivm->target; 221848b8605Smrg LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 222848b8605Smrg LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context); 223848b8605Smrg LLVMTypeRef elem_types[DRAW_JIT_CTX_NUM_FIELDS]; 224848b8605Smrg LLVMTypeRef context_type; 225848b8605Smrg 226848b8605Smrg elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* vs_constants */ 227848b8605Smrg LP_MAX_TGSI_CONST_BUFFERS); 228848b8605Smrg elem_types[1] = LLVMArrayType(int_type, /* num_vs_constants */ 229848b8605Smrg LP_MAX_TGSI_CONST_BUFFERS); 230848b8605Smrg elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4), 231848b8605Smrg DRAW_TOTAL_CLIP_PLANES), 0); 232b8e80941Smrg elem_types[3] = LLVMPointerType(float_type, 0); /* viewports */ 233848b8605Smrg elem_types[4] = LLVMArrayType(texture_type, 234848b8605Smrg PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */ 235848b8605Smrg elem_types[5] = LLVMArrayType(sampler_type, 236848b8605Smrg PIPE_MAX_SAMPLERS); /* samplers */ 237848b8605Smrg context_type = LLVMStructTypeInContext(gallivm->context, elem_types, 238b8e80941Smrg ARRAY_SIZE(elem_types), 0); 239b8e80941Smrg 240b8e80941Smrg (void) target; /* silence unused var warning for non-debug build */ 241848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, vs_constants, 242848b8605Smrg target, context_type, DRAW_JIT_CTX_CONSTANTS); 243848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, num_vs_constants, 244848b8605Smrg target, context_type, DRAW_JIT_CTX_NUM_CONSTANTS); 245848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, planes, 246848b8605Smrg target, context_type, DRAW_JIT_CTX_PLANES); 247b8e80941Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, viewports, 248848b8605Smrg target, context_type, DRAW_JIT_CTX_VIEWPORT); 249848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, textures, 250848b8605Smrg target, context_type, 251848b8605Smrg DRAW_JIT_CTX_TEXTURES); 252848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, samplers, 253848b8605Smrg target, context_type, 254848b8605Smrg DRAW_JIT_CTX_SAMPLERS); 255848b8605Smrg LP_CHECK_STRUCT_SIZE(struct draw_jit_context, 256848b8605Smrg target, context_type); 257848b8605Smrg 258848b8605Smrg return context_type; 259848b8605Smrg} 260848b8605Smrg 261848b8605Smrg 262848b8605Smrg/** 263848b8605Smrg * Create LLVM type for struct draw_gs_jit_context 264848b8605Smrg */ 265848b8605Smrgstatic LLVMTypeRef 266848b8605Smrgcreate_gs_jit_context_type(struct gallivm_state *gallivm, 267848b8605Smrg unsigned vector_length, 268848b8605Smrg LLVMTypeRef texture_type, LLVMTypeRef sampler_type, 269848b8605Smrg const char *struct_name) 270848b8605Smrg{ 271848b8605Smrg LLVMTargetDataRef target = gallivm->target; 272848b8605Smrg LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 273848b8605Smrg LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context); 274848b8605Smrg LLVMTypeRef elem_types[DRAW_GS_JIT_CTX_NUM_FIELDS]; 275848b8605Smrg LLVMTypeRef context_type; 276848b8605Smrg 277848b8605Smrg elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* constants */ 278848b8605Smrg LP_MAX_TGSI_CONST_BUFFERS); 279848b8605Smrg elem_types[1] = LLVMArrayType(int_type, /* num_constants */ 280848b8605Smrg LP_MAX_TGSI_CONST_BUFFERS); 281848b8605Smrg elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4), 282848b8605Smrg DRAW_TOTAL_CLIP_PLANES), 0); 283b8e80941Smrg elem_types[3] = LLVMPointerType(float_type, 0); /* viewports */ 284848b8605Smrg 285848b8605Smrg elem_types[4] = LLVMArrayType(texture_type, 286848b8605Smrg PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */ 287848b8605Smrg elem_types[5] = LLVMArrayType(sampler_type, 288848b8605Smrg PIPE_MAX_SAMPLERS); /* samplers */ 289848b8605Smrg 290848b8605Smrg elem_types[6] = LLVMPointerType(LLVMPointerType(int_type, 0), 0); 291848b8605Smrg elem_types[7] = LLVMPointerType(LLVMVectorType(int_type, 292848b8605Smrg vector_length), 0); 293848b8605Smrg elem_types[8] = LLVMPointerType(LLVMVectorType(int_type, 294848b8605Smrg vector_length), 0); 295848b8605Smrg 296848b8605Smrg context_type = LLVMStructTypeInContext(gallivm->context, elem_types, 297b8e80941Smrg ARRAY_SIZE(elem_types), 0); 298848b8605Smrg 299b8e80941Smrg (void) target; /* silence unused var warning for non-debug build */ 300848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, constants, 301848b8605Smrg target, context_type, DRAW_GS_JIT_CTX_CONSTANTS); 302848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, num_constants, 303848b8605Smrg target, context_type, DRAW_GS_JIT_CTX_NUM_CONSTANTS); 304848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, planes, 305848b8605Smrg target, context_type, DRAW_GS_JIT_CTX_PLANES); 306b8e80941Smrg LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, viewports, 307848b8605Smrg target, context_type, DRAW_GS_JIT_CTX_VIEWPORT); 308848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, textures, 309848b8605Smrg target, context_type, 310848b8605Smrg DRAW_GS_JIT_CTX_TEXTURES); 311848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, samplers, 312848b8605Smrg target, context_type, 313848b8605Smrg DRAW_GS_JIT_CTX_SAMPLERS); 314848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, prim_lengths, 315848b8605Smrg target, context_type, 316848b8605Smrg DRAW_GS_JIT_CTX_PRIM_LENGTHS); 317848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, emitted_vertices, 318848b8605Smrg target, context_type, 319848b8605Smrg DRAW_GS_JIT_CTX_EMITTED_VERTICES); 320848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, emitted_prims, 321848b8605Smrg target, context_type, 322848b8605Smrg DRAW_GS_JIT_CTX_EMITTED_PRIMS); 323848b8605Smrg LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context, 324848b8605Smrg target, context_type); 325848b8605Smrg 326848b8605Smrg return context_type; 327848b8605Smrg} 328848b8605Smrg 329848b8605Smrg 330848b8605Smrgstatic LLVMTypeRef 331848b8605Smrgcreate_gs_jit_input_type(struct gallivm_state *gallivm) 332848b8605Smrg{ 333848b8605Smrg LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 334848b8605Smrg LLVMTypeRef input_array; 335848b8605Smrg 336848b8605Smrg input_array = LLVMVectorType(float_type, TGSI_NUM_CHANNELS); /* num primitives */ 337848b8605Smrg input_array = LLVMArrayType(input_array, TGSI_NUM_CHANNELS); /* num channels */ 338848b8605Smrg input_array = LLVMArrayType(input_array, PIPE_MAX_SHADER_INPUTS); /* num attrs per vertex */ 339848b8605Smrg input_array = LLVMPointerType(input_array, 0); /* num vertices per prim */ 340848b8605Smrg 341848b8605Smrg return input_array; 342848b8605Smrg} 343848b8605Smrg 344848b8605Smrg/** 345848b8605Smrg * Create LLVM type for struct pipe_vertex_buffer 346848b8605Smrg */ 347848b8605Smrgstatic LLVMTypeRef 348848b8605Smrgcreate_jit_vertex_buffer_type(struct gallivm_state *gallivm, 349848b8605Smrg const char *struct_name) 350848b8605Smrg{ 351848b8605Smrg LLVMTargetDataRef target = gallivm->target; 352848b8605Smrg LLVMTypeRef elem_types[4]; 353848b8605Smrg LLVMTypeRef vb_type; 354848b8605Smrg 355b8e80941Smrg elem_types[0] = LLVMInt16TypeInContext(gallivm->context); 356b8e80941Smrg elem_types[1] = LLVMInt8TypeInContext(gallivm->context); 357b8e80941Smrg elem_types[2] = LLVMInt32TypeInContext(gallivm->context); 358848b8605Smrg elem_types[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0); 359848b8605Smrg 360848b8605Smrg vb_type = LLVMStructTypeInContext(gallivm->context, elem_types, 361b8e80941Smrg ARRAY_SIZE(elem_types), 0); 362848b8605Smrg 363b8e80941Smrg (void) target; /* silence unused var warning for non-debug build */ 364848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, stride, 365848b8605Smrg target, vb_type, 0); 366b8e80941Smrg LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, is_user_buffer, 367848b8605Smrg target, vb_type, 1); 368b8e80941Smrg LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, buffer_offset, 369b8e80941Smrg target, vb_type, 2); 370b8e80941Smrg LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, buffer.resource, 371b8e80941Smrg target, vb_type, 3); 372848b8605Smrg 373848b8605Smrg LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer, target, vb_type); 374848b8605Smrg 375848b8605Smrg return vb_type; 376848b8605Smrg} 377848b8605Smrg 378848b8605Smrg 379848b8605Smrg/** 380848b8605Smrg * Create LLVM type for struct vertex_header; 381848b8605Smrg */ 382848b8605Smrgstatic LLVMTypeRef 383848b8605Smrgcreate_jit_vertex_header(struct gallivm_state *gallivm, int data_elems) 384848b8605Smrg{ 385848b8605Smrg LLVMTargetDataRef target = gallivm->target; 386b8e80941Smrg LLVMTypeRef elem_types[3]; 387848b8605Smrg LLVMTypeRef vertex_header; 388848b8605Smrg char struct_name[24]; 389848b8605Smrg 390848b8605Smrg util_snprintf(struct_name, 23, "vertex_header%d", data_elems); 391848b8605Smrg 392848b8605Smrg elem_types[DRAW_JIT_VERTEX_VERTEX_ID] = LLVMIntTypeInContext(gallivm->context, 32); 393b8e80941Smrg elem_types[DRAW_JIT_VERTEX_CLIP_POS] = LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4); 394848b8605Smrg elem_types[DRAW_JIT_VERTEX_DATA] = LLVMArrayType(elem_types[1], data_elems); 395848b8605Smrg 396848b8605Smrg vertex_header = LLVMStructTypeInContext(gallivm->context, elem_types, 397b8e80941Smrg ARRAY_SIZE(elem_types), 0); 398848b8605Smrg 399848b8605Smrg /* these are bit-fields and we can't take address of them 400848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask, 401848b8605Smrg target, vertex_header, 402848b8605Smrg DRAW_JIT_VERTEX_CLIPMASK); 403848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag, 404848b8605Smrg target, vertex_header, 405848b8605Smrg DRAW_JIT_VERTEX_EDGEFLAG); 406848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad, 407848b8605Smrg target, vertex_header, 408848b8605Smrg DRAW_JIT_VERTEX_PAD); 409848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id, 410848b8605Smrg target, vertex_header, 411848b8605Smrg DRAW_JIT_VERTEX_VERTEX_ID); 412848b8605Smrg */ 413b8e80941Smrg (void) target; /* silence unused var warning for non-debug build */ 414b8e80941Smrg LP_CHECK_MEMBER_OFFSET(struct vertex_header, clip_pos, 415848b8605Smrg target, vertex_header, 416b8e80941Smrg DRAW_JIT_VERTEX_CLIP_POS); 417848b8605Smrg LP_CHECK_MEMBER_OFFSET(struct vertex_header, data, 418848b8605Smrg target, vertex_header, 419848b8605Smrg DRAW_JIT_VERTEX_DATA); 420848b8605Smrg 421848b8605Smrg assert(LLVMABISizeOfType(target, vertex_header) == 422848b8605Smrg offsetof(struct vertex_header, data[data_elems])); 423848b8605Smrg 424848b8605Smrg return vertex_header; 425848b8605Smrg} 426848b8605Smrg 427848b8605Smrg 428848b8605Smrg/** 429848b8605Smrg * Create LLVM types for various structures. 430848b8605Smrg */ 431848b8605Smrgstatic void 432848b8605Smrgcreate_jit_types(struct draw_llvm_variant *variant) 433848b8605Smrg{ 434848b8605Smrg struct gallivm_state *gallivm = variant->gallivm; 435848b8605Smrg LLVMTypeRef texture_type, sampler_type, context_type, buffer_type, 436848b8605Smrg vb_type; 437848b8605Smrg 438848b8605Smrg texture_type = create_jit_texture_type(gallivm, "texture"); 439848b8605Smrg sampler_type = create_jit_sampler_type(gallivm, "sampler"); 440848b8605Smrg 441848b8605Smrg context_type = create_jit_context_type(gallivm, texture_type, sampler_type, 442848b8605Smrg "draw_jit_context"); 443848b8605Smrg variant->context_ptr_type = LLVMPointerType(context_type, 0); 444848b8605Smrg 445848b8605Smrg buffer_type = create_jit_dvbuffer_type(gallivm, "draw_vertex_buffer"); 446848b8605Smrg variant->buffer_ptr_type = LLVMPointerType(buffer_type, 0); 447848b8605Smrg 448848b8605Smrg vb_type = create_jit_vertex_buffer_type(gallivm, "pipe_vertex_buffer"); 449848b8605Smrg variant->vb_ptr_type = LLVMPointerType(vb_type, 0); 450848b8605Smrg} 451848b8605Smrg 452848b8605Smrg 453848b8605Smrgstatic LLVMTypeRef 454848b8605Smrgget_context_ptr_type(struct draw_llvm_variant *variant) 455848b8605Smrg{ 456848b8605Smrg if (!variant->context_ptr_type) 457848b8605Smrg create_jit_types(variant); 458848b8605Smrg return variant->context_ptr_type; 459848b8605Smrg} 460848b8605Smrg 461848b8605Smrg 462848b8605Smrgstatic LLVMTypeRef 463848b8605Smrgget_buffer_ptr_type(struct draw_llvm_variant *variant) 464848b8605Smrg{ 465848b8605Smrg if (!variant->buffer_ptr_type) 466848b8605Smrg create_jit_types(variant); 467848b8605Smrg return variant->buffer_ptr_type; 468848b8605Smrg} 469848b8605Smrg 470848b8605Smrg 471848b8605Smrgstatic LLVMTypeRef 472848b8605Smrgget_vb_ptr_type(struct draw_llvm_variant *variant) 473848b8605Smrg{ 474848b8605Smrg if (!variant->vb_ptr_type) 475848b8605Smrg create_jit_types(variant); 476848b8605Smrg return variant->vb_ptr_type; 477848b8605Smrg} 478848b8605Smrg 479848b8605Smrgstatic LLVMTypeRef 480848b8605Smrgget_vertex_header_ptr_type(struct draw_llvm_variant *variant) 481848b8605Smrg{ 482848b8605Smrg if (!variant->vertex_header_ptr_type) 483848b8605Smrg create_jit_types(variant); 484848b8605Smrg return variant->vertex_header_ptr_type; 485848b8605Smrg} 486848b8605Smrg 487848b8605Smrg 488848b8605Smrg/** 489848b8605Smrg * Create per-context LLVM info. 490848b8605Smrg */ 491848b8605Smrgstruct draw_llvm * 492b8e80941Smrgdraw_llvm_create(struct draw_context *draw, LLVMContextRef context) 493848b8605Smrg{ 494848b8605Smrg struct draw_llvm *llvm; 495848b8605Smrg 496b8e80941Smrg if (!lp_build_init()) 497b8e80941Smrg return NULL; 498b8e80941Smrg 499848b8605Smrg llvm = CALLOC_STRUCT( draw_llvm ); 500848b8605Smrg if (!llvm) 501848b8605Smrg return NULL; 502848b8605Smrg 503848b8605Smrg llvm->draw = draw; 504848b8605Smrg 505b8e80941Smrg llvm->context = context; 506b8e80941Smrg if (!llvm->context) { 507b8e80941Smrg llvm->context = LLVMContextCreate(); 508b8e80941Smrg llvm->context_owned = true; 509b8e80941Smrg } 510b8e80941Smrg if (!llvm->context) 511b8e80941Smrg goto fail; 512b8e80941Smrg 513848b8605Smrg llvm->nr_variants = 0; 514848b8605Smrg make_empty_list(&llvm->vs_variants_list); 515848b8605Smrg 516848b8605Smrg llvm->nr_gs_variants = 0; 517848b8605Smrg make_empty_list(&llvm->gs_variants_list); 518848b8605Smrg 519848b8605Smrg return llvm; 520b8e80941Smrg 521b8e80941Smrgfail: 522b8e80941Smrg draw_llvm_destroy(llvm); 523b8e80941Smrg return NULL; 524848b8605Smrg} 525848b8605Smrg 526848b8605Smrg 527848b8605Smrg/** 528848b8605Smrg * Free per-context LLVM info. 529848b8605Smrg */ 530848b8605Smrgvoid 531848b8605Smrgdraw_llvm_destroy(struct draw_llvm *llvm) 532848b8605Smrg{ 533b8e80941Smrg if (llvm->context_owned) 534b8e80941Smrg LLVMContextDispose(llvm->context); 535b8e80941Smrg llvm->context = NULL; 536b8e80941Smrg 537848b8605Smrg /* XXX free other draw_llvm data? */ 538848b8605Smrg FREE(llvm); 539848b8605Smrg} 540848b8605Smrg 541848b8605Smrg 542848b8605Smrg/** 543848b8605Smrg * Create LLVM-generated code for a vertex shader. 544848b8605Smrg */ 545848b8605Smrgstruct draw_llvm_variant * 546848b8605Smrgdraw_llvm_create_variant(struct draw_llvm *llvm, 547848b8605Smrg unsigned num_inputs, 548848b8605Smrg const struct draw_llvm_variant_key *key) 549848b8605Smrg{ 550848b8605Smrg struct draw_llvm_variant *variant; 551848b8605Smrg struct llvm_vertex_shader *shader = 552848b8605Smrg llvm_vertex_shader(llvm->draw->vs.vertex_shader); 553848b8605Smrg LLVMTypeRef vertex_header; 554848b8605Smrg char module_name[64]; 555848b8605Smrg 556848b8605Smrg variant = MALLOC(sizeof *variant + 557848b8605Smrg shader->variant_key_size - 558848b8605Smrg sizeof variant->key); 559b8e80941Smrg if (!variant) 560848b8605Smrg return NULL; 561848b8605Smrg 562848b8605Smrg variant->llvm = llvm; 563848b8605Smrg variant->shader = shader; 564848b8605Smrg 565848b8605Smrg util_snprintf(module_name, sizeof(module_name), "draw_llvm_vs_variant%u", 566848b8605Smrg variant->shader->variants_cached); 567848b8605Smrg 568b8e80941Smrg variant->gallivm = gallivm_create(module_name, llvm->context); 569848b8605Smrg 570848b8605Smrg create_jit_types(variant); 571848b8605Smrg 572848b8605Smrg memcpy(&variant->key, key, shader->variant_key_size); 573848b8605Smrg 574b8e80941Smrg if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) { 575b8e80941Smrg tgsi_dump(llvm->draw->vs.vertex_shader->state.tokens, 0); 576b8e80941Smrg draw_llvm_dump_variant_key(&variant->key); 577b8e80941Smrg } 578b8e80941Smrg 579848b8605Smrg vertex_header = create_jit_vertex_header(variant->gallivm, num_inputs); 580848b8605Smrg 581848b8605Smrg variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0); 582848b8605Smrg 583b8e80941Smrg draw_llvm_generate(llvm, variant); 584848b8605Smrg 585848b8605Smrg gallivm_compile_module(variant->gallivm); 586848b8605Smrg 587848b8605Smrg variant->jit_func = (draw_jit_vert_func) 588848b8605Smrg gallivm_jit_function(variant->gallivm, variant->function); 589848b8605Smrg 590848b8605Smrg gallivm_free_ir(variant->gallivm); 591848b8605Smrg 592848b8605Smrg variant->list_item_global.base = variant; 593848b8605Smrg variant->list_item_local.base = variant; 594848b8605Smrg /*variant->no = */shader->variants_created++; 595848b8605Smrg variant->list_item_global.base = variant; 596848b8605Smrg 597848b8605Smrg return variant; 598848b8605Smrg} 599848b8605Smrg 600848b8605Smrg 601848b8605Smrgstatic void 602848b8605Smrggenerate_vs(struct draw_llvm_variant *variant, 603848b8605Smrg LLVMBuilderRef builder, 604848b8605Smrg struct lp_type vs_type, 605848b8605Smrg LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 606848b8605Smrg const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS], 607848b8605Smrg const struct lp_bld_tgsi_system_values *system_values, 608848b8605Smrg LLVMValueRef context_ptr, 609b8e80941Smrg const struct lp_build_sampler_soa *draw_sampler, 610848b8605Smrg boolean clamp_vertex_color) 611848b8605Smrg{ 612848b8605Smrg struct draw_llvm *llvm = variant->llvm; 613848b8605Smrg const struct tgsi_token *tokens = llvm->draw->vs.vertex_shader->state.tokens; 614848b8605Smrg LLVMValueRef consts_ptr = 615848b8605Smrg draw_jit_context_vs_constants(variant->gallivm, context_ptr); 616848b8605Smrg LLVMValueRef num_consts_ptr = 617848b8605Smrg draw_jit_context_num_vs_constants(variant->gallivm, context_ptr); 618848b8605Smrg 619848b8605Smrg lp_build_tgsi_soa(variant->gallivm, 620848b8605Smrg tokens, 621848b8605Smrg vs_type, 622848b8605Smrg NULL /*struct lp_build_mask_context *mask*/, 623848b8605Smrg consts_ptr, 624848b8605Smrg num_consts_ptr, 625848b8605Smrg system_values, 626848b8605Smrg inputs, 627848b8605Smrg outputs, 628b8e80941Smrg context_ptr, 629b8e80941Smrg NULL, 630b8e80941Smrg draw_sampler, 631848b8605Smrg &llvm->draw->vs.vertex_shader->info, 632848b8605Smrg NULL); 633848b8605Smrg 634848b8605Smrg { 635848b8605Smrg LLVMValueRef out; 636848b8605Smrg unsigned chan, attrib; 637848b8605Smrg struct lp_build_context bld; 638848b8605Smrg struct tgsi_shader_info* info = &llvm->draw->vs.vertex_shader->info; 639848b8605Smrg lp_build_context_init(&bld, variant->gallivm, vs_type); 640848b8605Smrg 641848b8605Smrg for (attrib = 0; attrib < info->num_outputs; ++attrib) { 642848b8605Smrg for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 643848b8605Smrg if (outputs[attrib][chan]) { 644848b8605Smrg switch (info->output_semantic_name[attrib]) { 645848b8605Smrg case TGSI_SEMANTIC_COLOR: 646848b8605Smrg case TGSI_SEMANTIC_BCOLOR: 647848b8605Smrg if (clamp_vertex_color) { 648848b8605Smrg out = LLVMBuildLoad(builder, outputs[attrib][chan], ""); 649848b8605Smrg out = lp_build_clamp(&bld, out, bld.zero, bld.one); 650848b8605Smrg LLVMBuildStore(builder, out, outputs[attrib][chan]); 651848b8605Smrg } 652848b8605Smrg break; 653848b8605Smrg } 654848b8605Smrg } 655848b8605Smrg } 656848b8605Smrg } 657848b8605Smrg } 658848b8605Smrg} 659848b8605Smrg 660b8e80941Smrg 661848b8605Smrgstatic void 662b8e80941Smrgfetch_instanced(struct gallivm_state *gallivm, 663b8e80941Smrg const struct util_format_description *format_desc, 664b8e80941Smrg struct lp_type vs_type, 665b8e80941Smrg LLVMValueRef vb_stride, 666b8e80941Smrg LLVMValueRef map_ptr, 667b8e80941Smrg LLVMValueRef buffer_size_adj, 668b8e80941Smrg LLVMValueRef *inputs, 669b8e80941Smrg LLVMValueRef index) 670848b8605Smrg{ 671b8e80941Smrg LLVMTypeRef i32_t = LLVMInt32TypeInContext(gallivm->context); 672b8e80941Smrg LLVMTypeRef aosf_t, aosi_t; 673b8e80941Smrg LLVMValueRef zero = LLVMConstNull(i32_t); 674848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 675b8e80941Smrg LLVMValueRef stride, buffer_overflowed, aos, index_valid; 676b8e80941Smrg unsigned i; 677848b8605Smrg 678b8e80941Smrg aosf_t = lp_build_vec_type(gallivm, lp_float32_vec4_type()); 679b8e80941Smrg aosi_t = lp_build_vec_type(gallivm, lp_int32_vec4_type()); 680848b8605Smrg 681b8e80941Smrg /* This mul can overflow. Wraparound is ok. */ 682b8e80941Smrg stride = LLVMBuildMul(builder, vb_stride, index, ""); 683b8e80941Smrg 684b8e80941Smrg buffer_overflowed = LLVMBuildICmp(builder, LLVMIntUGE, 685b8e80941Smrg stride, buffer_size_adj, 686848b8605Smrg "buffer_overflowed"); 687848b8605Smrg 688b8e80941Smrg if (0) { 689b8e80941Smrg lp_build_print_value(gallivm, " instance index = ", index); 690b8e80941Smrg lp_build_print_value(gallivm, " buffer overflowed = ", buffer_overflowed); 691848b8605Smrg } 692848b8605Smrg 693b8e80941Smrg index_valid = LLVMBuildNot(builder, buffer_overflowed, ""); 694b8e80941Smrg index_valid = LLVMBuildSExt(builder, index_valid, i32_t, ""); 695b8e80941Smrg stride = LLVMBuildAnd(builder, stride, index_valid, ""); 696b8e80941Smrg 697b8e80941Smrg aos = lp_build_fetch_rgba_aos(gallivm, 698b8e80941Smrg format_desc, 699b8e80941Smrg lp_float32_vec4_type(), 700b8e80941Smrg FALSE, 701b8e80941Smrg map_ptr, 702b8e80941Smrg stride, zero, zero, 703b8e80941Smrg NULL); 704b8e80941Smrg 705b8e80941Smrg index_valid = lp_build_broadcast(gallivm, aosi_t, index_valid); 706b8e80941Smrg aos = LLVMBuildBitCast(builder, aos, aosi_t, ""); 707b8e80941Smrg aos = LLVMBuildAnd(builder, aos, index_valid, ""); 708b8e80941Smrg aos = LLVMBuildBitCast(builder, aos, aosf_t, ""); 709b8e80941Smrg 710b8e80941Smrg for (i = 0; i < TGSI_NUM_CHANNELS; i++) { 711b8e80941Smrg LLVMValueRef index = lp_build_const_int32(gallivm, i); 712b8e80941Smrg inputs[i] = lp_build_extract_broadcast(gallivm, 713b8e80941Smrg lp_float32_vec4_type(), 714b8e80941Smrg vs_type, aos, index); 715b8e80941Smrg } 716848b8605Smrg} 717848b8605Smrg 718b8e80941Smrg 719848b8605Smrgstatic void 720b8e80941Smrgfetch_vector(struct gallivm_state *gallivm, 721b8e80941Smrg const struct util_format_description *format_desc, 722b8e80941Smrg struct lp_type vs_type, 723b8e80941Smrg LLVMValueRef vb_stride, 724b8e80941Smrg LLVMValueRef map_ptr, 725b8e80941Smrg LLVMValueRef buffer_size_adj, 726b8e80941Smrg LLVMValueRef *inputs, 727b8e80941Smrg LLVMValueRef indices) 728848b8605Smrg{ 729b8e80941Smrg LLVMBuilderRef builder = gallivm->builder; 730b8e80941Smrg struct lp_build_context blduivec; 731b8e80941Smrg struct lp_type fetch_type = vs_type; 732b8e80941Smrg LLVMValueRef offset, valid_mask; 733b8e80941Smrg unsigned i; 734848b8605Smrg 735b8e80941Smrg lp_build_context_init(&blduivec, gallivm, lp_uint_type(vs_type)); 736848b8605Smrg 737b8e80941Smrg vb_stride = lp_build_broadcast_scalar(&blduivec, vb_stride); 738b8e80941Smrg buffer_size_adj = lp_build_broadcast_scalar(&blduivec, buffer_size_adj); 739848b8605Smrg 740b8e80941Smrg /* This mul can overflow. Wraparound is ok. */ 741b8e80941Smrg offset = lp_build_mul(&blduivec, vb_stride, indices); 742848b8605Smrg 743b8e80941Smrg valid_mask = lp_build_compare(gallivm, blduivec.type, 744b8e80941Smrg PIPE_FUNC_LESS, offset, buffer_size_adj); 745848b8605Smrg 746b8e80941Smrg /* not valid elements use offset 0 */ 747b8e80941Smrg offset = LLVMBuildAnd(builder, offset, valid_mask, ""); 748848b8605Smrg 749b8e80941Smrg if (0) { 750b8e80941Smrg lp_build_print_value(gallivm, " indices = ", indices); 751b8e80941Smrg lp_build_print_value(gallivm, " offsets = ", offset); 752b8e80941Smrg lp_build_print_value(gallivm, " valid_mask = ", valid_mask); 753b8e80941Smrg } 754b8e80941Smrg 755b8e80941Smrg /* 756b8e80941Smrg * Unlike fetch_instanced, use SoA fetch instead of multiple AoS fetches. 757b8e80941Smrg * This should always produce better code. 758b8e80941Smrg */ 759848b8605Smrg 760b8e80941Smrg /* The type handling is annoying here... */ 761b8e80941Smrg if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB && 762b8e80941Smrg format_desc->channel[0].pure_integer) { 763b8e80941Smrg if (format_desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) { 764b8e80941Smrg fetch_type = lp_type_int_vec(vs_type.width, vs_type.width * vs_type.length); 765b8e80941Smrg } 766b8e80941Smrg else if (format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED) { 767b8e80941Smrg fetch_type = lp_type_uint_vec(vs_type.width, vs_type.width * vs_type.length); 768848b8605Smrg } 769b8e80941Smrg } 770b8e80941Smrg 771b8e80941Smrg lp_build_fetch_rgba_soa(gallivm, format_desc, 772b8e80941Smrg fetch_type, FALSE, map_ptr, offset, 773b8e80941Smrg blduivec.zero, blduivec.zero, 774b8e80941Smrg NULL, inputs); 775848b8605Smrg 776b8e80941Smrg for (i = 0; i < TGSI_NUM_CHANNELS; i++) { 777b8e80941Smrg inputs[i] = LLVMBuildBitCast(builder, inputs[i], 778b8e80941Smrg lp_build_vec_type(gallivm, vs_type), ""); 779b8e80941Smrg } 780b8e80941Smrg 781b8e80941Smrg /* out-of-bound fetches return all zeros */ 782b8e80941Smrg for (i = 0; i < TGSI_NUM_CHANNELS; i++) { 783b8e80941Smrg inputs[i] = LLVMBuildBitCast(builder, inputs[i], blduivec.vec_type, ""); 784b8e80941Smrg inputs[i] = LLVMBuildAnd(builder, inputs[i], valid_mask, ""); 785b8e80941Smrg inputs[i] = LLVMBuildBitCast(builder, inputs[i], 786b8e80941Smrg lp_build_vec_type(gallivm, vs_type), ""); 787848b8605Smrg } 788848b8605Smrg} 789848b8605Smrg 790848b8605Smrg 791848b8605Smrgstatic void 792848b8605Smrgstore_aos(struct gallivm_state *gallivm, 793848b8605Smrg LLVMValueRef io_ptr, 794848b8605Smrg LLVMValueRef index, 795848b8605Smrg LLVMValueRef value) 796848b8605Smrg{ 797848b8605Smrg LLVMTypeRef data_ptr_type = LLVMPointerType(lp_build_vec_type(gallivm, lp_float32_vec4_type()), 0); 798848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 799848b8605Smrg LLVMValueRef data_ptr = draw_jit_header_data(gallivm, io_ptr); 800848b8605Smrg LLVMValueRef indices[3]; 801848b8605Smrg 802848b8605Smrg indices[0] = lp_build_const_int32(gallivm, 0); 803848b8605Smrg indices[1] = index; 804848b8605Smrg indices[2] = lp_build_const_int32(gallivm, 0); 805848b8605Smrg 806848b8605Smrg data_ptr = LLVMBuildGEP(builder, data_ptr, indices, 3, ""); 807848b8605Smrg data_ptr = LLVMBuildPointerCast(builder, data_ptr, data_ptr_type, ""); 808848b8605Smrg 809848b8605Smrg#if DEBUG_STORE 810848b8605Smrg lp_build_printf(gallivm, " ---- %p storing attribute %d (io = %p)\n", data_ptr, index, io_ptr); 811848b8605Smrg#endif 812848b8605Smrg 813848b8605Smrg /* Unaligned store due to the vertex header */ 814b8e80941Smrg LLVMSetAlignment(LLVMBuildStore(builder, value, data_ptr), sizeof(float)); 815848b8605Smrg} 816848b8605Smrg 817848b8605Smrg/** 818848b8605Smrg * Adjust the mask to architecture endianess. The mask will the store in struct: 819848b8605Smrg * 820848b8605Smrg * struct vertex_header { 821848b8605Smrg * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES; 822848b8605Smrg * unsigned edgeflag:1; 823b8e80941Smrg * unsigned pad:1; 824848b8605Smrg * unsigned vertex_id:16; 825848b8605Smrg * [...] 826848b8605Smrg * } 827848b8605Smrg * 828848b8605Smrg * On little-endian machine nothing needs to done, however on bit-endian machine 829848b8605Smrg * the mask's fields need to be adjusted with the algorithm: 830848b8605Smrg * 831848b8605Smrg * uint32_t reverse (uint32_t x) 832848b8605Smrg * { 833848b8605Smrg * return (x >> 16) | // vertex_id 834848b8605Smrg * ((x & 0x3fff) << 18) | // clipmask 835b8e80941Smrg * ((x & 0x4000) << 3) | // pad 836848b8605Smrg * ((x & 0x8000) << 1); // edgeflag 837848b8605Smrg * } 838848b8605Smrg */ 839848b8605Smrgstatic LLVMValueRef 840848b8605Smrgadjust_mask(struct gallivm_state *gallivm, 841848b8605Smrg LLVMValueRef mask) 842848b8605Smrg{ 843848b8605Smrg#ifdef PIPE_ARCH_BIG_ENDIAN 844848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 845848b8605Smrg LLVMValueRef vertex_id; 846848b8605Smrg LLVMValueRef clipmask; 847b8e80941Smrg LLVMValueRef pad; 848848b8605Smrg LLVMValueRef edgeflag; 849848b8605Smrg 850848b8605Smrg vertex_id = LLVMBuildLShr(builder, mask, lp_build_const_int32(gallivm, 16), ""); 851848b8605Smrg clipmask = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x3fff), ""); 852848b8605Smrg clipmask = LLVMBuildShl(builder, clipmask, lp_build_const_int32(gallivm, 18), ""); 853b8e80941Smrg if (0) { 854b8e80941Smrg pad = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x4000), ""); 855b8e80941Smrg pad = LLVMBuildShl(builder, pad, lp_build_const_int32(gallivm, 3), ""); 856b8e80941Smrg } 857848b8605Smrg edgeflag = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x8000), ""); 858848b8605Smrg edgeflag = LLVMBuildShl(builder, edgeflag, lp_build_const_int32(gallivm, 1), ""); 859848b8605Smrg 860848b8605Smrg mask = LLVMBuildOr(builder, vertex_id, clipmask, ""); 861b8e80941Smrg if (0) { 862b8e80941Smrg mask = LLVMBuildOr(builder, mask, pad, ""); 863b8e80941Smrg } 864848b8605Smrg mask = LLVMBuildOr(builder, mask, edgeflag, ""); 865848b8605Smrg#endif 866848b8605Smrg return mask; 867848b8605Smrg} 868848b8605Smrg 869848b8605Smrgstatic void 870848b8605Smrgstore_aos_array(struct gallivm_state *gallivm, 871848b8605Smrg struct lp_type soa_type, 872848b8605Smrg LLVMValueRef io_ptr, 873848b8605Smrg LLVMValueRef *indices, 874848b8605Smrg LLVMValueRef* aos, 875848b8605Smrg int attrib, 876848b8605Smrg int num_outputs, 877848b8605Smrg LLVMValueRef clipmask, 878b8e80941Smrg boolean need_edgeflag) 879848b8605Smrg{ 880848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 881848b8605Smrg LLVMValueRef attr_index = lp_build_const_int32(gallivm, attrib); 882848b8605Smrg LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32]; 883848b8605Smrg LLVMValueRef linear_inds[LP_MAX_VECTOR_WIDTH / 32]; 884848b8605Smrg LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32]; 885848b8605Smrg int vector_length = soa_type.length; 886848b8605Smrg int i; 887848b8605Smrg 888848b8605Smrg debug_assert(TGSI_NUM_CHANNELS == 4); 889848b8605Smrg 890848b8605Smrg for (i = 0; i < vector_length; i++) { 891848b8605Smrg linear_inds[i] = lp_build_const_int32(gallivm, i); 892848b8605Smrg if (indices) { 893848b8605Smrg inds[i] = indices[i]; 894848b8605Smrg } else { 895848b8605Smrg inds[i] = linear_inds[i]; 896848b8605Smrg } 897848b8605Smrg io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, ""); 898848b8605Smrg } 899848b8605Smrg 900848b8605Smrg if (attrib == 0) { 901848b8605Smrg /* store vertex header for each of the n vertices */ 902848b8605Smrg LLVMValueRef val, cliptmp; 903848b8605Smrg int vertex_id_pad_edgeflag; 904848b8605Smrg 905848b8605Smrg /* If this assertion fails, it means we need to update the bit twidding 906848b8605Smrg * code here. See struct vertex_header in draw_private.h. 907848b8605Smrg */ 908848b8605Smrg assert(DRAW_TOTAL_CLIP_PLANES==14); 909b8e80941Smrg /* initialize vertex id:16 = 0xffff, pad:1 = 0, edgeflag:1 = 1 */ 910b8e80941Smrg if (!need_edgeflag) { 911b8e80941Smrg vertex_id_pad_edgeflag = (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES); 912b8e80941Smrg } 913b8e80941Smrg else { 914b8e80941Smrg vertex_id_pad_edgeflag = (0xffff << 16); 915b8e80941Smrg } 916b8e80941Smrg val = lp_build_const_int_vec(gallivm, lp_int_type(soa_type), 917b8e80941Smrg vertex_id_pad_edgeflag); 918848b8605Smrg /* OR with the clipmask */ 919848b8605Smrg cliptmp = LLVMBuildOr(builder, val, clipmask, ""); 920848b8605Smrg for (i = 0; i < vector_length; i++) { 921848b8605Smrg LLVMValueRef id_ptr = draw_jit_header_id(gallivm, io_ptrs[i]); 922848b8605Smrg val = LLVMBuildExtractElement(builder, cliptmp, linear_inds[i], ""); 923848b8605Smrg val = adjust_mask(gallivm, val); 924848b8605Smrg#if DEBUG_STORE 925848b8605Smrg lp_build_printf(gallivm, "io = %p, index %d, clipmask = %x\n", 926848b8605Smrg io_ptrs[i], inds[i], val); 927848b8605Smrg#endif 928848b8605Smrg LLVMBuildStore(builder, val, id_ptr); 929848b8605Smrg } 930848b8605Smrg } 931848b8605Smrg 932848b8605Smrg /* store for each of the n vertices */ 933848b8605Smrg for (i = 0; i < vector_length; i++) { 934848b8605Smrg store_aos(gallivm, io_ptrs[i], attr_index, aos[i]); 935848b8605Smrg } 936848b8605Smrg} 937848b8605Smrg 938848b8605Smrg 939848b8605Smrgstatic void 940848b8605Smrgconvert_to_aos(struct gallivm_state *gallivm, 941848b8605Smrg LLVMValueRef io, 942848b8605Smrg LLVMValueRef *indices, 943848b8605Smrg LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 944848b8605Smrg LLVMValueRef clipmask, 945848b8605Smrg int num_outputs, 946848b8605Smrg struct lp_type soa_type, 947b8e80941Smrg boolean need_edgeflag) 948848b8605Smrg{ 949848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 950848b8605Smrg unsigned chan, attrib, i; 951848b8605Smrg 952848b8605Smrg#if DEBUG_STORE 953848b8605Smrg lp_build_printf(gallivm, " # storing begin\n"); 954848b8605Smrg#endif 955848b8605Smrg for (attrib = 0; attrib < num_outputs; ++attrib) { 956848b8605Smrg LLVMValueRef soa[TGSI_NUM_CHANNELS]; 957848b8605Smrg LLVMValueRef aos[LP_MAX_VECTOR_WIDTH / 32]; 958848b8605Smrg for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 959848b8605Smrg if (outputs[attrib][chan]) { 960848b8605Smrg LLVMValueRef out = LLVMBuildLoad(builder, outputs[attrib][chan], ""); 961848b8605Smrg lp_build_name(out, "output%u.%c", attrib, "xyzw"[chan]); 962848b8605Smrg#if DEBUG_STORE 963848b8605Smrg lp_build_printf(gallivm, "output %d : %d ", 964848b8605Smrg LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 965848b8605Smrg attrib, 0), 966848b8605Smrg LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 967848b8605Smrg chan, 0)); 968848b8605Smrg lp_build_print_value(gallivm, "val = ", out); 969848b8605Smrg { 970848b8605Smrg LLVMValueRef iv = 971848b8605Smrg LLVMBuildBitCast(builder, out, lp_build_int_vec_type(gallivm, soa_type), ""); 972848b8605Smrg 973848b8605Smrg lp_build_print_value(gallivm, " ival = ", iv); 974848b8605Smrg } 975848b8605Smrg#endif 976848b8605Smrg soa[chan] = out; 977848b8605Smrg } 978848b8605Smrg else { 979848b8605Smrg soa[chan] = 0; 980848b8605Smrg } 981848b8605Smrg } 982848b8605Smrg 983848b8605Smrg 984848b8605Smrg if (soa_type.length == TGSI_NUM_CHANNELS) { 985848b8605Smrg lp_build_transpose_aos(gallivm, soa_type, soa, aos); 986848b8605Smrg } else { 987848b8605Smrg lp_build_transpose_aos(gallivm, soa_type, soa, soa); 988848b8605Smrg 989848b8605Smrg for (i = 0; i < soa_type.length; ++i) { 990848b8605Smrg aos[i] = lp_build_extract_range(gallivm, 991848b8605Smrg soa[i % TGSI_NUM_CHANNELS], 992848b8605Smrg (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS, 993848b8605Smrg TGSI_NUM_CHANNELS); 994848b8605Smrg } 995848b8605Smrg } 996848b8605Smrg 997848b8605Smrg store_aos_array(gallivm, 998848b8605Smrg soa_type, 999848b8605Smrg io, indices, 1000848b8605Smrg aos, 1001848b8605Smrg attrib, 1002848b8605Smrg num_outputs, 1003b8e80941Smrg clipmask, 1004b8e80941Smrg need_edgeflag); 1005848b8605Smrg } 1006848b8605Smrg#if DEBUG_STORE 1007848b8605Smrg lp_build_printf(gallivm, " # storing end\n"); 1008848b8605Smrg#endif 1009848b8605Smrg} 1010848b8605Smrg 1011848b8605Smrg 1012848b8605Smrg/** 1013848b8605Smrg * Stores original vertex positions in clip coordinates 1014848b8605Smrg */ 1015848b8605Smrgstatic void 1016848b8605Smrgstore_clip(struct gallivm_state *gallivm, 1017848b8605Smrg const struct lp_type vs_type, 1018848b8605Smrg LLVMValueRef io_ptr, 1019848b8605Smrg LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 1020b8e80941Smrg int idx) 1021848b8605Smrg{ 1022848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 1023848b8605Smrg LLVMValueRef soa[4]; 1024848b8605Smrg LLVMValueRef aos[LP_MAX_VECTOR_LENGTH]; 1025848b8605Smrg LLVMValueRef indices[2]; 1026848b8605Smrg LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32]; 1027848b8605Smrg LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32]; 1028848b8605Smrg LLVMValueRef clip_ptrs[LP_MAX_VECTOR_WIDTH / 32]; 1029848b8605Smrg LLVMTypeRef clip_ptr_type = 1030848b8605Smrg LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 1031848b8605Smrg 4), 0); 1032848b8605Smrg int i, j; 1033848b8605Smrg 1034848b8605Smrg indices[0] = 1035848b8605Smrg indices[1] = lp_build_const_int32(gallivm, 0); 1036848b8605Smrg 1037848b8605Smrg for (i = 0; i < vs_type.length; i++) { 1038848b8605Smrg inds[i] = lp_build_const_int32(gallivm, i); 1039848b8605Smrg io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, ""); 1040848b8605Smrg } 1041848b8605Smrg 1042848b8605Smrg soa[0] = LLVMBuildLoad(builder, outputs[idx][0], ""); /*x0 x1 .. xn*/ 1043848b8605Smrg soa[1] = LLVMBuildLoad(builder, outputs[idx][1], ""); /*y0 y1 .. yn*/ 1044848b8605Smrg soa[2] = LLVMBuildLoad(builder, outputs[idx][2], ""); /*z0 z1 .. zn*/ 1045848b8605Smrg soa[3] = LLVMBuildLoad(builder, outputs[idx][3], ""); /*w0 w1 .. wn*/ 1046848b8605Smrg 1047b8e80941Smrg for (i = 0; i < vs_type.length; i++) { 1048b8e80941Smrg clip_ptrs[i] = draw_jit_header_clip_pos(gallivm, io_ptrs[i]); 1049848b8605Smrg } 1050848b8605Smrg 1051848b8605Smrg lp_build_transpose_aos(gallivm, vs_type, soa, soa); 1052848b8605Smrg for (i = 0; i < vs_type.length; ++i) { 1053848b8605Smrg aos[i] = lp_build_extract_range(gallivm, 1054848b8605Smrg soa[i % TGSI_NUM_CHANNELS], 1055848b8605Smrg (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS, 1056848b8605Smrg TGSI_NUM_CHANNELS); 1057848b8605Smrg } 1058848b8605Smrg 1059848b8605Smrg for (j = 0; j < vs_type.length; j++) { 1060848b8605Smrg LLVMValueRef clip_ptr; 1061848b8605Smrg 1062848b8605Smrg clip_ptr = LLVMBuildGEP(builder, clip_ptrs[j], indices, 2, "clipo"); 1063848b8605Smrg clip_ptr = LLVMBuildPointerCast(builder, clip_ptr, clip_ptr_type, ""); 1064848b8605Smrg 1065848b8605Smrg /* Unaligned store */ 1066b8e80941Smrg LLVMSetAlignment(LLVMBuildStore(builder, aos[j], clip_ptr), sizeof(float)); 1067848b8605Smrg } 1068848b8605Smrg} 1069848b8605Smrg 1070848b8605Smrg 1071848b8605Smrg/** 1072848b8605Smrg * Transforms the outputs for viewport mapping 1073848b8605Smrg */ 1074848b8605Smrgstatic void 1075848b8605Smrggenerate_viewport(struct draw_llvm_variant *variant, 1076848b8605Smrg LLVMBuilderRef builder, 1077848b8605Smrg struct lp_type vs_type, 1078848b8605Smrg LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 1079848b8605Smrg LLVMValueRef context_ptr) 1080848b8605Smrg{ 1081848b8605Smrg int i; 1082848b8605Smrg struct gallivm_state *gallivm = variant->gallivm; 1083848b8605Smrg struct lp_type f32_type = vs_type; 1084848b8605Smrg const unsigned pos = variant->llvm->draw->vs.position_output; 1085848b8605Smrg LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type); 1086848b8605Smrg LLVMValueRef out3 = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 .. wn*/ 1087848b8605Smrg LLVMValueRef const1 = lp_build_const_vec(gallivm, f32_type, 1.0); /*1.0 1.0 1.0 1.0*/ 1088b8e80941Smrg LLVMValueRef vp_ptr = draw_jit_context_viewports(gallivm, context_ptr); 1089b8e80941Smrg 1090b8e80941Smrg /* We treat pipe_viewport_state as a float array */ 1091b8e80941Smrg const int scale_index_offset = offsetof(struct pipe_viewport_state, scale) / sizeof(float); 1092b8e80941Smrg const int trans_index_offset = offsetof(struct pipe_viewport_state, translate) / sizeof(float); 1093848b8605Smrg 1094848b8605Smrg /* for 1/w convention*/ 1095848b8605Smrg out3 = LLVMBuildFDiv(builder, const1, out3, ""); 1096848b8605Smrg LLVMBuildStore(builder, out3, outputs[pos][3]); 1097848b8605Smrg 1098848b8605Smrg /* Viewport Mapping */ 1099848b8605Smrg for (i=0; i<3; i++) { 1100848b8605Smrg LLVMValueRef out = LLVMBuildLoad(builder, outputs[pos][i], ""); /*x0 x1 .. xn*/ 1101848b8605Smrg LLVMValueRef scale; 1102848b8605Smrg LLVMValueRef trans; 1103848b8605Smrg LLVMValueRef scale_i; 1104848b8605Smrg LLVMValueRef trans_i; 1105848b8605Smrg LLVMValueRef index; 1106848b8605Smrg 1107b8e80941Smrg index = lp_build_const_int32(gallivm, i + scale_index_offset); 1108848b8605Smrg scale_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, ""); 1109848b8605Smrg 1110b8e80941Smrg index = lp_build_const_int32(gallivm, i + trans_index_offset); 1111848b8605Smrg trans_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, ""); 1112848b8605Smrg 1113848b8605Smrg scale = lp_build_broadcast(gallivm, vs_type_llvm, 1114848b8605Smrg LLVMBuildLoad(builder, scale_i, "scale")); 1115848b8605Smrg trans = lp_build_broadcast(gallivm, vs_type_llvm, 1116848b8605Smrg LLVMBuildLoad(builder, trans_i, "trans")); 1117848b8605Smrg 1118848b8605Smrg /* divide by w */ 1119848b8605Smrg out = LLVMBuildFMul(builder, out, out3, ""); 1120b8e80941Smrg /* mult by scale, add translation */ 1121b8e80941Smrg out = lp_build_fmuladd(builder, out, scale, trans); 1122848b8605Smrg 1123848b8605Smrg /* store transformed outputs */ 1124848b8605Smrg LLVMBuildStore(builder, out, outputs[pos][i]); 1125848b8605Smrg } 1126848b8605Smrg 1127848b8605Smrg} 1128848b8605Smrg 1129848b8605Smrg 1130848b8605Smrg/** 1131848b8605Smrg * Returns clipmask as nxi32 bitmask for the n vertices 1132848b8605Smrg */ 1133848b8605Smrgstatic LLVMValueRef 1134848b8605Smrggenerate_clipmask(struct draw_llvm *llvm, 1135848b8605Smrg struct gallivm_state *gallivm, 1136848b8605Smrg struct lp_type vs_type, 1137848b8605Smrg LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 1138b8e80941Smrg struct draw_llvm_variant_key *key, 1139848b8605Smrg LLVMValueRef context_ptr, 1140848b8605Smrg boolean *have_clipdist) 1141848b8605Smrg{ 1142848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 1143848b8605Smrg LLVMValueRef mask; /* stores the <nxi32> clipmasks */ 1144848b8605Smrg LLVMValueRef test, temp; 1145848b8605Smrg LLVMValueRef zero, shift; 1146848b8605Smrg LLVMValueRef pos_x, pos_y, pos_z, pos_w; 1147848b8605Smrg LLVMValueRef cv_x, cv_y, cv_z, cv_w; 1148848b8605Smrg LLVMValueRef plane1, planes, plane_ptr, sum; 1149848b8605Smrg struct lp_type f32_type = vs_type; 1150848b8605Smrg struct lp_type i32_type = lp_int_type(vs_type); 1151848b8605Smrg const unsigned pos = llvm->draw->vs.position_output; 1152848b8605Smrg const unsigned cv = llvm->draw->vs.clipvertex_output; 1153848b8605Smrg int num_written_clipdistance = llvm->draw->vs.vertex_shader->info.num_written_clipdistance; 1154b8e80941Smrg boolean have_cd = false; 1155b8e80941Smrg boolean clip_user = key->clip_user; 1156b8e80941Smrg unsigned ucp_enable = key->ucp_enable; 1157848b8605Smrg unsigned cd[2]; 1158848b8605Smrg 1159b8e80941Smrg cd[0] = llvm->draw->vs.ccdistance_output[0]; 1160b8e80941Smrg cd[1] = llvm->draw->vs.ccdistance_output[1]; 1161848b8605Smrg 1162848b8605Smrg if (cd[0] != pos || cd[1] != pos) 1163848b8605Smrg have_cd = true; 1164848b8605Smrg 1165848b8605Smrg if (num_written_clipdistance && !clip_user) { 1166848b8605Smrg clip_user = true; 1167848b8605Smrg ucp_enable = (1 << num_written_clipdistance) - 1; 1168848b8605Smrg } 1169848b8605Smrg 1170848b8605Smrg mask = lp_build_const_int_vec(gallivm, i32_type, 0); 1171848b8605Smrg temp = lp_build_const_int_vec(gallivm, i32_type, 0); 1172848b8605Smrg zero = lp_build_const_vec(gallivm, f32_type, 0); /* 0.0f 0.0f 0.0f 0.0f */ 1173848b8605Smrg shift = lp_build_const_int_vec(gallivm, i32_type, 1); /* 1 1 1 1 */ 1174848b8605Smrg 1175848b8605Smrg /* 1176848b8605Smrg * load clipvertex and position from correct locations. 1177848b8605Smrg * if they are the same just load them once. 1178848b8605Smrg */ 1179848b8605Smrg pos_x = LLVMBuildLoad(builder, outputs[pos][0], ""); /*x0 x1 .. xn */ 1180848b8605Smrg pos_y = LLVMBuildLoad(builder, outputs[pos][1], ""); /*y0 y1 .. yn */ 1181848b8605Smrg pos_z = LLVMBuildLoad(builder, outputs[pos][2], ""); /*z0 z1 .. zn */ 1182848b8605Smrg pos_w = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 .. wn */ 1183848b8605Smrg 1184848b8605Smrg if (clip_user && cv != pos) { 1185848b8605Smrg cv_x = LLVMBuildLoad(builder, outputs[cv][0], ""); /*x0 x1 .. xn */ 1186848b8605Smrg cv_y = LLVMBuildLoad(builder, outputs[cv][1], ""); /*y0 y1 .. yn */ 1187848b8605Smrg cv_z = LLVMBuildLoad(builder, outputs[cv][2], ""); /*z0 z1 .. zn */ 1188848b8605Smrg cv_w = LLVMBuildLoad(builder, outputs[cv][3], ""); /*w0 w1 .. wn */ 1189848b8605Smrg } else { 1190848b8605Smrg cv_x = pos_x; 1191848b8605Smrg cv_y = pos_y; 1192848b8605Smrg cv_z = pos_z; 1193848b8605Smrg cv_w = pos_w; 1194848b8605Smrg } 1195848b8605Smrg 1196b8e80941Smrg /* 1197b8e80941Smrg * Be careful with the comparisons and NaNs (using llvm's unordered 1198b8e80941Smrg * comparisons here). 1199b8e80941Smrg */ 1200848b8605Smrg /* Cliptest, for hardwired planes */ 1201b8e80941Smrg /* 1202b8e80941Smrg * XXX should take guardband into account (currently not in key). 1203b8e80941Smrg * Otherwise might run the draw pipeline stages for nothing. 1204b8e80941Smrg */ 1205b8e80941Smrg if (key->clip_xy) { 1206848b8605Smrg /* plane 1 */ 1207848b8605Smrg test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_x , pos_w); 1208848b8605Smrg temp = shift; 1209848b8605Smrg test = LLVMBuildAnd(builder, test, temp, ""); 1210848b8605Smrg mask = test; 1211848b8605Smrg 1212848b8605Smrg /* plane 2 */ 1213848b8605Smrg test = LLVMBuildFAdd(builder, pos_x, pos_w, ""); 1214848b8605Smrg test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test); 1215848b8605Smrg temp = LLVMBuildShl(builder, temp, shift, ""); 1216848b8605Smrg test = LLVMBuildAnd(builder, test, temp, ""); 1217848b8605Smrg mask = LLVMBuildOr(builder, mask, test, ""); 1218848b8605Smrg 1219848b8605Smrg /* plane 3 */ 1220848b8605Smrg test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_y, pos_w); 1221848b8605Smrg temp = LLVMBuildShl(builder, temp, shift, ""); 1222848b8605Smrg test = LLVMBuildAnd(builder, test, temp, ""); 1223848b8605Smrg mask = LLVMBuildOr(builder, mask, test, ""); 1224848b8605Smrg 1225848b8605Smrg /* plane 4 */ 1226848b8605Smrg test = LLVMBuildFAdd(builder, pos_y, pos_w, ""); 1227848b8605Smrg test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test); 1228848b8605Smrg temp = LLVMBuildShl(builder, temp, shift, ""); 1229848b8605Smrg test = LLVMBuildAnd(builder, test, temp, ""); 1230848b8605Smrg mask = LLVMBuildOr(builder, mask, test, ""); 1231848b8605Smrg } 1232848b8605Smrg 1233b8e80941Smrg if (key->clip_z) { 1234848b8605Smrg temp = lp_build_const_int_vec(gallivm, i32_type, 16); 1235b8e80941Smrg if (key->clip_halfz) { 1236848b8605Smrg /* plane 5 */ 1237848b8605Smrg test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, pos_z); 1238848b8605Smrg test = LLVMBuildAnd(builder, test, temp, ""); 1239848b8605Smrg mask = LLVMBuildOr(builder, mask, test, ""); 1240848b8605Smrg } 1241848b8605Smrg else { 1242848b8605Smrg /* plane 5 */ 1243848b8605Smrg test = LLVMBuildFAdd(builder, pos_z, pos_w, ""); 1244848b8605Smrg test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test); 1245848b8605Smrg test = LLVMBuildAnd(builder, test, temp, ""); 1246848b8605Smrg mask = LLVMBuildOr(builder, mask, test, ""); 1247848b8605Smrg } 1248848b8605Smrg /* plane 6 */ 1249848b8605Smrg test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_z, pos_w); 1250848b8605Smrg temp = LLVMBuildShl(builder, temp, shift, ""); 1251848b8605Smrg test = LLVMBuildAnd(builder, test, temp, ""); 1252848b8605Smrg mask = LLVMBuildOr(builder, mask, test, ""); 1253848b8605Smrg } 1254848b8605Smrg 1255848b8605Smrg if (clip_user) { 1256848b8605Smrg LLVMValueRef planes_ptr = draw_jit_context_planes(gallivm, context_ptr); 1257848b8605Smrg LLVMValueRef indices[3]; 1258848b8605Smrg LLVMValueRef is_nan_or_inf; 1259848b8605Smrg 1260848b8605Smrg /* userclip planes */ 1261848b8605Smrg while (ucp_enable) { 1262848b8605Smrg unsigned plane_idx = ffs(ucp_enable)-1; 1263848b8605Smrg ucp_enable &= ~(1 << plane_idx); 1264848b8605Smrg plane_idx += 6; 1265848b8605Smrg 1266848b8605Smrg if (have_cd && num_written_clipdistance) { 1267848b8605Smrg LLVMValueRef clipdist; 1268848b8605Smrg int i; 1269848b8605Smrg i = plane_idx - 6; 1270848b8605Smrg 1271848b8605Smrg *have_clipdist = TRUE; 1272848b8605Smrg if (i < 4) { 1273848b8605Smrg clipdist = LLVMBuildLoad(builder, outputs[cd[0]][i], ""); 1274848b8605Smrg } else { 1275848b8605Smrg clipdist = LLVMBuildLoad(builder, outputs[cd[1]][i-4], ""); 1276848b8605Smrg } 1277848b8605Smrg test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, clipdist); 1278848b8605Smrg is_nan_or_inf = lp_build_is_inf_or_nan(gallivm, vs_type, clipdist); 1279848b8605Smrg test = LLVMBuildOr(builder, test, is_nan_or_inf, ""); 1280b8e80941Smrg temp = lp_build_const_int_vec(gallivm, i32_type, 1LL << plane_idx); 1281848b8605Smrg test = LLVMBuildAnd(builder, test, temp, ""); 1282848b8605Smrg mask = LLVMBuildOr(builder, mask, test, ""); 1283848b8605Smrg } else { 1284848b8605Smrg LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type); 1285848b8605Smrg indices[0] = lp_build_const_int32(gallivm, 0); 1286848b8605Smrg indices[1] = lp_build_const_int32(gallivm, plane_idx); 1287848b8605Smrg 1288848b8605Smrg indices[2] = lp_build_const_int32(gallivm, 0); 1289848b8605Smrg plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, ""); 1290848b8605Smrg plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_x"); 1291848b8605Smrg planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1); 1292848b8605Smrg sum = LLVMBuildFMul(builder, planes, cv_x, ""); 1293848b8605Smrg 1294848b8605Smrg indices[2] = lp_build_const_int32(gallivm, 1); 1295848b8605Smrg plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, ""); 1296848b8605Smrg plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_y"); 1297848b8605Smrg planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1); 1298b8e80941Smrg sum = lp_build_fmuladd(builder, planes, cv_y, sum); 1299848b8605Smrg 1300848b8605Smrg indices[2] = lp_build_const_int32(gallivm, 2); 1301848b8605Smrg plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, ""); 1302848b8605Smrg plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_z"); 1303848b8605Smrg planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1); 1304b8e80941Smrg sum = lp_build_fmuladd(builder, planes, cv_z, sum); 1305848b8605Smrg 1306848b8605Smrg indices[2] = lp_build_const_int32(gallivm, 3); 1307848b8605Smrg plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, ""); 1308848b8605Smrg plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_w"); 1309848b8605Smrg planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1); 1310b8e80941Smrg sum = lp_build_fmuladd(builder, planes, cv_w, sum); 1311848b8605Smrg 1312848b8605Smrg test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, sum); 1313b8e80941Smrg temp = lp_build_const_int_vec(gallivm, i32_type, 1LL << plane_idx); 1314848b8605Smrg test = LLVMBuildAnd(builder, test, temp, ""); 1315848b8605Smrg mask = LLVMBuildOr(builder, mask, test, ""); 1316848b8605Smrg } 1317848b8605Smrg } 1318848b8605Smrg } 1319b8e80941Smrg if (key->need_edgeflags) { 1320b8e80941Smrg /* 1321b8e80941Smrg * This isn't really part of clipmask but stored the same in vertex 1322b8e80941Smrg * header later, so do it here. 1323b8e80941Smrg */ 1324b8e80941Smrg unsigned edge_attr = llvm->draw->vs.edgeflag_output; 1325b8e80941Smrg LLVMValueRef one = lp_build_const_vec(gallivm, f32_type, 1.0); 1326b8e80941Smrg LLVMValueRef edgeflag = LLVMBuildLoad(builder, outputs[edge_attr][0], ""); 1327b8e80941Smrg test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_EQUAL, one, edgeflag); 1328b8e80941Smrg temp = lp_build_const_int_vec(gallivm, i32_type, 1329b8e80941Smrg 1LL << DRAW_TOTAL_CLIP_PLANES); 1330b8e80941Smrg test = LLVMBuildAnd(builder, test, temp, ""); 1331b8e80941Smrg mask = LLVMBuildOr(builder, mask, test, ""); 1332b8e80941Smrg } 1333848b8605Smrg return mask; 1334848b8605Smrg} 1335848b8605Smrg 1336848b8605Smrg 1337848b8605Smrg/** 1338848b8605Smrg * Returns boolean if any clipping has occurred 1339b8e80941Smrg * Used zero/one i8 value to represent boolean 1340848b8605Smrg */ 1341848b8605Smrgstatic LLVMValueRef 1342b8e80941Smrgclipmask_booli8(struct gallivm_state *gallivm, 1343b8e80941Smrg const struct lp_type vs_type, 1344b8e80941Smrg LLVMValueRef clipmask_bool_ptr, 1345b8e80941Smrg boolean edgeflag_in_clipmask) 1346848b8605Smrg{ 1347848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 1348b8e80941Smrg LLVMTypeRef int8_type = LLVMInt8TypeInContext(gallivm->context); 1349848b8605Smrg LLVMValueRef clipmask_bool = LLVMBuildLoad(builder, clipmask_bool_ptr, ""); 1350b8e80941Smrg LLVMValueRef ret; 1351b8e80941Smrg struct lp_build_context bldivec; 1352b8e80941Smrg 1353b8e80941Smrg lp_build_context_init(&bldivec, gallivm, lp_int_type(vs_type)); 1354848b8605Smrg 1355848b8605Smrg /* 1356b8e80941Smrg * We need to invert the edgeflag bit from the clipmask here 1357b8e80941Smrg * (because the result is really if we want to run the pipeline or not 1358b8e80941Smrg * and we (may) need it if edgeflag was 0). 1359848b8605Smrg */ 1360b8e80941Smrg if (edgeflag_in_clipmask) { 1361b8e80941Smrg LLVMValueRef edge = lp_build_const_int_vec(gallivm, bldivec.type, 1362b8e80941Smrg 1LL << DRAW_TOTAL_CLIP_PLANES); 1363b8e80941Smrg clipmask_bool = LLVMBuildXor(builder, clipmask_bool, edge, ""); 1364848b8605Smrg } 1365b8e80941Smrg 1366b8e80941Smrg /* 1367b8e80941Smrg * XXX: probably should mask off bits from the mask which come from 1368b8e80941Smrg * vertices which were beyond the count (i.e. indices_valid for 1369b8e80941Smrg * linear fetches, for elts ones we don't have the correct mask 1370b8e80941Smrg * right now). Otherwise might run the pipeline for nothing, 1371b8e80941Smrg * though everything should still work. 1372b8e80941Smrg */ 1373b8e80941Smrg ret = lp_build_any_true_range(&bldivec, vs_type.length, clipmask_bool); 1374b8e80941Smrg ret = LLVMBuildZExt(builder, ret, int8_type, ""); 1375848b8605Smrg return ret; 1376848b8605Smrg} 1377848b8605Smrg 1378848b8605Smrgstatic LLVMValueRef 1379848b8605Smrgdraw_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface *gs_iface, 1380848b8605Smrg struct lp_build_tgsi_context * bld_base, 1381848b8605Smrg boolean is_vindex_indirect, 1382848b8605Smrg LLVMValueRef vertex_index, 1383848b8605Smrg boolean is_aindex_indirect, 1384848b8605Smrg LLVMValueRef attrib_index, 1385848b8605Smrg LLVMValueRef swizzle_index) 1386848b8605Smrg{ 1387848b8605Smrg const struct draw_gs_llvm_iface *gs = draw_gs_llvm_iface(gs_iface); 1388848b8605Smrg struct gallivm_state *gallivm = bld_base->base.gallivm; 1389848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 1390848b8605Smrg LLVMValueRef indices[3]; 1391848b8605Smrg LLVMValueRef res; 1392848b8605Smrg struct lp_type type = bld_base->base.type; 1393848b8605Smrg 1394848b8605Smrg if (is_vindex_indirect || is_aindex_indirect) { 1395848b8605Smrg int i; 1396848b8605Smrg res = bld_base->base.zero; 1397848b8605Smrg for (i = 0; i < type.length; ++i) { 1398848b8605Smrg LLVMValueRef idx = lp_build_const_int32(gallivm, i); 1399848b8605Smrg LLVMValueRef vert_chan_index = vertex_index; 1400848b8605Smrg LLVMValueRef attr_chan_index = attrib_index; 1401848b8605Smrg LLVMValueRef channel_vec, value; 1402848b8605Smrg 1403848b8605Smrg if (is_vindex_indirect) { 1404848b8605Smrg vert_chan_index = LLVMBuildExtractElement(builder, 1405848b8605Smrg vertex_index, idx, ""); 1406848b8605Smrg } 1407848b8605Smrg if (is_aindex_indirect) { 1408848b8605Smrg attr_chan_index = LLVMBuildExtractElement(builder, 1409848b8605Smrg attrib_index, idx, ""); 1410848b8605Smrg } 1411848b8605Smrg 1412848b8605Smrg indices[0] = vert_chan_index; 1413848b8605Smrg indices[1] = attr_chan_index; 1414848b8605Smrg indices[2] = swizzle_index; 1415848b8605Smrg 1416848b8605Smrg channel_vec = LLVMBuildGEP(builder, gs->input, indices, 3, ""); 1417848b8605Smrg channel_vec = LLVMBuildLoad(builder, channel_vec, ""); 1418848b8605Smrg value = LLVMBuildExtractElement(builder, channel_vec, idx, ""); 1419848b8605Smrg 1420848b8605Smrg res = LLVMBuildInsertElement(builder, res, value, idx, ""); 1421848b8605Smrg } 1422848b8605Smrg } else { 1423848b8605Smrg indices[0] = vertex_index; 1424848b8605Smrg indices[1] = attrib_index; 1425848b8605Smrg indices[2] = swizzle_index; 1426848b8605Smrg 1427848b8605Smrg res = LLVMBuildGEP(builder, gs->input, indices, 3, ""); 1428848b8605Smrg res = LLVMBuildLoad(builder, res, ""); 1429848b8605Smrg } 1430848b8605Smrg 1431848b8605Smrg return res; 1432848b8605Smrg} 1433848b8605Smrg 1434848b8605Smrgstatic void 1435848b8605Smrgdraw_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface *gs_base, 1436848b8605Smrg struct lp_build_tgsi_context * bld_base, 1437848b8605Smrg LLVMValueRef (*outputs)[4], 1438848b8605Smrg LLVMValueRef emitted_vertices_vec) 1439848b8605Smrg{ 1440848b8605Smrg const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base); 1441848b8605Smrg struct draw_gs_llvm_variant *variant = gs_iface->variant; 1442848b8605Smrg struct gallivm_state *gallivm = variant->gallivm; 1443848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 1444848b8605Smrg struct lp_type gs_type = bld_base->base.type; 1445848b8605Smrg LLVMValueRef clipmask = lp_build_const_int_vec(gallivm, 1446848b8605Smrg lp_int_type(gs_type), 0); 1447848b8605Smrg LLVMValueRef indices[LP_MAX_VECTOR_LENGTH]; 1448848b8605Smrg LLVMValueRef next_prim_offset = 1449848b8605Smrg lp_build_const_int32(gallivm, variant->shader->base.primitive_boundary); 1450848b8605Smrg LLVMValueRef io = variant->io_ptr; 1451848b8605Smrg unsigned i; 1452848b8605Smrg const struct tgsi_shader_info *gs_info = &variant->shader->base.info; 1453848b8605Smrg 1454848b8605Smrg for (i = 0; i < gs_type.length; ++i) { 1455848b8605Smrg LLVMValueRef ind = lp_build_const_int32(gallivm, i); 1456848b8605Smrg LLVMValueRef currently_emitted = 1457848b8605Smrg LLVMBuildExtractElement(builder, emitted_vertices_vec, ind, ""); 1458848b8605Smrg indices[i] = LLVMBuildMul(builder, ind, next_prim_offset, ""); 1459848b8605Smrg indices[i] = LLVMBuildAdd(builder, indices[i], currently_emitted, ""); 1460848b8605Smrg } 1461848b8605Smrg 1462848b8605Smrg convert_to_aos(gallivm, io, indices, 1463848b8605Smrg outputs, clipmask, 1464848b8605Smrg gs_info->num_outputs, gs_type, 1465848b8605Smrg FALSE); 1466848b8605Smrg} 1467848b8605Smrg 1468848b8605Smrgstatic void 1469848b8605Smrgdraw_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface *gs_base, 1470848b8605Smrg struct lp_build_tgsi_context * bld_base, 1471848b8605Smrg LLVMValueRef verts_per_prim_vec, 1472848b8605Smrg LLVMValueRef emitted_prims_vec) 1473848b8605Smrg{ 1474848b8605Smrg const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base); 1475848b8605Smrg struct draw_gs_llvm_variant *variant = gs_iface->variant; 1476848b8605Smrg struct gallivm_state *gallivm = variant->gallivm; 1477848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 1478848b8605Smrg LLVMValueRef prim_lengts_ptr = 1479848b8605Smrg draw_gs_jit_prim_lengths(variant->gallivm, variant->context_ptr); 1480848b8605Smrg unsigned i; 1481848b8605Smrg 1482848b8605Smrg for (i = 0; i < bld_base->base.type.length; ++i) { 1483848b8605Smrg LLVMValueRef ind = lp_build_const_int32(gallivm, i); 1484848b8605Smrg LLVMValueRef prims_emitted = 1485848b8605Smrg LLVMBuildExtractElement(builder, emitted_prims_vec, ind, ""); 1486848b8605Smrg LLVMValueRef store_ptr; 1487848b8605Smrg LLVMValueRef num_vertices = 1488848b8605Smrg LLVMBuildExtractElement(builder, verts_per_prim_vec, ind, ""); 1489848b8605Smrg 1490848b8605Smrg store_ptr = LLVMBuildGEP(builder, prim_lengts_ptr, &prims_emitted, 1, ""); 1491848b8605Smrg store_ptr = LLVMBuildLoad(builder, store_ptr, ""); 1492848b8605Smrg store_ptr = LLVMBuildGEP(builder, store_ptr, &ind, 1, ""); 1493848b8605Smrg LLVMBuildStore(builder, num_vertices, store_ptr); 1494848b8605Smrg } 1495848b8605Smrg} 1496848b8605Smrg 1497848b8605Smrgstatic void 1498848b8605Smrgdraw_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface *gs_base, 1499848b8605Smrg struct lp_build_tgsi_context * bld_base, 1500848b8605Smrg LLVMValueRef total_emitted_vertices_vec, 1501848b8605Smrg LLVMValueRef emitted_prims_vec) 1502848b8605Smrg{ 1503848b8605Smrg const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base); 1504848b8605Smrg struct draw_gs_llvm_variant *variant = gs_iface->variant; 1505848b8605Smrg struct gallivm_state *gallivm = variant->gallivm; 1506848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 1507848b8605Smrg LLVMValueRef emitted_verts_ptr = 1508848b8605Smrg draw_gs_jit_emitted_vertices(gallivm, variant->context_ptr); 1509848b8605Smrg LLVMValueRef emitted_prims_ptr = 1510848b8605Smrg draw_gs_jit_emitted_prims(gallivm, variant->context_ptr); 1511848b8605Smrg LLVMValueRef zero = lp_build_const_int32(gallivm, 0); 1512848b8605Smrg 1513848b8605Smrg emitted_verts_ptr = LLVMBuildGEP(builder, emitted_verts_ptr, &zero, 0, ""); 1514848b8605Smrg emitted_prims_ptr = LLVMBuildGEP(builder, emitted_prims_ptr, &zero, 0, ""); 1515848b8605Smrg 1516848b8605Smrg LLVMBuildStore(builder, total_emitted_vertices_vec, emitted_verts_ptr); 1517848b8605Smrg LLVMBuildStore(builder, emitted_prims_vec, emitted_prims_ptr); 1518848b8605Smrg} 1519848b8605Smrg 1520848b8605Smrgstatic void 1521b8e80941Smrgdraw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *variant) 1522848b8605Smrg{ 1523848b8605Smrg struct gallivm_state *gallivm = variant->gallivm; 1524848b8605Smrg LLVMContextRef context = gallivm->context; 1525848b8605Smrg LLVMTypeRef int32_type = LLVMInt32TypeInContext(context); 1526b8e80941Smrg LLVMTypeRef arg_types[11]; 1527b8e80941Smrg unsigned num_arg_types = ARRAY_SIZE(arg_types); 1528848b8605Smrg LLVMTypeRef func_type; 1529848b8605Smrg LLVMValueRef context_ptr; 1530848b8605Smrg LLVMBasicBlockRef block; 1531848b8605Smrg LLVMBuilderRef builder; 1532848b8605Smrg char func_name[64]; 1533848b8605Smrg struct lp_type vs_type; 1534b8e80941Smrg LLVMValueRef count, fetch_elts, start_or_maxelt; 1535b8e80941Smrg LLVMValueRef vertex_id_offset, start_instance; 1536848b8605Smrg LLVMValueRef stride, step, io_itr; 1537b8e80941Smrg LLVMValueRef ind_vec, start_vec, have_elts, fetch_max, tmp; 1538848b8605Smrg LLVMValueRef io_ptr, vbuffers_ptr, vb_ptr; 1539b8e80941Smrg LLVMValueRef vb_stride[PIPE_MAX_ATTRIBS]; 1540b8e80941Smrg LLVMValueRef map_ptr[PIPE_MAX_ATTRIBS]; 1541b8e80941Smrg LLVMValueRef buffer_size_adj[PIPE_MAX_ATTRIBS]; 1542b8e80941Smrg LLVMValueRef instance_index[PIPE_MAX_ATTRIBS]; 1543b8e80941Smrg LLVMValueRef fake_buf_ptr, fake_buf; 1544b8e80941Smrg 1545848b8605Smrg struct draw_context *draw = llvm->draw; 1546848b8605Smrg const struct tgsi_shader_info *vs_info = &draw->vs.vertex_shader->info; 1547848b8605Smrg unsigned i, j; 1548b8e80941Smrg struct lp_build_context bld, blduivec; 1549848b8605Smrg struct lp_build_loop_state lp_loop; 1550b8e80941Smrg struct lp_build_if_state if_ctx; 1551848b8605Smrg const int vector_length = lp_native_vector_width / 32; 1552848b8605Smrg LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS]; 1553848b8605Smrg struct lp_build_sampler_soa *sampler = 0; 1554848b8605Smrg LLVMValueRef ret, clipmask_bool_ptr; 1555848b8605Smrg struct draw_llvm_variant_key *key = &variant->key; 1556848b8605Smrg /* If geometry shader is present we need to skip both the viewport 1557848b8605Smrg * transformation and clipping otherwise the inputs to the geometry 1558848b8605Smrg * shader will be incorrect. 1559b8e80941Smrg * The code can't handle vp transform when vs writes vp index neither 1560b8e80941Smrg * (though this would be fixable here, but couldn't just broadcast 1561b8e80941Smrg * the values). 1562848b8605Smrg */ 1563b8e80941Smrg const boolean bypass_viewport = key->has_gs || key->bypass_viewport || 1564b8e80941Smrg vs_info->writes_viewport_index; 1565848b8605Smrg const boolean enable_cliptest = !key->has_gs && (key->clip_xy || 1566b8e80941Smrg key->clip_z || 1567b8e80941Smrg key->clip_user || 1568b8e80941Smrg key->need_edgeflags); 1569848b8605Smrg LLVMValueRef variant_func; 1570b8e80941Smrg const unsigned pos = draw->vs.position_output; 1571b8e80941Smrg const unsigned cv = draw->vs.clipvertex_output; 1572848b8605Smrg boolean have_clipdist = FALSE; 1573848b8605Smrg struct lp_bld_tgsi_system_values system_values; 1574848b8605Smrg 1575848b8605Smrg memset(&system_values, 0, sizeof(system_values)); 1576848b8605Smrg 1577b8e80941Smrg util_snprintf(func_name, sizeof(func_name), "draw_llvm_vs_variant%u", 1578b8e80941Smrg variant->shader->variants_cached); 1579848b8605Smrg 1580848b8605Smrg i = 0; 1581848b8605Smrg arg_types[i++] = get_context_ptr_type(variant); /* context */ 1582848b8605Smrg arg_types[i++] = get_vertex_header_ptr_type(variant); /* vertex_header */ 1583848b8605Smrg arg_types[i++] = get_buffer_ptr_type(variant); /* vbuffers */ 1584b8e80941Smrg arg_types[i++] = int32_type; /* count */ 1585b8e80941Smrg arg_types[i++] = int32_type; /* start/fetch_elt_max */ 1586b8e80941Smrg arg_types[i++] = int32_type; /* stride */ 1587b8e80941Smrg arg_types[i++] = get_vb_ptr_type(variant); /* pipe_vertex_buffer's */ 1588b8e80941Smrg arg_types[i++] = int32_type; /* instance_id */ 1589b8e80941Smrg arg_types[i++] = int32_type; /* vertex_id_offset */ 1590b8e80941Smrg arg_types[i++] = int32_type; /* start_instance */ 1591b8e80941Smrg arg_types[i++] = LLVMPointerType(int32_type, 0); /* fetch_elts */ 1592b8e80941Smrg 1593b8e80941Smrg func_type = LLVMFunctionType(LLVMInt8TypeInContext(context), 1594b8e80941Smrg arg_types, num_arg_types, 0); 1595848b8605Smrg 1596848b8605Smrg variant_func = LLVMAddFunction(gallivm->module, func_name, func_type); 1597b8e80941Smrg variant->function = variant_func; 1598848b8605Smrg 1599848b8605Smrg LLVMSetFunctionCallConv(variant_func, LLVMCCallConv); 1600848b8605Smrg for (i = 0; i < num_arg_types; ++i) 1601848b8605Smrg if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) 1602b8e80941Smrg lp_add_function_attr(variant_func, i + 1, LP_FUNC_ATTR_NOALIAS); 1603848b8605Smrg 1604848b8605Smrg context_ptr = LLVMGetParam(variant_func, 0); 1605848b8605Smrg io_ptr = LLVMGetParam(variant_func, 1); 1606848b8605Smrg vbuffers_ptr = LLVMGetParam(variant_func, 2); 1607b8e80941Smrg count = LLVMGetParam(variant_func, 3); 1608b8e80941Smrg /* 1609b8e80941Smrg * XXX: the maxelt part is unused. Not really useful, since we cannot 1610b8e80941Smrg * get index buffer overflows due to vsplit (which provides its own 1611b8e80941Smrg * elts buffer, with a different size than what's passed in here). 1612b8e80941Smrg */ 1613b8e80941Smrg start_or_maxelt = LLVMGetParam(variant_func, 4); 1614b8e80941Smrg /* 1615b8e80941Smrg * XXX: stride is actually unused. The stride we use is strictly calculated 1616b8e80941Smrg * from the number of outputs (including the draw_extra outputs). 1617b8e80941Smrg * Should probably fix some day (we need a new vs just because of extra 1618b8e80941Smrg * outputs which the generated vs won't touch). 1619b8e80941Smrg */ 1620b8e80941Smrg stride = LLVMGetParam(variant_func, 5); 1621b8e80941Smrg vb_ptr = LLVMGetParam(variant_func, 6); 1622b8e80941Smrg system_values.instance_id = LLVMGetParam(variant_func, 7); 1623b8e80941Smrg vertex_id_offset = LLVMGetParam(variant_func, 8); 1624b8e80941Smrg start_instance = LLVMGetParam(variant_func, 9); 1625b8e80941Smrg fetch_elts = LLVMGetParam(variant_func, 10); 1626848b8605Smrg 1627848b8605Smrg lp_build_name(context_ptr, "context"); 1628848b8605Smrg lp_build_name(io_ptr, "io"); 1629848b8605Smrg lp_build_name(vbuffers_ptr, "vbuffers"); 1630b8e80941Smrg lp_build_name(count, "count"); 1631b8e80941Smrg lp_build_name(start_or_maxelt, "start_or_maxelt"); 1632848b8605Smrg lp_build_name(stride, "stride"); 1633848b8605Smrg lp_build_name(vb_ptr, "vb"); 1634848b8605Smrg lp_build_name(system_values.instance_id, "instance_id"); 1635848b8605Smrg lp_build_name(vertex_id_offset, "vertex_id_offset"); 1636b8e80941Smrg lp_build_name(start_instance, "start_instance"); 1637b8e80941Smrg lp_build_name(fetch_elts, "fetch_elts"); 1638848b8605Smrg 1639848b8605Smrg /* 1640848b8605Smrg * Function body 1641848b8605Smrg */ 1642848b8605Smrg 1643848b8605Smrg block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry"); 1644848b8605Smrg builder = gallivm->builder; 1645848b8605Smrg LLVMPositionBuilderAtEnd(builder, block); 1646848b8605Smrg 1647848b8605Smrg memset(&vs_type, 0, sizeof vs_type); 1648848b8605Smrg vs_type.floating = TRUE; /* floating point values */ 1649848b8605Smrg vs_type.sign = TRUE; /* values are signed */ 1650848b8605Smrg vs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */ 1651848b8605Smrg vs_type.width = 32; /* 32-bit float */ 1652848b8605Smrg vs_type.length = vector_length; 1653848b8605Smrg 1654b8e80941Smrg lp_build_context_init(&bld, gallivm, lp_type_uint(32)); 1655b8e80941Smrg lp_build_context_init(&blduivec, gallivm, lp_uint_type(vs_type)); 1656b8e80941Smrg 1657848b8605Smrg /* hold temporary "bool" clipmask */ 1658b8e80941Smrg clipmask_bool_ptr = lp_build_alloca(gallivm, blduivec.vec_type, ""); 1659b8e80941Smrg 1660b8e80941Smrg fake_buf = lp_build_alloca_undef(gallivm, 1661b8e80941Smrg LLVMVectorType(LLVMInt64TypeInContext(context), 4), ""); 1662b8e80941Smrg fake_buf = LLVMBuildBitCast(builder, fake_buf, 1663b8e80941Smrg LLVMPointerType(LLVMInt8TypeInContext(context), 0), ""); 1664b8e80941Smrg fake_buf_ptr = LLVMBuildGEP(builder, fake_buf, &bld.zero, 1, ""); 1665848b8605Smrg 1666848b8605Smrg /* code generated texture sampling */ 1667b8e80941Smrg sampler = draw_llvm_sampler_soa_create(draw_llvm_variant_key_samplers(key)); 1668848b8605Smrg 1669848b8605Smrg step = lp_build_const_int32(gallivm, vector_length); 1670848b8605Smrg 1671b8e80941Smrg ind_vec = blduivec.undef; 1672b8e80941Smrg for (i = 0; i < vs_type.length; i++) { 1673b8e80941Smrg LLVMValueRef index = lp_build_const_int32(gallivm, i); 1674b8e80941Smrg ind_vec = LLVMBuildInsertElement(builder, ind_vec, index, index, ""); 1675b8e80941Smrg } 1676848b8605Smrg 1677b8e80941Smrg have_elts = LLVMBuildICmp(builder, LLVMIntNE, 1678b8e80941Smrg LLVMConstPointerNull(arg_types[10]), fetch_elts, ""); 1679b8e80941Smrg 1680b8e80941Smrg fetch_max = LLVMBuildSub(builder, count, bld.one, "fetch_max"); 1681b8e80941Smrg fetch_max = lp_build_broadcast_scalar(&blduivec, fetch_max); 1682b8e80941Smrg /* 1683b8e80941Smrg * Only needed for non-indexed path. 1684b8e80941Smrg */ 1685b8e80941Smrg start_vec = lp_build_broadcast_scalar(&blduivec, start_or_maxelt); 1686b8e80941Smrg 1687b8e80941Smrg /* 1688b8e80941Smrg * Pre-calculate everything which is constant per shader invocation. 1689b8e80941Smrg */ 1690b8e80941Smrg for (j = 0; j < key->nr_vertex_elements; ++j) { 1691b8e80941Smrg LLVMValueRef vb_buffer_offset, buffer_size, temp_ptr; 1692b8e80941Smrg LLVMValueRef vb_info, vbuffer_ptr, buf_offset, ofbit; 1693b8e80941Smrg struct pipe_vertex_element *velem = &key->vertex_element[j]; 1694b8e80941Smrg LLVMValueRef vb_index = 1695b8e80941Smrg lp_build_const_int32(gallivm, velem->vertex_buffer_index); 1696b8e80941Smrg LLVMValueRef bsize = lp_build_const_int32(gallivm, 1697b8e80941Smrg util_format_get_blocksize(velem->src_format)); 1698b8e80941Smrg LLVMValueRef src_offset = lp_build_const_int32(gallivm, 1699b8e80941Smrg velem->src_offset); 1700b8e80941Smrg struct lp_build_if_state if_ctx; 1701b8e80941Smrg 1702b8e80941Smrg if (velem->src_format != PIPE_FORMAT_NONE) { 1703b8e80941Smrg vbuffer_ptr = LLVMBuildGEP(builder, vbuffers_ptr, &vb_index, 1, ""); 1704b8e80941Smrg vb_info = LLVMBuildGEP(builder, vb_ptr, &vb_index, 1, ""); 1705b8e80941Smrg vb_stride[j] = draw_jit_vbuffer_stride(gallivm, vb_info); 1706b8e80941Smrg vb_stride[j] = LLVMBuildZExt(gallivm->builder, vb_stride[j], 1707b8e80941Smrg LLVMInt32TypeInContext(context), ""); 1708b8e80941Smrg vb_buffer_offset = draw_jit_vbuffer_offset(gallivm, vb_info); 1709b8e80941Smrg map_ptr[j] = draw_jit_dvbuffer_map(gallivm, vbuffer_ptr); 1710b8e80941Smrg buffer_size = draw_jit_dvbuffer_size(gallivm, vbuffer_ptr); 1711b8e80941Smrg 1712b8e80941Smrg ofbit = NULL; 1713b8e80941Smrg /* 1714b8e80941Smrg * We'll set buffer_size_adj to zero if we have of, so it will 1715b8e80941Smrg * always overflow later automatically without having to keep ofbit. 1716b8e80941Smrg * Overflows (with normal wraparound) doing the actual offset 1717b8e80941Smrg * calculation should be ok, just not for the buffer size calc. 1718b8e80941Smrg * It would also be possible to detect such overflows and return 1719b8e80941Smrg * zeros if that happens, but this would be more complex. 1720b8e80941Smrg */ 1721b8e80941Smrg buf_offset = lp_build_add(&bld, vb_buffer_offset, src_offset); 1722b8e80941Smrg tmp = lp_build_sub(&bld, bsize, bld.one); 1723b8e80941Smrg buffer_size_adj[j] = lp_build_usub_overflow(gallivm, buffer_size, tmp, 1724b8e80941Smrg &ofbit); 1725b8e80941Smrg buffer_size_adj[j] = lp_build_usub_overflow(gallivm, buffer_size_adj[j], 1726b8e80941Smrg buf_offset, &ofbit); 1727b8e80941Smrg 1728b8e80941Smrg /* 1729b8e80941Smrg * We can't easily set fake vertex buffers outside the generated code. 1730b8e80941Smrg * Hence, set fake vertex buffers here instead basically, so fetch 1731b8e80941Smrg * code can always fetch using offset 0, eliminating all control flow 1732b8e80941Smrg * inside the main loop. 1733b8e80941Smrg * (Alternatively, could have control flow per vector skipping fetch 1734b8e80941Smrg * if ofbit is true.) 1735b8e80941Smrg */ 1736b8e80941Smrg if (velem->instance_divisor) { 1737b8e80941Smrg /* 1738b8e80941Smrg * Index is equal to the start instance plus the number of current 1739b8e80941Smrg * instance divided by the divisor. In this case we compute it as: 1740b8e80941Smrg * index = start_instance + (instance_id / divisor). 1741b8e80941Smrg * Note we could actually do the fetch here, outside the loop - 1742b8e80941Smrg * it's all constant, hopefully llvm recognizes this. 1743b8e80941Smrg */ 1744b8e80941Smrg LLVMValueRef current_instance; 1745b8e80941Smrg current_instance = LLVMBuildUDiv(builder, system_values.instance_id, 1746b8e80941Smrg lp_build_const_int32(gallivm, 1747b8e80941Smrg velem->instance_divisor), 1748b8e80941Smrg "instance_divisor"); 1749b8e80941Smrg instance_index[j] = lp_build_uadd_overflow(gallivm, start_instance, 1750b8e80941Smrg current_instance, &ofbit); 1751b8e80941Smrg } 1752b8e80941Smrg 1753b8e80941Smrg buffer_size_adj[j] = LLVMBuildSelect(builder, ofbit, bld.zero, 1754b8e80941Smrg buffer_size_adj[j], ""); 1755b8e80941Smrg 1756b8e80941Smrg temp_ptr = lp_build_alloca_undef(gallivm, 1757b8e80941Smrg LLVMPointerType(LLVMInt8TypeInContext(context), 0), ""); 1758b8e80941Smrg 1759b8e80941Smrg lp_build_if(&if_ctx, gallivm, ofbit); 1760b8e80941Smrg { 1761b8e80941Smrg LLVMBuildStore(builder, fake_buf_ptr, temp_ptr); 1762b8e80941Smrg } 1763b8e80941Smrg lp_build_else(&if_ctx); 1764b8e80941Smrg { 1765b8e80941Smrg map_ptr[j] = LLVMBuildGEP(builder, map_ptr[j], &buf_offset, 1, ""); 1766b8e80941Smrg LLVMBuildStore(builder, map_ptr[j], temp_ptr); 1767b8e80941Smrg } 1768b8e80941Smrg lp_build_endif(&if_ctx); 1769b8e80941Smrg map_ptr[j] = LLVMBuildLoad(builder, temp_ptr, "map_ptr"); 1770b8e80941Smrg 1771b8e80941Smrg if (0) { 1772b8e80941Smrg lp_build_printf(gallivm, "velem %d, vbuf index = %u, vb_stride = %u\n", 1773b8e80941Smrg lp_build_const_int32(gallivm, j), 1774b8e80941Smrg vb_index, vb_stride[j]); 1775b8e80941Smrg lp_build_printf(gallivm, 1776b8e80941Smrg " vb_buffer_offset = %u, src_offset = %u, buf_offset = %u\n", 1777b8e80941Smrg vb_buffer_offset, src_offset, buf_offset); 1778b8e80941Smrg lp_build_printf(gallivm, " buffer size = %u, blocksize = %u\n", 1779b8e80941Smrg buffer_size, bsize); 1780b8e80941Smrg lp_build_printf(gallivm, " instance_id = %u\n", system_values.instance_id); 1781b8e80941Smrg } 1782b8e80941Smrg } 1783b8e80941Smrg } 1784b8e80941Smrg 1785b8e80941Smrg lp_build_loop_begin(&lp_loop, gallivm, bld.zero); 1786848b8605Smrg { 1787848b8605Smrg LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS]; 1788848b8605Smrg LLVMValueRef io; 1789848b8605Smrg LLVMValueRef clipmask; /* holds the clipmask value */ 1790b8e80941Smrg LLVMValueRef true_index_array, index_store; 1791848b8605Smrg const LLVMValueRef (*ptr_aos)[TGSI_NUM_CHANNELS]; 1792848b8605Smrg 1793848b8605Smrg io_itr = lp_loop.counter; 1794848b8605Smrg 1795848b8605Smrg io = LLVMBuildGEP(builder, io_ptr, &io_itr, 1, ""); 1796848b8605Smrg#if DEBUG_STORE 1797848b8605Smrg lp_build_printf(gallivm, " --- io %d = %p, loop counter %d\n", 1798848b8605Smrg io_itr, io, lp_loop.counter); 1799848b8605Smrg#endif 1800b8e80941Smrg 1801b8e80941Smrg true_index_array = lp_build_broadcast_scalar(&blduivec, lp_loop.counter); 1802b8e80941Smrg true_index_array = LLVMBuildAdd(builder, true_index_array, ind_vec, ""); 1803b8e80941Smrg 1804b8e80941Smrg /* 1805b8e80941Smrg * Limit indices to fetch_max, otherwise might try to access indices 1806b8e80941Smrg * beyond index buffer (or rather vsplit elt buffer) size. 1807b8e80941Smrg * Could probably safely (?) skip this for non-indexed draws and 1808b8e80941Smrg * simplify things minimally (by removing it could combine the ind_vec 1809b8e80941Smrg * and start_vec adds). I think the only effect for non-indexed draws will 1810b8e80941Smrg * be that for the invalid elements they will be all fetched from the 1811b8e80941Smrg * same location as the last valid one, but noone should really care. 1812b8e80941Smrg */ 1813b8e80941Smrg true_index_array = lp_build_min(&blduivec, true_index_array, fetch_max); 1814b8e80941Smrg 1815b8e80941Smrg index_store = lp_build_alloca_undef(gallivm, blduivec.vec_type, "index_store"); 1816b8e80941Smrg 1817b8e80941Smrg lp_build_if(&if_ctx, gallivm, have_elts); 1818b8e80941Smrg { 1819b8e80941Smrg /* 1820b8e80941Smrg * Note: you'd expect some comparison/clamp against fetch_elt_max 1821b8e80941Smrg * here. 1822b8e80941Smrg * There used to be one here but it was incorrect: overflow was 1823b8e80941Smrg * detected if index > fetch_elt_max - but the correct condition 1824b8e80941Smrg * would be index >= fetch_elt_max (since this is just size of elts 1825b8e80941Smrg * buffer / element size). 1826b8e80941Smrg * Using the correct condition however will cause failures - due to 1827b8e80941Smrg * vsplit/vcache code which rebases indices. So, as an example, if 1828b8e80941Smrg * fetch_elt_max is just 1 and fetch_count 2, vsplit cache will 1829b8e80941Smrg * replace all invalid indices with 0 - which in case of elt_bias 1830b8e80941Smrg * not being zero will get a different fetch index than the valid 1831b8e80941Smrg * index 0. So, just rely on vsplit code preventing out-of-bounds 1832b8e80941Smrg * fetches. This is also why it's safe to do elts fetch even if there 1833b8e80941Smrg * was no index buffer bound - the real buffer is never seen here, at 1834b8e80941Smrg * least not if there are index buffer overflows... 1835b8e80941Smrg */ 1836b8e80941Smrg 1837b8e80941Smrg /* 1838b8e80941Smrg * XXX should not have to do this, as scale can be handled 1839b8e80941Smrg * natively by loads (hits asserts though). 1840b8e80941Smrg */ 1841b8e80941Smrg tmp = lp_build_shl_imm(&blduivec, true_index_array, 2); 1842b8e80941Smrg fetch_elts = LLVMBuildBitCast(builder, fetch_elts, 1843b8e80941Smrg LLVMPointerType(LLVMInt8TypeInContext(context), 1844b8e80941Smrg 0), ""); 1845b8e80941Smrg tmp = lp_build_gather(gallivm, vs_type.length, 1846b8e80941Smrg 32, bld.type, TRUE, 1847b8e80941Smrg fetch_elts, tmp, FALSE); 1848b8e80941Smrg LLVMBuildStore(builder, tmp, index_store); 1849b8e80941Smrg } 1850b8e80941Smrg lp_build_else(&if_ctx); 1851b8e80941Smrg { 1852b8e80941Smrg tmp = LLVMBuildAdd(builder, true_index_array, start_vec, ""); 1853b8e80941Smrg LLVMBuildStore(builder, tmp, index_store); 1854b8e80941Smrg } 1855b8e80941Smrg lp_build_endif(&if_ctx); 1856b8e80941Smrg 1857b8e80941Smrg true_index_array = LLVMBuildLoad(builder, index_store, ""); 1858b8e80941Smrg 1859b8e80941Smrg for (j = 0; j < key->nr_vertex_elements; ++j) { 1860b8e80941Smrg struct pipe_vertex_element *velem = &key->vertex_element[j]; 1861b8e80941Smrg const struct util_format_description *format_desc = 1862b8e80941Smrg util_format_description(velem->src_format); 1863b8e80941Smrg 1864b8e80941Smrg if (format_desc->format == PIPE_FORMAT_NONE) { 1865b8e80941Smrg for (i = 0; i < TGSI_NUM_CHANNELS; i++) { 1866b8e80941Smrg inputs[j][i] = lp_build_zero(gallivm, vs_type); 1867848b8605Smrg } 1868848b8605Smrg } 1869b8e80941Smrg else if (velem->instance_divisor) { 1870b8e80941Smrg fetch_instanced(gallivm, format_desc, vs_type, 1871b8e80941Smrg vb_stride[j], map_ptr[j], 1872b8e80941Smrg buffer_size_adj[j], 1873b8e80941Smrg inputs[j], instance_index[j]); 1874b8e80941Smrg } 1875b8e80941Smrg else { 1876b8e80941Smrg fetch_vector(gallivm, format_desc, vs_type, 1877b8e80941Smrg vb_stride[j], map_ptr[j], 1878b8e80941Smrg buffer_size_adj[j], 1879b8e80941Smrg inputs[j], true_index_array); 1880848b8605Smrg } 1881848b8605Smrg } 1882b8e80941Smrg 1883b8e80941Smrg /* In the paths with elts vertex id has to be unaffected by the 1884b8e80941Smrg * index bias and because indices inside our elements array have 1885b8e80941Smrg * already had index bias applied we need to subtract it here to 1886b8e80941Smrg * get back to the original index. 1887b8e80941Smrg * in the linear paths vertex id has to be unaffected by the 1888b8e80941Smrg * original start index and because we abuse the 'start' variable 1889b8e80941Smrg * to either represent the actual start index or the index at which 1890b8e80941Smrg * the primitive was split (we split rendering into chunks of at 1891b8e80941Smrg * most 4095-vertices) we need to back out the original start 1892b8e80941Smrg * index out of our vertex id here. 1893b8e80941Smrg */ 1894b8e80941Smrg system_values.basevertex = lp_build_broadcast_scalar(&blduivec, 1895b8e80941Smrg vertex_id_offset); 1896b8e80941Smrg system_values.vertex_id = true_index_array; 1897b8e80941Smrg system_values.vertex_id_nobase = LLVMBuildSub(builder, true_index_array, 1898b8e80941Smrg system_values.basevertex, ""); 1899848b8605Smrg 1900848b8605Smrg ptr_aos = (const LLVMValueRef (*)[TGSI_NUM_CHANNELS]) inputs; 1901848b8605Smrg generate_vs(variant, 1902848b8605Smrg builder, 1903848b8605Smrg vs_type, 1904848b8605Smrg outputs, 1905848b8605Smrg ptr_aos, 1906848b8605Smrg &system_values, 1907848b8605Smrg context_ptr, 1908848b8605Smrg sampler, 1909848b8605Smrg key->clamp_vertex_color); 1910848b8605Smrg 1911848b8605Smrg if (pos != -1 && cv != -1) { 1912848b8605Smrg /* store original positions in clip before further manipulation */ 1913b8e80941Smrg store_clip(gallivm, vs_type, io, outputs, pos); 1914848b8605Smrg 1915848b8605Smrg /* do cliptest */ 1916848b8605Smrg if (enable_cliptest) { 1917848b8605Smrg LLVMValueRef temp = LLVMBuildLoad(builder, clipmask_bool_ptr, ""); 1918848b8605Smrg /* allocate clipmask, assign it integer type */ 1919848b8605Smrg clipmask = generate_clipmask(llvm, 1920848b8605Smrg gallivm, 1921848b8605Smrg vs_type, 1922848b8605Smrg outputs, 1923b8e80941Smrg key, 1924848b8605Smrg context_ptr, &have_clipdist); 1925848b8605Smrg temp = LLVMBuildOr(builder, clipmask, temp, ""); 1926848b8605Smrg /* store temporary clipping boolean value */ 1927848b8605Smrg LLVMBuildStore(builder, temp, clipmask_bool_ptr); 1928848b8605Smrg } 1929848b8605Smrg else { 1930b8e80941Smrg clipmask = blduivec.zero; 1931848b8605Smrg } 1932848b8605Smrg 1933848b8605Smrg /* do viewport mapping */ 1934848b8605Smrg if (!bypass_viewport) { 1935848b8605Smrg generate_viewport(variant, builder, vs_type, outputs, context_ptr); 1936848b8605Smrg } 1937848b8605Smrg } 1938848b8605Smrg else { 1939b8e80941Smrg clipmask = blduivec.zero; 1940848b8605Smrg } 1941848b8605Smrg 1942848b8605Smrg /* store clipmask in vertex header, 1943848b8605Smrg * original positions in clip 1944848b8605Smrg * and transformed positions in data 1945848b8605Smrg */ 1946848b8605Smrg convert_to_aos(gallivm, io, NULL, outputs, clipmask, 1947848b8605Smrg vs_info->num_outputs, vs_type, 1948b8e80941Smrg enable_cliptest && key->need_edgeflags); 1949848b8605Smrg } 1950848b8605Smrg lp_build_loop_end_cond(&lp_loop, count, step, LLVMIntUGE); 1951848b8605Smrg 1952848b8605Smrg sampler->destroy(sampler); 1953848b8605Smrg 1954848b8605Smrg /* return clipping boolean value for function */ 1955b8e80941Smrg ret = clipmask_booli8(gallivm, vs_type, clipmask_bool_ptr, 1956b8e80941Smrg enable_cliptest && key->need_edgeflags); 1957848b8605Smrg 1958848b8605Smrg LLVMBuildRet(builder, ret); 1959848b8605Smrg 1960848b8605Smrg gallivm_verify_function(gallivm, variant_func); 1961848b8605Smrg} 1962848b8605Smrg 1963848b8605Smrg 1964848b8605Smrgstruct draw_llvm_variant_key * 1965848b8605Smrgdraw_llvm_make_variant_key(struct draw_llvm *llvm, char *store) 1966848b8605Smrg{ 1967848b8605Smrg unsigned i; 1968848b8605Smrg struct draw_llvm_variant_key *key; 1969848b8605Smrg struct draw_sampler_static_state *draw_sampler; 1970848b8605Smrg 1971848b8605Smrg key = (struct draw_llvm_variant_key *)store; 1972848b8605Smrg 1973b8e80941Smrg memset(key, 0, offsetof(struct draw_llvm_variant_key, vertex_element[0])); 1974848b8605Smrg 1975b8e80941Smrg key->clamp_vertex_color = llvm->draw->rasterizer->clamp_vertex_color; /**/ 1976848b8605Smrg 1977848b8605Smrg /* will have to rig this up properly later */ 1978848b8605Smrg key->clip_xy = llvm->draw->clip_xy; 1979848b8605Smrg key->clip_z = llvm->draw->clip_z; 1980848b8605Smrg key->clip_user = llvm->draw->clip_user; 1981b8e80941Smrg key->bypass_viewport = llvm->draw->bypass_viewport; 1982848b8605Smrg key->clip_halfz = llvm->draw->rasterizer->clip_halfz; 1983b8e80941Smrg /* XXX assumes edgeflag output not at 0 */ 1984848b8605Smrg key->need_edgeflags = (llvm->draw->vs.edgeflag_output ? TRUE : FALSE); 1985848b8605Smrg key->ucp_enable = llvm->draw->rasterizer->clip_plane_enable; 1986848b8605Smrg key->has_gs = llvm->draw->gs.geometry_shader != NULL; 1987848b8605Smrg key->num_outputs = draw_total_vs_outputs(llvm->draw); 1988848b8605Smrg 1989848b8605Smrg /* All variants of this shader will have the same value for 1990848b8605Smrg * nr_samplers. Not yet trying to compact away holes in the 1991848b8605Smrg * sampler array. 1992848b8605Smrg */ 1993848b8605Smrg key->nr_samplers = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER] + 1; 1994848b8605Smrg if (llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) { 1995848b8605Smrg key->nr_sampler_views = 1996848b8605Smrg llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1; 1997848b8605Smrg } 1998848b8605Smrg else { 1999848b8605Smrg key->nr_sampler_views = key->nr_samplers; 2000848b8605Smrg } 2001848b8605Smrg 2002b8e80941Smrg /* Presumably all variants of the shader should have the same 2003b8e80941Smrg * number of vertex elements - ie the number of shader inputs. 2004b8e80941Smrg * NOTE: we NEED to store the needed number of needed inputs 2005b8e80941Smrg * here, not the number of provided elements to match keysize 2006b8e80941Smrg * (and the offset of sampler state in the key). 2007b8e80941Smrg * If we have excess number of vertex elements, this is valid, 2008b8e80941Smrg * but the excess ones don't matter. 2009b8e80941Smrg * If we don't have enough vertex elements (which looks not really 2010b8e80941Smrg * valid but we'll handle it gracefully) fill out missing ones with 2011b8e80941Smrg * zero (we'll recognize these later by PIPE_FORMAT_NONE). 2012b8e80941Smrg */ 2013b8e80941Smrg key->nr_vertex_elements = 2014b8e80941Smrg llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_INPUT] + 1; 2015b8e80941Smrg 2016b8e80941Smrg if (llvm->draw->pt.nr_vertex_elements < key->nr_vertex_elements) { 2017b8e80941Smrg debug_printf("draw: vs with %d inputs but only have %d vertex elements\n", 2018b8e80941Smrg key->nr_vertex_elements, llvm->draw->pt.nr_vertex_elements); 2019b8e80941Smrg memset(key->vertex_element, 0, 2020b8e80941Smrg sizeof(struct pipe_vertex_element) * key->nr_vertex_elements); 2021b8e80941Smrg } 2022848b8605Smrg memcpy(key->vertex_element, 2023848b8605Smrg llvm->draw->pt.vertex_element, 2024b8e80941Smrg sizeof(struct pipe_vertex_element) * 2025b8e80941Smrg MIN2(key->nr_vertex_elements, llvm->draw->pt.nr_vertex_elements)); 2026848b8605Smrg 2027b8e80941Smrg draw_sampler = draw_llvm_variant_key_samplers(key); 2028b8e80941Smrg memset(draw_sampler, 0, 2029b8e80941Smrg MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler); 2030848b8605Smrg 2031848b8605Smrg for (i = 0 ; i < key->nr_samplers; i++) { 2032848b8605Smrg lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state, 2033848b8605Smrg llvm->draw->samplers[PIPE_SHADER_VERTEX][i]); 2034848b8605Smrg } 2035848b8605Smrg for (i = 0 ; i < key->nr_sampler_views; i++) { 2036848b8605Smrg lp_sampler_static_texture_state(&draw_sampler[i].texture_state, 2037848b8605Smrg llvm->draw->sampler_views[PIPE_SHADER_VERTEX][i]); 2038848b8605Smrg } 2039848b8605Smrg 2040848b8605Smrg return key; 2041848b8605Smrg} 2042848b8605Smrg 2043848b8605Smrg 2044848b8605Smrgvoid 2045848b8605Smrgdraw_llvm_dump_variant_key(struct draw_llvm_variant_key *key) 2046848b8605Smrg{ 2047848b8605Smrg unsigned i; 2048848b8605Smrg struct draw_sampler_static_state *sampler = draw_llvm_variant_key_samplers(key); 2049848b8605Smrg 2050848b8605Smrg debug_printf("clamp_vertex_color = %u\n", key->clamp_vertex_color); 2051848b8605Smrg debug_printf("clip_xy = %u\n", key->clip_xy); 2052848b8605Smrg debug_printf("clip_z = %u\n", key->clip_z); 2053848b8605Smrg debug_printf("clip_user = %u\n", key->clip_user); 2054848b8605Smrg debug_printf("bypass_viewport = %u\n", key->bypass_viewport); 2055848b8605Smrg debug_printf("clip_halfz = %u\n", key->clip_halfz); 2056848b8605Smrg debug_printf("need_edgeflags = %u\n", key->need_edgeflags); 2057848b8605Smrg debug_printf("has_gs = %u\n", key->has_gs); 2058848b8605Smrg debug_printf("ucp_enable = %u\n", key->ucp_enable); 2059848b8605Smrg 2060848b8605Smrg for (i = 0 ; i < key->nr_vertex_elements; i++) { 2061848b8605Smrg debug_printf("vertex_element[%i].src_offset = %u\n", i, key->vertex_element[i].src_offset); 2062848b8605Smrg debug_printf("vertex_element[%i].instance_divisor = %u\n", i, key->vertex_element[i].instance_divisor); 2063848b8605Smrg debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i, key->vertex_element[i].vertex_buffer_index); 2064848b8605Smrg debug_printf("vertex_element[%i].src_format = %s\n", i, util_format_name(key->vertex_element[i].src_format)); 2065848b8605Smrg } 2066848b8605Smrg 2067848b8605Smrg for (i = 0 ; i < key->nr_sampler_views; i++) { 2068848b8605Smrg debug_printf("sampler[%i].src_format = %s\n", i, util_format_name(sampler[i].texture_state.format)); 2069848b8605Smrg } 2070848b8605Smrg} 2071848b8605Smrg 2072848b8605Smrg 2073848b8605Smrgvoid 2074848b8605Smrgdraw_llvm_set_mapped_texture(struct draw_context *draw, 2075b8e80941Smrg enum pipe_shader_type shader_stage, 2076848b8605Smrg unsigned sview_idx, 2077848b8605Smrg uint32_t width, uint32_t height, uint32_t depth, 2078848b8605Smrg uint32_t first_level, uint32_t last_level, 2079848b8605Smrg const void *base_ptr, 2080848b8605Smrg uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS], 2081848b8605Smrg uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS], 2082848b8605Smrg uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS]) 2083848b8605Smrg{ 2084848b8605Smrg unsigned j; 2085848b8605Smrg struct draw_jit_texture *jit_tex; 2086848b8605Smrg 2087848b8605Smrg assert(shader_stage == PIPE_SHADER_VERTEX || 2088848b8605Smrg shader_stage == PIPE_SHADER_GEOMETRY); 2089848b8605Smrg 2090848b8605Smrg if (shader_stage == PIPE_SHADER_VERTEX) { 2091b8e80941Smrg assert(sview_idx < ARRAY_SIZE(draw->llvm->jit_context.textures)); 2092848b8605Smrg 2093848b8605Smrg jit_tex = &draw->llvm->jit_context.textures[sview_idx]; 2094848b8605Smrg } else if (shader_stage == PIPE_SHADER_GEOMETRY) { 2095b8e80941Smrg assert(sview_idx < ARRAY_SIZE(draw->llvm->gs_jit_context.textures)); 2096848b8605Smrg 2097848b8605Smrg jit_tex = &draw->llvm->gs_jit_context.textures[sview_idx]; 2098848b8605Smrg } else { 2099848b8605Smrg assert(0); 2100848b8605Smrg return; 2101848b8605Smrg } 2102848b8605Smrg 2103848b8605Smrg jit_tex->width = width; 2104848b8605Smrg jit_tex->height = height; 2105848b8605Smrg jit_tex->depth = depth; 2106848b8605Smrg jit_tex->first_level = first_level; 2107848b8605Smrg jit_tex->last_level = last_level; 2108848b8605Smrg jit_tex->base = base_ptr; 2109848b8605Smrg 2110848b8605Smrg for (j = first_level; j <= last_level; j++) { 2111848b8605Smrg jit_tex->mip_offsets[j] = mip_offsets[j]; 2112848b8605Smrg jit_tex->row_stride[j] = row_stride[j]; 2113848b8605Smrg jit_tex->img_stride[j] = img_stride[j]; 2114848b8605Smrg } 2115848b8605Smrg} 2116848b8605Smrg 2117848b8605Smrg 2118848b8605Smrgvoid 2119848b8605Smrgdraw_llvm_set_sampler_state(struct draw_context *draw, 2120b8e80941Smrg enum pipe_shader_type shader_type) 2121848b8605Smrg{ 2122848b8605Smrg unsigned i; 2123848b8605Smrg 2124848b8605Smrg if (shader_type == PIPE_SHADER_VERTEX) { 2125848b8605Smrg for (i = 0; i < draw->num_samplers[PIPE_SHADER_VERTEX]; i++) { 2126848b8605Smrg struct draw_jit_sampler *jit_sam = &draw->llvm->jit_context.samplers[i]; 2127848b8605Smrg 2128b8e80941Smrg if (draw->samplers[PIPE_SHADER_VERTEX][i]) { 2129848b8605Smrg const struct pipe_sampler_state *s 2130848b8605Smrg = draw->samplers[PIPE_SHADER_VERTEX][i]; 2131848b8605Smrg jit_sam->min_lod = s->min_lod; 2132848b8605Smrg jit_sam->max_lod = s->max_lod; 2133848b8605Smrg jit_sam->lod_bias = s->lod_bias; 2134848b8605Smrg COPY_4V(jit_sam->border_color, s->border_color.f); 2135848b8605Smrg } 2136848b8605Smrg } 2137848b8605Smrg } else if (shader_type == PIPE_SHADER_GEOMETRY) { 2138848b8605Smrg for (i = 0; i < draw->num_samplers[PIPE_SHADER_GEOMETRY]; i++) { 2139848b8605Smrg struct draw_jit_sampler *jit_sam = &draw->llvm->gs_jit_context.samplers[i]; 2140848b8605Smrg 2141b8e80941Smrg if (draw->samplers[PIPE_SHADER_GEOMETRY][i]) { 2142848b8605Smrg const struct pipe_sampler_state *s 2143848b8605Smrg = draw->samplers[PIPE_SHADER_GEOMETRY][i]; 2144848b8605Smrg jit_sam->min_lod = s->min_lod; 2145848b8605Smrg jit_sam->max_lod = s->max_lod; 2146848b8605Smrg jit_sam->lod_bias = s->lod_bias; 2147848b8605Smrg COPY_4V(jit_sam->border_color, s->border_color.f); 2148848b8605Smrg } 2149848b8605Smrg } 2150848b8605Smrg } 2151848b8605Smrg} 2152848b8605Smrg 2153848b8605Smrg 2154848b8605Smrgvoid 2155848b8605Smrgdraw_llvm_destroy_variant(struct draw_llvm_variant *variant) 2156848b8605Smrg{ 2157848b8605Smrg struct draw_llvm *llvm = variant->llvm; 2158848b8605Smrg 2159b8e80941Smrg if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) { 2160b8e80941Smrg debug_printf("Deleting VS variant: %u vs variants,\t%u total variants\n", 2161b8e80941Smrg variant->shader->variants_cached, llvm->nr_variants); 2162b8e80941Smrg } 2163b8e80941Smrg 2164848b8605Smrg gallivm_destroy(variant->gallivm); 2165848b8605Smrg 2166848b8605Smrg remove_from_list(&variant->list_item_local); 2167848b8605Smrg variant->shader->variants_cached--; 2168848b8605Smrg remove_from_list(&variant->list_item_global); 2169848b8605Smrg llvm->nr_variants--; 2170848b8605Smrg FREE(variant); 2171848b8605Smrg} 2172848b8605Smrg 2173848b8605Smrg 2174848b8605Smrg/** 2175848b8605Smrg * Create LLVM types for various structures. 2176848b8605Smrg */ 2177848b8605Smrgstatic void 2178848b8605Smrgcreate_gs_jit_types(struct draw_gs_llvm_variant *var) 2179848b8605Smrg{ 2180848b8605Smrg struct gallivm_state *gallivm = var->gallivm; 2181848b8605Smrg LLVMTypeRef texture_type, sampler_type, context_type; 2182848b8605Smrg 2183848b8605Smrg texture_type = create_jit_texture_type(gallivm, "texture"); 2184848b8605Smrg sampler_type = create_jit_sampler_type(gallivm, "sampler"); 2185848b8605Smrg 2186848b8605Smrg context_type = create_gs_jit_context_type(gallivm, 2187848b8605Smrg var->shader->base.vector_length, 2188848b8605Smrg texture_type, sampler_type, 2189848b8605Smrg "draw_gs_jit_context"); 2190848b8605Smrg var->context_ptr_type = LLVMPointerType(context_type, 0); 2191848b8605Smrg 2192848b8605Smrg var->input_array_type = create_gs_jit_input_type(gallivm); 2193848b8605Smrg} 2194848b8605Smrg 2195848b8605Smrgstatic LLVMTypeRef 2196848b8605Smrgget_gs_context_ptr_type(struct draw_gs_llvm_variant *variant) 2197848b8605Smrg{ 2198848b8605Smrg if (!variant->context_ptr_type) 2199848b8605Smrg create_gs_jit_types(variant); 2200848b8605Smrg return variant->context_ptr_type; 2201848b8605Smrg} 2202848b8605Smrg 2203848b8605Smrgstatic LLVMValueRef 2204848b8605Smrggenerate_mask_value(struct draw_gs_llvm_variant *variant, 2205848b8605Smrg struct lp_type gs_type) 2206848b8605Smrg{ 2207848b8605Smrg struct gallivm_state *gallivm = variant->gallivm; 2208848b8605Smrg LLVMBuilderRef builder = gallivm->builder; 2209848b8605Smrg struct lp_type mask_type = lp_int_type(gs_type); 2210848b8605Smrg LLVMValueRef num_prims; 2211848b8605Smrg LLVMValueRef mask_val = lp_build_const_vec(gallivm, mask_type, 0); 2212848b8605Smrg unsigned i; 2213848b8605Smrg 2214848b8605Smrg num_prims = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, mask_type), 2215848b8605Smrg variant->num_prims); 2216b8e80941Smrg for (i = 0; i < gs_type.length; i++) { 2217848b8605Smrg LLVMValueRef idx = lp_build_const_int32(gallivm, i); 2218848b8605Smrg mask_val = LLVMBuildInsertElement(builder, mask_val, idx, idx, ""); 2219848b8605Smrg } 2220848b8605Smrg mask_val = lp_build_compare(gallivm, mask_type, 2221848b8605Smrg PIPE_FUNC_GREATER, num_prims, mask_val); 2222848b8605Smrg 2223848b8605Smrg return mask_val; 2224848b8605Smrg} 2225848b8605Smrg 2226848b8605Smrgstatic void 2227848b8605Smrgdraw_gs_llvm_generate(struct draw_llvm *llvm, 2228848b8605Smrg struct draw_gs_llvm_variant *variant) 2229848b8605Smrg{ 2230848b8605Smrg struct gallivm_state *gallivm = variant->gallivm; 2231848b8605Smrg LLVMContextRef context = gallivm->context; 2232848b8605Smrg LLVMTypeRef int32_type = LLVMInt32TypeInContext(context); 2233b8e80941Smrg LLVMTypeRef arg_types[7]; 2234848b8605Smrg LLVMTypeRef func_type; 2235848b8605Smrg LLVMValueRef variant_func; 2236848b8605Smrg LLVMValueRef context_ptr; 2237848b8605Smrg LLVMValueRef prim_id_ptr; 2238848b8605Smrg LLVMBasicBlockRef block; 2239848b8605Smrg LLVMBuilderRef builder; 2240848b8605Smrg LLVMValueRef io_ptr, input_array, num_prims, mask_val; 2241848b8605Smrg struct lp_build_sampler_soa *sampler = 0; 2242848b8605Smrg struct lp_build_context bld; 2243848b8605Smrg struct lp_bld_tgsi_system_values system_values; 2244848b8605Smrg char func_name[64]; 2245848b8605Smrg struct lp_type gs_type; 2246848b8605Smrg unsigned i; 2247848b8605Smrg struct draw_gs_llvm_iface gs_iface; 2248848b8605Smrg const struct tgsi_token *tokens = variant->shader->base.state.tokens; 2249848b8605Smrg LLVMValueRef consts_ptr, num_consts_ptr; 2250848b8605Smrg LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS]; 2251848b8605Smrg struct lp_build_mask_context mask; 2252848b8605Smrg const struct tgsi_shader_info *gs_info = &variant->shader->base.info; 2253848b8605Smrg unsigned vector_length = variant->shader->base.vector_length; 2254848b8605Smrg 2255848b8605Smrg memset(&system_values, 0, sizeof(system_values)); 2256848b8605Smrg 2257848b8605Smrg util_snprintf(func_name, sizeof(func_name), "draw_llvm_gs_variant%u", 2258848b8605Smrg variant->shader->variants_cached); 2259848b8605Smrg 2260848b8605Smrg assert(variant->vertex_header_ptr_type); 2261848b8605Smrg 2262848b8605Smrg arg_types[0] = get_gs_context_ptr_type(variant); /* context */ 2263848b8605Smrg arg_types[1] = variant->input_array_type; /* input */ 2264848b8605Smrg arg_types[2] = variant->vertex_header_ptr_type; /* vertex_header */ 2265848b8605Smrg arg_types[3] = int32_type; /* num_prims */ 2266848b8605Smrg arg_types[4] = int32_type; /* instance_id */ 2267848b8605Smrg arg_types[5] = LLVMPointerType( 2268848b8605Smrg LLVMVectorType(int32_type, vector_length), 0); /* prim_id_ptr */ 2269b8e80941Smrg arg_types[6] = int32_type; 2270848b8605Smrg 2271b8e80941Smrg func_type = LLVMFunctionType(int32_type, arg_types, ARRAY_SIZE(arg_types), 0); 2272848b8605Smrg 2273848b8605Smrg variant_func = LLVMAddFunction(gallivm->module, func_name, func_type); 2274848b8605Smrg 2275848b8605Smrg variant->function = variant_func; 2276848b8605Smrg 2277848b8605Smrg LLVMSetFunctionCallConv(variant_func, LLVMCCallConv); 2278848b8605Smrg 2279b8e80941Smrg for (i = 0; i < ARRAY_SIZE(arg_types); ++i) 2280848b8605Smrg if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) 2281b8e80941Smrg lp_add_function_attr(variant_func, i + 1, LP_FUNC_ATTR_NOALIAS); 2282848b8605Smrg 2283848b8605Smrg context_ptr = LLVMGetParam(variant_func, 0); 2284848b8605Smrg input_array = LLVMGetParam(variant_func, 1); 2285848b8605Smrg io_ptr = LLVMGetParam(variant_func, 2); 2286848b8605Smrg num_prims = LLVMGetParam(variant_func, 3); 2287848b8605Smrg system_values.instance_id = LLVMGetParam(variant_func, 4); 2288848b8605Smrg prim_id_ptr = LLVMGetParam(variant_func, 5); 2289b8e80941Smrg system_values.invocation_id = LLVMGetParam(variant_func, 6); 2290848b8605Smrg 2291848b8605Smrg lp_build_name(context_ptr, "context"); 2292848b8605Smrg lp_build_name(input_array, "input"); 2293848b8605Smrg lp_build_name(io_ptr, "io"); 2294848b8605Smrg lp_build_name(num_prims, "num_prims"); 2295848b8605Smrg lp_build_name(system_values.instance_id, "instance_id"); 2296848b8605Smrg lp_build_name(prim_id_ptr, "prim_id_ptr"); 2297b8e80941Smrg lp_build_name(system_values.invocation_id, "invocation_id"); 2298848b8605Smrg 2299848b8605Smrg variant->context_ptr = context_ptr; 2300848b8605Smrg variant->io_ptr = io_ptr; 2301848b8605Smrg variant->num_prims = num_prims; 2302848b8605Smrg 2303848b8605Smrg gs_iface.base.fetch_input = draw_gs_llvm_fetch_input; 2304848b8605Smrg gs_iface.base.emit_vertex = draw_gs_llvm_emit_vertex; 2305848b8605Smrg gs_iface.base.end_primitive = draw_gs_llvm_end_primitive; 2306848b8605Smrg gs_iface.base.gs_epilogue = draw_gs_llvm_epilogue; 2307848b8605Smrg gs_iface.input = input_array; 2308848b8605Smrg gs_iface.variant = variant; 2309848b8605Smrg 2310848b8605Smrg /* 2311848b8605Smrg * Function body 2312848b8605Smrg */ 2313848b8605Smrg 2314848b8605Smrg block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry"); 2315848b8605Smrg builder = gallivm->builder; 2316848b8605Smrg LLVMPositionBuilderAtEnd(builder, block); 2317848b8605Smrg 2318848b8605Smrg lp_build_context_init(&bld, gallivm, lp_type_int(32)); 2319848b8605Smrg 2320848b8605Smrg memset(&gs_type, 0, sizeof gs_type); 2321848b8605Smrg gs_type.floating = TRUE; /* floating point values */ 2322848b8605Smrg gs_type.sign = TRUE; /* values are signed */ 2323848b8605Smrg gs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */ 2324848b8605Smrg gs_type.width = 32; /* 32-bit float */ 2325848b8605Smrg gs_type.length = vector_length; 2326848b8605Smrg 2327848b8605Smrg consts_ptr = draw_gs_jit_context_constants(variant->gallivm, context_ptr); 2328848b8605Smrg num_consts_ptr = 2329848b8605Smrg draw_gs_jit_context_num_constants(variant->gallivm, context_ptr); 2330848b8605Smrg 2331848b8605Smrg /* code generated texture sampling */ 2332b8e80941Smrg sampler = draw_llvm_sampler_soa_create(variant->key.samplers); 2333848b8605Smrg 2334848b8605Smrg mask_val = generate_mask_value(variant, gs_type); 2335848b8605Smrg lp_build_mask_begin(&mask, gallivm, gs_type, mask_val); 2336848b8605Smrg 2337848b8605Smrg if (gs_info->uses_primid) { 2338848b8605Smrg system_values.prim_id = LLVMBuildLoad(builder, prim_id_ptr, "prim_id"); 2339848b8605Smrg } 2340848b8605Smrg 2341848b8605Smrg if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) { 2342848b8605Smrg tgsi_dump(tokens, 0); 2343848b8605Smrg draw_gs_llvm_dump_variant_key(&variant->key); 2344848b8605Smrg } 2345848b8605Smrg 2346848b8605Smrg lp_build_tgsi_soa(variant->gallivm, 2347848b8605Smrg tokens, 2348848b8605Smrg gs_type, 2349848b8605Smrg &mask, 2350848b8605Smrg consts_ptr, 2351848b8605Smrg num_consts_ptr, 2352848b8605Smrg &system_values, 2353848b8605Smrg NULL, 2354848b8605Smrg outputs, 2355b8e80941Smrg context_ptr, 2356b8e80941Smrg NULL, 2357848b8605Smrg sampler, 2358848b8605Smrg &llvm->draw->gs.geometry_shader->info, 2359848b8605Smrg (const struct lp_build_tgsi_gs_iface *)&gs_iface); 2360848b8605Smrg 2361848b8605Smrg sampler->destroy(sampler); 2362848b8605Smrg 2363848b8605Smrg lp_build_mask_end(&mask); 2364848b8605Smrg 2365848b8605Smrg LLVMBuildRet(builder, lp_build_zero(gallivm, lp_type_uint(32))); 2366848b8605Smrg 2367848b8605Smrg gallivm_verify_function(gallivm, variant_func); 2368848b8605Smrg} 2369848b8605Smrg 2370848b8605Smrg 2371848b8605Smrgstruct draw_gs_llvm_variant * 2372848b8605Smrgdraw_gs_llvm_create_variant(struct draw_llvm *llvm, 2373848b8605Smrg unsigned num_outputs, 2374848b8605Smrg const struct draw_gs_llvm_variant_key *key) 2375848b8605Smrg{ 2376848b8605Smrg struct draw_gs_llvm_variant *variant; 2377848b8605Smrg struct llvm_geometry_shader *shader = 2378848b8605Smrg llvm_geometry_shader(llvm->draw->gs.geometry_shader); 2379848b8605Smrg LLVMTypeRef vertex_header; 2380848b8605Smrg char module_name[64]; 2381848b8605Smrg 2382848b8605Smrg variant = MALLOC(sizeof *variant + 2383848b8605Smrg shader->variant_key_size - 2384848b8605Smrg sizeof variant->key); 2385b8e80941Smrg if (!variant) 2386848b8605Smrg return NULL; 2387848b8605Smrg 2388848b8605Smrg variant->llvm = llvm; 2389848b8605Smrg variant->shader = shader; 2390848b8605Smrg 2391848b8605Smrg util_snprintf(module_name, sizeof(module_name), "draw_llvm_gs_variant%u", 2392848b8605Smrg variant->shader->variants_cached); 2393848b8605Smrg 2394b8e80941Smrg variant->gallivm = gallivm_create(module_name, llvm->context); 2395848b8605Smrg 2396848b8605Smrg create_gs_jit_types(variant); 2397848b8605Smrg 2398848b8605Smrg memcpy(&variant->key, key, shader->variant_key_size); 2399848b8605Smrg 2400848b8605Smrg vertex_header = create_jit_vertex_header(variant->gallivm, num_outputs); 2401848b8605Smrg 2402848b8605Smrg variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0); 2403848b8605Smrg 2404848b8605Smrg draw_gs_llvm_generate(llvm, variant); 2405848b8605Smrg 2406848b8605Smrg gallivm_compile_module(variant->gallivm); 2407848b8605Smrg 2408848b8605Smrg variant->jit_func = (draw_gs_jit_func) 2409848b8605Smrg gallivm_jit_function(variant->gallivm, variant->function); 2410848b8605Smrg 2411848b8605Smrg gallivm_free_ir(variant->gallivm); 2412848b8605Smrg 2413848b8605Smrg variant->list_item_global.base = variant; 2414848b8605Smrg variant->list_item_local.base = variant; 2415848b8605Smrg /*variant->no = */shader->variants_created++; 2416848b8605Smrg variant->list_item_global.base = variant; 2417848b8605Smrg 2418848b8605Smrg return variant; 2419848b8605Smrg} 2420848b8605Smrg 2421848b8605Smrgvoid 2422848b8605Smrgdraw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant *variant) 2423848b8605Smrg{ 2424848b8605Smrg struct draw_llvm *llvm = variant->llvm; 2425848b8605Smrg 2426b8e80941Smrg if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) { 2427b8e80941Smrg debug_printf("Deleting GS variant: %u gs variants,\t%u total variants\n", 2428b8e80941Smrg variant->shader->variants_cached, llvm->nr_gs_variants); 2429b8e80941Smrg } 2430b8e80941Smrg 2431848b8605Smrg gallivm_destroy(variant->gallivm); 2432848b8605Smrg 2433848b8605Smrg remove_from_list(&variant->list_item_local); 2434848b8605Smrg variant->shader->variants_cached--; 2435848b8605Smrg remove_from_list(&variant->list_item_global); 2436848b8605Smrg llvm->nr_gs_variants--; 2437848b8605Smrg FREE(variant); 2438848b8605Smrg} 2439848b8605Smrg 2440848b8605Smrgstruct draw_gs_llvm_variant_key * 2441848b8605Smrgdraw_gs_llvm_make_variant_key(struct draw_llvm *llvm, char *store) 2442848b8605Smrg{ 2443848b8605Smrg unsigned i; 2444848b8605Smrg struct draw_gs_llvm_variant_key *key; 2445848b8605Smrg struct draw_sampler_static_state *draw_sampler; 2446848b8605Smrg 2447848b8605Smrg key = (struct draw_gs_llvm_variant_key *)store; 2448848b8605Smrg 2449b8e80941Smrg memset(key, 0, offsetof(struct draw_gs_llvm_variant_key, samplers[0])); 2450b8e80941Smrg 2451848b8605Smrg key->num_outputs = draw_total_gs_outputs(llvm->draw); 2452848b8605Smrg 2453848b8605Smrg /* All variants of this shader will have the same value for 2454848b8605Smrg * nr_samplers. Not yet trying to compact away holes in the 2455848b8605Smrg * sampler array. 2456848b8605Smrg */ 2457848b8605Smrg key->nr_samplers = llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER] + 1; 2458848b8605Smrg if (llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) { 2459848b8605Smrg key->nr_sampler_views = 2460848b8605Smrg llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1; 2461848b8605Smrg } 2462848b8605Smrg else { 2463848b8605Smrg key->nr_sampler_views = key->nr_samplers; 2464848b8605Smrg } 2465848b8605Smrg 2466848b8605Smrg draw_sampler = key->samplers; 2467848b8605Smrg 2468848b8605Smrg memset(draw_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler); 2469848b8605Smrg 2470848b8605Smrg for (i = 0 ; i < key->nr_samplers; i++) { 2471848b8605Smrg lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state, 2472848b8605Smrg llvm->draw->samplers[PIPE_SHADER_GEOMETRY][i]); 2473848b8605Smrg } 2474848b8605Smrg for (i = 0 ; i < key->nr_sampler_views; i++) { 2475848b8605Smrg lp_sampler_static_texture_state(&draw_sampler[i].texture_state, 2476848b8605Smrg llvm->draw->sampler_views[PIPE_SHADER_GEOMETRY][i]); 2477848b8605Smrg } 2478848b8605Smrg 2479848b8605Smrg return key; 2480848b8605Smrg} 2481848b8605Smrg 2482848b8605Smrgvoid 2483848b8605Smrgdraw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key *key) 2484848b8605Smrg{ 2485848b8605Smrg unsigned i; 2486848b8605Smrg struct draw_sampler_static_state *sampler = key->samplers; 2487848b8605Smrg 2488848b8605Smrg for (i = 0 ; i < key->nr_sampler_views; i++) { 2489848b8605Smrg debug_printf("sampler[%i].src_format = %s\n", i, 2490848b8605Smrg util_format_name(sampler[i].texture_state.format)); 2491848b8605Smrg } 2492848b8605Smrg} 2493