draw_llvm.c revision 7ec681f3
1/************************************************************************** 2 * 3 * Copyright 2010 VMware, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include "draw_llvm.h" 29 30#include "draw_context.h" 31#include "draw_vs.h" 32#include "draw_gs.h" 33 34#include "gallivm/lp_bld_arit.h" 35#include "gallivm/lp_bld_arit_overflow.h" 36#include "gallivm/lp_bld_bitarit.h" 37#include "gallivm/lp_bld_gather.h" 38#include "gallivm/lp_bld_logic.h" 39#include "gallivm/lp_bld_const.h" 40#include "gallivm/lp_bld_coro.h" 41#include "gallivm/lp_bld_swizzle.h" 42#include "gallivm/lp_bld_struct.h" 43#include "gallivm/lp_bld_type.h" 44#include "gallivm/lp_bld_flow.h" 45#include "gallivm/lp_bld_debug.h" 46#include "gallivm/lp_bld_tgsi.h" 47#include "gallivm/lp_bld_nir.h" 48#include "gallivm/lp_bld_printf.h" 49#include "gallivm/lp_bld_intr.h" 50#include "gallivm/lp_bld_init.h" 51#include "gallivm/lp_bld_type.h" 52#include "gallivm/lp_bld_pack.h" 53#include "gallivm/lp_bld_format.h" 54#include "gallivm/lp_bld_misc.h" 55#include "tgsi/tgsi_exec.h" 56#include "tgsi/tgsi_dump.h" 57 58#include "util/u_math.h" 59#include "util/u_pointer.h" 60#include "util/u_string.h" 61#include "util/simple_list.h" 62#include "nir_serialize.h" 63#include "util/mesa-sha1.h" 64#define DEBUG_STORE 0 65 66 67static void 68draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *var); 69 70 71struct draw_gs_llvm_iface { 72 struct lp_build_gs_iface base; 73 74 struct draw_gs_llvm_variant *variant; 75 LLVMValueRef input; 76}; 77 78static inline const struct draw_gs_llvm_iface * 79draw_gs_llvm_iface(const struct lp_build_gs_iface *iface) 80{ 81 return (const struct draw_gs_llvm_iface *)iface; 82} 83 84struct draw_tcs_llvm_iface { 85 struct lp_build_tcs_iface base; 86 87 struct draw_tcs_llvm_variant *variant; 88 LLVMValueRef input; 89 LLVMValueRef output; 90}; 91 92static inline const struct draw_tcs_llvm_iface * 93draw_tcs_llvm_iface(const struct lp_build_tcs_iface *iface) 94{ 95 return (const struct draw_tcs_llvm_iface *)iface; 96} 97 98struct draw_tes_llvm_iface { 99 struct lp_build_tes_iface base; 100 101 struct draw_tes_llvm_variant *variant; 102 LLVMValueRef input; 103}; 104 105static inline const struct draw_tes_llvm_iface * 106draw_tes_llvm_iface(const struct lp_build_tes_iface *iface) 107{ 108 return (const struct draw_tes_llvm_iface *)iface; 109} 110 111/** 112 * Create LLVM type for draw_vertex_buffer. 113 */ 114static LLVMTypeRef 115create_jit_dvbuffer_type(struct gallivm_state *gallivm, 116 const char *struct_name) 117{ 118 LLVMTargetDataRef target = gallivm->target; 119 LLVMTypeRef dvbuffer_type; 120 LLVMTypeRef elem_types[DRAW_JIT_DVBUFFER_NUM_FIELDS]; 121 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context); 122 123 elem_types[DRAW_JIT_DVBUFFER_MAP] = 124 LLVMPointerType(LLVMIntTypeInContext(gallivm->context, 8), 0); 125 elem_types[DRAW_JIT_DVBUFFER_SIZE] = int32_type; 126 127 dvbuffer_type = LLVMStructTypeInContext(gallivm->context, elem_types, 128 ARRAY_SIZE(elem_types), 0); 129 130 (void) target; /* silence unused var warning for non-debug build */ 131 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer, map, 132 target, dvbuffer_type, 133 DRAW_JIT_DVBUFFER_MAP); 134 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer, size, 135 target, dvbuffer_type, 136 DRAW_JIT_DVBUFFER_SIZE); 137 138 return dvbuffer_type; 139} 140 141/** 142 * Create LLVM type for struct draw_jit_texture 143 */ 144static LLVMTypeRef 145create_jit_texture_type(struct gallivm_state *gallivm, const char *struct_name) 146{ 147 LLVMTargetDataRef target = gallivm->target; 148 LLVMTypeRef texture_type; 149 LLVMTypeRef elem_types[DRAW_JIT_TEXTURE_NUM_FIELDS]; 150 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context); 151 152 elem_types[DRAW_JIT_TEXTURE_WIDTH] = 153 elem_types[DRAW_JIT_TEXTURE_HEIGHT] = 154 elem_types[DRAW_JIT_TEXTURE_DEPTH] = 155 elem_types[DRAW_JIT_TEXTURE_NUM_SAMPLES] = 156 elem_types[DRAW_JIT_TEXTURE_SAMPLE_STRIDE] = 157 elem_types[DRAW_JIT_TEXTURE_FIRST_LEVEL] = 158 elem_types[DRAW_JIT_TEXTURE_LAST_LEVEL] = int32_type; 159 elem_types[DRAW_JIT_TEXTURE_BASE] = 160 LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0); 161 elem_types[DRAW_JIT_TEXTURE_ROW_STRIDE] = 162 elem_types[DRAW_JIT_TEXTURE_IMG_STRIDE] = 163 elem_types[DRAW_JIT_TEXTURE_MIP_OFFSETS] = 164 LLVMArrayType(int32_type, PIPE_MAX_TEXTURE_LEVELS); 165 166 texture_type = LLVMStructTypeInContext(gallivm->context, elem_types, 167 ARRAY_SIZE(elem_types), 0); 168 169 (void) target; /* silence unused var warning for non-debug build */ 170 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, width, 171 target, texture_type, 172 DRAW_JIT_TEXTURE_WIDTH); 173 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, height, 174 target, texture_type, 175 DRAW_JIT_TEXTURE_HEIGHT); 176 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, depth, 177 target, texture_type, 178 DRAW_JIT_TEXTURE_DEPTH); 179 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, base, 180 target, texture_type, 181 DRAW_JIT_TEXTURE_BASE); 182 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, row_stride, 183 target, texture_type, 184 DRAW_JIT_TEXTURE_ROW_STRIDE); 185 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, img_stride, 186 target, texture_type, 187 DRAW_JIT_TEXTURE_IMG_STRIDE); 188 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, first_level, 189 target, texture_type, 190 DRAW_JIT_TEXTURE_FIRST_LEVEL); 191 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, last_level, 192 target, texture_type, 193 DRAW_JIT_TEXTURE_LAST_LEVEL); 194 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, mip_offsets, 195 target, texture_type, 196 DRAW_JIT_TEXTURE_MIP_OFFSETS); 197 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, num_samples, 198 target, texture_type, 199 DRAW_JIT_TEXTURE_NUM_SAMPLES); 200 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, sample_stride, 201 target, texture_type, 202 DRAW_JIT_TEXTURE_SAMPLE_STRIDE); 203 204 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture, target, texture_type); 205 206 return texture_type; 207} 208 209 210/** 211 * Create LLVM type for struct draw_jit_sampler 212 */ 213static LLVMTypeRef 214create_jit_sampler_type(struct gallivm_state *gallivm, const char *struct_name) 215{ 216 LLVMTargetDataRef target = gallivm->target; 217 LLVMTypeRef sampler_type; 218 LLVMTypeRef elem_types[DRAW_JIT_SAMPLER_NUM_FIELDS]; 219 220 elem_types[DRAW_JIT_SAMPLER_MIN_LOD] = 221 elem_types[DRAW_JIT_SAMPLER_MAX_LOD] = 222 elem_types[DRAW_JIT_SAMPLER_LOD_BIAS] = 223 elem_types[DRAW_JIT_SAMPLER_MAX_ANISO] = LLVMFloatTypeInContext(gallivm->context); 224 elem_types[DRAW_JIT_SAMPLER_BORDER_COLOR] = 225 LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4); 226 227 sampler_type = LLVMStructTypeInContext(gallivm->context, elem_types, 228 ARRAY_SIZE(elem_types), 0); 229 230 (void) target; /* silence unused var warning for non-debug build */ 231 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, min_lod, 232 target, sampler_type, 233 DRAW_JIT_SAMPLER_MIN_LOD); 234 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, max_lod, 235 target, sampler_type, 236 DRAW_JIT_SAMPLER_MAX_LOD); 237 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, lod_bias, 238 target, sampler_type, 239 DRAW_JIT_SAMPLER_LOD_BIAS); 240 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, border_color, 241 target, sampler_type, 242 DRAW_JIT_SAMPLER_BORDER_COLOR); 243 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, max_aniso, 244 target, sampler_type, 245 DRAW_JIT_SAMPLER_MAX_ANISO); 246 247 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler, target, sampler_type); 248 249 return sampler_type; 250} 251 252/** 253 * Create LLVM type for struct draw_jit_texture 254 */ 255static LLVMTypeRef 256create_jit_image_type(struct gallivm_state *gallivm, const char *struct_name) 257{ 258 LLVMTargetDataRef target = gallivm->target; 259 LLVMTypeRef image_type; 260 LLVMTypeRef elem_types[DRAW_JIT_IMAGE_NUM_FIELDS]; 261 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context); 262 263 elem_types[DRAW_JIT_IMAGE_WIDTH] = 264 elem_types[DRAW_JIT_IMAGE_HEIGHT] = 265 elem_types[DRAW_JIT_IMAGE_DEPTH] = 266 elem_types[DRAW_JIT_IMAGE_ROW_STRIDE] = 267 elem_types[DRAW_JIT_IMAGE_IMG_STRIDE] = 268 elem_types[DRAW_JIT_IMAGE_NUM_SAMPLES] = 269 elem_types[DRAW_JIT_IMAGE_SAMPLE_STRIDE] = int32_type; 270 elem_types[DRAW_JIT_IMAGE_BASE] = 271 LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0); 272 273 image_type = LLVMStructTypeInContext(gallivm->context, elem_types, 274 ARRAY_SIZE(elem_types), 0); 275 276 (void) target; /* silence unused var warning for non-debug build */ 277 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, width, 278 target, image_type, 279 DRAW_JIT_IMAGE_WIDTH); 280 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, height, 281 target, image_type, 282 DRAW_JIT_IMAGE_HEIGHT); 283 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, depth, 284 target, image_type, 285 DRAW_JIT_IMAGE_DEPTH); 286 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, base, 287 target, image_type, 288 DRAW_JIT_IMAGE_BASE); 289 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, row_stride, 290 target, image_type, 291 DRAW_JIT_IMAGE_ROW_STRIDE); 292 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, img_stride, 293 target, image_type, 294 DRAW_JIT_IMAGE_IMG_STRIDE); 295 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, num_samples, 296 target, image_type, 297 DRAW_JIT_IMAGE_NUM_SAMPLES); 298 LP_CHECK_MEMBER_OFFSET(struct draw_jit_image, sample_stride, 299 target, image_type, 300 DRAW_JIT_IMAGE_SAMPLE_STRIDE); 301 302 LP_CHECK_STRUCT_SIZE(struct draw_jit_image, target, image_type); 303 304 return image_type; 305} 306 307/** 308 * Create LLVM type for struct draw_jit_context 309 */ 310static LLVMTypeRef 311create_jit_context_type(struct gallivm_state *gallivm, 312 LLVMTypeRef texture_type, LLVMTypeRef sampler_type, 313 LLVMTypeRef image_type, 314 const char *struct_name) 315{ 316 LLVMTargetDataRef target = gallivm->target; 317 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 318 LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context); 319 LLVMTypeRef elem_types[DRAW_JIT_CTX_NUM_FIELDS]; 320 LLVMTypeRef context_type; 321 322 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* vs_constants */ 323 LP_MAX_TGSI_CONST_BUFFERS); 324 elem_types[1] = LLVMArrayType(int_type, /* num_vs_constants */ 325 LP_MAX_TGSI_CONST_BUFFERS); 326 elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4), 327 DRAW_TOTAL_CLIP_PLANES), 0); 328 elem_types[3] = LLVMPointerType(float_type, 0); /* viewports */ 329 elem_types[4] = LLVMArrayType(texture_type, 330 PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */ 331 elem_types[5] = LLVMArrayType(sampler_type, 332 PIPE_MAX_SAMPLERS); /* samplers */ 333 elem_types[6] = LLVMArrayType(image_type, 334 PIPE_MAX_SHADER_IMAGES); /* images */ 335 elem_types[7] = LLVMArrayType(LLVMPointerType(int_type, 0), /* vs_ssbo */ 336 LP_MAX_TGSI_SHADER_BUFFERS); 337 elem_types[8] = LLVMArrayType(int_type, /* num_vs_ssbos */ 338 LP_MAX_TGSI_SHADER_BUFFERS); 339 elem_types[9] = LLVMPointerType(float_type, 0); /* aniso table */ 340 context_type = LLVMStructTypeInContext(gallivm->context, elem_types, 341 ARRAY_SIZE(elem_types), 0); 342 343 (void) target; /* silence unused var warning for non-debug build */ 344 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, vs_constants, 345 target, context_type, DRAW_JIT_CTX_CONSTANTS); 346 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, num_vs_constants, 347 target, context_type, DRAW_JIT_CTX_NUM_CONSTANTS); 348 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, planes, 349 target, context_type, DRAW_JIT_CTX_PLANES); 350 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, viewports, 351 target, context_type, DRAW_JIT_CTX_VIEWPORT); 352 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, textures, 353 target, context_type, 354 DRAW_JIT_CTX_TEXTURES); 355 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, samplers, 356 target, context_type, 357 DRAW_JIT_CTX_SAMPLERS); 358 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, images, 359 target, context_type, DRAW_JIT_CTX_IMAGES); 360 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, vs_ssbos, 361 target, context_type, DRAW_JIT_CTX_SSBOS); 362 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, num_vs_ssbos, 363 target, context_type, DRAW_JIT_CTX_NUM_SSBOS); 364 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, aniso_filter_table, 365 target, context_type, DRAW_JIT_CTX_ANISO_FILTER_TABLE); 366 LP_CHECK_STRUCT_SIZE(struct draw_jit_context, 367 target, context_type); 368 369 return context_type; 370} 371 372 373/** 374 * Create LLVM type for struct draw_gs_jit_context 375 */ 376static LLVMTypeRef 377create_gs_jit_context_type(struct gallivm_state *gallivm, 378 unsigned vector_length, 379 LLVMTypeRef texture_type, LLVMTypeRef sampler_type, 380 LLVMTypeRef image_type, 381 const char *struct_name) 382{ 383 LLVMTargetDataRef target = gallivm->target; 384 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 385 LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context); 386 LLVMTypeRef elem_types[DRAW_GS_JIT_CTX_NUM_FIELDS]; 387 LLVMTypeRef context_type; 388 389 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* constants */ 390 LP_MAX_TGSI_CONST_BUFFERS); 391 elem_types[1] = LLVMArrayType(int_type, /* num_constants */ 392 LP_MAX_TGSI_CONST_BUFFERS); 393 elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4), 394 DRAW_TOTAL_CLIP_PLANES), 0); 395 elem_types[3] = LLVMPointerType(float_type, 0); /* viewports */ 396 397 elem_types[4] = LLVMArrayType(texture_type, 398 PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */ 399 elem_types[5] = LLVMArrayType(sampler_type, 400 PIPE_MAX_SAMPLERS); /* samplers */ 401 elem_types[6] = LLVMArrayType(image_type, 402 PIPE_MAX_SHADER_IMAGES); /* images */ 403 elem_types[7] = LLVMPointerType(LLVMPointerType(int_type, 0), 0); 404 elem_types[8] = LLVMPointerType(LLVMVectorType(int_type, 405 vector_length), 0); 406 elem_types[9] = LLVMPointerType(LLVMVectorType(int_type, 407 vector_length), 0); 408 409 elem_types[10] = LLVMArrayType(LLVMPointerType(int_type, 0), /* ssbos */ 410 LP_MAX_TGSI_SHADER_BUFFERS); 411 elem_types[11] = LLVMArrayType(int_type, /* num_ssbos */ 412 LP_MAX_TGSI_SHADER_BUFFERS); 413 elem_types[12] = LLVMPointerType(float_type, 0); /* aniso table */ 414 context_type = LLVMStructTypeInContext(gallivm->context, elem_types, 415 ARRAY_SIZE(elem_types), 0); 416 417 (void) target; /* silence unused var warning for non-debug build */ 418 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, constants, 419 target, context_type, DRAW_GS_JIT_CTX_CONSTANTS); 420 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, num_constants, 421 target, context_type, DRAW_GS_JIT_CTX_NUM_CONSTANTS); 422 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, planes, 423 target, context_type, DRAW_GS_JIT_CTX_PLANES); 424 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, viewports, 425 target, context_type, DRAW_GS_JIT_CTX_VIEWPORT); 426 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, textures, 427 target, context_type, 428 DRAW_GS_JIT_CTX_TEXTURES); 429 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, samplers, 430 target, context_type, 431 DRAW_GS_JIT_CTX_SAMPLERS); 432 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, prim_lengths, 433 target, context_type, 434 DRAW_GS_JIT_CTX_PRIM_LENGTHS); 435 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, emitted_vertices, 436 target, context_type, 437 DRAW_GS_JIT_CTX_EMITTED_VERTICES); 438 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, emitted_prims, 439 target, context_type, 440 DRAW_GS_JIT_CTX_EMITTED_PRIMS); 441 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, ssbos, 442 target, context_type, DRAW_GS_JIT_CTX_SSBOS); 443 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, num_ssbos, 444 target, context_type, DRAW_GS_JIT_CTX_NUM_SSBOS); 445 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, images, 446 target, context_type, DRAW_GS_JIT_CTX_IMAGES); 447 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, aniso_filter_table, 448 target, context_type, DRAW_GS_JIT_CTX_ANISO_FILTER_TABLE); 449 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context, 450 target, context_type); 451 452 return context_type; 453} 454 455 456static LLVMTypeRef 457create_gs_jit_input_type(struct gallivm_state *gallivm) 458{ 459 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 460 LLVMTypeRef input_array; 461 462 input_array = LLVMVectorType(float_type, TGSI_NUM_CHANNELS); /* num primitives */ 463 input_array = LLVMArrayType(input_array, TGSI_NUM_CHANNELS); /* num channels */ 464 input_array = LLVMArrayType(input_array, PIPE_MAX_SHADER_INPUTS); /* num attrs per vertex */ 465 input_array = LLVMPointerType(input_array, 0); /* num vertices per prim */ 466 467 return input_array; 468} 469 470/** 471 * Create LLVM type for struct pipe_vertex_buffer 472 */ 473static LLVMTypeRef 474create_jit_vertex_buffer_type(struct gallivm_state *gallivm, 475 const char *struct_name) 476{ 477 LLVMTargetDataRef target = gallivm->target; 478 LLVMTypeRef elem_types[4]; 479 LLVMTypeRef vb_type; 480 481 elem_types[0] = LLVMInt16TypeInContext(gallivm->context); 482 elem_types[1] = LLVMInt8TypeInContext(gallivm->context); 483 elem_types[2] = LLVMInt32TypeInContext(gallivm->context); 484 elem_types[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0); 485 486 vb_type = LLVMStructTypeInContext(gallivm->context, elem_types, 487 ARRAY_SIZE(elem_types), 0); 488 489 (void) target; /* silence unused var warning for non-debug build */ 490 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, stride, 491 target, vb_type, 0); 492 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, is_user_buffer, 493 target, vb_type, 1); 494 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, buffer_offset, 495 target, vb_type, 2); 496 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, buffer.resource, 497 target, vb_type, 3); 498 499 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer, target, vb_type); 500 501 return vb_type; 502} 503 504 505/** 506 * Create LLVM type for struct vertex_header; 507 */ 508static LLVMTypeRef 509create_jit_vertex_header(struct gallivm_state *gallivm, int data_elems) 510{ 511 LLVMTargetDataRef target = gallivm->target; 512 LLVMTypeRef elem_types[3]; 513 LLVMTypeRef vertex_header; 514 char struct_name[24]; 515 516 snprintf(struct_name, 23, "vertex_header%d", data_elems); 517 518 elem_types[DRAW_JIT_VERTEX_VERTEX_ID] = LLVMIntTypeInContext(gallivm->context, 32); 519 elem_types[DRAW_JIT_VERTEX_CLIP_POS] = LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4); 520 elem_types[DRAW_JIT_VERTEX_DATA] = LLVMArrayType(elem_types[1], data_elems); 521 522 vertex_header = LLVMStructTypeInContext(gallivm->context, elem_types, 523 ARRAY_SIZE(elem_types), 0); 524 525 /* these are bit-fields and we can't take address of them 526 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask, 527 target, vertex_header, 528 DRAW_JIT_VERTEX_CLIPMASK); 529 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag, 530 target, vertex_header, 531 DRAW_JIT_VERTEX_EDGEFLAG); 532 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad, 533 target, vertex_header, 534 DRAW_JIT_VERTEX_PAD); 535 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id, 536 target, vertex_header, 537 DRAW_JIT_VERTEX_VERTEX_ID); 538 */ 539 (void) target; /* silence unused var warning for non-debug build */ 540 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clip_pos, 541 target, vertex_header, 542 DRAW_JIT_VERTEX_CLIP_POS); 543 LP_CHECK_MEMBER_OFFSET(struct vertex_header, data, 544 target, vertex_header, 545 DRAW_JIT_VERTEX_DATA); 546 547 assert(LLVMABISizeOfType(target, vertex_header) == 548 offsetof(struct vertex_header, data[data_elems])); 549 550 return vertex_header; 551} 552 553/** 554 * Create LLVM type for struct draw_tcs_jit_context 555 */ 556static LLVMTypeRef 557create_tcs_jit_context_type(struct gallivm_state *gallivm, 558 unsigned vector_length, 559 LLVMTypeRef texture_type, LLVMTypeRef sampler_type, 560 LLVMTypeRef image_type, 561 const char *struct_name) 562{ 563 LLVMTargetDataRef target = gallivm->target; 564 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 565 LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context); 566 LLVMTypeRef elem_types[DRAW_TCS_JIT_CTX_NUM_FIELDS]; 567 LLVMTypeRef context_type; 568 569 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* constants */ 570 LP_MAX_TGSI_CONST_BUFFERS); 571 elem_types[1] = LLVMArrayType(int_type, /* num_constants */ 572 LP_MAX_TGSI_CONST_BUFFERS); 573 elem_types[2] = LLVMInt32TypeInContext(gallivm->context); 574 elem_types[3] = LLVMInt32TypeInContext(gallivm->context); 575 576 elem_types[4] = LLVMArrayType(texture_type, 577 PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */ 578 elem_types[5] = LLVMArrayType(sampler_type, 579 PIPE_MAX_SAMPLERS); /* samplers */ 580 elem_types[6] = LLVMArrayType(image_type, 581 PIPE_MAX_SHADER_IMAGES); /* images */ 582 583 elem_types[7] = LLVMArrayType(LLVMPointerType(int_type, 0), /* ssbos */ 584 LP_MAX_TGSI_SHADER_BUFFERS); 585 elem_types[8] = LLVMArrayType(int_type, /* num_ssbos */ 586 LP_MAX_TGSI_SHADER_BUFFERS); 587 elem_types[9] = LLVMPointerType(float_type, 0); /* aniso table */ 588 context_type = LLVMStructTypeInContext(gallivm->context, elem_types, 589 ARRAY_SIZE(elem_types), 0); 590 591 (void) target; /* silence unused var warning for non-debug build */ 592 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, constants, 593 target, context_type, DRAW_TCS_JIT_CTX_CONSTANTS); 594 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, num_constants, 595 target, context_type, DRAW_TCS_JIT_CTX_NUM_CONSTANTS); 596 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, textures, 597 target, context_type, 598 DRAW_TCS_JIT_CTX_TEXTURES); 599 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, samplers, 600 target, context_type, 601 DRAW_TCS_JIT_CTX_SAMPLERS); 602 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, ssbos, 603 target, context_type, DRAW_TCS_JIT_CTX_SSBOS); 604 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, num_ssbos, 605 target, context_type, DRAW_TCS_JIT_CTX_NUM_SSBOS); 606 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, images, 607 target, context_type, DRAW_TCS_JIT_CTX_IMAGES); 608 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, aniso_filter_table, 609 target, context_type, DRAW_TCS_JIT_CTX_ANISO_FILTER_TABLE); 610 LP_CHECK_STRUCT_SIZE(struct draw_tcs_jit_context, 611 target, context_type); 612 613 return context_type; 614} 615 616static LLVMTypeRef 617create_tcs_jit_input_type(struct gallivm_state *gallivm) 618{ 619 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 620 LLVMTypeRef input_array; 621 622 input_array = LLVMArrayType(float_type, TGSI_NUM_CHANNELS); /* num channels */ 623 input_array = LLVMArrayType(input_array, NUM_TCS_INPUTS); /* num attrs per vertex */ 624 input_array = LLVMPointerType(input_array, 0); /* num vertices per prim */ 625 626 return input_array; 627} 628 629static LLVMTypeRef 630create_tcs_jit_output_type(struct gallivm_state *gallivm) 631{ 632 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 633 LLVMTypeRef output_array; 634 635 output_array = LLVMArrayType(float_type, TGSI_NUM_CHANNELS); /* num channels */ 636 output_array = LLVMArrayType(output_array, PIPE_MAX_SHADER_INPUTS); /* num attrs per vertex */ 637 output_array = LLVMPointerType(output_array, 0); /* num vertices per prim */ 638 639 return output_array; 640} 641 642static LLVMTypeRef 643create_tes_jit_input_type(struct gallivm_state *gallivm) 644{ 645 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 646 LLVMTypeRef input_array; 647 648 input_array = LLVMArrayType(float_type, TGSI_NUM_CHANNELS); /* num channels */ 649 input_array = LLVMArrayType(input_array, PIPE_MAX_SHADER_INPUTS); /* num attrs per vertex */ 650 input_array = LLVMPointerType(input_array, 0); /* num vertices per prim */ 651 652 return input_array; 653} 654 655/** 656 * Create LLVM type for struct draw_tes_jit_context 657 */ 658static LLVMTypeRef 659create_tes_jit_context_type(struct gallivm_state *gallivm, 660 unsigned vector_length, 661 LLVMTypeRef texture_type, LLVMTypeRef sampler_type, 662 LLVMTypeRef image_type, 663 const char *struct_name) 664{ 665 LLVMTargetDataRef target = gallivm->target; 666 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 667 LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context); 668 LLVMTypeRef elem_types[DRAW_TCS_JIT_CTX_NUM_FIELDS]; 669 LLVMTypeRef context_type; 670 671 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* constants */ 672 LP_MAX_TGSI_CONST_BUFFERS); 673 elem_types[1] = LLVMArrayType(int_type, /* num_constants */ 674 LP_MAX_TGSI_CONST_BUFFERS); 675 elem_types[2] = LLVMInt32TypeInContext(gallivm->context); 676 elem_types[3] = LLVMInt32TypeInContext(gallivm->context); 677 678 elem_types[4] = LLVMArrayType(texture_type, 679 PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */ 680 elem_types[5] = LLVMArrayType(sampler_type, 681 PIPE_MAX_SAMPLERS); /* samplers */ 682 elem_types[6] = LLVMArrayType(image_type, 683 PIPE_MAX_SHADER_IMAGES); /* images */ 684 685 elem_types[7] = LLVMArrayType(LLVMPointerType(int_type, 0), /* ssbos */ 686 LP_MAX_TGSI_SHADER_BUFFERS); 687 elem_types[8] = LLVMArrayType(int_type, /* num_ssbos */ 688 LP_MAX_TGSI_SHADER_BUFFERS); 689 elem_types[9] = LLVMPointerType(float_type, 0); /* aniso table */ 690 context_type = LLVMStructTypeInContext(gallivm->context, elem_types, 691 ARRAY_SIZE(elem_types), 0); 692 693 (void) target; /* silence unused var warning for non-debug build */ 694 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, constants, 695 target, context_type, DRAW_TCS_JIT_CTX_CONSTANTS); 696 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, num_constants, 697 target, context_type, DRAW_TCS_JIT_CTX_NUM_CONSTANTS); 698 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, textures, 699 target, context_type, 700 DRAW_TCS_JIT_CTX_TEXTURES); 701 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, samplers, 702 target, context_type, 703 DRAW_TCS_JIT_CTX_SAMPLERS); 704 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, ssbos, 705 target, context_type, DRAW_TCS_JIT_CTX_SSBOS); 706 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, num_ssbos, 707 target, context_type, DRAW_TCS_JIT_CTX_NUM_SSBOS); 708 LP_CHECK_MEMBER_OFFSET(struct draw_tes_jit_context, images, 709 target, context_type, DRAW_TCS_JIT_CTX_IMAGES); 710 LP_CHECK_MEMBER_OFFSET(struct draw_tcs_jit_context, aniso_filter_table, 711 target, context_type, DRAW_TCS_JIT_CTX_ANISO_FILTER_TABLE); 712 LP_CHECK_STRUCT_SIZE(struct draw_tes_jit_context, 713 target, context_type); 714 715 return context_type; 716} 717 718/** 719 * Create LLVM types for various structures. 720 */ 721static void 722create_jit_types(struct draw_llvm_variant *variant) 723{ 724 struct gallivm_state *gallivm = variant->gallivm; 725 LLVMTypeRef texture_type, sampler_type, context_type, buffer_type, 726 vb_type, image_type; 727 728 texture_type = create_jit_texture_type(gallivm, "texture"); 729 sampler_type = create_jit_sampler_type(gallivm, "sampler"); 730 image_type = create_jit_image_type(gallivm, "image"); 731 732 context_type = create_jit_context_type(gallivm, texture_type, sampler_type, 733 image_type, 734 "draw_jit_context"); 735 variant->context_ptr_type = LLVMPointerType(context_type, 0); 736 737 buffer_type = create_jit_dvbuffer_type(gallivm, "draw_vertex_buffer"); 738 variant->buffer_ptr_type = LLVMPointerType(buffer_type, 0); 739 740 vb_type = create_jit_vertex_buffer_type(gallivm, "pipe_vertex_buffer"); 741 variant->vb_ptr_type = LLVMPointerType(vb_type, 0); 742} 743 744 745static LLVMTypeRef 746get_context_ptr_type(struct draw_llvm_variant *variant) 747{ 748 if (!variant->context_ptr_type) 749 create_jit_types(variant); 750 return variant->context_ptr_type; 751} 752 753 754static LLVMTypeRef 755get_buffer_ptr_type(struct draw_llvm_variant *variant) 756{ 757 if (!variant->buffer_ptr_type) 758 create_jit_types(variant); 759 return variant->buffer_ptr_type; 760} 761 762 763static LLVMTypeRef 764get_vb_ptr_type(struct draw_llvm_variant *variant) 765{ 766 if (!variant->vb_ptr_type) 767 create_jit_types(variant); 768 return variant->vb_ptr_type; 769} 770 771static LLVMTypeRef 772get_vertex_header_ptr_type(struct draw_llvm_variant *variant) 773{ 774 if (!variant->vertex_header_ptr_type) 775 create_jit_types(variant); 776 return variant->vertex_header_ptr_type; 777} 778 779 780/** 781 * Create per-context LLVM info. 782 */ 783struct draw_llvm * 784draw_llvm_create(struct draw_context *draw, LLVMContextRef context) 785{ 786 struct draw_llvm *llvm; 787 788 if (!lp_build_init()) 789 return NULL; 790 791 llvm = CALLOC_STRUCT( draw_llvm ); 792 if (!llvm) 793 return NULL; 794 795 llvm->draw = draw; 796 797 llvm->context = context; 798 if (!llvm->context) { 799 llvm->context = LLVMContextCreate(); 800 llvm->context_owned = true; 801 } 802 if (!llvm->context) 803 goto fail; 804 805 llvm->nr_variants = 0; 806 make_empty_list(&llvm->vs_variants_list); 807 808 llvm->nr_gs_variants = 0; 809 make_empty_list(&llvm->gs_variants_list); 810 811 llvm->nr_tcs_variants = 0; 812 make_empty_list(&llvm->tcs_variants_list); 813 814 llvm->nr_tes_variants = 0; 815 make_empty_list(&llvm->tes_variants_list); 816 817 return llvm; 818 819fail: 820 draw_llvm_destroy(llvm); 821 return NULL; 822} 823 824 825/** 826 * Free per-context LLVM info. 827 */ 828void 829draw_llvm_destroy(struct draw_llvm *llvm) 830{ 831 if (llvm->context_owned) 832 LLVMContextDispose(llvm->context); 833 llvm->context = NULL; 834 835 /* XXX free other draw_llvm data? */ 836 FREE(llvm); 837} 838 839static void 840draw_get_ir_cache_key(struct nir_shader *nir, 841 const void *key, size_t key_size, 842 uint32_t val_32bit, 843 unsigned char ir_sha1_cache_key[20]) 844{ 845 struct blob blob = { 0 }; 846 unsigned ir_size; 847 void *ir_binary; 848 849 blob_init(&blob); 850 nir_serialize(&blob, nir, true); 851 ir_binary = blob.data; 852 ir_size = blob.size; 853 854 struct mesa_sha1 ctx; 855 _mesa_sha1_init(&ctx); 856 _mesa_sha1_update(&ctx, key, key_size); 857 _mesa_sha1_update(&ctx, ir_binary, ir_size); 858 _mesa_sha1_update(&ctx, &val_32bit, 4); 859 _mesa_sha1_final(&ctx, ir_sha1_cache_key); 860 861 blob_finish(&blob); 862} 863 864/** 865 * Create LLVM-generated code for a vertex shader. 866 */ 867struct draw_llvm_variant * 868draw_llvm_create_variant(struct draw_llvm *llvm, 869 unsigned num_inputs, 870 const struct draw_llvm_variant_key *key) 871{ 872 struct draw_llvm_variant *variant; 873 struct llvm_vertex_shader *shader = 874 llvm_vertex_shader(llvm->draw->vs.vertex_shader); 875 LLVMTypeRef vertex_header; 876 char module_name[64]; 877 unsigned char ir_sha1_cache_key[20]; 878 struct lp_cached_code cached = { 0 }; 879 bool needs_caching = false; 880 variant = MALLOC(sizeof *variant + 881 shader->variant_key_size - 882 sizeof variant->key); 883 if (!variant) 884 return NULL; 885 886 variant->llvm = llvm; 887 variant->shader = shader; 888 memcpy(&variant->key, key, shader->variant_key_size); 889 890 snprintf(module_name, sizeof(module_name), "draw_llvm_vs_variant%u", 891 variant->shader->variants_cached); 892 893 if (shader->base.state.ir.nir && llvm->draw->disk_cache_cookie) { 894 draw_get_ir_cache_key(shader->base.state.ir.nir, 895 key, 896 shader->variant_key_size, 897 num_inputs, 898 ir_sha1_cache_key); 899 900 llvm->draw->disk_cache_find_shader(llvm->draw->disk_cache_cookie, 901 &cached, 902 ir_sha1_cache_key); 903 if (!cached.data_size) 904 needs_caching = true; 905 } 906 variant->gallivm = gallivm_create(module_name, llvm->context, &cached); 907 908 create_jit_types(variant); 909 910 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) { 911 if (llvm->draw->vs.vertex_shader->state.type == PIPE_SHADER_IR_TGSI) 912 tgsi_dump(llvm->draw->vs.vertex_shader->state.tokens, 0); 913 else 914 nir_print_shader(llvm->draw->vs.vertex_shader->state.ir.nir, stderr); 915 draw_llvm_dump_variant_key(&variant->key); 916 } 917 918 vertex_header = create_jit_vertex_header(variant->gallivm, num_inputs); 919 920 variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0); 921 922 draw_llvm_generate(llvm, variant); 923 924 gallivm_compile_module(variant->gallivm); 925 926 variant->jit_func = (draw_jit_vert_func) 927 gallivm_jit_function(variant->gallivm, variant->function); 928 929 if (needs_caching) 930 llvm->draw->disk_cache_insert_shader(llvm->draw->disk_cache_cookie, 931 &cached, 932 ir_sha1_cache_key); 933 gallivm_free_ir(variant->gallivm); 934 935 variant->list_item_global.base = variant; 936 variant->list_item_local.base = variant; 937 /*variant->no = */shader->variants_created++; 938 variant->list_item_global.base = variant; 939 940 return variant; 941} 942 943static void 944do_clamp_vertex_color(struct gallivm_state *gallivm, 945 struct lp_type type, 946 const struct tgsi_shader_info *info, 947 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS]) 948{ 949 LLVMBuilderRef builder = gallivm->builder; 950 LLVMValueRef out; 951 unsigned chan, attrib; 952 struct lp_build_context bld; 953 lp_build_context_init(&bld, gallivm, type); 954 955 for (attrib = 0; attrib < info->num_outputs; ++attrib) { 956 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 957 if (outputs[attrib][chan]) { 958 switch (info->output_semantic_name[attrib]) { 959 case TGSI_SEMANTIC_COLOR: 960 case TGSI_SEMANTIC_BCOLOR: 961 out = LLVMBuildLoad(builder, outputs[attrib][chan], ""); 962 out = lp_build_clamp(&bld, out, bld.zero, bld.one); 963 LLVMBuildStore(builder, out, outputs[attrib][chan]); 964 break; 965 } 966 } 967 } 968 } 969} 970 971static void 972generate_vs(struct draw_llvm_variant *variant, 973 LLVMBuilderRef builder, 974 struct lp_type vs_type, 975 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 976 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS], 977 const struct lp_bld_tgsi_system_values *system_values, 978 LLVMValueRef context_ptr, 979 const struct lp_build_sampler_soa *draw_sampler, 980 const struct lp_build_image_soa *draw_image, 981 boolean clamp_vertex_color, 982 struct lp_build_mask_context *bld_mask) 983{ 984 struct draw_llvm *llvm = variant->llvm; 985 const struct tgsi_token *tokens = llvm->draw->vs.vertex_shader->state.tokens; 986 LLVMValueRef consts_ptr = 987 draw_jit_context_vs_constants(variant->gallivm, context_ptr); 988 LLVMValueRef num_consts_ptr = 989 draw_jit_context_num_vs_constants(variant->gallivm, context_ptr); 990 LLVMValueRef ssbos_ptr = 991 draw_jit_context_vs_ssbos(variant->gallivm, context_ptr); 992 LLVMValueRef num_ssbos_ptr = 993 draw_jit_context_num_vs_ssbos(variant->gallivm, context_ptr); 994 995 struct lp_build_tgsi_params params; 996 memset(¶ms, 0, sizeof(params)); 997 998 params.type = vs_type; 999 params.mask = bld_mask; 1000 params.consts_ptr = consts_ptr; 1001 params.const_sizes_ptr = num_consts_ptr; 1002 params.system_values = system_values; 1003 params.inputs = inputs; 1004 params.context_ptr = context_ptr; 1005 params.sampler = draw_sampler; 1006 params.info = &llvm->draw->vs.vertex_shader->info; 1007 params.ssbo_ptr = ssbos_ptr; 1008 params.ssbo_sizes_ptr = num_ssbos_ptr; 1009 params.image = draw_image; 1010 params.aniso_filter_table = draw_jit_context_aniso_filter_table(variant->gallivm, context_ptr); 1011 1012 if (llvm->draw->vs.vertex_shader->state.ir.nir && 1013 llvm->draw->vs.vertex_shader->state.type == PIPE_SHADER_IR_NIR) 1014 lp_build_nir_soa(variant->gallivm, 1015 llvm->draw->vs.vertex_shader->state.ir.nir, 1016 ¶ms, 1017 outputs); 1018 else 1019 lp_build_tgsi_soa(variant->gallivm, 1020 tokens, 1021 ¶ms, 1022 outputs); 1023 1024 if (clamp_vertex_color) { 1025 const struct tgsi_shader_info *info = &llvm->draw->vs.vertex_shader->info; 1026 do_clamp_vertex_color(variant->gallivm, 1027 vs_type, info, 1028 outputs); 1029 } 1030} 1031 1032 1033static void 1034fetch_instanced(struct gallivm_state *gallivm, 1035 const struct util_format_description *format_desc, 1036 struct lp_type vs_type, 1037 LLVMValueRef vb_stride, 1038 LLVMValueRef map_ptr, 1039 LLVMValueRef buffer_size_adj, 1040 LLVMValueRef *inputs, 1041 LLVMValueRef index) 1042{ 1043 LLVMTypeRef i32_t = LLVMInt32TypeInContext(gallivm->context); 1044 LLVMTypeRef aosf_t, aosi_t; 1045 LLVMValueRef zero = LLVMConstNull(i32_t); 1046 LLVMBuilderRef builder = gallivm->builder; 1047 LLVMValueRef stride, buffer_overflowed, aos, index_valid; 1048 unsigned i; 1049 1050 aosf_t = lp_build_vec_type(gallivm, lp_float32_vec4_type()); 1051 aosi_t = lp_build_vec_type(gallivm, lp_int32_vec4_type()); 1052 1053 /* This mul can overflow. Wraparound is ok. */ 1054 stride = LLVMBuildMul(builder, vb_stride, index, ""); 1055 1056 buffer_overflowed = LLVMBuildICmp(builder, LLVMIntUGE, 1057 stride, buffer_size_adj, 1058 "buffer_overflowed"); 1059 1060 if (0) { 1061 lp_build_print_value(gallivm, " instance index = ", index); 1062 lp_build_print_value(gallivm, " buffer overflowed = ", buffer_overflowed); 1063 } 1064 1065 index_valid = LLVMBuildNot(builder, buffer_overflowed, ""); 1066 index_valid = LLVMBuildSExt(builder, index_valid, i32_t, ""); 1067 stride = LLVMBuildAnd(builder, stride, index_valid, ""); 1068 1069 aos = lp_build_fetch_rgba_aos(gallivm, 1070 format_desc, 1071 lp_float32_vec4_type(), 1072 FALSE, 1073 map_ptr, 1074 stride, zero, zero, 1075 NULL); 1076 1077 index_valid = lp_build_broadcast(gallivm, aosi_t, index_valid); 1078 aos = LLVMBuildBitCast(builder, aos, aosi_t, ""); 1079 aos = LLVMBuildAnd(builder, aos, index_valid, ""); 1080 aos = LLVMBuildBitCast(builder, aos, aosf_t, ""); 1081 1082 for (i = 0; i < TGSI_NUM_CHANNELS; i++) { 1083 LLVMValueRef index = lp_build_const_int32(gallivm, i); 1084 inputs[i] = lp_build_extract_broadcast(gallivm, 1085 lp_float32_vec4_type(), 1086 vs_type, aos, index); 1087 } 1088} 1089 1090 1091static void 1092fetch_vector(struct gallivm_state *gallivm, 1093 const struct util_format_description *format_desc, 1094 struct lp_type vs_type, 1095 LLVMValueRef vb_stride, 1096 LLVMValueRef map_ptr, 1097 LLVMValueRef buffer_size_adj, 1098 LLVMValueRef *inputs, 1099 LLVMValueRef indices) 1100{ 1101 LLVMBuilderRef builder = gallivm->builder; 1102 struct lp_build_context blduivec; 1103 struct lp_type fetch_type = vs_type; 1104 LLVMValueRef offset, valid_mask; 1105 unsigned i; 1106 1107 lp_build_context_init(&blduivec, gallivm, lp_uint_type(vs_type)); 1108 1109 vb_stride = lp_build_broadcast_scalar(&blduivec, vb_stride); 1110 buffer_size_adj = lp_build_broadcast_scalar(&blduivec, buffer_size_adj); 1111 1112 /* This mul can overflow. Wraparound is ok. */ 1113 offset = lp_build_mul(&blduivec, vb_stride, indices); 1114 1115 valid_mask = lp_build_compare(gallivm, blduivec.type, 1116 PIPE_FUNC_LESS, offset, buffer_size_adj); 1117 1118 /* not valid elements use offset 0 */ 1119 offset = LLVMBuildAnd(builder, offset, valid_mask, ""); 1120 1121 if (0) { 1122 lp_build_print_value(gallivm, " indices = ", indices); 1123 lp_build_print_value(gallivm, " offsets = ", offset); 1124 lp_build_print_value(gallivm, " valid_mask = ", valid_mask); 1125 } 1126 1127 /* 1128 * Unlike fetch_instanced, use SoA fetch instead of multiple AoS fetches. 1129 * This should always produce better code. 1130 */ 1131 1132 /* The type handling is annoying here... */ 1133 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB && 1134 format_desc->channel[0].pure_integer) { 1135 if (format_desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) { 1136 fetch_type = lp_type_int_vec(vs_type.width, vs_type.width * vs_type.length); 1137 } 1138 else if (format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED) { 1139 fetch_type = lp_type_uint_vec(vs_type.width, vs_type.width * vs_type.length); 1140 } 1141 } 1142 1143 lp_build_fetch_rgba_soa(gallivm, format_desc, 1144 fetch_type, FALSE, map_ptr, offset, 1145 blduivec.zero, blduivec.zero, 1146 NULL, inputs); 1147 1148 for (i = 0; i < TGSI_NUM_CHANNELS; i++) { 1149 inputs[i] = LLVMBuildBitCast(builder, inputs[i], 1150 lp_build_vec_type(gallivm, vs_type), ""); 1151 } 1152 1153 /* out-of-bound fetches return all zeros */ 1154 for (i = 0; i < format_desc->nr_channels; i++) { 1155 inputs[i] = LLVMBuildBitCast(builder, inputs[i], blduivec.vec_type, ""); 1156 inputs[i] = LLVMBuildAnd(builder, inputs[i], valid_mask, ""); 1157 inputs[i] = LLVMBuildBitCast(builder, inputs[i], 1158 lp_build_vec_type(gallivm, vs_type), ""); 1159 } 1160} 1161 1162 1163static void 1164store_aos(struct gallivm_state *gallivm, 1165 LLVMValueRef io_ptr, 1166 LLVMValueRef index, 1167 LLVMValueRef value) 1168{ 1169 LLVMTypeRef data_ptr_type = LLVMPointerType(lp_build_vec_type(gallivm, lp_float32_vec4_type()), 0); 1170 LLVMBuilderRef builder = gallivm->builder; 1171 LLVMValueRef data_ptr = draw_jit_header_data(gallivm, io_ptr); 1172 LLVMValueRef indices[3]; 1173 1174 indices[0] = lp_build_const_int32(gallivm, 0); 1175 indices[1] = index; 1176 indices[2] = lp_build_const_int32(gallivm, 0); 1177 1178 data_ptr = LLVMBuildGEP(builder, data_ptr, indices, 3, ""); 1179 data_ptr = LLVMBuildPointerCast(builder, data_ptr, data_ptr_type, ""); 1180 1181#if DEBUG_STORE 1182 lp_build_printf(gallivm, " ---- %p storing attribute %d (io = %p)\n", data_ptr, index, io_ptr); 1183#endif 1184 1185 /* Unaligned store due to the vertex header */ 1186 LLVMSetAlignment(LLVMBuildStore(builder, value, data_ptr), sizeof(float)); 1187} 1188 1189/** 1190 * Adjust the mask to architecture endianess. The mask will the store in struct: 1191 * 1192 * struct vertex_header { 1193 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES; 1194 * unsigned edgeflag:1; 1195 * unsigned pad:1; 1196 * unsigned vertex_id:16; 1197 * [...] 1198 * } 1199 * 1200 * On little-endian machine nothing needs to done, however on bit-endian machine 1201 * the mask's fields need to be adjusted with the algorithm: 1202 * 1203 * uint32_t reverse (uint32_t x) 1204 * { 1205 * return (x >> 16) | // vertex_id 1206 * ((x & 0x3fff) << 18) | // clipmask 1207 * ((x & 0x4000) << 3) | // edgeflag 1208 * ((x & 0x8000) << 1); // pad 1209 * } 1210 */ 1211static LLVMValueRef 1212adjust_mask(struct gallivm_state *gallivm, 1213 LLVMValueRef mask) 1214{ 1215#if UTIL_ARCH_BIG_ENDIAN 1216 LLVMBuilderRef builder = gallivm->builder; 1217 LLVMValueRef vertex_id; 1218 LLVMValueRef clipmask; 1219 LLVMValueRef pad; 1220 LLVMValueRef edgeflag; 1221 1222 vertex_id = LLVMBuildLShr(builder, mask, lp_build_const_int32(gallivm, 16), ""); 1223 clipmask = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x3fff), ""); 1224 clipmask = LLVMBuildShl(builder, clipmask, lp_build_const_int32(gallivm, 18), ""); 1225 if (0) { 1226 pad = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x8000), ""); 1227 pad = LLVMBuildShl(builder, pad, lp_build_const_int32(gallivm, 1), ""); 1228 } 1229 edgeflag = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x4000), ""); 1230 edgeflag = LLVMBuildShl(builder, edgeflag, lp_build_const_int32(gallivm, 3), ""); 1231 1232 mask = LLVMBuildOr(builder, vertex_id, clipmask, ""); 1233 if (0) { 1234 mask = LLVMBuildOr(builder, mask, pad, ""); 1235 } 1236 mask = LLVMBuildOr(builder, mask, edgeflag, ""); 1237#endif 1238 return mask; 1239} 1240 1241static void 1242store_aos_array(struct gallivm_state *gallivm, 1243 struct lp_type soa_type, 1244 LLVMValueRef io_ptr, 1245 LLVMValueRef *indices, 1246 LLVMValueRef* aos, 1247 int attrib, 1248 int num_outputs, 1249 LLVMValueRef clipmask, 1250 boolean need_edgeflag) 1251{ 1252 LLVMBuilderRef builder = gallivm->builder; 1253 LLVMValueRef attr_index = lp_build_const_int32(gallivm, attrib); 1254 LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32]; 1255 LLVMValueRef linear_inds[LP_MAX_VECTOR_WIDTH / 32]; 1256 LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32]; 1257 int vector_length = soa_type.length; 1258 int i; 1259 1260 debug_assert(TGSI_NUM_CHANNELS == 4); 1261 1262 for (i = 0; i < vector_length; i++) { 1263 linear_inds[i] = lp_build_const_int32(gallivm, i); 1264 if (indices) { 1265 inds[i] = indices[i]; 1266 } else { 1267 inds[i] = linear_inds[i]; 1268 } 1269 io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, ""); 1270 } 1271 1272 if (attrib == 0) { 1273 /* store vertex header for each of the n vertices */ 1274 LLVMValueRef val, cliptmp; 1275 int vertex_id_pad_edgeflag; 1276 1277 /* If this assertion fails, it means we need to update the bit twidding 1278 * code here. See struct vertex_header in draw_private.h. 1279 */ 1280 assert(DRAW_TOTAL_CLIP_PLANES==14); 1281 /* initialize vertex id:16 = 0xffff, pad:1 = 0, edgeflag:1 = 1 */ 1282 if (!need_edgeflag) { 1283 vertex_id_pad_edgeflag = (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES); 1284 } 1285 else { 1286 vertex_id_pad_edgeflag = (0xffff << 16); 1287 } 1288 val = lp_build_const_int_vec(gallivm, lp_int_type(soa_type), 1289 vertex_id_pad_edgeflag); 1290 /* OR with the clipmask */ 1291 cliptmp = LLVMBuildOr(builder, val, clipmask, ""); 1292 for (i = 0; i < vector_length; i++) { 1293 LLVMValueRef id_ptr = draw_jit_header_id(gallivm, io_ptrs[i]); 1294 val = LLVMBuildExtractElement(builder, cliptmp, linear_inds[i], ""); 1295 val = adjust_mask(gallivm, val); 1296#if DEBUG_STORE 1297 lp_build_printf(gallivm, "io = %p, index %d, clipmask = %x\n", 1298 io_ptrs[i], inds[i], val); 1299#endif 1300 LLVMBuildStore(builder, val, id_ptr); 1301 } 1302 } 1303 1304 /* store for each of the n vertices */ 1305 for (i = 0; i < vector_length; i++) { 1306 store_aos(gallivm, io_ptrs[i], attr_index, aos[i]); 1307 } 1308} 1309 1310 1311static void 1312convert_to_aos(struct gallivm_state *gallivm, 1313 LLVMValueRef io, 1314 LLVMValueRef *indices, 1315 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 1316 LLVMValueRef clipmask, 1317 int num_outputs, 1318 struct lp_type soa_type, 1319 boolean need_edgeflag) 1320{ 1321 LLVMBuilderRef builder = gallivm->builder; 1322 unsigned chan, attrib, i; 1323 1324#if DEBUG_STORE 1325 lp_build_printf(gallivm, " # storing begin\n"); 1326#endif 1327 for (attrib = 0; attrib < num_outputs; ++attrib) { 1328 LLVMValueRef soa[TGSI_NUM_CHANNELS]; 1329 LLVMValueRef aos[LP_MAX_VECTOR_WIDTH / 32]; 1330 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 1331 if (outputs[attrib][chan]) { 1332 LLVMValueRef out = LLVMBuildLoad(builder, outputs[attrib][chan], ""); 1333 lp_build_name(out, "output%u.%c", attrib, "xyzw"[chan]); 1334#if DEBUG_STORE 1335 lp_build_printf(gallivm, "output %d : %d ", 1336 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 1337 attrib, 0), 1338 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 1339 chan, 0)); 1340 lp_build_print_value(gallivm, "val = ", out); 1341 { 1342 LLVMValueRef iv = 1343 LLVMBuildBitCast(builder, out, lp_build_int_vec_type(gallivm, soa_type), ""); 1344 1345 lp_build_print_value(gallivm, " ival = ", iv); 1346 } 1347#endif 1348 soa[chan] = out; 1349 } 1350 else { 1351 soa[chan] = 0; 1352 } 1353 } 1354 1355 1356 if (soa_type.length == TGSI_NUM_CHANNELS) { 1357 lp_build_transpose_aos(gallivm, soa_type, soa, aos); 1358 } else { 1359 lp_build_transpose_aos(gallivm, soa_type, soa, soa); 1360 1361 for (i = 0; i < soa_type.length; ++i) { 1362 aos[i] = lp_build_extract_range(gallivm, 1363 soa[i % TGSI_NUM_CHANNELS], 1364 (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS, 1365 TGSI_NUM_CHANNELS); 1366 } 1367 } 1368 1369 store_aos_array(gallivm, 1370 soa_type, 1371 io, indices, 1372 aos, 1373 attrib, 1374 num_outputs, 1375 clipmask, 1376 need_edgeflag); 1377 } 1378#if DEBUG_STORE 1379 lp_build_printf(gallivm, " # storing end\n"); 1380#endif 1381} 1382 1383 1384/** 1385 * Stores original vertex positions in clip coordinates 1386 */ 1387static void 1388store_clip(struct gallivm_state *gallivm, 1389 const struct lp_type vs_type, 1390 LLVMValueRef io_ptr, 1391 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 1392 int idx) 1393{ 1394 LLVMBuilderRef builder = gallivm->builder; 1395 LLVMValueRef soa[4]; 1396 LLVMValueRef aos[LP_MAX_VECTOR_LENGTH]; 1397 LLVMValueRef indices[2]; 1398 LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32]; 1399 LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32]; 1400 LLVMValueRef clip_ptrs[LP_MAX_VECTOR_WIDTH / 32]; 1401 LLVMTypeRef clip_ptr_type = 1402 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 1403 4), 0); 1404 int i, j; 1405 1406 indices[0] = 1407 indices[1] = lp_build_const_int32(gallivm, 0); 1408 1409 for (i = 0; i < vs_type.length; i++) { 1410 inds[i] = lp_build_const_int32(gallivm, i); 1411 io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, ""); 1412 } 1413 1414 soa[0] = LLVMBuildLoad(builder, outputs[idx][0], ""); /*x0 x1 .. xn*/ 1415 soa[1] = LLVMBuildLoad(builder, outputs[idx][1], ""); /*y0 y1 .. yn*/ 1416 soa[2] = LLVMBuildLoad(builder, outputs[idx][2], ""); /*z0 z1 .. zn*/ 1417 soa[3] = LLVMBuildLoad(builder, outputs[idx][3], ""); /*w0 w1 .. wn*/ 1418 1419 for (i = 0; i < vs_type.length; i++) { 1420 clip_ptrs[i] = draw_jit_header_clip_pos(gallivm, io_ptrs[i]); 1421 } 1422 1423 lp_build_transpose_aos(gallivm, vs_type, soa, soa); 1424 for (i = 0; i < vs_type.length; ++i) { 1425 aos[i] = lp_build_extract_range(gallivm, 1426 soa[i % TGSI_NUM_CHANNELS], 1427 (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS, 1428 TGSI_NUM_CHANNELS); 1429 } 1430 1431 for (j = 0; j < vs_type.length; j++) { 1432 LLVMValueRef clip_ptr; 1433 1434 clip_ptr = LLVMBuildGEP(builder, clip_ptrs[j], indices, 2, "clipo"); 1435 clip_ptr = LLVMBuildPointerCast(builder, clip_ptr, clip_ptr_type, ""); 1436 1437 /* Unaligned store */ 1438 LLVMSetAlignment(LLVMBuildStore(builder, aos[j], clip_ptr), sizeof(float)); 1439 } 1440} 1441 1442 1443/** 1444 * Transforms the outputs for viewport mapping 1445 */ 1446static void 1447generate_viewport(struct draw_llvm_variant *variant, 1448 LLVMBuilderRef builder, 1449 struct lp_type vs_type, 1450 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 1451 LLVMValueRef context_ptr) 1452{ 1453 int i; 1454 struct gallivm_state *gallivm = variant->gallivm; 1455 struct lp_type f32_type = vs_type; 1456 const unsigned pos = variant->llvm->draw->vs.position_output; 1457 LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type); 1458 LLVMValueRef out3 = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 .. wn*/ 1459 LLVMValueRef const1 = lp_build_const_vec(gallivm, f32_type, 1.0); /*1.0 1.0 1.0 1.0*/ 1460 LLVMValueRef vp_ptr = draw_jit_context_viewports(gallivm, context_ptr); 1461 1462 /* We treat pipe_viewport_state as a float array */ 1463 const int scale_index_offset = offsetof(struct pipe_viewport_state, scale) / sizeof(float); 1464 const int trans_index_offset = offsetof(struct pipe_viewport_state, translate) / sizeof(float); 1465 1466 /* for 1/w convention*/ 1467 out3 = LLVMBuildFDiv(builder, const1, out3, ""); 1468 LLVMBuildStore(builder, out3, outputs[pos][3]); 1469 1470 /* Viewport Mapping */ 1471 for (i=0; i<3; i++) { 1472 LLVMValueRef out = LLVMBuildLoad(builder, outputs[pos][i], ""); /*x0 x1 .. xn*/ 1473 LLVMValueRef scale; 1474 LLVMValueRef trans; 1475 LLVMValueRef scale_i; 1476 LLVMValueRef trans_i; 1477 LLVMValueRef index; 1478 1479 index = lp_build_const_int32(gallivm, i + scale_index_offset); 1480 scale_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, ""); 1481 1482 index = lp_build_const_int32(gallivm, i + trans_index_offset); 1483 trans_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, ""); 1484 1485 scale = lp_build_broadcast(gallivm, vs_type_llvm, 1486 LLVMBuildLoad(builder, scale_i, "scale")); 1487 trans = lp_build_broadcast(gallivm, vs_type_llvm, 1488 LLVMBuildLoad(builder, trans_i, "trans")); 1489 1490 /* divide by w */ 1491 out = LLVMBuildFMul(builder, out, out3, ""); 1492 /* mult by scale, add translation */ 1493 out = lp_build_fmuladd(builder, out, scale, trans); 1494 1495 /* store transformed outputs */ 1496 LLVMBuildStore(builder, out, outputs[pos][i]); 1497 } 1498 1499} 1500 1501 1502/** 1503 * Returns clipmask as nxi32 bitmask for the n vertices 1504 */ 1505static LLVMValueRef 1506generate_clipmask(struct draw_llvm *llvm, 1507 struct gallivm_state *gallivm, 1508 struct lp_type vs_type, 1509 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 1510 struct draw_llvm_variant_key *key, 1511 LLVMValueRef context_ptr, 1512 boolean *have_clipdist) 1513{ 1514 LLVMBuilderRef builder = gallivm->builder; 1515 LLVMValueRef mask; /* stores the <nxi32> clipmasks */ 1516 LLVMValueRef test, temp; 1517 LLVMValueRef zero, shift; 1518 LLVMValueRef pos_x, pos_y, pos_z, pos_w; 1519 LLVMValueRef cv_x, cv_y, cv_z, cv_w; 1520 LLVMValueRef plane1, planes, plane_ptr, sum; 1521 struct lp_type f32_type = vs_type; 1522 struct lp_type i32_type = lp_int_type(vs_type); 1523 const unsigned pos = llvm->draw->vs.position_output; 1524 const unsigned cv = llvm->draw->vs.clipvertex_output; 1525 int num_written_clipdistance = llvm->draw->vs.vertex_shader->info.num_written_clipdistance; 1526 boolean have_cd = false; 1527 boolean clip_user = key->clip_user; 1528 unsigned ucp_enable = key->ucp_enable; 1529 unsigned cd[2]; 1530 1531 cd[0] = llvm->draw->vs.ccdistance_output[0]; 1532 cd[1] = llvm->draw->vs.ccdistance_output[1]; 1533 1534 if (cd[0] != pos || cd[1] != pos) 1535 have_cd = true; 1536 1537 if (num_written_clipdistance && !clip_user) { 1538 clip_user = true; 1539 ucp_enable = (1 << num_written_clipdistance) - 1; 1540 } 1541 1542 mask = lp_build_const_int_vec(gallivm, i32_type, 0); 1543 temp = lp_build_const_int_vec(gallivm, i32_type, 0); 1544 zero = lp_build_const_vec(gallivm, f32_type, 0); /* 0.0f 0.0f 0.0f 0.0f */ 1545 shift = lp_build_const_int_vec(gallivm, i32_type, 1); /* 1 1 1 1 */ 1546 1547 /* 1548 * load clipvertex and position from correct locations. 1549 * if they are the same just load them once. 1550 */ 1551 pos_x = LLVMBuildLoad(builder, outputs[pos][0], ""); /*x0 x1 .. xn */ 1552 pos_y = LLVMBuildLoad(builder, outputs[pos][1], ""); /*y0 y1 .. yn */ 1553 pos_z = LLVMBuildLoad(builder, outputs[pos][2], ""); /*z0 z1 .. zn */ 1554 pos_w = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 .. wn */ 1555 1556 if (clip_user && cv != pos) { 1557 cv_x = LLVMBuildLoad(builder, outputs[cv][0], ""); /*x0 x1 .. xn */ 1558 cv_y = LLVMBuildLoad(builder, outputs[cv][1], ""); /*y0 y1 .. yn */ 1559 cv_z = LLVMBuildLoad(builder, outputs[cv][2], ""); /*z0 z1 .. zn */ 1560 cv_w = LLVMBuildLoad(builder, outputs[cv][3], ""); /*w0 w1 .. wn */ 1561 } else { 1562 cv_x = pos_x; 1563 cv_y = pos_y; 1564 cv_z = pos_z; 1565 cv_w = pos_w; 1566 } 1567 1568 /* 1569 * Be careful with the comparisons and NaNs (using llvm's unordered 1570 * comparisons here). 1571 */ 1572 /* Cliptest, for hardwired planes */ 1573 /* 1574 * XXX should take guardband into account (currently not in key). 1575 * Otherwise might run the draw pipeline stages for nothing. 1576 */ 1577 if (key->clip_xy) { 1578 /* plane 1 */ 1579 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_x , pos_w); 1580 temp = shift; 1581 test = LLVMBuildAnd(builder, test, temp, ""); 1582 mask = test; 1583 1584 /* plane 2 */ 1585 test = LLVMBuildFAdd(builder, pos_x, pos_w, ""); 1586 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test); 1587 temp = LLVMBuildShl(builder, temp, shift, ""); 1588 test = LLVMBuildAnd(builder, test, temp, ""); 1589 mask = LLVMBuildOr(builder, mask, test, ""); 1590 1591 /* plane 3 */ 1592 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_y, pos_w); 1593 temp = LLVMBuildShl(builder, temp, shift, ""); 1594 test = LLVMBuildAnd(builder, test, temp, ""); 1595 mask = LLVMBuildOr(builder, mask, test, ""); 1596 1597 /* plane 4 */ 1598 test = LLVMBuildFAdd(builder, pos_y, pos_w, ""); 1599 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test); 1600 temp = LLVMBuildShl(builder, temp, shift, ""); 1601 test = LLVMBuildAnd(builder, test, temp, ""); 1602 mask = LLVMBuildOr(builder, mask, test, ""); 1603 } 1604 1605 if (key->clip_z) { 1606 temp = lp_build_const_int_vec(gallivm, i32_type, 16); 1607 if (key->clip_halfz) { 1608 /* plane 5 */ 1609 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, pos_z); 1610 test = LLVMBuildAnd(builder, test, temp, ""); 1611 mask = LLVMBuildOr(builder, mask, test, ""); 1612 } 1613 else { 1614 /* plane 5 */ 1615 test = LLVMBuildFAdd(builder, pos_z, pos_w, ""); 1616 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test); 1617 test = LLVMBuildAnd(builder, test, temp, ""); 1618 mask = LLVMBuildOr(builder, mask, test, ""); 1619 } 1620 /* plane 6 */ 1621 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_z, pos_w); 1622 temp = LLVMBuildShl(builder, temp, shift, ""); 1623 test = LLVMBuildAnd(builder, test, temp, ""); 1624 mask = LLVMBuildOr(builder, mask, test, ""); 1625 } 1626 1627 if (clip_user) { 1628 LLVMValueRef planes_ptr = draw_jit_context_planes(gallivm, context_ptr); 1629 LLVMValueRef indices[3]; 1630 LLVMValueRef is_nan_or_inf; 1631 1632 /* userclip planes */ 1633 while (ucp_enable) { 1634 unsigned plane_idx = ffs(ucp_enable)-1; 1635 ucp_enable &= ~(1 << plane_idx); 1636 plane_idx += 6; 1637 1638 if (have_cd && num_written_clipdistance) { 1639 LLVMValueRef clipdist; 1640 int i; 1641 i = plane_idx - 6; 1642 1643 *have_clipdist = TRUE; 1644 if (i < 4) { 1645 clipdist = LLVMBuildLoad(builder, outputs[cd[0]][i], ""); 1646 } else { 1647 clipdist = LLVMBuildLoad(builder, outputs[cd[1]][i-4], ""); 1648 } 1649 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, clipdist); 1650 is_nan_or_inf = lp_build_is_inf_or_nan(gallivm, vs_type, clipdist); 1651 test = LLVMBuildOr(builder, test, is_nan_or_inf, ""); 1652 temp = lp_build_const_int_vec(gallivm, i32_type, 1LL << plane_idx); 1653 test = LLVMBuildAnd(builder, test, temp, ""); 1654 mask = LLVMBuildOr(builder, mask, test, ""); 1655 } else { 1656 LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type); 1657 indices[0] = lp_build_const_int32(gallivm, 0); 1658 indices[1] = lp_build_const_int32(gallivm, plane_idx); 1659 1660 indices[2] = lp_build_const_int32(gallivm, 0); 1661 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, ""); 1662 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_x"); 1663 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1); 1664 sum = LLVMBuildFMul(builder, planes, cv_x, ""); 1665 1666 indices[2] = lp_build_const_int32(gallivm, 1); 1667 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, ""); 1668 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_y"); 1669 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1); 1670 sum = lp_build_fmuladd(builder, planes, cv_y, sum); 1671 1672 indices[2] = lp_build_const_int32(gallivm, 2); 1673 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, ""); 1674 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_z"); 1675 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1); 1676 sum = lp_build_fmuladd(builder, planes, cv_z, sum); 1677 1678 indices[2] = lp_build_const_int32(gallivm, 3); 1679 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, ""); 1680 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_w"); 1681 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1); 1682 sum = lp_build_fmuladd(builder, planes, cv_w, sum); 1683 1684 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, sum); 1685 temp = lp_build_const_int_vec(gallivm, i32_type, 1LL << plane_idx); 1686 test = LLVMBuildAnd(builder, test, temp, ""); 1687 mask = LLVMBuildOr(builder, mask, test, ""); 1688 } 1689 } 1690 } 1691 if (key->need_edgeflags) { 1692 /* 1693 * This isn't really part of clipmask but stored the same in vertex 1694 * header later, so do it here. 1695 */ 1696 unsigned edge_attr = llvm->draw->vs.edgeflag_output; 1697 LLVMValueRef one = lp_build_const_vec(gallivm, f32_type, 1.0); 1698 LLVMValueRef edgeflag = LLVMBuildLoad(builder, outputs[edge_attr][0], ""); 1699 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_EQUAL, one, edgeflag); 1700 temp = lp_build_const_int_vec(gallivm, i32_type, 1701 1LL << DRAW_TOTAL_CLIP_PLANES); 1702 test = LLVMBuildAnd(builder, test, temp, ""); 1703 mask = LLVMBuildOr(builder, mask, test, ""); 1704 } 1705 return mask; 1706} 1707 1708 1709/** 1710 * Returns boolean if any clipping has occurred 1711 * Used zero/one i8 value to represent boolean 1712 */ 1713static LLVMValueRef 1714clipmask_booli8(struct gallivm_state *gallivm, 1715 const struct lp_type vs_type, 1716 LLVMValueRef clipmask_bool_ptr, 1717 boolean edgeflag_in_clipmask) 1718{ 1719 LLVMBuilderRef builder = gallivm->builder; 1720 LLVMTypeRef int8_type = LLVMInt8TypeInContext(gallivm->context); 1721 LLVMValueRef clipmask_bool = LLVMBuildLoad(builder, clipmask_bool_ptr, ""); 1722 LLVMValueRef ret; 1723 struct lp_build_context bldivec; 1724 1725 lp_build_context_init(&bldivec, gallivm, lp_int_type(vs_type)); 1726 1727 /* 1728 * We need to invert the edgeflag bit from the clipmask here 1729 * (because the result is really if we want to run the pipeline or not 1730 * and we (may) need it if edgeflag was 0). 1731 */ 1732 if (edgeflag_in_clipmask) { 1733 LLVMValueRef edge = lp_build_const_int_vec(gallivm, bldivec.type, 1734 1LL << DRAW_TOTAL_CLIP_PLANES); 1735 clipmask_bool = LLVMBuildXor(builder, clipmask_bool, edge, ""); 1736 } 1737 1738 /* 1739 * XXX: probably should mask off bits from the mask which come from 1740 * vertices which were beyond the count (i.e. indices_valid for 1741 * linear fetches, for elts ones we don't have the correct mask 1742 * right now). Otherwise might run the pipeline for nothing, 1743 * though everything should still work. 1744 */ 1745 ret = lp_build_any_true_range(&bldivec, vs_type.length, clipmask_bool); 1746 ret = LLVMBuildZExt(builder, ret, int8_type, ""); 1747 return ret; 1748} 1749 1750static LLVMValueRef 1751draw_gs_llvm_fetch_input(const struct lp_build_gs_iface *gs_iface, 1752 struct lp_build_context * bld, 1753 boolean is_vindex_indirect, 1754 LLVMValueRef vertex_index, 1755 boolean is_aindex_indirect, 1756 LLVMValueRef attrib_index, 1757 LLVMValueRef swizzle_index) 1758{ 1759 const struct draw_gs_llvm_iface *gs = draw_gs_llvm_iface(gs_iface); 1760 struct gallivm_state *gallivm = bld->gallivm; 1761 LLVMBuilderRef builder = gallivm->builder; 1762 LLVMValueRef indices[3]; 1763 LLVMValueRef res; 1764 struct lp_type type = bld->type; 1765 1766 if (is_vindex_indirect || is_aindex_indirect) { 1767 int i; 1768 res = bld->zero; 1769 for (i = 0; i < type.length; ++i) { 1770 LLVMValueRef idx = lp_build_const_int32(gallivm, i); 1771 LLVMValueRef vert_chan_index = vertex_index; 1772 LLVMValueRef attr_chan_index = attrib_index; 1773 LLVMValueRef channel_vec, value; 1774 1775 if (is_vindex_indirect) { 1776 vert_chan_index = LLVMBuildExtractElement(builder, 1777 vertex_index, idx, ""); 1778 } 1779 if (is_aindex_indirect) { 1780 attr_chan_index = LLVMBuildExtractElement(builder, 1781 attrib_index, idx, ""); 1782 } 1783 1784 indices[0] = vert_chan_index; 1785 indices[1] = attr_chan_index; 1786 indices[2] = swizzle_index; 1787 1788 channel_vec = LLVMBuildGEP(builder, gs->input, indices, 3, ""); 1789 channel_vec = LLVMBuildLoad(builder, channel_vec, ""); 1790 value = LLVMBuildExtractElement(builder, channel_vec, idx, ""); 1791 1792 res = LLVMBuildInsertElement(builder, res, value, idx, ""); 1793 } 1794 } else { 1795 indices[0] = vertex_index; 1796 indices[1] = attrib_index; 1797 indices[2] = swizzle_index; 1798 1799 res = LLVMBuildGEP(builder, gs->input, indices, 3, ""); 1800 res = LLVMBuildLoad(builder, res, ""); 1801 } 1802 1803 return res; 1804} 1805 1806static void 1807draw_gs_llvm_emit_vertex(const struct lp_build_gs_iface *gs_base, 1808 struct lp_build_context * bld, 1809 LLVMValueRef (*outputs)[4], 1810 LLVMValueRef emitted_vertices_vec, 1811 LLVMValueRef mask_vec, LLVMValueRef stream_id) 1812{ 1813 const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base); 1814 struct draw_gs_llvm_variant *variant = gs_iface->variant; 1815 struct gallivm_state *gallivm = variant->gallivm; 1816 LLVMBuilderRef builder = gallivm->builder; 1817 struct lp_type gs_type = bld->type; 1818 LLVMValueRef clipmask = lp_build_const_int_vec(gallivm, 1819 lp_int_type(gs_type), 0); 1820 LLVMValueRef indices[LP_MAX_VECTOR_LENGTH]; 1821 LLVMValueRef next_prim_offset = 1822 lp_build_const_int32(gallivm, variant->shader->base.primitive_boundary); 1823 LLVMValueRef io = variant->io_ptr; 1824 unsigned i; 1825 const struct tgsi_shader_info *gs_info = &variant->shader->base.info; 1826 1827 LLVMValueRef cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, mask_vec, lp_build_const_int_vec(gallivm, bld->type, 0), ""); 1828 for (i = 0; i < gs_type.length; ++i) { 1829 LLVMValueRef ind = lp_build_const_int32(gallivm, i); 1830 LLVMValueRef currently_emitted = 1831 LLVMBuildExtractElement(builder, emitted_vertices_vec, ind, ""); 1832 indices[i] = LLVMBuildMul(builder, ind, next_prim_offset, ""); 1833 indices[i] = LLVMBuildAdd(builder, indices[i], currently_emitted, ""); 1834 indices[i] = LLVMBuildSelect(builder, LLVMBuildExtractElement(builder, cond, ind, ""), indices[i], 1835 lp_build_const_int32(gallivm, variant->shader->base.primitive_boundary - 1), ""); 1836 } 1837 1838 LLVMValueRef stream_idx = LLVMBuildExtractElement(builder, stream_id, lp_build_const_int32(gallivm, 0), ""); 1839 LLVMValueRef cnd = LLVMBuildICmp(builder, LLVMIntULT, stream_idx, lp_build_const_int32(gallivm, variant->shader->base.num_vertex_streams), ""); 1840 struct lp_build_if_state if_ctx; 1841 lp_build_if(&if_ctx, gallivm, cnd); 1842 io = lp_build_pointer_get(builder, io, LLVMBuildExtractElement(builder, stream_id, lp_build_const_int32(gallivm, 0), "")); 1843 1844 if (variant->key.clamp_vertex_color) { 1845 do_clamp_vertex_color(gallivm, gs_type, 1846 gs_info, outputs); 1847 } 1848 convert_to_aos(gallivm, io, indices, 1849 outputs, clipmask, 1850 gs_info->num_outputs, gs_type, 1851 FALSE); 1852 lp_build_endif(&if_ctx); 1853} 1854 1855static void 1856draw_gs_llvm_end_primitive(const struct lp_build_gs_iface *gs_base, 1857 struct lp_build_context * bld, 1858 LLVMValueRef total_emitted_vertices_vec_ptr, 1859 LLVMValueRef verts_per_prim_vec, 1860 LLVMValueRef emitted_prims_vec, 1861 LLVMValueRef mask_vec, unsigned stream) 1862{ 1863 const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base); 1864 struct draw_gs_llvm_variant *variant = gs_iface->variant; 1865 struct gallivm_state *gallivm = variant->gallivm; 1866 LLVMBuilderRef builder = gallivm->builder; 1867 LLVMValueRef prim_lengts_ptr = 1868 draw_gs_jit_prim_lengths(variant->gallivm, variant->context_ptr); 1869 unsigned i; 1870 1871 LLVMValueRef cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, mask_vec, lp_build_const_int_vec(gallivm, bld->type, 0), ""); 1872 for (i = 0; i < bld->type.length; ++i) { 1873 LLVMValueRef ind = lp_build_const_int32(gallivm, i); 1874 LLVMValueRef prims_emitted = 1875 LLVMBuildExtractElement(builder, emitted_prims_vec, ind, ""); 1876 LLVMValueRef store_ptr; 1877 LLVMValueRef num_vertices = 1878 LLVMBuildExtractElement(builder, verts_per_prim_vec, ind, ""); 1879 1880 LLVMValueRef this_cond = LLVMBuildExtractElement(gallivm->builder, cond, ind, ""); 1881 struct lp_build_if_state ifthen; 1882 lp_build_if(&ifthen, gallivm, this_cond); 1883 prims_emitted = LLVMBuildMul(gallivm->builder, prims_emitted, lp_build_const_int32(gallivm, variant->shader->base.num_vertex_streams), ""); 1884 prims_emitted = LLVMBuildAdd(gallivm->builder, prims_emitted, lp_build_const_int32(gallivm, stream), ""); 1885 store_ptr = LLVMBuildGEP(builder, prim_lengts_ptr, &prims_emitted, 1, ""); 1886 store_ptr = LLVMBuildLoad(builder, store_ptr, ""); 1887 store_ptr = LLVMBuildGEP(builder, store_ptr, &ind, 1, ""); 1888 LLVMBuildStore(builder, num_vertices, store_ptr); 1889 lp_build_endif(&ifthen); 1890 } 1891} 1892 1893static void 1894draw_gs_llvm_epilogue(const struct lp_build_gs_iface *gs_base, 1895 LLVMValueRef total_emitted_vertices_vec, 1896 LLVMValueRef emitted_prims_vec, unsigned stream) 1897{ 1898 const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base); 1899 struct draw_gs_llvm_variant *variant = gs_iface->variant; 1900 struct gallivm_state *gallivm = variant->gallivm; 1901 LLVMBuilderRef builder = gallivm->builder; 1902 LLVMValueRef emitted_verts_ptr = 1903 draw_gs_jit_emitted_vertices(gallivm, variant->context_ptr); 1904 LLVMValueRef emitted_prims_ptr = 1905 draw_gs_jit_emitted_prims(gallivm, variant->context_ptr); 1906 LLVMValueRef stream_val = lp_build_const_int32(gallivm, stream); 1907 1908 emitted_verts_ptr = LLVMBuildGEP(builder, emitted_verts_ptr, &stream_val, 1, ""); 1909 emitted_prims_ptr = LLVMBuildGEP(builder, emitted_prims_ptr, &stream_val, 1, ""); 1910 1911 LLVMBuildStore(builder, total_emitted_vertices_vec, emitted_verts_ptr); 1912 LLVMBuildStore(builder, emitted_prims_vec, emitted_prims_ptr); 1913} 1914 1915static void 1916draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *variant) 1917{ 1918 struct gallivm_state *gallivm = variant->gallivm; 1919 LLVMContextRef context = gallivm->context; 1920 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context); 1921 LLVMTypeRef arg_types[13]; 1922 unsigned num_arg_types = ARRAY_SIZE(arg_types); 1923 LLVMTypeRef func_type; 1924 LLVMValueRef context_ptr; 1925 LLVMBasicBlockRef block; 1926 LLVMBuilderRef builder; 1927 char func_name[64]; 1928 struct lp_type vs_type; 1929 LLVMValueRef count, fetch_elts, start_or_maxelt; 1930 LLVMValueRef vertex_id_offset; 1931 LLVMValueRef stride, step, io_itr; 1932 LLVMValueRef ind_vec, start_vec, have_elts, fetch_max, tmp; 1933 LLVMValueRef io_ptr, vbuffers_ptr, vb_ptr; 1934 LLVMValueRef vb_stride[PIPE_MAX_ATTRIBS]; 1935 LLVMValueRef map_ptr[PIPE_MAX_ATTRIBS]; 1936 LLVMValueRef buffer_size_adj[PIPE_MAX_ATTRIBS]; 1937 LLVMValueRef instance_index[PIPE_MAX_ATTRIBS]; 1938 LLVMValueRef fake_buf_ptr, fake_buf; 1939 1940 struct draw_context *draw = llvm->draw; 1941 const struct tgsi_shader_info *vs_info = &draw->vs.vertex_shader->info; 1942 unsigned i, j; 1943 struct lp_build_context bld, blduivec; 1944 struct lp_build_loop_state lp_loop; 1945 struct lp_build_if_state if_ctx; 1946 const int vector_length = lp_native_vector_width / 32; 1947 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS]; 1948 struct lp_build_sampler_soa *sampler = 0; 1949 struct lp_build_image_soa *image = NULL; 1950 LLVMValueRef ret, clipmask_bool_ptr; 1951 struct draw_llvm_variant_key *key = &variant->key; 1952 /* If geometry shader is present we need to skip both the viewport 1953 * transformation and clipping otherwise the inputs to the geometry 1954 * shader will be incorrect. 1955 * The code can't handle vp transform when vs writes vp index neither 1956 * (though this would be fixable here, but couldn't just broadcast 1957 * the values). 1958 */ 1959 const boolean bypass_viewport = key->has_gs_or_tes || key->bypass_viewport || 1960 vs_info->writes_viewport_index; 1961 const boolean enable_cliptest = !key->has_gs_or_tes && (key->clip_xy || 1962 key->clip_z || 1963 key->clip_user || 1964 key->need_edgeflags); 1965 LLVMValueRef variant_func; 1966 const unsigned pos = draw->vs.position_output; 1967 const unsigned cv = draw->vs.clipvertex_output; 1968 boolean have_clipdist = FALSE; 1969 struct lp_bld_tgsi_system_values system_values; 1970 1971 memset(&system_values, 0, sizeof(system_values)); 1972 memset(&outputs, 0, sizeof(outputs)); 1973 snprintf(func_name, sizeof(func_name), "draw_llvm_vs_variant"); 1974 1975 i = 0; 1976 arg_types[i++] = get_context_ptr_type(variant); /* context */ 1977 arg_types[i++] = get_vertex_header_ptr_type(variant); /* vertex_header */ 1978 arg_types[i++] = get_buffer_ptr_type(variant); /* vbuffers */ 1979 arg_types[i++] = int32_type; /* count */ 1980 arg_types[i++] = int32_type; /* start/fetch_elt_max */ 1981 arg_types[i++] = int32_type; /* stride */ 1982 arg_types[i++] = get_vb_ptr_type(variant); /* pipe_vertex_buffer's */ 1983 arg_types[i++] = int32_type; /* instance_id */ 1984 arg_types[i++] = int32_type; /* vertex_id_offset */ 1985 arg_types[i++] = int32_type; /* start_instance */ 1986 arg_types[i++] = LLVMPointerType(int32_type, 0); /* fetch_elts */ 1987 arg_types[i++] = int32_type; /* draw_id */ 1988 arg_types[i++] = int32_type; /* view_id */ 1989 1990 func_type = LLVMFunctionType(LLVMInt8TypeInContext(context), 1991 arg_types, num_arg_types, 0); 1992 1993 variant_func = LLVMAddFunction(gallivm->module, func_name, func_type); 1994 variant->function = variant_func; 1995 1996 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv); 1997 for (i = 0; i < num_arg_types; ++i) 1998 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) 1999 lp_add_function_attr(variant_func, i + 1, LP_FUNC_ATTR_NOALIAS); 2000 2001 if (gallivm->cache && gallivm->cache->data_size) 2002 return; 2003 context_ptr = LLVMGetParam(variant_func, 0); 2004 io_ptr = LLVMGetParam(variant_func, 1); 2005 vbuffers_ptr = LLVMGetParam(variant_func, 2); 2006 count = LLVMGetParam(variant_func, 3); 2007 /* 2008 * XXX: the maxelt part is unused. Not really useful, since we cannot 2009 * get index buffer overflows due to vsplit (which provides its own 2010 * elts buffer, with a different size than what's passed in here). 2011 */ 2012 start_or_maxelt = LLVMGetParam(variant_func, 4); 2013 /* 2014 * XXX: stride is actually unused. The stride we use is strictly calculated 2015 * from the number of outputs (including the draw_extra outputs). 2016 * Should probably fix some day (we need a new vs just because of extra 2017 * outputs which the generated vs won't touch). 2018 */ 2019 stride = LLVMGetParam(variant_func, 5); 2020 vb_ptr = LLVMGetParam(variant_func, 6); 2021 system_values.instance_id = LLVMGetParam(variant_func, 7); 2022 vertex_id_offset = LLVMGetParam(variant_func, 8); 2023 system_values.base_instance = LLVMGetParam(variant_func, 9); 2024 fetch_elts = LLVMGetParam(variant_func, 10); 2025 system_values.draw_id = LLVMGetParam(variant_func, 11); 2026 system_values.view_index = LLVMGetParam(variant_func, 12); 2027 2028 lp_build_name(context_ptr, "context"); 2029 lp_build_name(io_ptr, "io"); 2030 lp_build_name(vbuffers_ptr, "vbuffers"); 2031 lp_build_name(count, "count"); 2032 lp_build_name(start_or_maxelt, "start_or_maxelt"); 2033 lp_build_name(stride, "stride"); 2034 lp_build_name(vb_ptr, "vb"); 2035 lp_build_name(system_values.instance_id, "instance_id"); 2036 lp_build_name(vertex_id_offset, "vertex_id_offset"); 2037 lp_build_name(system_values.base_instance, "start_instance"); 2038 lp_build_name(fetch_elts, "fetch_elts"); 2039 lp_build_name(system_values.draw_id, "draw_id"); 2040 2041 /* 2042 * Function body 2043 */ 2044 2045 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry"); 2046 builder = gallivm->builder; 2047 LLVMPositionBuilderAtEnd(builder, block); 2048 2049 memset(&vs_type, 0, sizeof vs_type); 2050 vs_type.floating = TRUE; /* floating point values */ 2051 vs_type.sign = TRUE; /* values are signed */ 2052 vs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */ 2053 vs_type.width = 32; /* 32-bit float */ 2054 vs_type.length = vector_length; 2055 2056 lp_build_context_init(&bld, gallivm, lp_type_uint(32)); 2057 lp_build_context_init(&blduivec, gallivm, lp_uint_type(vs_type)); 2058 2059 /* hold temporary "bool" clipmask */ 2060 clipmask_bool_ptr = lp_build_alloca(gallivm, blduivec.vec_type, ""); 2061 2062 fake_buf = lp_build_alloca_undef(gallivm, 2063 LLVMVectorType(LLVMInt64TypeInContext(context), 4), ""); 2064 fake_buf = LLVMBuildBitCast(builder, fake_buf, 2065 LLVMPointerType(LLVMInt8TypeInContext(context), 0), ""); 2066 fake_buf_ptr = LLVMBuildGEP(builder, fake_buf, &bld.zero, 1, ""); 2067 2068 /* code generated texture sampling */ 2069 sampler = draw_llvm_sampler_soa_create(draw_llvm_variant_key_samplers(key), key->nr_samplers); 2070 2071 image = draw_llvm_image_soa_create(draw_llvm_variant_key_images(key), 2072 key->nr_images); 2073 2074 step = lp_build_const_int32(gallivm, vector_length); 2075 2076 ind_vec = blduivec.undef; 2077 for (i = 0; i < vs_type.length; i++) { 2078 LLVMValueRef index = lp_build_const_int32(gallivm, i); 2079 ind_vec = LLVMBuildInsertElement(builder, ind_vec, index, index, ""); 2080 } 2081 2082 have_elts = LLVMBuildICmp(builder, LLVMIntNE, 2083 LLVMConstPointerNull(arg_types[10]), fetch_elts, ""); 2084 2085 fetch_max = LLVMBuildSub(builder, count, bld.one, "fetch_max"); 2086 fetch_max = lp_build_broadcast_scalar(&blduivec, fetch_max); 2087 /* 2088 * Only needed for non-indexed path. 2089 */ 2090 start_vec = lp_build_broadcast_scalar(&blduivec, start_or_maxelt); 2091 2092 /* 2093 * Pre-calculate everything which is constant per shader invocation. 2094 */ 2095 for (j = 0; j < key->nr_vertex_elements; ++j) { 2096 LLVMValueRef vb_buffer_offset, buffer_size, temp_ptr; 2097 LLVMValueRef vb_info, vbuffer_ptr, buf_offset, ofbit; 2098 struct pipe_vertex_element *velem = &key->vertex_element[j]; 2099 LLVMValueRef vb_index = 2100 lp_build_const_int32(gallivm, velem->vertex_buffer_index); 2101 LLVMValueRef bsize = lp_build_const_int32(gallivm, 2102 util_format_get_blocksize(velem->src_format)); 2103 LLVMValueRef src_offset = lp_build_const_int32(gallivm, 2104 velem->src_offset); 2105 struct lp_build_if_state if_ctx; 2106 2107 if (velem->src_format != PIPE_FORMAT_NONE) { 2108 vbuffer_ptr = LLVMBuildGEP(builder, vbuffers_ptr, &vb_index, 1, ""); 2109 vb_info = LLVMBuildGEP(builder, vb_ptr, &vb_index, 1, ""); 2110 vb_stride[j] = draw_jit_vbuffer_stride(gallivm, vb_info); 2111 vb_stride[j] = LLVMBuildZExt(gallivm->builder, vb_stride[j], 2112 LLVMInt32TypeInContext(context), ""); 2113 vb_buffer_offset = draw_jit_vbuffer_offset(gallivm, vb_info); 2114 map_ptr[j] = draw_jit_dvbuffer_map(gallivm, vbuffer_ptr); 2115 buffer_size = draw_jit_dvbuffer_size(gallivm, vbuffer_ptr); 2116 2117 ofbit = NULL; 2118 /* 2119 * We'll set buffer_size_adj to zero if we have of, so it will 2120 * always overflow later automatically without having to keep ofbit. 2121 * Overflows (with normal wraparound) doing the actual offset 2122 * calculation should be ok, just not for the buffer size calc. 2123 * It would also be possible to detect such overflows and return 2124 * zeros if that happens, but this would be more complex. 2125 */ 2126 buf_offset = lp_build_add(&bld, vb_buffer_offset, src_offset); 2127 tmp = lp_build_sub(&bld, bsize, bld.one); 2128 buffer_size_adj[j] = lp_build_usub_overflow(gallivm, buffer_size, tmp, 2129 &ofbit); 2130 buffer_size_adj[j] = lp_build_usub_overflow(gallivm, buffer_size_adj[j], 2131 buf_offset, &ofbit); 2132 2133 /* 2134 * We can't easily set fake vertex buffers outside the generated code. 2135 * Hence, set fake vertex buffers here instead basically, so fetch 2136 * code can always fetch using offset 0, eliminating all control flow 2137 * inside the main loop. 2138 * (Alternatively, could have control flow per vector skipping fetch 2139 * if ofbit is true.) 2140 */ 2141 if (velem->instance_divisor) { 2142 /* 2143 * Index is equal to the start instance plus the number of current 2144 * instance divided by the divisor. In this case we compute it as: 2145 * index = start_instance + (instance_id / divisor). 2146 * Note we could actually do the fetch here, outside the loop - 2147 * it's all constant, hopefully llvm recognizes this. 2148 */ 2149 LLVMValueRef current_instance; 2150 current_instance = LLVMBuildUDiv(builder, system_values.instance_id, 2151 lp_build_const_int32(gallivm, 2152 velem->instance_divisor), 2153 "instance_divisor"); 2154 instance_index[j] = lp_build_uadd_overflow(gallivm, system_values.base_instance, 2155 current_instance, &ofbit); 2156 } 2157 2158 buffer_size_adj[j] = LLVMBuildSelect(builder, ofbit, bld.zero, 2159 buffer_size_adj[j], ""); 2160 2161 temp_ptr = lp_build_alloca_undef(gallivm, 2162 LLVMPointerType(LLVMInt8TypeInContext(context), 0), ""); 2163 2164 lp_build_if(&if_ctx, gallivm, ofbit); 2165 { 2166 LLVMBuildStore(builder, fake_buf_ptr, temp_ptr); 2167 } 2168 lp_build_else(&if_ctx); 2169 { 2170 map_ptr[j] = LLVMBuildGEP(builder, map_ptr[j], &buf_offset, 1, ""); 2171 LLVMBuildStore(builder, map_ptr[j], temp_ptr); 2172 } 2173 lp_build_endif(&if_ctx); 2174 map_ptr[j] = LLVMBuildLoad(builder, temp_ptr, "map_ptr"); 2175 2176 if (0) { 2177 lp_build_printf(gallivm, "velem %d, vbuf index = %u, vb_stride = %u\n", 2178 lp_build_const_int32(gallivm, j), 2179 vb_index, vb_stride[j]); 2180 lp_build_printf(gallivm, 2181 " vb_buffer_offset = %u, src_offset = %u, buf_offset = %u\n", 2182 vb_buffer_offset, src_offset, buf_offset); 2183 lp_build_printf(gallivm, " buffer size = %u, blocksize = %u\n", 2184 buffer_size, bsize); 2185 lp_build_printf(gallivm, " instance_id = %u\n", system_values.instance_id); 2186 } 2187 } 2188 } 2189 2190 lp_build_loop_begin(&lp_loop, gallivm, bld.zero); 2191 { 2192 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS]; 2193 LLVMValueRef io; 2194 LLVMValueRef clipmask; /* holds the clipmask value */ 2195 LLVMValueRef true_index_array, index_store; 2196 const LLVMValueRef (*ptr_aos)[TGSI_NUM_CHANNELS]; 2197 2198 io_itr = lp_loop.counter; 2199 2200 io = LLVMBuildGEP(builder, io_ptr, &io_itr, 1, ""); 2201#if DEBUG_STORE 2202 lp_build_printf(gallivm, " --- io %d = %p, loop counter %d\n", 2203 io_itr, io, lp_loop.counter); 2204#endif 2205 2206 true_index_array = lp_build_broadcast_scalar(&blduivec, lp_loop.counter); 2207 true_index_array = LLVMBuildAdd(builder, true_index_array, ind_vec, ""); 2208 2209 LLVMValueRef exec_mask = lp_build_cmp(&blduivec, PIPE_FUNC_LEQUAL, true_index_array, fetch_max); 2210 /* 2211 * Limit indices to fetch_max, otherwise might try to access indices 2212 * beyond index buffer (or rather vsplit elt buffer) size. 2213 * Could probably safely (?) skip this for non-indexed draws and 2214 * simplify things minimally (by removing it could combine the ind_vec 2215 * and start_vec adds). I think the only effect for non-indexed draws will 2216 * be that for the invalid elements they will be all fetched from the 2217 * same location as the last valid one, but noone should really care. 2218 */ 2219 true_index_array = lp_build_min(&blduivec, true_index_array, fetch_max); 2220 2221 index_store = lp_build_alloca_undef(gallivm, blduivec.vec_type, "index_store"); 2222 2223 lp_build_if(&if_ctx, gallivm, have_elts); 2224 { 2225 /* 2226 * Note: you'd expect some comparison/clamp against fetch_elt_max 2227 * here. 2228 * There used to be one here but it was incorrect: overflow was 2229 * detected if index > fetch_elt_max - but the correct condition 2230 * would be index >= fetch_elt_max (since this is just size of elts 2231 * buffer / element size). 2232 * Using the correct condition however will cause failures - due to 2233 * vsplit/vcache code which rebases indices. So, as an example, if 2234 * fetch_elt_max is just 1 and fetch_count 2, vsplit cache will 2235 * replace all invalid indices with 0 - which in case of elt_bias 2236 * not being zero will get a different fetch index than the valid 2237 * index 0. So, just rely on vsplit code preventing out-of-bounds 2238 * fetches. This is also why it's safe to do elts fetch even if there 2239 * was no index buffer bound - the real buffer is never seen here, at 2240 * least not if there are index buffer overflows... 2241 */ 2242 2243 /* 2244 * XXX should not have to do this, as scale can be handled 2245 * natively by loads (hits asserts though). 2246 */ 2247 tmp = lp_build_shl_imm(&blduivec, true_index_array, 2); 2248 fetch_elts = LLVMBuildBitCast(builder, fetch_elts, 2249 LLVMPointerType(LLVMInt8TypeInContext(context), 2250 0), ""); 2251 tmp = lp_build_gather(gallivm, vs_type.length, 2252 32, bld.type, TRUE, 2253 fetch_elts, tmp, FALSE); 2254 LLVMBuildStore(builder, tmp, index_store); 2255 } 2256 lp_build_else(&if_ctx); 2257 { 2258 tmp = LLVMBuildAdd(builder, true_index_array, start_vec, ""); 2259 LLVMBuildStore(builder, tmp, index_store); 2260 } 2261 lp_build_endif(&if_ctx); 2262 2263 true_index_array = LLVMBuildLoad(builder, index_store, ""); 2264 2265 for (j = 0; j < key->nr_vertex_elements; ++j) { 2266 struct pipe_vertex_element *velem = &key->vertex_element[j]; 2267 const struct util_format_description *format_desc = 2268 util_format_description(velem->src_format); 2269 2270 if (format_desc->format == PIPE_FORMAT_NONE) { 2271 for (i = 0; i < TGSI_NUM_CHANNELS; i++) { 2272 inputs[j][i] = lp_build_zero(gallivm, vs_type); 2273 } 2274 } 2275 else if (velem->instance_divisor) { 2276 fetch_instanced(gallivm, format_desc, vs_type, 2277 vb_stride[j], map_ptr[j], 2278 buffer_size_adj[j], 2279 inputs[j], instance_index[j]); 2280 } 2281 else { 2282 fetch_vector(gallivm, format_desc, vs_type, 2283 vb_stride[j], map_ptr[j], 2284 buffer_size_adj[j], 2285 inputs[j], true_index_array); 2286 } 2287 } 2288 2289 struct lp_build_mask_context mask; 2290 2291 lp_build_mask_begin(&mask, gallivm, vs_type, exec_mask); 2292 /* In the paths with elts vertex id has to be unaffected by the 2293 * index bias and because indices inside our elements array have 2294 * already had index bias applied we need to subtract it here to 2295 * get back to the original index. 2296 * in the linear paths vertex id has to be unaffected by the 2297 * original start index and because we abuse the 'start' variable 2298 * to either represent the actual start index or the index at which 2299 * the primitive was split (we split rendering into chunks of at 2300 * most 4095-vertices) we need to back out the original start 2301 * index out of our vertex id here. 2302 * for ARB_shader_draw_parameters, base_vertex should be 0 for non-indexed draws. 2303 */ 2304 LLVMValueRef base_vertex = lp_build_select(&bld, have_elts, vertex_id_offset, lp_build_const_int32(gallivm, 0)); 2305 system_values.basevertex = lp_build_broadcast_scalar(&blduivec, base_vertex); 2306 /* first vertex is for Vulkan base vertex support */ 2307 LLVMValueRef first_vertex = lp_build_select(&bld, have_elts, vertex_id_offset, start_or_maxelt); 2308 system_values.firstvertex = lp_build_broadcast_scalar(&blduivec, first_vertex); 2309 system_values.vertex_id = true_index_array; 2310 system_values.vertex_id_nobase = LLVMBuildSub(builder, true_index_array, 2311 lp_build_broadcast_scalar(&blduivec, vertex_id_offset), ""); 2312 2313 ptr_aos = (const LLVMValueRef (*)[TGSI_NUM_CHANNELS]) inputs; 2314 generate_vs(variant, 2315 builder, 2316 vs_type, 2317 outputs, 2318 ptr_aos, 2319 &system_values, 2320 context_ptr, 2321 sampler, 2322 image, 2323 key->clamp_vertex_color, 2324 &mask); 2325 2326 lp_build_mask_end(&mask); 2327 if (pos != -1 && cv != -1) { 2328 /* store original positions in clip before further manipulation */ 2329 store_clip(gallivm, vs_type, io, outputs, pos); 2330 2331 /* do cliptest */ 2332 if (enable_cliptest) { 2333 LLVMValueRef temp = LLVMBuildLoad(builder, clipmask_bool_ptr, ""); 2334 /* allocate clipmask, assign it integer type */ 2335 clipmask = generate_clipmask(llvm, 2336 gallivm, 2337 vs_type, 2338 outputs, 2339 key, 2340 context_ptr, &have_clipdist); 2341 temp = LLVMBuildOr(builder, clipmask, temp, ""); 2342 /* store temporary clipping boolean value */ 2343 LLVMBuildStore(builder, temp, clipmask_bool_ptr); 2344 } 2345 else { 2346 clipmask = blduivec.zero; 2347 } 2348 2349 /* do viewport mapping */ 2350 if (!bypass_viewport) { 2351 generate_viewport(variant, builder, vs_type, outputs, context_ptr); 2352 } 2353 } 2354 else { 2355 clipmask = blduivec.zero; 2356 } 2357 2358 /* store clipmask in vertex header, 2359 * original positions in clip 2360 * and transformed positions in data 2361 */ 2362 convert_to_aos(gallivm, io, NULL, outputs, clipmask, 2363 vs_info->num_outputs, vs_type, 2364 enable_cliptest && key->need_edgeflags); 2365 } 2366 lp_build_loop_end_cond(&lp_loop, count, step, LLVMIntUGE); 2367 2368 sampler->destroy(sampler); 2369 image->destroy(image); 2370 2371 /* return clipping boolean value for function */ 2372 ret = clipmask_booli8(gallivm, vs_type, clipmask_bool_ptr, 2373 enable_cliptest && key->need_edgeflags); 2374 2375 LLVMBuildRet(builder, ret); 2376 2377 gallivm_verify_function(gallivm, variant_func); 2378} 2379 2380 2381struct draw_llvm_variant_key * 2382draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store) 2383{ 2384 unsigned i; 2385 struct draw_llvm_variant_key *key; 2386 struct draw_sampler_static_state *draw_sampler; 2387 struct draw_image_static_state *draw_image; 2388 2389 key = (struct draw_llvm_variant_key *)store; 2390 2391 memset(key, 0, offsetof(struct draw_llvm_variant_key, vertex_element[0])); 2392 2393 2394 /* will have to rig this up properly later */ 2395 key->clip_xy = llvm->draw->clip_xy; 2396 key->clip_z = llvm->draw->clip_z; 2397 key->clip_user = llvm->draw->clip_user; 2398 key->bypass_viewport = llvm->draw->bypass_viewport; 2399 key->clip_halfz = llvm->draw->rasterizer->clip_halfz; 2400 /* XXX assumes edgeflag output not at 0 */ 2401 key->need_edgeflags = (llvm->draw->vs.edgeflag_output ? TRUE : FALSE); 2402 key->ucp_enable = llvm->draw->rasterizer->clip_plane_enable; 2403 key->has_gs_or_tes = llvm->draw->gs.geometry_shader != NULL || llvm->draw->tes.tess_eval_shader != NULL; 2404 key->num_outputs = draw_total_vs_outputs(llvm->draw); 2405 2406 key->clamp_vertex_color = !key->has_gs_or_tes && 2407 llvm->draw->rasterizer->clamp_vertex_color; 2408 2409 /* All variants of this shader will have the same value for 2410 * nr_samplers. Not yet trying to compact away holes in the 2411 * sampler array. 2412 */ 2413 key->nr_samplers = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER] + 1; 2414 if (llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) { 2415 key->nr_sampler_views = 2416 llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1; 2417 } 2418 else { 2419 key->nr_sampler_views = key->nr_samplers; 2420 } 2421 2422 key->nr_images = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_IMAGE] + 1; 2423 2424 /* Presumably all variants of the shader should have the same 2425 * number of vertex elements - ie the number of shader inputs. 2426 * NOTE: we NEED to store the needed number of needed inputs 2427 * here, not the number of provided elements to match keysize 2428 * (and the offset of sampler state in the key). 2429 * If we have excess number of vertex elements, this is valid, 2430 * but the excess ones don't matter. 2431 * If we don't have enough vertex elements (which looks not really 2432 * valid but we'll handle it gracefully) fill out missing ones with 2433 * zero (we'll recognize these later by PIPE_FORMAT_NONE). 2434 */ 2435 key->nr_vertex_elements = 2436 llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_INPUT] + 1; 2437 2438 if (llvm->draw->pt.nr_vertex_elements < key->nr_vertex_elements) { 2439 debug_printf("draw: vs with %d inputs but only have %d vertex elements\n", 2440 key->nr_vertex_elements, llvm->draw->pt.nr_vertex_elements); 2441 memset(key->vertex_element, 0, 2442 sizeof(struct pipe_vertex_element) * key->nr_vertex_elements); 2443 } 2444 memcpy(key->vertex_element, 2445 llvm->draw->pt.vertex_element, 2446 sizeof(struct pipe_vertex_element) * 2447 MIN2(key->nr_vertex_elements, llvm->draw->pt.nr_vertex_elements)); 2448 2449 draw_sampler = draw_llvm_variant_key_samplers(key); 2450 memset(draw_sampler, 0, 2451 MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler); 2452 2453 for (i = 0 ; i < key->nr_samplers; i++) { 2454 lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state, 2455 llvm->draw->samplers[PIPE_SHADER_VERTEX][i]); 2456 } 2457 for (i = 0 ; i < key->nr_sampler_views; i++) { 2458 lp_sampler_static_texture_state(&draw_sampler[i].texture_state, 2459 llvm->draw->sampler_views[PIPE_SHADER_VERTEX][i]); 2460 } 2461 2462 draw_image = draw_llvm_variant_key_images(key); 2463 memset(draw_image, 0, 2464 key->nr_images * sizeof *draw_image); 2465 for (i = 0; i < key->nr_images; i++) { 2466 lp_sampler_static_texture_state_image(&draw_image[i].image_state, 2467 llvm->draw->images[PIPE_SHADER_VERTEX][i]); 2468 } 2469 return key; 2470} 2471 2472 2473void 2474draw_llvm_dump_variant_key(struct draw_llvm_variant_key *key) 2475{ 2476 unsigned i; 2477 struct draw_sampler_static_state *sampler = draw_llvm_variant_key_samplers(key); 2478 struct draw_image_static_state *image = draw_llvm_variant_key_images(key); 2479 debug_printf("clamp_vertex_color = %u\n", key->clamp_vertex_color); 2480 debug_printf("clip_xy = %u\n", key->clip_xy); 2481 debug_printf("clip_z = %u\n", key->clip_z); 2482 debug_printf("clip_user = %u\n", key->clip_user); 2483 debug_printf("bypass_viewport = %u\n", key->bypass_viewport); 2484 debug_printf("clip_halfz = %u\n", key->clip_halfz); 2485 debug_printf("need_edgeflags = %u\n", key->need_edgeflags); 2486 debug_printf("has_gs_or_tes = %u\n", key->has_gs_or_tes); 2487 debug_printf("ucp_enable = %u\n", key->ucp_enable); 2488 2489 for (i = 0 ; i < key->nr_vertex_elements; i++) { 2490 debug_printf("vertex_element[%i].src_offset = %u\n", i, key->vertex_element[i].src_offset); 2491 debug_printf("vertex_element[%i].instance_divisor = %u\n", i, key->vertex_element[i].instance_divisor); 2492 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i, key->vertex_element[i].vertex_buffer_index); 2493 debug_printf("vertex_element[%i].src_format = %s\n", i, util_format_name(key->vertex_element[i].src_format)); 2494 } 2495 2496 for (i = 0 ; i < key->nr_sampler_views; i++) { 2497 debug_printf("sampler[%i].src_format = %s\n", i, util_format_name(sampler[i].texture_state.format)); 2498 } 2499 2500 for (i = 0 ; i < key->nr_images; i++) 2501 debug_printf("images[%i].format = %s\n", i, util_format_name(image[i].image_state.format)); 2502} 2503 2504 2505void 2506draw_llvm_set_mapped_texture(struct draw_context *draw, 2507 enum pipe_shader_type shader_stage, 2508 unsigned sview_idx, 2509 uint32_t width, uint32_t height, uint32_t depth, 2510 uint32_t first_level, uint32_t last_level, 2511 uint32_t num_samples, 2512 uint32_t sample_stride, 2513 const void *base_ptr, 2514 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS], 2515 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS], 2516 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS]) 2517{ 2518 unsigned j; 2519 struct draw_jit_texture *jit_tex; 2520 2521 switch (shader_stage) { 2522 case PIPE_SHADER_VERTEX: 2523 assert(sview_idx < ARRAY_SIZE(draw->llvm->jit_context.textures)); 2524 jit_tex = &draw->llvm->jit_context.textures[sview_idx]; 2525 break; 2526 case PIPE_SHADER_GEOMETRY: 2527 assert(sview_idx < ARRAY_SIZE(draw->llvm->gs_jit_context.textures)); 2528 jit_tex = &draw->llvm->gs_jit_context.textures[sview_idx]; 2529 break; 2530 case PIPE_SHADER_TESS_CTRL: 2531 assert(sview_idx < ARRAY_SIZE(draw->llvm->tcs_jit_context.textures)); 2532 jit_tex = &draw->llvm->tcs_jit_context.textures[sview_idx]; 2533 break; 2534 case PIPE_SHADER_TESS_EVAL: 2535 assert(sview_idx < ARRAY_SIZE(draw->llvm->tes_jit_context.textures)); 2536 jit_tex = &draw->llvm->tes_jit_context.textures[sview_idx]; 2537 break; 2538 default: 2539 assert(0); 2540 return; 2541 } 2542 2543 jit_tex->width = width; 2544 jit_tex->height = height; 2545 jit_tex->depth = depth; 2546 jit_tex->first_level = first_level; 2547 jit_tex->last_level = last_level; 2548 jit_tex->base = base_ptr; 2549 jit_tex->num_samples = num_samples; 2550 jit_tex->sample_stride = sample_stride; 2551 2552 for (j = first_level; j <= last_level; j++) { 2553 jit_tex->mip_offsets[j] = mip_offsets[j]; 2554 jit_tex->row_stride[j] = row_stride[j]; 2555 jit_tex->img_stride[j] = img_stride[j]; 2556 } 2557} 2558 2559void 2560draw_llvm_set_mapped_image(struct draw_context *draw, 2561 enum pipe_shader_type shader_stage, 2562 unsigned idx, 2563 uint32_t width, uint32_t height, uint32_t depth, 2564 const void *base_ptr, 2565 uint32_t row_stride, 2566 uint32_t img_stride, 2567 uint32_t num_samples, 2568 uint32_t sample_stride) 2569{ 2570 struct draw_jit_image *jit_image; 2571 2572 switch (shader_stage) { 2573 case PIPE_SHADER_VERTEX: 2574 assert(idx < ARRAY_SIZE(draw->llvm->jit_context.images)); 2575 jit_image = &draw->llvm->jit_context.images[idx]; 2576 break; 2577 case PIPE_SHADER_GEOMETRY: 2578 assert(idx < ARRAY_SIZE(draw->llvm->gs_jit_context.images)); 2579 jit_image = &draw->llvm->gs_jit_context.images[idx]; 2580 break; 2581 case PIPE_SHADER_TESS_CTRL: 2582 assert(idx < ARRAY_SIZE(draw->llvm->tcs_jit_context.images)); 2583 jit_image = &draw->llvm->tcs_jit_context.images[idx]; 2584 break; 2585 case PIPE_SHADER_TESS_EVAL: 2586 assert(idx < ARRAY_SIZE(draw->llvm->tes_jit_context.images)); 2587 jit_image = &draw->llvm->tes_jit_context.images[idx]; 2588 break; 2589 default: 2590 assert(0); 2591 return; 2592 } 2593 2594 jit_image->width = width; 2595 jit_image->height = height; 2596 jit_image->depth = depth; 2597 jit_image->base = base_ptr; 2598 2599 jit_image->row_stride = row_stride; 2600 jit_image->img_stride = img_stride; 2601 jit_image->num_samples = num_samples; 2602 jit_image->sample_stride = sample_stride; 2603} 2604 2605 2606void 2607draw_llvm_set_sampler_state(struct draw_context *draw, 2608 enum pipe_shader_type shader_type) 2609{ 2610 unsigned i; 2611 2612 switch (shader_type) { 2613 case PIPE_SHADER_VERTEX: 2614 for (i = 0; i < draw->num_samplers[PIPE_SHADER_VERTEX]; i++) { 2615 struct draw_jit_sampler *jit_sam = &draw->llvm->jit_context.samplers[i]; 2616 2617 if (draw->samplers[PIPE_SHADER_VERTEX][i]) { 2618 const struct pipe_sampler_state *s 2619 = draw->samplers[PIPE_SHADER_VERTEX][i]; 2620 jit_sam->min_lod = s->min_lod; 2621 jit_sam->max_lod = s->max_lod; 2622 jit_sam->lod_bias = s->lod_bias; 2623 jit_sam->max_aniso = s->max_anisotropy; 2624 COPY_4V(jit_sam->border_color, s->border_color.f); 2625 } 2626 } 2627 break; 2628 case PIPE_SHADER_GEOMETRY: 2629 for (i = 0; i < draw->num_samplers[PIPE_SHADER_GEOMETRY]; i++) { 2630 struct draw_jit_sampler *jit_sam = &draw->llvm->gs_jit_context.samplers[i]; 2631 2632 if (draw->samplers[PIPE_SHADER_GEOMETRY][i]) { 2633 const struct pipe_sampler_state *s 2634 = draw->samplers[PIPE_SHADER_GEOMETRY][i]; 2635 jit_sam->min_lod = s->min_lod; 2636 jit_sam->max_lod = s->max_lod; 2637 jit_sam->lod_bias = s->lod_bias; 2638 jit_sam->max_aniso = s->max_anisotropy; 2639 COPY_4V(jit_sam->border_color, s->border_color.f); 2640 } 2641 } 2642 break; 2643 case PIPE_SHADER_TESS_CTRL: 2644 for (i = 0; i < draw->num_samplers[PIPE_SHADER_TESS_CTRL]; i++) { 2645 struct draw_jit_sampler *jit_sam = &draw->llvm->tcs_jit_context.samplers[i]; 2646 2647 if (draw->samplers[PIPE_SHADER_TESS_CTRL][i]) { 2648 const struct pipe_sampler_state *s 2649 = draw->samplers[PIPE_SHADER_TESS_CTRL][i]; 2650 jit_sam->min_lod = s->min_lod; 2651 jit_sam->max_lod = s->max_lod; 2652 jit_sam->lod_bias = s->lod_bias; 2653 jit_sam->max_aniso = s->max_anisotropy; 2654 COPY_4V(jit_sam->border_color, s->border_color.f); 2655 } 2656 } 2657 break; 2658 case PIPE_SHADER_TESS_EVAL: 2659 for (i = 0; i < draw->num_samplers[PIPE_SHADER_TESS_EVAL]; i++) { 2660 struct draw_jit_sampler *jit_sam = &draw->llvm->tes_jit_context.samplers[i]; 2661 2662 if (draw->samplers[PIPE_SHADER_TESS_EVAL][i]) { 2663 const struct pipe_sampler_state *s 2664 = draw->samplers[PIPE_SHADER_TESS_EVAL][i]; 2665 jit_sam->min_lod = s->min_lod; 2666 jit_sam->max_lod = s->max_lod; 2667 jit_sam->lod_bias = s->lod_bias; 2668 jit_sam->max_aniso = s->max_anisotropy; 2669 COPY_4V(jit_sam->border_color, s->border_color.f); 2670 } 2671 } 2672 break; 2673 default: 2674 assert(0); 2675 break; 2676 } 2677} 2678 2679 2680void 2681draw_llvm_destroy_variant(struct draw_llvm_variant *variant) 2682{ 2683 struct draw_llvm *llvm = variant->llvm; 2684 2685 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) { 2686 debug_printf("Deleting VS variant: %u vs variants,\t%u total variants\n", 2687 variant->shader->variants_cached, llvm->nr_variants); 2688 } 2689 2690 gallivm_destroy(variant->gallivm); 2691 2692 remove_from_list(&variant->list_item_local); 2693 variant->shader->variants_cached--; 2694 remove_from_list(&variant->list_item_global); 2695 llvm->nr_variants--; 2696 FREE(variant); 2697} 2698 2699 2700/** 2701 * Create LLVM types for various structures. 2702 */ 2703static void 2704create_gs_jit_types(struct draw_gs_llvm_variant *var) 2705{ 2706 struct gallivm_state *gallivm = var->gallivm; 2707 LLVMTypeRef texture_type, sampler_type, image_type, context_type; 2708 2709 texture_type = create_jit_texture_type(gallivm, "texture"); 2710 sampler_type = create_jit_sampler_type(gallivm, "sampler"); 2711 image_type = create_jit_image_type(gallivm, "image"); 2712 2713 context_type = create_gs_jit_context_type(gallivm, 2714 var->shader->base.vector_length, 2715 texture_type, sampler_type, 2716 image_type, 2717 "draw_gs_jit_context"); 2718 var->context_ptr_type = LLVMPointerType(context_type, 0); 2719 2720 var->input_array_type = create_gs_jit_input_type(gallivm); 2721} 2722 2723static LLVMTypeRef 2724get_gs_context_ptr_type(struct draw_gs_llvm_variant *variant) 2725{ 2726 if (!variant->context_ptr_type) 2727 create_gs_jit_types(variant); 2728 return variant->context_ptr_type; 2729} 2730 2731static LLVMValueRef 2732generate_mask_value(struct draw_gs_llvm_variant *variant, 2733 struct lp_type gs_type) 2734{ 2735 struct gallivm_state *gallivm = variant->gallivm; 2736 LLVMBuilderRef builder = gallivm->builder; 2737 struct lp_type mask_type = lp_int_type(gs_type); 2738 LLVMValueRef num_prims; 2739 LLVMValueRef mask_val = lp_build_const_vec(gallivm, mask_type, 0); 2740 unsigned i; 2741 2742 num_prims = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, mask_type), 2743 variant->num_prims); 2744 for (i = 0; i < gs_type.length; i++) { 2745 LLVMValueRef idx = lp_build_const_int32(gallivm, i); 2746 mask_val = LLVMBuildInsertElement(builder, mask_val, idx, idx, ""); 2747 } 2748 mask_val = lp_build_compare(gallivm, mask_type, 2749 PIPE_FUNC_GREATER, num_prims, mask_val); 2750 2751 return mask_val; 2752} 2753 2754static void 2755draw_gs_llvm_generate(struct draw_llvm *llvm, 2756 struct draw_gs_llvm_variant *variant) 2757{ 2758 struct gallivm_state *gallivm = variant->gallivm; 2759 LLVMContextRef context = gallivm->context; 2760 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context); 2761 LLVMTypeRef arg_types[8]; 2762 LLVMTypeRef func_type; 2763 LLVMValueRef variant_func; 2764 LLVMValueRef context_ptr; 2765 LLVMValueRef prim_id_ptr; 2766 LLVMBasicBlockRef block; 2767 LLVMBuilderRef builder; 2768 LLVMValueRef io_ptr, input_array, num_prims, mask_val; 2769 struct lp_build_sampler_soa *sampler = 0; 2770 struct lp_build_image_soa *image = NULL; 2771 struct lp_build_context bld; 2772 struct lp_bld_tgsi_system_values system_values; 2773 char func_name[64]; 2774 struct lp_type gs_type; 2775 unsigned i; 2776 struct draw_gs_llvm_iface gs_iface; 2777 const struct tgsi_token *tokens = variant->shader->base.state.tokens; 2778 LLVMValueRef consts_ptr, num_consts_ptr; 2779 LLVMValueRef ssbos_ptr, num_ssbos_ptr; 2780 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS]; 2781 struct lp_build_mask_context mask; 2782 const struct tgsi_shader_info *gs_info = &variant->shader->base.info; 2783 unsigned vector_length = variant->shader->base.vector_length; 2784 2785 memset(&system_values, 0, sizeof(system_values)); 2786 memset(&outputs, 0, sizeof(outputs)); 2787 2788 snprintf(func_name, sizeof(func_name), "draw_llvm_gs_variant"); 2789 2790 assert(variant->vertex_header_ptr_type); 2791 2792 arg_types[0] = get_gs_context_ptr_type(variant); /* context */ 2793 arg_types[1] = variant->input_array_type; /* input */ 2794 arg_types[2] = LLVMPointerType(variant->vertex_header_ptr_type, 0); /* vertex_header */ 2795 arg_types[3] = int32_type; /* num_prims */ 2796 arg_types[4] = int32_type; /* instance_id */ 2797 arg_types[5] = LLVMPointerType( 2798 LLVMVectorType(int32_type, vector_length), 0); /* prim_id_ptr */ 2799 arg_types[6] = int32_type; 2800 arg_types[7] = int32_type; 2801 2802 func_type = LLVMFunctionType(int32_type, arg_types, ARRAY_SIZE(arg_types), 0); 2803 2804 variant_func = LLVMAddFunction(gallivm->module, func_name, func_type); 2805 2806 variant->function = variant_func; 2807 2808 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv); 2809 2810 for (i = 0; i < ARRAY_SIZE(arg_types); ++i) 2811 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) 2812 lp_add_function_attr(variant_func, i + 1, LP_FUNC_ATTR_NOALIAS); 2813 2814 if (gallivm->cache && gallivm->cache->data_size) 2815 return; 2816 context_ptr = LLVMGetParam(variant_func, 0); 2817 input_array = LLVMGetParam(variant_func, 1); 2818 io_ptr = LLVMGetParam(variant_func, 2); 2819 num_prims = LLVMGetParam(variant_func, 3); 2820 system_values.instance_id = LLVMGetParam(variant_func, 4); 2821 prim_id_ptr = LLVMGetParam(variant_func, 5); 2822 system_values.invocation_id = LLVMGetParam(variant_func, 6); 2823 system_values.view_index = LLVMGetParam(variant_func, 7); 2824 2825 lp_build_name(context_ptr, "context"); 2826 lp_build_name(input_array, "input"); 2827 lp_build_name(io_ptr, "io"); 2828 lp_build_name(num_prims, "num_prims"); 2829 lp_build_name(system_values.instance_id, "instance_id"); 2830 lp_build_name(prim_id_ptr, "prim_id_ptr"); 2831 lp_build_name(system_values.invocation_id, "invocation_id"); 2832 lp_build_name(system_values.view_index, "view_index"); 2833 2834 variant->context_ptr = context_ptr; 2835 variant->io_ptr = io_ptr; 2836 variant->num_prims = num_prims; 2837 2838 gs_iface.base.fetch_input = draw_gs_llvm_fetch_input; 2839 gs_iface.base.emit_vertex = draw_gs_llvm_emit_vertex; 2840 gs_iface.base.end_primitive = draw_gs_llvm_end_primitive; 2841 gs_iface.base.gs_epilogue = draw_gs_llvm_epilogue; 2842 gs_iface.input = input_array; 2843 gs_iface.variant = variant; 2844 2845 /* 2846 * Function body 2847 */ 2848 2849 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry"); 2850 builder = gallivm->builder; 2851 LLVMPositionBuilderAtEnd(builder, block); 2852 2853 lp_build_context_init(&bld, gallivm, lp_type_int(32)); 2854 2855 memset(&gs_type, 0, sizeof gs_type); 2856 gs_type.floating = TRUE; /* floating point values */ 2857 gs_type.sign = TRUE; /* values are signed */ 2858 gs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */ 2859 gs_type.width = 32; /* 32-bit float */ 2860 gs_type.length = vector_length; 2861 2862 consts_ptr = draw_gs_jit_context_constants(variant->gallivm, context_ptr); 2863 num_consts_ptr = 2864 draw_gs_jit_context_num_constants(variant->gallivm, context_ptr); 2865 2866 ssbos_ptr = draw_gs_jit_context_ssbos(variant->gallivm, context_ptr); 2867 num_ssbos_ptr = 2868 draw_gs_jit_context_num_ssbos(variant->gallivm, context_ptr); 2869 2870 /* code generated texture sampling */ 2871 sampler = draw_llvm_sampler_soa_create(variant->key.samplers, variant->key.nr_samplers); 2872 image = draw_llvm_image_soa_create(draw_gs_llvm_variant_key_images(&variant->key), 2873 variant->key.nr_images); 2874 mask_val = generate_mask_value(variant, gs_type); 2875 lp_build_mask_begin(&mask, gallivm, gs_type, mask_val); 2876 2877 if (gs_info->uses_primid) { 2878 system_values.prim_id = LLVMBuildLoad(builder, prim_id_ptr, "prim_id"); 2879 } 2880 2881 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) { 2882 if (llvm->draw->gs.geometry_shader->state.type == PIPE_SHADER_IR_TGSI) 2883 tgsi_dump(tokens, 0); 2884 else 2885 nir_print_shader(llvm->draw->gs.geometry_shader->state.ir.nir, stderr); 2886 draw_gs_llvm_dump_variant_key(&variant->key); 2887 } 2888 2889 struct lp_build_tgsi_params params; 2890 memset(¶ms, 0, sizeof(params)); 2891 2892 params.type = gs_type; 2893 params.mask = &mask; 2894 params.consts_ptr = consts_ptr; 2895 params.const_sizes_ptr = num_consts_ptr; 2896 params.system_values = &system_values; 2897 params.context_ptr = context_ptr; 2898 params.sampler = sampler; 2899 params.info = &llvm->draw->gs.geometry_shader->info; 2900 params.gs_iface = (const struct lp_build_gs_iface *)&gs_iface; 2901 params.ssbo_ptr = ssbos_ptr; 2902 params.ssbo_sizes_ptr = num_ssbos_ptr; 2903 params.image = image; 2904 params.gs_vertex_streams = variant->shader->base.num_vertex_streams; 2905 params.aniso_filter_table = draw_gs_jit_context_aniso_filter_table(gallivm, context_ptr); 2906 2907 if (llvm->draw->gs.geometry_shader->state.type == PIPE_SHADER_IR_TGSI) 2908 lp_build_tgsi_soa(variant->gallivm, 2909 tokens, 2910 ¶ms, 2911 outputs); 2912 else 2913 lp_build_nir_soa(variant->gallivm, 2914 llvm->draw->gs.geometry_shader->state.ir.nir, 2915 ¶ms, 2916 outputs); 2917 2918 sampler->destroy(sampler); 2919 image->destroy(image); 2920 2921 lp_build_mask_end(&mask); 2922 2923 LLVMBuildRet(builder, lp_build_zero(gallivm, lp_type_uint(32))); 2924 2925 gallivm_verify_function(gallivm, variant_func); 2926} 2927 2928struct draw_gs_llvm_variant * 2929draw_gs_llvm_create_variant(struct draw_llvm *llvm, 2930 unsigned num_outputs, 2931 const struct draw_gs_llvm_variant_key *key) 2932{ 2933 struct draw_gs_llvm_variant *variant; 2934 struct llvm_geometry_shader *shader = 2935 llvm_geometry_shader(llvm->draw->gs.geometry_shader); 2936 LLVMTypeRef vertex_header; 2937 char module_name[64]; 2938 unsigned char ir_sha1_cache_key[20]; 2939 struct lp_cached_code cached = { 0 }; 2940 bool needs_caching = false; 2941 2942 variant = MALLOC(sizeof *variant + 2943 shader->variant_key_size - 2944 sizeof variant->key); 2945 if (!variant) 2946 return NULL; 2947 2948 variant->llvm = llvm; 2949 variant->shader = shader; 2950 2951 snprintf(module_name, sizeof(module_name), "draw_llvm_gs_variant%u", 2952 variant->shader->variants_cached); 2953 2954 memcpy(&variant->key, key, shader->variant_key_size); 2955 2956 if (shader->base.state.ir.nir && llvm->draw->disk_cache_cookie) { 2957 draw_get_ir_cache_key(shader->base.state.ir.nir, 2958 key, 2959 shader->variant_key_size, 2960 num_outputs, 2961 ir_sha1_cache_key); 2962 2963 llvm->draw->disk_cache_find_shader(llvm->draw->disk_cache_cookie, 2964 &cached, 2965 ir_sha1_cache_key); 2966 if (!cached.data_size) 2967 needs_caching = true; 2968 } 2969 variant->gallivm = gallivm_create(module_name, llvm->context, &cached); 2970 2971 create_gs_jit_types(variant); 2972 2973 vertex_header = create_jit_vertex_header(variant->gallivm, num_outputs); 2974 2975 variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0); 2976 2977 draw_gs_llvm_generate(llvm, variant); 2978 2979 gallivm_compile_module(variant->gallivm); 2980 2981 variant->jit_func = (draw_gs_jit_func) 2982 gallivm_jit_function(variant->gallivm, variant->function); 2983 2984 if (needs_caching) 2985 llvm->draw->disk_cache_insert_shader(llvm->draw->disk_cache_cookie, 2986 &cached, 2987 ir_sha1_cache_key); 2988 gallivm_free_ir(variant->gallivm); 2989 2990 variant->list_item_global.base = variant; 2991 variant->list_item_local.base = variant; 2992 /*variant->no = */shader->variants_created++; 2993 variant->list_item_global.base = variant; 2994 2995 return variant; 2996} 2997 2998void 2999draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant *variant) 3000{ 3001 struct draw_llvm *llvm = variant->llvm; 3002 3003 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) { 3004 debug_printf("Deleting GS variant: %u gs variants,\t%u total variants\n", 3005 variant->shader->variants_cached, llvm->nr_gs_variants); 3006 } 3007 3008 gallivm_destroy(variant->gallivm); 3009 3010 remove_from_list(&variant->list_item_local); 3011 variant->shader->variants_cached--; 3012 remove_from_list(&variant->list_item_global); 3013 llvm->nr_gs_variants--; 3014 FREE(variant); 3015} 3016 3017struct draw_gs_llvm_variant_key * 3018draw_gs_llvm_make_variant_key(struct draw_llvm *llvm, char *store) 3019{ 3020 unsigned i; 3021 struct draw_gs_llvm_variant_key *key; 3022 struct draw_sampler_static_state *draw_sampler; 3023 struct draw_image_static_state *draw_image; 3024 3025 key = (struct draw_gs_llvm_variant_key *)store; 3026 3027 memset(key, 0, offsetof(struct draw_gs_llvm_variant_key, samplers[0])); 3028 3029 key->num_outputs = draw_total_gs_outputs(llvm->draw); 3030 3031 key->clamp_vertex_color = llvm->draw->rasterizer->clamp_vertex_color; 3032 3033 /* All variants of this shader will have the same value for 3034 * nr_samplers. Not yet trying to compact away holes in the 3035 * sampler array. 3036 */ 3037 key->nr_samplers = llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER] + 1; 3038 if (llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) { 3039 key->nr_sampler_views = 3040 llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1; 3041 } 3042 else { 3043 key->nr_sampler_views = key->nr_samplers; 3044 } 3045 3046 key->nr_images = llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_IMAGE] + 1; 3047 3048 draw_sampler = key->samplers; 3049 3050 memset(draw_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler); 3051 3052 for (i = 0 ; i < key->nr_samplers; i++) { 3053 lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state, 3054 llvm->draw->samplers[PIPE_SHADER_GEOMETRY][i]); 3055 } 3056 for (i = 0 ; i < key->nr_sampler_views; i++) { 3057 lp_sampler_static_texture_state(&draw_sampler[i].texture_state, 3058 llvm->draw->sampler_views[PIPE_SHADER_GEOMETRY][i]); 3059 } 3060 3061 draw_image = draw_gs_llvm_variant_key_images(key); 3062 memset(draw_image, 0, 3063 key->nr_images * sizeof *draw_image); 3064 for (i = 0; i < key->nr_images; i++) { 3065 lp_sampler_static_texture_state_image(&draw_image[i].image_state, 3066 llvm->draw->images[PIPE_SHADER_GEOMETRY][i]); 3067 } 3068 return key; 3069} 3070 3071void 3072draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key *key) 3073{ 3074 unsigned i; 3075 struct draw_sampler_static_state *sampler = key->samplers; 3076 struct draw_image_static_state *image = draw_gs_llvm_variant_key_images(key); 3077 3078 debug_printf("clamp_vertex_color = %u\n", key->clamp_vertex_color); 3079 for (i = 0 ; i < key->nr_sampler_views; i++) { 3080 debug_printf("sampler[%i].src_format = %s\n", i, 3081 util_format_name(sampler[i].texture_state.format)); 3082 } 3083 3084 for (i = 0 ; i < key->nr_images; i++) 3085 debug_printf("images[%i].format = %s\n", i, util_format_name(image[i].image_state.format)); 3086 3087} 3088 3089static void 3090create_tcs_jit_types(struct draw_tcs_llvm_variant *var) 3091{ 3092 struct gallivm_state *gallivm = var->gallivm; 3093 LLVMTypeRef texture_type, sampler_type, image_type, context_type; 3094 3095 texture_type = create_jit_texture_type(gallivm, "texture"); 3096 sampler_type = create_jit_sampler_type(gallivm, "sampler"); 3097 image_type = create_jit_image_type(gallivm, "image"); 3098 3099 context_type = create_tcs_jit_context_type(gallivm, 3100 0, 3101 texture_type, sampler_type, 3102 image_type, 3103 "draw_tcs_jit_context"); 3104 var->input_array_type = create_tcs_jit_input_type(gallivm); 3105 var->output_array_type = create_tcs_jit_output_type(gallivm); 3106 var->context_ptr_type = LLVMPointerType(context_type, 0); 3107} 3108 3109static LLVMTypeRef 3110get_tcs_context_ptr_type(struct draw_tcs_llvm_variant *variant) 3111{ 3112 if (!variant->context_ptr_type) 3113 create_tcs_jit_types(variant); 3114 return variant->context_ptr_type; 3115} 3116 3117static LLVMValueRef 3118draw_tcs_llvm_emit_fetch_input(const struct lp_build_tcs_iface *tes_iface, 3119 struct lp_build_context *bld, 3120 boolean is_vindex_indirect, 3121 LLVMValueRef vertex_index, 3122 boolean is_aindex_indirect, 3123 LLVMValueRef attrib_index, 3124 boolean is_sindex_indirect, 3125 LLVMValueRef swizzle_index) 3126{ 3127 const struct draw_tcs_llvm_iface *tcs = draw_tcs_llvm_iface(tes_iface); 3128 struct gallivm_state *gallivm = bld->gallivm; 3129 LLVMBuilderRef builder = gallivm->builder; 3130 LLVMValueRef indices[3]; 3131 LLVMValueRef res; 3132 struct lp_type type = bld->type; 3133 3134 if (is_vindex_indirect || is_aindex_indirect || is_sindex_indirect) { 3135 int i; 3136 3137 res = bld->zero; 3138 for (i = 0; i < type.length; ++i) { 3139 LLVMValueRef idx = lp_build_const_int32(gallivm, i); 3140 LLVMValueRef vert_chan_index = vertex_index; 3141 LLVMValueRef attr_chan_index = attrib_index; 3142 LLVMValueRef swiz_chan_index = swizzle_index; 3143 LLVMValueRef channel_vec; 3144 3145 if (is_vindex_indirect) { 3146 vert_chan_index = LLVMBuildExtractElement(builder, 3147 vertex_index, idx, ""); 3148 } 3149 if (is_aindex_indirect) { 3150 attr_chan_index = LLVMBuildExtractElement(builder, 3151 attrib_index, idx, ""); 3152 } 3153 if (is_sindex_indirect) { 3154 swiz_chan_index = LLVMBuildExtractElement(builder, 3155 swizzle_index, idx, ""); 3156 } 3157 3158 indices[0] = vert_chan_index; 3159 indices[1] = attr_chan_index; 3160 indices[2] = swiz_chan_index; 3161 3162 channel_vec = LLVMBuildGEP(builder, tcs->input, indices, 3, ""); 3163 channel_vec = LLVMBuildLoad(builder, channel_vec, ""); 3164 3165 res = LLVMBuildInsertElement(builder, res, channel_vec, idx, ""); 3166 } 3167 } else { 3168 indices[0] = vertex_index; 3169 indices[1] = attrib_index; 3170 indices[2] = swizzle_index; 3171 3172 res = LLVMBuildGEP(builder, tcs->input, indices, 3, ""); 3173 res = LLVMBuildLoad(builder, res, ""); 3174 res = lp_build_broadcast_scalar(bld, res); 3175 } 3176 return res; 3177} 3178 3179static LLVMValueRef 3180draw_tcs_llvm_emit_fetch_output(const struct lp_build_tcs_iface *tes_iface, 3181 struct lp_build_context *bld, 3182 boolean is_vindex_indirect, 3183 LLVMValueRef vertex_index, 3184 boolean is_aindex_indirect, 3185 LLVMValueRef attrib_index, 3186 boolean is_sindex_indirect, 3187 LLVMValueRef swizzle_index, 3188 uint32_t name) 3189{ 3190 const struct draw_tcs_llvm_iface *tcs = draw_tcs_llvm_iface(tes_iface); 3191 struct gallivm_state *gallivm = bld->gallivm; 3192 LLVMBuilderRef builder = gallivm->builder; 3193 LLVMValueRef indices[3]; 3194 LLVMValueRef res; 3195 struct lp_type type = bld->type; 3196 3197 if (is_vindex_indirect || is_aindex_indirect || is_sindex_indirect) { 3198 int i; 3199 3200 res = bld->zero; 3201 for (i = 0; i < type.length; ++i) { 3202 LLVMValueRef idx = lp_build_const_int32(gallivm, i); 3203 LLVMValueRef vert_chan_index = vertex_index; 3204 LLVMValueRef attr_chan_index = attrib_index; 3205 LLVMValueRef swiz_chan_index = swizzle_index; 3206 LLVMValueRef channel_vec; 3207 3208 if (is_vindex_indirect) { 3209 vert_chan_index = LLVMBuildExtractElement(builder, 3210 vertex_index, idx, ""); 3211 } 3212 if (is_aindex_indirect) { 3213 attr_chan_index = LLVMBuildExtractElement(builder, 3214 attrib_index, idx, ""); 3215 } 3216 if (is_sindex_indirect) { 3217 swiz_chan_index = LLVMBuildExtractElement(builder, 3218 swizzle_index, idx, ""); 3219 } 3220 3221 indices[0] = vert_chan_index; 3222 indices[1] = attr_chan_index; 3223 indices[2] = swiz_chan_index; 3224 3225 channel_vec = LLVMBuildGEP(builder, tcs->output, indices, 3, ""); 3226 channel_vec = LLVMBuildLoad(builder, channel_vec, ""); 3227 3228 res = LLVMBuildInsertElement(builder, res, channel_vec, idx, ""); 3229 } 3230 } else { 3231 indices[0] = vertex_index ? vertex_index : lp_build_const_int32(gallivm, 0); 3232 indices[1] = attrib_index; 3233 indices[2] = swizzle_index; 3234 3235 res = LLVMBuildGEP(builder, tcs->output, indices, 3, ""); 3236 res = LLVMBuildLoad(builder, res, ""); 3237 res = lp_build_broadcast_scalar(bld, res); 3238 } 3239 return res; 3240} 3241 3242static void 3243draw_tcs_llvm_emit_store_output(const struct lp_build_tcs_iface *tes_iface, 3244 struct lp_build_context *bld, 3245 unsigned name, 3246 boolean is_vindex_indirect, 3247 LLVMValueRef vertex_index, 3248 boolean is_aindex_indirect, 3249 LLVMValueRef attrib_index, 3250 boolean is_sindex_indirect, 3251 LLVMValueRef swizzle_index, 3252 LLVMValueRef value, 3253 LLVMValueRef mask_vec) 3254{ 3255 const struct draw_tcs_llvm_iface *tcs = draw_tcs_llvm_iface(tes_iface); 3256 struct gallivm_state *gallivm = bld->gallivm; 3257 LLVMBuilderRef builder = gallivm->builder; 3258 LLVMValueRef indices[3]; 3259 LLVMValueRef res; 3260 struct lp_type type = bld->type; 3261 3262 if (is_vindex_indirect || is_aindex_indirect || is_sindex_indirect) { 3263 int i; 3264 3265 for (i = 0; i < type.length; ++i) { 3266 LLVMValueRef idx = lp_build_const_int32(gallivm, i); 3267 LLVMValueRef vert_chan_index = vertex_index ? vertex_index : lp_build_const_int32(gallivm, 0); 3268 LLVMValueRef attr_chan_index = attrib_index; 3269 LLVMValueRef swiz_chan_index = swizzle_index; 3270 LLVMValueRef channel_vec; 3271 3272 if (is_vindex_indirect) { 3273 vert_chan_index = LLVMBuildExtractElement(builder, 3274 vertex_index, idx, ""); 3275 } 3276 if (is_aindex_indirect) { 3277 attr_chan_index = LLVMBuildExtractElement(builder, 3278 attrib_index, idx, ""); 3279 } 3280 3281 if (is_sindex_indirect) { 3282 swiz_chan_index = LLVMBuildExtractElement(builder, 3283 swizzle_index, idx, ""); 3284 } 3285 3286 indices[0] = vert_chan_index; 3287 indices[1] = attr_chan_index; 3288 indices[2] = swiz_chan_index; 3289 3290 channel_vec = LLVMBuildGEP(builder, tcs->output, indices, 3, ""); 3291 3292 res = LLVMBuildExtractElement(builder, value, idx, ""); 3293 3294 struct lp_build_if_state ifthen; 3295 LLVMValueRef cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, mask_vec, lp_build_const_int_vec(gallivm, bld->type, 0), ""); 3296 cond = LLVMBuildExtractElement(gallivm->builder, cond, idx, ""); 3297 lp_build_if(&ifthen, gallivm, cond); 3298 LLVMBuildStore(builder, res, channel_vec); 3299 lp_build_endif(&ifthen); 3300 } 3301 } else { 3302 indices[0] = vertex_index ? vertex_index : lp_build_const_int32(gallivm, 0); 3303 indices[1] = attrib_index; 3304 indices[2] = swizzle_index; 3305 3306 res = LLVMBuildGEP(builder, tcs->output, indices, 3, ""); 3307 for (unsigned i = 0; i < type.length; ++i) { 3308 LLVMValueRef idx = lp_build_const_int32(gallivm, i); 3309 LLVMValueRef val = LLVMBuildExtractElement(builder, value, idx, ""); 3310 3311 struct lp_build_if_state ifthen; 3312 LLVMValueRef cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, mask_vec, lp_build_const_int_vec(gallivm, bld->type, 0), ""); 3313 cond = LLVMBuildExtractElement(gallivm->builder, cond, idx, ""); 3314 lp_build_if(&ifthen, gallivm, cond); 3315 LLVMBuildStore(builder, val, res); 3316 lp_build_endif(&ifthen); 3317 } 3318 } 3319} 3320 3321 3322static LLVMValueRef 3323generate_tcs_mask_value(struct draw_tcs_llvm_variant *variant, 3324 struct lp_type tcs_type, LLVMValueRef limit, LLVMValueRef loop_counter) 3325{ 3326 struct gallivm_state *gallivm = variant->gallivm; 3327 LLVMBuilderRef builder = gallivm->builder; 3328 struct lp_type mask_type = lp_int_type(tcs_type); 3329 LLVMValueRef num_vecs; 3330 LLVMValueRef mask_val = lp_build_const_vec(gallivm, mask_type, 0); 3331 unsigned i; 3332 3333 num_vecs = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, mask_type), limit); 3334 for (i = 0; i < tcs_type.length; i++) { 3335 LLVMValueRef idx = lp_build_const_int32(gallivm, i); 3336 mask_val = LLVMBuildInsertElement(builder, mask_val, LLVMBuildAdd(builder, loop_counter, idx, ""), idx, ""); 3337 } 3338 mask_val = lp_build_compare(gallivm, mask_type, 3339 PIPE_FUNC_GREATER, num_vecs, mask_val); 3340 3341 return mask_val; 3342} 3343 3344static void 3345draw_tcs_llvm_generate(struct draw_llvm *llvm, 3346 struct draw_tcs_llvm_variant *variant) 3347{ 3348 struct gallivm_state *gallivm = variant->gallivm; 3349 LLVMContextRef context = gallivm->context; 3350 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context); 3351 LLVMTypeRef arg_types[7]; 3352 LLVMTypeRef func_type, coro_func_type; 3353 LLVMValueRef variant_func, variant_coro; 3354 LLVMValueRef context_ptr; 3355 LLVMValueRef view_index; 3356 LLVMValueRef input_array, output_array, prim_id, patch_vertices_in; 3357 LLVMValueRef mask_val; 3358 LLVMBasicBlockRef block; 3359 LLVMBuilderRef builder; 3360 struct lp_build_context bld, bldvec; 3361 struct lp_build_sampler_soa *sampler = 0; 3362 struct lp_build_image_soa *image = NULL; 3363 struct lp_bld_tgsi_system_values system_values; 3364 char func_name[64], func_name_coro[64]; 3365 unsigned i; 3366 struct draw_tcs_llvm_iface tcs_iface; 3367 struct lp_build_mask_context mask; 3368 LLVMValueRef consts_ptr, num_consts_ptr; 3369 LLVMValueRef ssbos_ptr, num_ssbos_ptr; 3370 struct lp_type tcs_type; 3371 unsigned vector_length = variant->shader->base.vector_length; 3372 3373 memset(&system_values, 0, sizeof(system_values)); 3374 3375 snprintf(func_name, sizeof(func_name), "draw_llvm_tcs_variant"); 3376 3377 snprintf(func_name_coro, sizeof(func_name_coro), "draw_llvm_tcs_coro_variant"); 3378 3379 arg_types[0] = get_tcs_context_ptr_type(variant); /* context */ 3380 arg_types[1] = variant->input_array_type; /* input */ 3381 arg_types[2] = variant->output_array_type; 3382 arg_types[3] = int32_type; 3383 arg_types[4] = int32_type; 3384 arg_types[5] = int32_type; 3385 arg_types[6] = int32_type; /* coroutine only */ 3386 3387 func_type = LLVMFunctionType(int32_type, arg_types, ARRAY_SIZE(arg_types) - 1, 0); 3388 3389 coro_func_type = LLVMFunctionType(LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0), arg_types, ARRAY_SIZE(arg_types), 0); 3390 3391 variant_func = LLVMAddFunction(gallivm->module, func_name, func_type); 3392 3393 variant_coro = LLVMAddFunction(gallivm->module, func_name_coro, coro_func_type); 3394 3395 variant->function = variant_func; 3396 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv); 3397 3398 LLVMSetFunctionCallConv(variant_coro, LLVMCCallConv); 3399 3400 for (i = 0; i < ARRAY_SIZE(arg_types); ++i) { 3401 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) { 3402 lp_add_function_attr(variant_coro, i + 1, LP_FUNC_ATTR_NOALIAS); 3403 lp_add_function_attr(variant_func, i + 1, LP_FUNC_ATTR_NOALIAS); 3404 } 3405 } 3406 3407 if (gallivm->cache && gallivm->cache->data_size) 3408 return; 3409 context_ptr = LLVMGetParam(variant_func, 0); 3410 input_array = LLVMGetParam(variant_func, 1); 3411 output_array = LLVMGetParam(variant_func, 2); 3412 prim_id = LLVMGetParam(variant_func, 3); 3413 patch_vertices_in = LLVMGetParam(variant_func, 4); 3414 view_index = LLVMGetParam(variant_func, 5); 3415 3416 lp_build_name(context_ptr, "context"); 3417 lp_build_name(input_array, "input"); 3418 lp_build_name(output_array, "output"); 3419 lp_build_name(prim_id, "prim_id"); 3420 lp_build_name(patch_vertices_in, "patch_vertices_in"); 3421 lp_build_name(view_index, "view_index"); 3422 3423 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry"); 3424 builder = gallivm->builder; 3425 LLVMPositionBuilderAtEnd(builder, block); 3426 3427 lp_build_context_init(&bld, gallivm, lp_type_int(32)); 3428 3429 memset(&tcs_type, 0, sizeof tcs_type); 3430 tcs_type.floating = TRUE; /* floating point values */ 3431 tcs_type.sign = TRUE; /* values are signed */ 3432 tcs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */ 3433 tcs_type.width = 32; /* 32-bit float */ 3434 tcs_type.length = vector_length; 3435 3436 lp_build_context_init(&bldvec, variant->gallivm, lp_int_type(tcs_type)); 3437 3438 LLVMValueRef count = lp_build_const_int32(gallivm, variant->shader->base.vertices_out); 3439 LLVMValueRef step = lp_build_const_int32(gallivm, vector_length); 3440 3441 struct lp_build_loop_state loop_state[2]; 3442 LLVMValueRef num_inner_loop; 3443 unsigned count_align = util_align_npot(variant->shader->base.vertices_out, tcs_type.length); 3444 num_inner_loop = lp_build_const_int32(gallivm, count_align / tcs_type.length); 3445 LLVMTypeRef hdl_ptr_type = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0); 3446 LLVMValueRef coro_hdls = LLVMBuildArrayAlloca(gallivm->builder, hdl_ptr_type, num_inner_loop, "coro_hdls"); 3447 unsigned end_coroutine = INT_MAX; 3448 lp_build_loop_begin(&loop_state[1], gallivm, 3449 lp_build_const_int32(gallivm, 0)); /* coroutine reentry loop */ 3450 lp_build_loop_begin(&loop_state[0], gallivm, 3451 lp_build_const_int32(gallivm, 0)); /* inner loop */ 3452 { 3453 LLVMValueRef args[7]; 3454 args[0] = context_ptr; 3455 args[1] = input_array; 3456 args[2] = output_array; 3457 args[3] = prim_id; 3458 args[4] = patch_vertices_in; 3459 args[5] = view_index; 3460 args[6] = loop_state[0].counter; 3461 LLVMValueRef coro_entry = LLVMBuildGEP(builder, coro_hdls, &loop_state[0].counter, 1, ""); 3462 LLVMValueRef coro_hdl = LLVMBuildLoad(builder, coro_entry, "coro_hdl"); 3463 3464 struct lp_build_if_state ifstate; 3465 LLVMValueRef cmp = LLVMBuildICmp(builder, LLVMIntEQ, loop_state[1].counter, 3466 lp_build_const_int32(gallivm, 0), ""); 3467 /* first time here - call the coroutine function entry point */ 3468 lp_build_if(&ifstate, gallivm, cmp); 3469 LLVMValueRef coro_ret = LLVMBuildCall(builder, variant_coro, args, 7, ""); 3470 LLVMBuildStore(builder, coro_ret, coro_entry); 3471 lp_build_else(&ifstate); 3472 /* subsequent calls for this invocation - check if done. */ 3473 LLVMValueRef coro_done = lp_build_coro_done(gallivm, coro_hdl); 3474 struct lp_build_if_state ifstate2; 3475 lp_build_if(&ifstate2, gallivm, coro_done); 3476 /* if done destroy and force loop exit */ 3477 lp_build_coro_destroy(gallivm, coro_hdl); 3478 lp_build_loop_force_set_counter(&loop_state[1], lp_build_const_int32(gallivm, end_coroutine - 1)); 3479 lp_build_else(&ifstate2); 3480 /* otherwise resume the coroutine */ 3481 lp_build_coro_resume(gallivm, coro_hdl); 3482 lp_build_endif(&ifstate2); 3483 lp_build_endif(&ifstate); 3484 lp_build_loop_force_reload_counter(&loop_state[1]); 3485 } 3486 lp_build_loop_end_cond(&loop_state[0], 3487 num_inner_loop, 3488 NULL, LLVMIntUGE); 3489 lp_build_loop_end_cond(&loop_state[1], 3490 lp_build_const_int32(gallivm, end_coroutine), 3491 NULL, LLVMIntEQ); 3492 LLVMBuildRet(builder, lp_build_zero(gallivm, lp_type_uint(32))); 3493 3494 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_coro, "entry"); 3495 LLVMPositionBuilderAtEnd(builder, block); 3496 3497 context_ptr = LLVMGetParam(variant_coro, 0); 3498 input_array = LLVMGetParam(variant_coro, 1); 3499 output_array = LLVMGetParam(variant_coro, 2); 3500 prim_id = LLVMGetParam(variant_coro, 3); 3501 patch_vertices_in = LLVMGetParam(variant_coro, 4); 3502 view_index = LLVMGetParam(variant_coro, 5); 3503 3504 consts_ptr = draw_tcs_jit_context_constants(variant->gallivm, context_ptr); 3505 num_consts_ptr = 3506 draw_tcs_jit_context_num_constants(variant->gallivm, context_ptr); 3507 3508 ssbos_ptr = draw_tcs_jit_context_ssbos(variant->gallivm, context_ptr); 3509 num_ssbos_ptr = 3510 draw_tcs_jit_context_num_ssbos(variant->gallivm, context_ptr); 3511 sampler = draw_llvm_sampler_soa_create(variant->key.samplers, variant->key.nr_samplers); 3512 image = draw_llvm_image_soa_create(draw_tcs_llvm_variant_key_images(&variant->key), 3513 variant->key.nr_images); 3514 3515 LLVMValueRef counter = LLVMGetParam(variant_coro, 6); 3516 LLVMValueRef invocvec = LLVMGetUndef(LLVMVectorType(int32_type, vector_length)); 3517 for (i = 0; i < vector_length; i++) { 3518 LLVMValueRef loop_iter = lp_build_const_int32(gallivm, i); 3519 LLVMValueRef idx = LLVMBuildAdd(builder, LLVMBuildMul(builder, counter, step, ""), loop_iter, ""); 3520 invocvec = LLVMBuildInsertElement(builder, invocvec, idx, loop_iter, ""); 3521 } 3522 3523 system_values.invocation_id = invocvec; 3524 system_values.prim_id = lp_build_broadcast_scalar(&bldvec, prim_id); 3525 system_values.view_index = view_index; 3526 system_values.vertices_in = lp_build_broadcast_scalar(&bldvec, patch_vertices_in); 3527 tcs_iface.input = input_array; 3528 tcs_iface.output = output_array; 3529 tcs_iface.base.emit_fetch_input = draw_tcs_llvm_emit_fetch_input; 3530 tcs_iface.base.emit_fetch_output = draw_tcs_llvm_emit_fetch_output; 3531 tcs_iface.base.emit_store_output = draw_tcs_llvm_emit_store_output; 3532 3533 3534 { 3535 LLVMValueRef coro_id = lp_build_coro_id(gallivm); 3536 LLVMValueRef coro_hdl = lp_build_coro_begin_alloc_mem(gallivm, coro_id); 3537 3538 mask_val = generate_tcs_mask_value(variant, tcs_type, count, LLVMBuildMul(builder, counter, step, "")); 3539 lp_build_mask_begin(&mask, gallivm, tcs_type, mask_val); 3540 3541 struct lp_build_coro_suspend_info coro_info; 3542 3543 LLVMBasicBlockRef sus_block = LLVMAppendBasicBlockInContext(gallivm->context, variant_coro, "suspend"); 3544 LLVMBasicBlockRef clean_block = LLVMAppendBasicBlockInContext(gallivm->context, variant_coro, "cleanup"); 3545 3546 coro_info.suspend = sus_block; 3547 coro_info.cleanup = clean_block; 3548 3549 struct lp_build_tgsi_params params; 3550 memset(¶ms, 0, sizeof(params)); 3551 3552 params.type = tcs_type; 3553 params.mask = &mask; 3554 params.consts_ptr = consts_ptr; 3555 params.const_sizes_ptr = num_consts_ptr; 3556 params.system_values = &system_values; 3557 params.context_ptr = context_ptr; 3558 params.sampler = sampler; 3559 params.info = &llvm->draw->tcs.tess_ctrl_shader->info; 3560 params.ssbo_ptr = ssbos_ptr; 3561 params.ssbo_sizes_ptr = num_ssbos_ptr; 3562 params.image = image; 3563 params.coro = &coro_info; 3564 params.tcs_iface = &tcs_iface.base; 3565 params.aniso_filter_table = draw_tcs_jit_context_aniso_filter_table(gallivm, context_ptr); 3566 3567 lp_build_nir_soa(variant->gallivm, 3568 llvm->draw->tcs.tess_ctrl_shader->state.ir.nir, 3569 ¶ms, NULL); 3570 3571 lp_build_mask_end(&mask); 3572 3573 lp_build_coro_suspend_switch(gallivm, &coro_info, NULL, true); 3574 LLVMPositionBuilderAtEnd(builder, clean_block); 3575 3576 lp_build_coro_free_mem(gallivm, coro_id, coro_hdl); 3577 3578 LLVMBuildBr(builder, sus_block); 3579 LLVMPositionBuilderAtEnd(builder, sus_block); 3580 3581 lp_build_coro_end(gallivm, coro_hdl); 3582 LLVMBuildRet(builder, coro_hdl); 3583 } 3584 3585 sampler->destroy(sampler); 3586 image->destroy(image); 3587 gallivm_verify_function(gallivm, variant_func); 3588 gallivm_verify_function(gallivm, variant_coro); 3589} 3590 3591struct draw_tcs_llvm_variant * 3592draw_tcs_llvm_create_variant(struct draw_llvm *llvm, 3593 unsigned num_outputs, 3594 const struct draw_tcs_llvm_variant_key *key) 3595{ 3596 struct draw_tcs_llvm_variant *variant; 3597 struct llvm_tess_ctrl_shader *shader = llvm_tess_ctrl_shader(llvm->draw->tcs.tess_ctrl_shader); 3598 char module_name[64]; 3599 unsigned char ir_sha1_cache_key[20]; 3600 struct lp_cached_code cached = { 0 }; 3601 bool needs_caching = false; 3602 3603 variant = MALLOC(sizeof *variant + 3604 shader->variant_key_size - sizeof variant->key); 3605 if (!variant) 3606 return NULL; 3607 3608 variant->llvm = llvm; 3609 variant->shader = shader; 3610 3611 snprintf(module_name, sizeof(module_name), "draw_llvm_tcs_variant%u", 3612 variant->shader->variants_cached); 3613 3614 memcpy(&variant->key, key, shader->variant_key_size); 3615 3616 if (shader->base.state.ir.nir && llvm->draw->disk_cache_cookie) { 3617 draw_get_ir_cache_key(shader->base.state.ir.nir, 3618 key, 3619 shader->variant_key_size, 3620 num_outputs, 3621 ir_sha1_cache_key); 3622 3623 llvm->draw->disk_cache_find_shader(llvm->draw->disk_cache_cookie, 3624 &cached, 3625 ir_sha1_cache_key); 3626 if (!cached.data_size) 3627 needs_caching = true; 3628 } 3629 3630 variant->gallivm = gallivm_create(module_name, llvm->context, &cached); 3631 3632 create_tcs_jit_types(variant); 3633 3634 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) { 3635 nir_print_shader(llvm->draw->tcs.tess_ctrl_shader->state.ir.nir, stderr); 3636 draw_tcs_llvm_dump_variant_key(&variant->key); 3637 } 3638 3639 lp_build_coro_declare_malloc_hooks(variant->gallivm); 3640 draw_tcs_llvm_generate(llvm, variant); 3641 3642 gallivm_compile_module(variant->gallivm); 3643 3644 lp_build_coro_add_malloc_hooks(variant->gallivm); 3645 variant->jit_func = (draw_tcs_jit_func) 3646 gallivm_jit_function(variant->gallivm, variant->function); 3647 3648 if (needs_caching) 3649 llvm->draw->disk_cache_insert_shader(llvm->draw->disk_cache_cookie, 3650 &cached, 3651 ir_sha1_cache_key); 3652 gallivm_free_ir(variant->gallivm); 3653 3654 variant->list_item_global.base = variant; 3655 variant->list_item_local.base = variant; 3656 /*variant->no = */shader->variants_created++; 3657 variant->list_item_global.base = variant; 3658 3659 return variant; 3660} 3661 3662void 3663draw_tcs_llvm_destroy_variant(struct draw_tcs_llvm_variant *variant) 3664{ 3665 struct draw_llvm *llvm = variant->llvm; 3666 3667 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) { 3668 debug_printf("Deleting TCS variant: %u tcs variants,\t%u total variants\n", 3669 variant->shader->variants_cached, llvm->nr_tcs_variants); 3670 } 3671 3672 gallivm_destroy(variant->gallivm); 3673 3674 remove_from_list(&variant->list_item_local); 3675 variant->shader->variants_cached--; 3676 remove_from_list(&variant->list_item_global); 3677 llvm->nr_tcs_variants--; 3678 FREE(variant); 3679} 3680 3681struct draw_tcs_llvm_variant_key * 3682draw_tcs_llvm_make_variant_key(struct draw_llvm *llvm, char *store) 3683{ 3684 unsigned i; 3685 struct draw_tcs_llvm_variant_key *key; 3686 struct draw_sampler_static_state *draw_sampler; 3687 struct draw_image_static_state *draw_image; 3688 3689 key = (struct draw_tcs_llvm_variant_key *)store; 3690 3691 memset(key, 0, offsetof(struct draw_tcs_llvm_variant_key, samplers[0])); 3692 3693 /* All variants of this shader will have the same value for 3694 * nr_samplers. Not yet trying to compact away holes in the 3695 * sampler array. 3696 */ 3697 key->nr_samplers = llvm->draw->tcs.tess_ctrl_shader->info.file_max[TGSI_FILE_SAMPLER] + 1; 3698 if (llvm->draw->tcs.tess_ctrl_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) { 3699 key->nr_sampler_views = 3700 llvm->draw->tcs.tess_ctrl_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1; 3701 } 3702 else { 3703 key->nr_sampler_views = key->nr_samplers; 3704 } 3705 3706 key->nr_images = llvm->draw->tcs.tess_ctrl_shader->info.file_max[TGSI_FILE_IMAGE] + 1; 3707 3708 draw_sampler = key->samplers; 3709 3710 memset(draw_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler); 3711 3712 for (i = 0 ; i < key->nr_samplers; i++) { 3713 lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state, 3714 llvm->draw->samplers[PIPE_SHADER_TESS_CTRL][i]); 3715 } 3716 for (i = 0 ; i < key->nr_sampler_views; i++) { 3717 lp_sampler_static_texture_state(&draw_sampler[i].texture_state, 3718 llvm->draw->sampler_views[PIPE_SHADER_TESS_CTRL][i]); 3719 } 3720 3721 draw_image = draw_tcs_llvm_variant_key_images(key); 3722 memset(draw_image, 0, 3723 key->nr_images * sizeof *draw_image); 3724 for (i = 0; i < key->nr_images; i++) { 3725 lp_sampler_static_texture_state_image(&draw_image[i].image_state, 3726 llvm->draw->images[PIPE_SHADER_TESS_CTRL][i]); 3727 } 3728 return key; 3729} 3730 3731void 3732draw_tcs_llvm_dump_variant_key(struct draw_tcs_llvm_variant_key *key) 3733{ 3734 unsigned i; 3735 struct draw_sampler_static_state *sampler = key->samplers; 3736 struct draw_image_static_state *image = draw_tcs_llvm_variant_key_images(key); 3737 for (i = 0 ; i < key->nr_sampler_views; i++) { 3738 debug_printf("sampler[%i].src_format = %s\n", i, 3739 util_format_name(sampler[i].texture_state.format)); 3740 } 3741 3742 for (i = 0 ; i < key->nr_images; i++) 3743 debug_printf("images[%i].format = %s\n", i, util_format_name(image[i].image_state.format)); 3744 3745} 3746 3747static void 3748create_tes_jit_types(struct draw_tes_llvm_variant *var) 3749{ 3750 struct gallivm_state *gallivm = var->gallivm; 3751 LLVMTypeRef texture_type, sampler_type, image_type, context_type; 3752 3753 texture_type = create_jit_texture_type(gallivm, "texture"); 3754 sampler_type = create_jit_sampler_type(gallivm, "sampler"); 3755 image_type = create_jit_image_type(gallivm, "image"); 3756 3757 context_type = create_tes_jit_context_type(gallivm, 3758 0, 3759 texture_type, sampler_type, 3760 image_type, 3761 "draw_tes_jit_context"); 3762 var->context_ptr_type = LLVMPointerType(context_type, 0); 3763 3764 var->input_array_type = create_tes_jit_input_type(gallivm); 3765} 3766 3767static LLVMTypeRef 3768get_tes_context_ptr_type(struct draw_tes_llvm_variant *variant) 3769{ 3770 if (!variant->context_ptr_type) 3771 create_tes_jit_types(variant); 3772 return variant->context_ptr_type; 3773} 3774 3775static LLVMValueRef 3776generate_tes_mask_value(struct draw_tes_llvm_variant *variant, 3777 struct lp_type tes_type, LLVMValueRef limit, LLVMValueRef loop_counter) 3778{ 3779 struct gallivm_state *gallivm = variant->gallivm; 3780 LLVMBuilderRef builder = gallivm->builder; 3781 struct lp_type mask_type = lp_int_type(tes_type); 3782 LLVMValueRef num_prims; 3783 LLVMValueRef mask_val = lp_build_const_vec(gallivm, mask_type, 0); 3784 unsigned i; 3785 3786 num_prims = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, mask_type), limit); 3787 for (i = 0; i < tes_type.length; i++) { 3788 LLVMValueRef idx = lp_build_const_int32(gallivm, i); 3789 mask_val = LLVMBuildInsertElement(builder, mask_val, LLVMBuildAdd(builder, loop_counter, idx, ""), idx, ""); 3790 } 3791 mask_val = lp_build_compare(gallivm, mask_type, 3792 PIPE_FUNC_GREATER, num_prims, mask_val); 3793 3794 return mask_val; 3795} 3796 3797static LLVMValueRef 3798draw_tes_llvm_fetch_vertex_input(const struct lp_build_tes_iface *tes_iface, 3799 struct lp_build_context *bld, 3800 boolean is_vindex_indirect, 3801 LLVMValueRef vertex_index, 3802 boolean is_aindex_indirect, 3803 LLVMValueRef attrib_index, 3804 boolean is_sindex_indirect, 3805 LLVMValueRef swizzle_index) 3806{ 3807 const struct draw_tes_llvm_iface *tes = draw_tes_llvm_iface(tes_iface); 3808 struct gallivm_state *gallivm = bld->gallivm; 3809 LLVMBuilderRef builder = gallivm->builder; 3810 LLVMValueRef indices[3]; 3811 LLVMValueRef res; 3812 struct lp_type type = bld->type; 3813 3814 if (is_vindex_indirect || is_aindex_indirect || is_sindex_indirect) { 3815 int i; 3816 3817 res = bld->zero; 3818 3819 for (i = 0; i < type.length; ++i) { 3820 LLVMValueRef idx = lp_build_const_int32(gallivm, i); 3821 LLVMValueRef vert_chan_index = vertex_index; 3822 LLVMValueRef attr_chan_index = attrib_index; 3823 LLVMValueRef swiz_chan_index = swizzle_index; 3824 LLVMValueRef channel_vec; 3825 3826 if (is_vindex_indirect) { 3827 vert_chan_index = LLVMBuildExtractElement(builder, 3828 vertex_index, idx, ""); 3829 } 3830 if (is_aindex_indirect) { 3831 attr_chan_index = LLVMBuildExtractElement(builder, 3832 attrib_index, idx, ""); 3833 } 3834 if (is_sindex_indirect) { 3835 swiz_chan_index = LLVMBuildExtractElement(builder, 3836 swizzle_index, idx, ""); 3837 } 3838 3839 indices[0] = vert_chan_index; 3840 indices[1] = attr_chan_index; 3841 indices[2] = swiz_chan_index; 3842 3843 channel_vec = LLVMBuildGEP(builder, tes->input, indices, 3, ""); 3844 channel_vec = LLVMBuildLoad(builder, channel_vec, ""); 3845 3846 res = LLVMBuildInsertElement(builder, res, channel_vec, idx, ""); 3847 } 3848 } else { 3849 indices[0] = vertex_index; 3850 indices[1] = attrib_index; 3851 indices[2] = swizzle_index; 3852 3853 res = LLVMBuildGEP(builder, tes->input, indices, 3, ""); 3854 res = LLVMBuildLoad(builder, res, ""); 3855 res = lp_build_broadcast_scalar(bld, res); 3856 } 3857 return res; 3858} 3859 3860static LLVMValueRef 3861draw_tes_llvm_fetch_patch_input(const struct lp_build_tes_iface *tes_iface, 3862 struct lp_build_context *bld, 3863 boolean is_aindex_indirect, 3864 LLVMValueRef attrib_index, 3865 LLVMValueRef swizzle_index) 3866{ 3867 const struct draw_tes_llvm_iface *tes = draw_tes_llvm_iface(tes_iface); 3868 struct gallivm_state *gallivm = bld->gallivm; 3869 LLVMBuilderRef builder = gallivm->builder; 3870 LLVMValueRef indices[3]; 3871 LLVMValueRef res; 3872 struct lp_type type = bld->type; 3873 3874 if (is_aindex_indirect) { 3875 int i; 3876 3877 res = bld->zero; 3878 3879 for (i = 0; i < type.length; ++i) { 3880 LLVMValueRef idx = lp_build_const_int32(gallivm, i); 3881 LLVMValueRef attr_chan_index = attrib_index; 3882 LLVMValueRef channel_vec; 3883 3884 if (is_aindex_indirect) { 3885 attr_chan_index = LLVMBuildExtractElement(builder, 3886 attrib_index, idx, ""); 3887 } 3888 3889 indices[0] = lp_build_const_int32(gallivm, 0); 3890 indices[1] = attr_chan_index; 3891 indices[2] = swizzle_index; 3892 3893 channel_vec = LLVMBuildGEP(builder, tes->input, indices, 3, ""); 3894 channel_vec = LLVMBuildLoad(builder, channel_vec, ""); 3895 3896 res = LLVMBuildInsertElement(builder, res, channel_vec, idx, ""); 3897 } 3898 } else { 3899 indices[0] = lp_build_const_int32(gallivm, 0); 3900 indices[1] = attrib_index; 3901 indices[2] = swizzle_index; 3902 3903 res = LLVMBuildGEP(builder, tes->input, indices, 3, ""); 3904 res = LLVMBuildLoad(builder, res, ""); 3905 res = lp_build_broadcast_scalar(bld, res); 3906 } 3907 return res; 3908} 3909 3910static void 3911draw_tes_llvm_generate(struct draw_llvm *llvm, 3912 struct draw_tes_llvm_variant *variant) 3913{ 3914 struct gallivm_state *gallivm = variant->gallivm; 3915 LLVMContextRef context = gallivm->context; 3916 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context); 3917 LLVMTypeRef flt_type = LLVMFloatTypeInContext(context); 3918 LLVMTypeRef arg_types[11]; 3919 LLVMTypeRef func_type; 3920 LLVMValueRef variant_func; 3921 LLVMValueRef context_ptr; 3922 LLVMValueRef tess_coord[2], io_ptr, input_array, num_tess_coord; 3923 LLVMValueRef view_index; 3924 LLVMValueRef tess_inner, tess_outer, prim_id, patch_vertices_in; 3925 LLVMBasicBlockRef block; 3926 LLVMBuilderRef builder; 3927 LLVMValueRef mask_val; 3928 struct lp_build_context bld, bldvec; 3929 struct lp_build_sampler_soa *sampler = 0; 3930 struct lp_build_image_soa *image = NULL; 3931 struct lp_bld_tgsi_system_values system_values; 3932 char func_name[64]; 3933 unsigned i; 3934 struct draw_tes_llvm_iface tes_iface; 3935 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS]; 3936 struct lp_build_mask_context mask; 3937 LLVMValueRef consts_ptr, num_consts_ptr; 3938 LLVMValueRef ssbos_ptr, num_ssbos_ptr; 3939 LLVMValueRef step; 3940 struct lp_type tes_type; 3941 unsigned vector_length = variant->shader->base.vector_length; 3942 3943 memset(&system_values, 0, sizeof(system_values)); 3944 memset(&outputs, 0, sizeof(outputs)); 3945 3946 snprintf(func_name, sizeof(func_name), "draw_llvm_tes_variant"); 3947 3948 arg_types[0] = get_tes_context_ptr_type(variant); /* context */ 3949 arg_types[1] = variant->input_array_type; /* input */ 3950 arg_types[2] = variant->vertex_header_ptr_type; 3951 arg_types[3] = int32_type; 3952 arg_types[4] = int32_type; 3953 arg_types[5] = LLVMPointerType(flt_type, 0); 3954 arg_types[6] = LLVMPointerType(flt_type, 0); 3955 arg_types[7] = LLVMPointerType(LLVMArrayType(flt_type, 4), 0); 3956 arg_types[8] = LLVMPointerType(LLVMArrayType(flt_type, 2), 0); 3957 arg_types[9] = int32_type; 3958 arg_types[10] = int32_type; 3959 3960 func_type = LLVMFunctionType(int32_type, arg_types, ARRAY_SIZE(arg_types), 0); 3961 variant_func = LLVMAddFunction(gallivm->module, func_name, func_type); 3962 3963 variant->function = variant_func; 3964 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv); 3965 3966 for (i = 0; i < ARRAY_SIZE(arg_types); ++i) 3967 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) 3968 lp_add_function_attr(variant_func, i + 1, LP_FUNC_ATTR_NOALIAS); 3969 3970 if (gallivm->cache && gallivm->cache->data_size) 3971 return; 3972 context_ptr = LLVMGetParam(variant_func, 0); 3973 input_array = LLVMGetParam(variant_func, 1); 3974 io_ptr = LLVMGetParam(variant_func, 2); 3975 prim_id = LLVMGetParam(variant_func, 3); 3976 num_tess_coord = LLVMGetParam(variant_func, 4); 3977 tess_coord[0] = LLVMGetParam(variant_func, 5); 3978 tess_coord[1] = LLVMGetParam(variant_func, 6); 3979 tess_outer = LLVMGetParam(variant_func, 7); 3980 tess_inner = LLVMGetParam(variant_func, 8); 3981 patch_vertices_in = LLVMGetParam(variant_func, 9); 3982 view_index = LLVMGetParam(variant_func, 10); 3983 3984 lp_build_name(context_ptr, "context"); 3985 lp_build_name(input_array, "input"); 3986 lp_build_name(io_ptr, "io"); 3987 lp_build_name(prim_id, "prim_id"); 3988 lp_build_name(num_tess_coord, "num_tess_coord"); 3989 lp_build_name(tess_coord[0], "tess_coord[0]"); 3990 lp_build_name(tess_coord[1], "tess_coord[1]"); 3991 lp_build_name(tess_outer, "tess_outer"); 3992 lp_build_name(tess_inner, "tess_inner"); 3993 lp_build_name(patch_vertices_in, "patch_vertices_in"); 3994 lp_build_name(view_index, "view_index"); 3995 3996 tes_iface.base.fetch_vertex_input = draw_tes_llvm_fetch_vertex_input; 3997 tes_iface.base.fetch_patch_input = draw_tes_llvm_fetch_patch_input; 3998 tes_iface.input = input_array; 3999 tes_iface.variant = variant; 4000 4001 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry"); 4002 builder = gallivm->builder; 4003 LLVMPositionBuilderAtEnd(builder, block); 4004 4005 lp_build_context_init(&bld, gallivm, lp_type_int(32)); 4006 4007 memset(&tes_type, 0, sizeof tes_type); 4008 tes_type.floating = TRUE; /* floating point values */ 4009 tes_type.sign = TRUE; /* values are signed */ 4010 tes_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */ 4011 tes_type.width = 32; /* 32-bit float */ 4012 tes_type.length = vector_length; 4013 4014 lp_build_context_init(&bldvec, variant->gallivm, lp_int_type(tes_type)); 4015 consts_ptr = draw_tes_jit_context_constants(variant->gallivm, context_ptr); 4016 num_consts_ptr = 4017 draw_tes_jit_context_num_constants(variant->gallivm, context_ptr); 4018 4019 ssbos_ptr = draw_tes_jit_context_ssbos(variant->gallivm, context_ptr); 4020 num_ssbos_ptr = 4021 draw_tes_jit_context_num_ssbos(variant->gallivm, context_ptr); 4022 sampler = draw_llvm_sampler_soa_create(variant->key.samplers, variant->key.nr_samplers); 4023 image = draw_llvm_image_soa_create(draw_tes_llvm_variant_key_images(&variant->key), 4024 variant->key.nr_images); 4025 step = lp_build_const_int32(gallivm, vector_length); 4026 4027 system_values.tess_outer = LLVMBuildLoad(builder, tess_outer, ""); 4028 system_values.tess_inner = LLVMBuildLoad(builder, tess_inner, ""); 4029 4030 system_values.prim_id = lp_build_broadcast_scalar(&bldvec, prim_id); 4031 4032 system_values.view_index = view_index; 4033 4034 system_values.vertices_in = lp_build_broadcast_scalar(&bldvec, patch_vertices_in); 4035 4036 if (variant->key.primid_needed) { 4037 int slot = variant->key.primid_output; 4038 for (unsigned i = 0; i < 4; i++) { 4039 outputs[slot][i] = lp_build_alloca(gallivm, lp_build_int_vec_type(gallivm, tes_type), "primid"); 4040 LLVMBuildStore(builder, system_values.prim_id, outputs[slot][i]); 4041 } 4042 } 4043 struct lp_build_loop_state lp_loop; 4044 lp_build_loop_begin(&lp_loop, gallivm, bld.zero); 4045 { 4046 LLVMValueRef io; 4047 4048 io = LLVMBuildGEP(builder, io_ptr, &lp_loop.counter, 1, ""); 4049 mask_val = generate_tes_mask_value(variant, tes_type, num_tess_coord, lp_loop.counter); 4050 lp_build_mask_begin(&mask, gallivm, tes_type, mask_val); 4051 4052 system_values.tess_coord = LLVMGetUndef(LLVMArrayType(LLVMVectorType(flt_type, vector_length), 3)); 4053 for (i = 0; i < 3; i++) { 4054 LLVMValueRef tess_coord_chan = LLVMGetUndef(LLVMVectorType(flt_type, vector_length)); 4055 for (unsigned j = 0; j < vector_length; j++) { 4056 LLVMValueRef idx = LLVMBuildAdd(builder, lp_loop.counter, lp_build_const_int32(gallivm, j), ""); 4057 LLVMValueRef tc_val; 4058 if (i == 2) { 4059 if (variant->shader->base.prim_mode == PIPE_PRIM_TRIANGLES) { 4060 tc_val = lp_build_const_float(gallivm, 1.0); 4061 tc_val = LLVMBuildFSub(builder, tc_val, lp_build_pointer_get(builder, tess_coord[0], idx), ""); 4062 tc_val = LLVMBuildFSub(builder, tc_val, lp_build_pointer_get(builder, tess_coord[1], idx), ""); 4063 } else 4064 tc_val = lp_build_const_float(gallivm, 0.0); 4065 } else 4066 tc_val = lp_build_pointer_get(builder, tess_coord[i], idx); 4067 4068 tess_coord_chan = LLVMBuildInsertElement(builder, tess_coord_chan, tc_val, lp_build_const_int32(gallivm, j), ""); 4069 } 4070 system_values.tess_coord = LLVMBuildInsertValue(builder, system_values.tess_coord, tess_coord_chan, i, ""); 4071 } 4072 4073 struct lp_build_tgsi_params params; 4074 memset(¶ms, 0, sizeof(params)); 4075 4076 params.type = tes_type; 4077 params.mask = &mask; 4078 params.consts_ptr = consts_ptr; 4079 params.const_sizes_ptr = num_consts_ptr; 4080 params.system_values = &system_values; 4081 params.context_ptr = context_ptr; 4082 params.sampler = sampler; 4083 params.info = &llvm->draw->tes.tess_eval_shader->info; 4084 params.ssbo_ptr = ssbos_ptr; 4085 params.ssbo_sizes_ptr = num_ssbos_ptr; 4086 params.image = image; 4087 params.tes_iface = &tes_iface.base; 4088 params.aniso_filter_table = draw_tes_jit_context_aniso_filter_table(variant->gallivm, context_ptr); 4089 4090 lp_build_nir_soa(variant->gallivm, 4091 llvm->draw->tes.tess_eval_shader->state.ir.nir, 4092 ¶ms, 4093 outputs); 4094 4095 lp_build_mask_end(&mask); 4096 4097 if (variant->key.clamp_vertex_color) { 4098 const struct tgsi_shader_info *info = &llvm->draw->tes.tess_eval_shader->info; 4099 do_clamp_vertex_color(variant->gallivm, 4100 tes_type, info, 4101 outputs); 4102 } 4103 LLVMValueRef clipmask = lp_build_const_int_vec(gallivm, 4104 lp_int_type(tes_type), 0); 4105 4106 convert_to_aos(gallivm, io, NULL, outputs, clipmask, 4107 draw_total_tes_outputs(llvm->draw), tes_type, FALSE); 4108 } 4109 lp_build_loop_end_cond(&lp_loop, num_tess_coord, step, LLVMIntUGE); 4110 sampler->destroy(sampler); 4111 image->destroy(image); 4112 4113 LLVMBuildRet(builder, lp_build_zero(gallivm, lp_type_uint(32))); 4114 gallivm_verify_function(gallivm, variant_func); 4115} 4116 4117struct draw_tes_llvm_variant * 4118draw_tes_llvm_create_variant(struct draw_llvm *llvm, 4119 unsigned num_outputs, 4120 const struct draw_tes_llvm_variant_key *key) 4121{ 4122 struct draw_tes_llvm_variant *variant; 4123 struct llvm_tess_eval_shader *shader = llvm_tess_eval_shader(llvm->draw->tes.tess_eval_shader); 4124 LLVMTypeRef vertex_header; 4125 char module_name[64]; 4126 unsigned char ir_sha1_cache_key[20]; 4127 struct lp_cached_code cached = { 0 }; 4128 bool needs_caching = false; 4129 4130 variant = MALLOC(sizeof *variant + 4131 shader->variant_key_size - sizeof variant->key); 4132 if (!variant) 4133 return NULL; 4134 4135 variant->llvm = llvm; 4136 variant->shader = shader; 4137 4138 snprintf(module_name, sizeof(module_name), "draw_llvm_tes_variant%u", 4139 variant->shader->variants_cached); 4140 4141 memcpy(&variant->key, key, shader->variant_key_size); 4142 if (shader->base.state.ir.nir && llvm->draw->disk_cache_cookie) { 4143 draw_get_ir_cache_key(shader->base.state.ir.nir, 4144 key, 4145 shader->variant_key_size, 4146 num_outputs, 4147 ir_sha1_cache_key); 4148 4149 llvm->draw->disk_cache_find_shader(llvm->draw->disk_cache_cookie, 4150 &cached, 4151 ir_sha1_cache_key); 4152 if (!cached.data_size) 4153 needs_caching = true; 4154 } 4155 variant->gallivm = gallivm_create(module_name, llvm->context, &cached); 4156 4157 create_tes_jit_types(variant); 4158 4159 vertex_header = create_jit_vertex_header(variant->gallivm, num_outputs); 4160 4161 variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0); 4162 4163 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) { 4164 nir_print_shader(llvm->draw->tes.tess_eval_shader->state.ir.nir, stderr); 4165 draw_tes_llvm_dump_variant_key(&variant->key); 4166 } 4167 4168 draw_tes_llvm_generate(llvm, variant); 4169 4170 gallivm_compile_module(variant->gallivm); 4171 4172 variant->jit_func = (draw_tes_jit_func) 4173 gallivm_jit_function(variant->gallivm, variant->function); 4174 4175 if (needs_caching) 4176 llvm->draw->disk_cache_insert_shader(llvm->draw->disk_cache_cookie, 4177 &cached, 4178 ir_sha1_cache_key); 4179 gallivm_free_ir(variant->gallivm); 4180 4181 variant->list_item_global.base = variant; 4182 variant->list_item_local.base = variant; 4183 /*variant->no = */shader->variants_created++; 4184 variant->list_item_global.base = variant; 4185 4186 return variant; 4187} 4188 4189void 4190draw_tes_llvm_destroy_variant(struct draw_tes_llvm_variant *variant) 4191{ 4192 struct draw_llvm *llvm = variant->llvm; 4193 4194 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) { 4195 debug_printf("Deleting TES variant: %u tes variants,\t%u total variants\n", 4196 variant->shader->variants_cached, llvm->nr_tes_variants); 4197 } 4198 4199 gallivm_destroy(variant->gallivm); 4200 4201 remove_from_list(&variant->list_item_local); 4202 variant->shader->variants_cached--; 4203 remove_from_list(&variant->list_item_global); 4204 llvm->nr_tes_variants--; 4205 FREE(variant); 4206} 4207 4208struct draw_tes_llvm_variant_key * 4209draw_tes_llvm_make_variant_key(struct draw_llvm *llvm, char *store) 4210{ 4211 unsigned i; 4212 struct draw_tes_llvm_variant_key *key; 4213 struct draw_sampler_static_state *draw_sampler; 4214 struct draw_image_static_state *draw_image; 4215 4216 key = (struct draw_tes_llvm_variant_key *)store; 4217 4218 memset(key, 0, offsetof(struct draw_tes_llvm_variant_key, samplers[0])); 4219 4220 int primid_output = draw_find_shader_output(llvm->draw, TGSI_SEMANTIC_PRIMID, 0); 4221 if (primid_output >= 0) { 4222 key->primid_output = primid_output; 4223 key->primid_needed = true; 4224 } 4225 4226 key->clamp_vertex_color = llvm->draw->rasterizer->clamp_vertex_color && 4227 llvm->draw->gs.geometry_shader == NULL; 4228 4229 /* All variants of this shader will have the same value for 4230 * nr_samplers. Not yet trying to compact away holes in the 4231 * sampler array. 4232 */ 4233 key->nr_samplers = llvm->draw->tes.tess_eval_shader->info.file_max[TGSI_FILE_SAMPLER] + 1; 4234 if (llvm->draw->tes.tess_eval_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) { 4235 key->nr_sampler_views = 4236 llvm->draw->tes.tess_eval_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1; 4237 } 4238 else { 4239 key->nr_sampler_views = key->nr_samplers; 4240 } 4241 4242 key->nr_images = llvm->draw->tes.tess_eval_shader->info.file_max[TGSI_FILE_IMAGE] + 1; 4243 4244 draw_sampler = key->samplers; 4245 4246 memset(draw_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler); 4247 4248 for (i = 0 ; i < key->nr_samplers; i++) { 4249 lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state, 4250 llvm->draw->samplers[PIPE_SHADER_TESS_EVAL][i]); 4251 } 4252 for (i = 0 ; i < key->nr_sampler_views; i++) { 4253 lp_sampler_static_texture_state(&draw_sampler[i].texture_state, 4254 llvm->draw->sampler_views[PIPE_SHADER_TESS_EVAL][i]); 4255 } 4256 4257 draw_image = draw_tes_llvm_variant_key_images(key); 4258 memset(draw_image, 0, 4259 key->nr_images * sizeof *draw_image); 4260 for (i = 0; i < key->nr_images; i++) { 4261 lp_sampler_static_texture_state_image(&draw_image[i].image_state, 4262 llvm->draw->images[PIPE_SHADER_TESS_EVAL][i]); 4263 } 4264 return key; 4265} 4266 4267void 4268draw_tes_llvm_dump_variant_key(struct draw_tes_llvm_variant_key *key) 4269{ 4270 unsigned i; 4271 struct draw_sampler_static_state *sampler = key->samplers; 4272 struct draw_image_static_state *image = draw_tes_llvm_variant_key_images(key); 4273 4274 if (key->primid_needed) 4275 debug_printf("prim id output %d\n", key->primid_output); 4276 debug_printf("clamp_vertex_color = %u\n", key->clamp_vertex_color); 4277 for (i = 0 ; i < key->nr_sampler_views; i++) { 4278 debug_printf("sampler[%i].src_format = %s\n", i, 4279 util_format_name(sampler[i].texture_state.format)); 4280 } 4281 4282 for (i = 0 ; i < key->nr_images; i++) 4283 debug_printf("images[%i].format = %s\n", i, util_format_name(image[i].image_state.format)); 4284 4285} 4286