15b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell/************************************************************************** 25b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * 35b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * Copyright 2010 VMware. 45b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * All Rights Reserved. 55b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * 65b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a 75b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * copy of this software and associated documentation files (the 85b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * "Software"), to deal in the Software without restriction, including 95b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * without limitation the rights to use, copy, modify, merge, publish, 105b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * distribute, sub license, and/or sell copies of the Software, and to 115b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * permit persons to whom the Software is furnished to do so, subject to 125b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * the following conditions: 135b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * 145b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * The above copyright notice and this permission notice (including the 155b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * next paragraph) shall be included in all copies or substantial portions 165b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * of the Software. 175b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * 185b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 195b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 205b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 215b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 225b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 235b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 245b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 255b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * 265b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell **************************************************************************/ 275b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 285b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 295b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "util/u_math.h" 305b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "util/u_memory.h" 315b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "util/u_simple_list.h" 325b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "os/os_time.h" 33315f8daab1598a4de709d469a559b5aa09107404Hui Qi Tay#include "gallivm/lp_bld_arit.h" 34efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul#include "gallivm/lp_bld_const.h" 355b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "gallivm/lp_bld_debug.h" 365b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "gallivm/lp_bld_init.h" 37f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton#include "gallivm/lp_bld_logic.h" 385b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "gallivm/lp_bld_intr.h" 397f0dc5ea1bb330c6589125baf4017c51a14dce8eHui Qi Tay#include "gallivm/lp_bld_flow.h" 40315f8daab1598a4de709d469a559b5aa09107404Hui Qi Tay#include "gallivm/lp_bld_type.h" 415b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 425b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "lp_perf.h" 435b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "lp_debug.h" 445b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "lp_flush.h" 455b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "lp_screen.h" 465b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "lp_context.h" 475b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "lp_state.h" 485b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "lp_state_fs.h" 495b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell#include "lp_state_setup.h" 505b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 515b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 525b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 535b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell/* currently organized to interpolate full float[4] attributes even 545b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * when some elements are unused. Later, can pack vertex data more 555b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * closely. 565b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 575b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 585b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 595b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstruct lp_setup_args 605b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 615b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell /* Function arguments: 625b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 635b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef v0; 645b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef v1; 655b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef v2; 665b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef facing; /* boolean */ 675b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef a0; 685b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dadx; 695b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dady; 705b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 715b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell /* Derived: 725b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 735b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef x0_center; 745b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef y0_center; 755b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dy20_ooa; 765b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dy01_ooa; 775b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dx20_ooa; 78d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay LLVMValueRef dx01_ooa; 795b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell}; 805b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 81efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul 82efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul 83efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulstatic LLVMTypeRef 84efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paultype4f(struct gallivm_state *gallivm) 855b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 86efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul return LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4); 875b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 885b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 895b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 905b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell/* Equivalent of _mm_setr_ps(a,b,c,d) 915b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 92efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulstatic LLVMValueRef 93efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulvec4f(struct gallivm_state *gallivm, 94efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef a, LLVMValueRef b, LLVMValueRef c, LLVMValueRef d, 95efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul const char *name) 965b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 97efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMBuilderRef bld = gallivm->builder; 98efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef i0 = lp_build_const_int32(gallivm, 0); 99efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef i1 = lp_build_const_int32(gallivm, 1); 100efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef i2 = lp_build_const_int32(gallivm, 2); 101efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef i3 = lp_build_const_int32(gallivm, 3); 1025b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 103efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef res = LLVMGetUndef(type4f(gallivm)); 1045b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1055b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell res = LLVMBuildInsertElement(bld, res, a, i0, ""); 1065b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell res = LLVMBuildInsertElement(bld, res, b, i1, ""); 1075b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell res = LLVMBuildInsertElement(bld, res, c, i2, ""); 1085b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell res = LLVMBuildInsertElement(bld, res, d, i3, name); 1095b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1105b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell return res; 1115b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 1125b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1135b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell/* Equivalent of _mm_set1_ps(a) 1145b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 115efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulstatic LLVMValueRef 116efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulvec4f_from_scalar(struct gallivm_state *gallivm, 117efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef a, 118efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul const char *name) 1195b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 120efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMBuilderRef bld = gallivm->builder; 121efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef res = LLVMGetUndef(type4f(gallivm)); 1225b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell int i; 1235b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1245b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell for(i = 0; i < 4; ++i) { 125efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef index = lp_build_const_int32(gallivm, i); 1265b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell res = LLVMBuildInsertElement(bld, res, a, index, i == 3 ? name : ""); 1275b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 1285b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1295b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell return res; 1305b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 1315b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1325b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 133efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulstore_coef(struct gallivm_state *gallivm, 1345b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_args *args, 1355b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell unsigned slot, 1365b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef a0, 1375b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dadx, 1385b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dady) 1395b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 140efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMBuilderRef builder = gallivm->builder; 141efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef idx = lp_build_const_int32(gallivm, slot); 1423469715a8a171512cf9b528702e70393f01c6041José Fonseca 1435b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMBuildStore(builder, 1445b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell a0, 1455b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMBuildGEP(builder, args->a0, &idx, 1, "")); 1465b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1475b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMBuildStore(builder, 1485b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell dadx, 1495b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMBuildGEP(builder, args->dadx, &idx, 1, "")); 1505b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1515b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMBuildStore(builder, 1525b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell dady, 1535b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMBuildGEP(builder, args->dady, &idx, 1, "")); 1545b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 1555b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1565b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1575b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1585b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 159efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulemit_constant_coef4(struct gallivm_state *gallivm, 1605b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_args *args, 1615b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell unsigned slot, 162d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay LLVMValueRef vert) 1635b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 164efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef zero = lp_build_const_float(gallivm, 0.0); 165efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef zerovec = vec4f_from_scalar(gallivm, zero, "zero"); 166efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul store_coef(gallivm, args, slot, vert, zerovec, zerovec); 1675b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 1685b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1695b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1705b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1715b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell/** 1725b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * Setup the fragment input attribute with the front-facing value. 1735b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * \param frontface is the triangle front facing? 1745b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 1755b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 176efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulemit_facing_coef(struct gallivm_state *gallivm, 1775b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_args *args, 1785b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell unsigned slot ) 1795b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 180efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMBuilderRef builder = gallivm->builder; 181efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 1825b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef a0_0 = args->facing; 183efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef a0_0f = LLVMBuildSIToFP(builder, a0_0, float_type, ""); 184efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef zero = lp_build_const_float(gallivm, 0.0); 185efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef a0 = vec4f(gallivm, a0_0f, zero, zero, zero, "facing"); 186efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef zerovec = vec4f_from_scalar(gallivm, zero, "zero"); 1875b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 188efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul store_coef(gallivm, args, slot, a0, zerovec, zerovec); 1895b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 1905b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1915b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 1925b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic LLVMValueRef 193efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulvert_attrib(struct gallivm_state *gallivm, 1945b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef vert, 1955b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell int attr, 1965b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell int elem, 1975b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell const char *name) 1985b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 199efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMBuilderRef b = gallivm->builder; 2005b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef idx[2]; 201efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul idx[0] = lp_build_const_int32(gallivm, attr); 202efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul idx[1] = lp_build_const_int32(gallivm, elem); 2035b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell return LLVMBuildLoad(b, LLVMBuildGEP(b, vert, idx, 2, ""), name); 2045b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 2055b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 2065b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 207d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Taystatic void 208efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paullp_twoside(struct gallivm_state *gallivm, 209d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay struct lp_setup_args *args, 210546c5ffa11d70631348e5776df7a4168f07600f6Keith Whitwell const struct lp_setup_variant_key *key, 2113469715a8a171512cf9b528702e70393f01c6041José Fonseca int bcolor_slot, 2123469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef attribv[3]) 213d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay{ 214efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMBuilderRef b = gallivm->builder; 215d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay LLVMValueRef a0_back, a1_back, a2_back; 216efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef idx2 = lp_build_const_int32(gallivm, bcolor_slot); 217d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 218d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay LLVMValueRef facing = args->facing; 219efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef front_facing = LLVMBuildICmp(b, LLVMIntEQ, facing, lp_build_const_int32(gallivm, 0), ""); /** need i1 for if condition */ 220d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 221d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay a0_back = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v0, &idx2, 1, ""), "v0a_back"); 222d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay a1_back = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v1, &idx2, 1, ""), "v1a_back"); 223d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay a2_back = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v2, &idx2, 1, ""), "v2a_back"); 224d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 225d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay /* Possibly swap the front and back attrib values, 226d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay * 227d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay * Prefer select to if so we don't have to worry about phis or 228d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay * allocas. 229d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay */ 2303469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[0] = LLVMBuildSelect(b, front_facing, a0_back, attribv[0], ""); 2313469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[1] = LLVMBuildSelect(b, front_facing, a1_back, attribv[1], ""); 2323469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[2] = LLVMBuildSelect(b, front_facing, a2_back, attribv[2], ""); 233d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 234d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay} 235d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 236d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Taystatic void 237efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paullp_do_offset_tri(struct gallivm_state *gallivm, 238d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay struct lp_setup_args *args, 2393469715a8a171512cf9b528702e70393f01c6041José Fonseca const struct lp_setup_variant_key *key, 2403469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef inv_det, 2413469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef dxyz01, 2423469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef dxyz20, 2433469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef attribv[3]) 244d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay{ 245efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMBuilderRef b = gallivm->builder; 246d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay struct lp_build_context bld; 247d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay LLVMValueRef zoffset, mult; 248d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay LLVMValueRef z0_new, z1_new, z2_new; 2493469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef dzdxdzdy, dzdx, dzdy, dzxyz20, dyzzx01, dyzzx01_dzxyz20, dzx01_dyz20; 2503469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef z0z1, z0z1z2; 2513469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef max, max_value, res12; 2523469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef shuffles[4]; 2533469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMTypeRef shuf_type = LLVMInt32TypeInContext(gallivm->context); 2543469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef onei = lp_build_const_int32(gallivm, 1); 2553469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef zeroi = lp_build_const_int32(gallivm, 0); 2563469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef twoi = lp_build_const_int32(gallivm, 2); 2573469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef threei = lp_build_const_int32(gallivm, 3); 2583469715a8a171512cf9b528702e70393f01c6041José Fonseca 2593469715a8a171512cf9b528702e70393f01c6041José Fonseca /* (res12) = cross(e,f).xy */ 2603469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[0] = twoi; 2613469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[1] = zeroi; 2623469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[2] = onei; 2633469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[3] = twoi; 2643469715a8a171512cf9b528702e70393f01c6041José Fonseca dzxyz20 = LLVMBuildShuffleVector(b, dxyz20, dxyz20, LLVMConstVector(shuffles, 4), ""); 2653469715a8a171512cf9b528702e70393f01c6041José Fonseca 2663469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[0] = onei; 2673469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[1] = twoi; 2683469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[2] = twoi; 2693469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[3] = zeroi; 2703469715a8a171512cf9b528702e70393f01c6041José Fonseca dyzzx01 = LLVMBuildShuffleVector(b, dxyz01, dxyz01, LLVMConstVector(shuffles, 4), ""); 2713469715a8a171512cf9b528702e70393f01c6041José Fonseca 2723469715a8a171512cf9b528702e70393f01c6041José Fonseca dyzzx01_dzxyz20 = LLVMBuildFMul(b, dzxyz20, dyzzx01, "dyzzx01_dzxyz20"); 2733469715a8a171512cf9b528702e70393f01c6041José Fonseca 2743469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[0] = twoi; 2753469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[1] = threei; 2763469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[2] = LLVMGetUndef(shuf_type); 2773469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[3] = LLVMGetUndef(shuf_type); 2783469715a8a171512cf9b528702e70393f01c6041José Fonseca dzx01_dyz20 = LLVMBuildShuffleVector(b, dyzzx01_dzxyz20, dyzzx01_dzxyz20, 2793469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMConstVector(shuffles, 4), ""); 2803469715a8a171512cf9b528702e70393f01c6041José Fonseca 2813469715a8a171512cf9b528702e70393f01c6041José Fonseca res12 = LLVMBuildFSub(b, dyzzx01_dzxyz20, dzx01_dyz20, "res12"); 282d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 283d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay /* dzdx = fabsf(res1 * inv_det), dydx = fabsf(res2 * inv_det)*/ 2843469715a8a171512cf9b528702e70393f01c6041José Fonseca lp_build_context_init(&bld, gallivm, lp_type_float_vec(32, 128)); 2853469715a8a171512cf9b528702e70393f01c6041José Fonseca dzdxdzdy = LLVMBuildFMul(b, res12, inv_det, "dzdxdzdy"); 2863469715a8a171512cf9b528702e70393f01c6041José Fonseca dzdxdzdy = lp_build_abs(&bld, dzdxdzdy); 2873469715a8a171512cf9b528702e70393f01c6041José Fonseca 2883469715a8a171512cf9b528702e70393f01c6041José Fonseca dzdx = LLVMBuildExtractElement(b, dzdxdzdy, zeroi, ""); 2893469715a8a171512cf9b528702e70393f01c6041José Fonseca dzdy = LLVMBuildExtractElement(b, dzdxdzdy, onei, ""); 290d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 291d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay /* zoffset = offset->units + MAX2(dzdx, dzdy) * offset->scale */ 292d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay max = LLVMBuildFCmp(b, LLVMRealUGT, dzdx, dzdy, ""); 293d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay max_value = LLVMBuildSelect(b, max, dzdx, dzdy, "max"); 294d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 295efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul mult = LLVMBuildFMul(b, max_value, lp_build_const_float(gallivm, key->scale), ""); 296efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul zoffset = LLVMBuildFAdd(b, lp_build_const_float(gallivm, key->units), mult, "zoffset"); 297d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 2983469715a8a171512cf9b528702e70393f01c6041José Fonseca /* yuck */ 2993469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[0] = twoi; 3003469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[1] = lp_build_const_int32(gallivm, 6); 3013469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[2] = LLVMGetUndef(shuf_type); 3023469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[3] = LLVMGetUndef(shuf_type); 3033469715a8a171512cf9b528702e70393f01c6041José Fonseca z0z1 = LLVMBuildShuffleVector(b, attribv[0], attribv[1], LLVMConstVector(shuffles, 4), ""); 3043469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[0] = zeroi; 3053469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[1] = onei; 3063469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[2] = lp_build_const_int32(gallivm, 6); 3073469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[3] = LLVMGetUndef(shuf_type); 3083469715a8a171512cf9b528702e70393f01c6041José Fonseca z0z1z2 = LLVMBuildShuffleVector(b, z0z1, attribv[2], LLVMConstVector(shuffles, 4), ""); 3093469715a8a171512cf9b528702e70393f01c6041José Fonseca zoffset = vec4f_from_scalar(gallivm, zoffset, ""); 3103469715a8a171512cf9b528702e70393f01c6041José Fonseca 311d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay /* clamp and do offset */ 3123469715a8a171512cf9b528702e70393f01c6041José Fonseca z0z1z2 = lp_build_clamp(&bld, LLVMBuildFAdd(b, z0z1z2, zoffset, ""), bld.zero, bld.one); 313d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 314d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay /* insert into args->a0.z, a1.z, a2.z: 3153469715a8a171512cf9b528702e70393f01c6041José Fonseca */ 3163469715a8a171512cf9b528702e70393f01c6041José Fonseca z0_new = LLVMBuildExtractElement(b, z0z1z2, zeroi, ""); 3173469715a8a171512cf9b528702e70393f01c6041José Fonseca z1_new = LLVMBuildExtractElement(b, z0z1z2, onei, ""); 3183469715a8a171512cf9b528702e70393f01c6041José Fonseca z2_new = LLVMBuildExtractElement(b, z0z1z2, twoi, ""); 3193469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[0] = LLVMBuildInsertElement(b, attribv[0], z0_new, twoi, ""); 3203469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[1] = LLVMBuildInsertElement(b, attribv[1], z1_new, twoi, ""); 3213469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[2] = LLVMBuildInsertElement(b, attribv[2], z2_new, twoi, ""); 322d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay} 323d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 324d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Taystatic void 325efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulload_attribute(struct gallivm_state *gallivm, 326d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay struct lp_setup_args *args, 327d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay const struct lp_setup_variant_key *key, 3283469715a8a171512cf9b528702e70393f01c6041José Fonseca unsigned vert_attr, 3293469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef attribv[3]) 330d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay{ 331efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMBuilderRef b = gallivm->builder; 332efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef idx = lp_build_const_int32(gallivm, vert_attr); 333d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 334d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay /* Load the vertex data 335d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay */ 3363469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[0] = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v0, &idx, 1, ""), "v0a"); 3373469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[1] = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v1, &idx, 1, ""), "v1a"); 3383469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[2] = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v2, &idx, 1, ""), "v2a"); 339d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 340d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 3413469715a8a171512cf9b528702e70393f01c6041José Fonseca /* Potentially modify it according to twoside, etc: 342d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay */ 343546c5ffa11d70631348e5776df7a4168f07600f6Keith Whitwell if (key->twoside) { 3440cbb49aff59f7a671d153d9baa70aa78b07da538José Fonseca if (vert_attr == key->color_slot && key->bcolor_slot >= 0) 3453469715a8a171512cf9b528702e70393f01c6041José Fonseca lp_twoside(gallivm, args, key, key->bcolor_slot, attribv); 3460cbb49aff59f7a671d153d9baa70aa78b07da538José Fonseca else if (vert_attr == key->spec_slot && key->bspec_slot >= 0) 3473469715a8a171512cf9b528702e70393f01c6041José Fonseca lp_twoside(gallivm, args, key, key->bspec_slot, attribv); 348d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay } 349d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay} 350d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay 3515b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 352efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulemit_coef4( struct gallivm_state *gallivm, 3535b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_args *args, 3545b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell unsigned slot, 3555b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef a0, 3565b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef a1, 3575b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef a2) 3585b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 359efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMBuilderRef b = gallivm->builder; 3605b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dy20_ooa = args->dy20_ooa; 3615b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dy01_ooa = args->dy01_ooa; 3625b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dx20_ooa = args->dx20_ooa; 3635b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dx01_ooa = args->dx01_ooa; 3645b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef x0_center = args->x0_center; 3655b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef y0_center = args->y0_center; 3665b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 3675b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef da01 = LLVMBuildFSub(b, a0, a1, "da01"); 3685b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef da20 = LLVMBuildFSub(b, a2, a0, "da20"); 3695b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 3705b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell /* Calculate dadx (vec4f) 3715b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 3725b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef da01_dy20_ooa = LLVMBuildFMul(b, da01, dy20_ooa, "da01_dy20_ooa"); 3735b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef da20_dy01_ooa = LLVMBuildFMul(b, da20, dy01_ooa, "da20_dy01_ooa"); 3745b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dadx = LLVMBuildFSub(b, da01_dy20_ooa, da20_dy01_ooa, "dadx"); 3755b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 3765b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell /* Calculate dady (vec4f) 3775b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 3785b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef da01_dx20_ooa = LLVMBuildFMul(b, da01, dx20_ooa, "da01_dx20_ooa"); 3795b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef da20_dx01_ooa = LLVMBuildFMul(b, da20, dx01_ooa, "da20_dx01_ooa"); 3805b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dady = LLVMBuildFSub(b, da20_dx01_ooa, da01_dx20_ooa, "dady"); 3815b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 3825b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell /* Calculate a0 - the attribute value at the origin 3835b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 3845b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dadx_x0 = LLVMBuildFMul(b, dadx, x0_center, "dadx_x0"); 3855b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef dady_y0 = LLVMBuildFMul(b, dady, y0_center, "dady_y0"); 3865b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef attr_v0 = LLVMBuildFAdd(b, dadx_x0, dady_y0, "attr_v0"); 3875b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef attr_0 = LLVMBuildFSub(b, a0, attr_v0, "attr_0"); 3885b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 389efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul store_coef(gallivm, args, slot, attr_0, dadx, dady); 3905b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 3915b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 3925b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 3935b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 394efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulemit_linear_coef( struct gallivm_state *gallivm, 3955b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_args *args, 3963469715a8a171512cf9b528702e70393f01c6041José Fonseca unsigned slot, 3973469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef attribv[3]) 3985b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 399d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay /* nothing to do anymore */ 400efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul emit_coef4(gallivm, 401d4b5cf6c05d4fcb0acaa36490f7d3379e7fb3c0eHui Qi Tay args, slot, 4023469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[0], 4033469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[1], 4043469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[2]); 4055b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 4065b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 4075b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 4085b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell/** 4095b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * Compute a0, dadx and dady for a perspective-corrected interpolant, 4105b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * for a triangle. 4115b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * We basically multiply the vertex value by 1/w before computing 4125b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * the plane coefficients (a0, dadx, dady). 4135b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * Later, when we compute the value at a particular fragment position we'll 4145b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * divide the interpolated value by the interpolated W at that fragment. 4155b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 4165b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 4173469715a8a171512cf9b528702e70393f01c6041José Fonsecaapply_perspective_corr( struct gallivm_state *gallivm, 4183469715a8a171512cf9b528702e70393f01c6041José Fonseca struct lp_setup_args *args, 4193469715a8a171512cf9b528702e70393f01c6041José Fonseca unsigned slot, 4203469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef attribv[3]) 4215b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 422efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMBuilderRef b = gallivm->builder; 423efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul 4245b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell /* premultiply by 1/w (v[0][3] is always 1/w): 4255b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 426efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef v0_oow = vec4f_from_scalar(gallivm, vert_attrib(gallivm, args->v0, 0, 3, ""), "v0_oow"); 427efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef v1_oow = vec4f_from_scalar(gallivm, vert_attrib(gallivm, args->v1, 0, 3, ""), "v1_oow"); 428efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMValueRef v2_oow = vec4f_from_scalar(gallivm, vert_attrib(gallivm, args->v2, 0, 3, ""), "v2_oow"); 4295b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 4303469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[0] = LLVMBuildFMul(b, attribv[0], v0_oow, "v0_oow_v0a"); 4313469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[1] = LLVMBuildFMul(b, attribv[1], v1_oow, "v1_oow_v1a"); 4323469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[2] = LLVMBuildFMul(b, attribv[2], v2_oow, "v2_oow_v2a"); 4335b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 4345b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 4355b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 4365b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 437efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulemit_position_coef( struct gallivm_state *gallivm, 4385b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_args *args, 4393469715a8a171512cf9b528702e70393f01c6041José Fonseca int slot, 4403469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef attribv[3]) 4415b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 4423469715a8a171512cf9b528702e70393f01c6041José Fonseca emit_linear_coef(gallivm, args, slot, attribv); 4435b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 4445b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 4455b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 446f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton/** 447f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton * Applys cylindrical wrapping to vertex attributes if enabled. 448f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton * Input coordinates must be in [0, 1] range, otherwise results are undefined. 449f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton * 450f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton * @param cyl_wrap TGSI_CYLINDRICAL_WRAP_x flags 451f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton */ 452f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Bentonstatic void 453f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Bentonemit_apply_cyl_wrap(struct gallivm_state *gallivm, 454f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton struct lp_setup_args *args, 4553469715a8a171512cf9b528702e70393f01c6041José Fonseca uint cyl_wrap, 4563469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef attribv[3]) 4573469715a8a171512cf9b528702e70393f01c6041José Fonseca 458f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton{ 459f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton LLVMBuilderRef builder = gallivm->builder; 460f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton struct lp_type type = lp_float32_vec4_type(); 461f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton LLVMTypeRef float_vec_type = lp_build_vec_type(gallivm, type); 462f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton LLVMValueRef pos_half; 463f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton LLVMValueRef neg_half; 464f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton LLVMValueRef cyl_mask; 465f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton LLVMValueRef offset; 466f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton LLVMValueRef delta; 467f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton LLVMValueRef one; 468f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton 469f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton if (!cyl_wrap) 470f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton return; 471f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton 472f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton /* Constants */ 473f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton pos_half = lp_build_const_vec(gallivm, type, +0.5f); 474f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton neg_half = lp_build_const_vec(gallivm, type, -0.5f); 475f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton cyl_mask = lp_build_const_mask_aos(gallivm, type, cyl_wrap); 476f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton 477f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton one = lp_build_const_vec(gallivm, type, 1.0f); 478f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton one = LLVMBuildBitCast(builder, one, lp_build_int_vec_type(gallivm, type), ""); 479f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton one = LLVMBuildAnd(builder, one, cyl_mask, ""); 480f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton 481f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton /* Edge v0 -> v1 */ 4823469715a8a171512cf9b528702e70393f01c6041José Fonseca delta = LLVMBuildFSub(builder, attribv[1], attribv[0], ""); 483f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton 4843469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = lp_build_compare(gallivm, type, PIPE_FUNC_GREATER, delta, pos_half); 4853469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = LLVMBuildAnd(builder, offset, one, ""); 4863469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = LLVMBuildBitCast(builder, offset, float_vec_type, ""); 4873469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[0] = LLVMBuildFAdd(builder, attribv[0], offset, ""); 488f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton 4893469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = lp_build_compare(gallivm, type, PIPE_FUNC_LESS, delta, neg_half); 4903469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = LLVMBuildAnd(builder, offset, one, ""); 4913469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = LLVMBuildBitCast(builder, offset, float_vec_type, ""); 4923469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[1] = LLVMBuildFAdd(builder, attribv[1], offset, ""); 493f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton 494f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton /* Edge v1 -> v2 */ 4953469715a8a171512cf9b528702e70393f01c6041José Fonseca delta = LLVMBuildFSub(builder, attribv[2], attribv[1], ""); 496f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton 4973469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = lp_build_compare(gallivm, type, PIPE_FUNC_GREATER, delta, pos_half); 4983469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = LLVMBuildAnd(builder, offset, one, ""); 4993469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = LLVMBuildBitCast(builder, offset, float_vec_type, ""); 5003469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[1] = LLVMBuildFAdd(builder, attribv[1], offset, ""); 501f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton 5023469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = lp_build_compare(gallivm, type, PIPE_FUNC_LESS, delta, neg_half); 5033469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = LLVMBuildAnd(builder, offset, one, ""); 5043469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = LLVMBuildBitCast(builder, offset, float_vec_type, ""); 5053469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[2] = LLVMBuildFAdd(builder, attribv[2], offset, ""); 506f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton 507f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton /* Edge v2 -> v0 */ 5083469715a8a171512cf9b528702e70393f01c6041José Fonseca delta = LLVMBuildFSub(builder, attribv[0], attribv[2], ""); 509f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton 5103469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = lp_build_compare(gallivm, type, PIPE_FUNC_GREATER, delta, pos_half); 5113469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = LLVMBuildAnd(builder, offset, one, ""); 5123469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = LLVMBuildBitCast(builder, offset, float_vec_type, ""); 5133469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[2] = LLVMBuildFAdd(builder, attribv[2], offset, ""); 514f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton 5153469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = lp_build_compare(gallivm, type, PIPE_FUNC_LESS, delta, neg_half); 5163469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = LLVMBuildAnd(builder, offset, one, ""); 5173469715a8a171512cf9b528702e70393f01c6041José Fonseca offset = LLVMBuildBitCast(builder, offset, float_vec_type, ""); 5183469715a8a171512cf9b528702e70393f01c6041José Fonseca attribv[0] = LLVMBuildFAdd(builder, attribv[0], offset, ""); 519f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton} 5205b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 5215b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 5225b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell/** 5235b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * Compute the inputs-> dadx, dady, a0 values. 5245b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 5255b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 526efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulemit_tri_coef( struct gallivm_state *gallivm, 5273469715a8a171512cf9b528702e70393f01c6041José Fonseca const struct lp_setup_variant_key *key, 5283469715a8a171512cf9b528702e70393f01c6041José Fonseca struct lp_setup_args *args) 5295b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 5305b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell unsigned slot; 5315b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 5323469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef attribs[3]; 5335b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 5343469715a8a171512cf9b528702e70393f01c6041José Fonseca /* setup interpolation for all the remaining attributes: 5355b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 5365b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell for (slot = 0; slot < key->num_inputs; slot++) { 5375b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell switch (key->inputs[slot].interp) { 5385b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell case LP_INTERP_CONSTANT: 5393469715a8a171512cf9b528702e70393f01c6041José Fonseca load_attribute(gallivm, args, key, key->inputs[slot].src_index, attribs); 5403469715a8a171512cf9b528702e70393f01c6041José Fonseca if (key->flatshade_first) { 5413469715a8a171512cf9b528702e70393f01c6041José Fonseca emit_constant_coef4(gallivm, args, slot+1, attribs[0]); 5423469715a8a171512cf9b528702e70393f01c6041José Fonseca } 5433469715a8a171512cf9b528702e70393f01c6041José Fonseca else { 5443469715a8a171512cf9b528702e70393f01c6041José Fonseca emit_constant_coef4(gallivm, args, slot+1, attribs[2]); 5453469715a8a171512cf9b528702e70393f01c6041José Fonseca } 5463469715a8a171512cf9b528702e70393f01c6041José Fonseca break; 5475b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 5485b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell case LP_INTERP_LINEAR: 5493469715a8a171512cf9b528702e70393f01c6041José Fonseca load_attribute(gallivm, args, key, key->inputs[slot].src_index, attribs); 5503469715a8a171512cf9b528702e70393f01c6041José Fonseca emit_apply_cyl_wrap(gallivm, args, key->inputs[slot].cyl_wrap, attribs); 5513469715a8a171512cf9b528702e70393f01c6041José Fonseca emit_linear_coef(gallivm, args, slot+1, attribs); 5525b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell break; 5535b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 5545b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell case LP_INTERP_PERSPECTIVE: 5553469715a8a171512cf9b528702e70393f01c6041José Fonseca load_attribute(gallivm, args, key, key->inputs[slot].src_index, attribs); 5563469715a8a171512cf9b528702e70393f01c6041José Fonseca emit_apply_cyl_wrap(gallivm, args, key->inputs[slot].cyl_wrap, attribs); 5573469715a8a171512cf9b528702e70393f01c6041José Fonseca apply_perspective_corr(gallivm, args, slot+1, attribs); 5583469715a8a171512cf9b528702e70393f01c6041José Fonseca emit_linear_coef(gallivm, args, slot+1, attribs); 5595b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell break; 5605b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 5615b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell case LP_INTERP_POSITION: 5625b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell /* 5635b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * The generated pixel interpolators will pick up the coeffs from 5645b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * slot 0. 5655b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 5665b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell break; 5675b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 5685b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell case LP_INTERP_FACING: 569efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul emit_facing_coef(gallivm, args, slot+1); 5705b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell break; 5715b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 5725b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell default: 5735b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell assert(0); 5745b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 5755b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 5765b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 5775b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 5785b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 5795b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell/* XXX: generic code: 5805b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 5815b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 5825b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellset_noalias(LLVMBuilderRef builder, 5835b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMValueRef function, 5845b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell const LLVMTypeRef *arg_types, 5855b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell int nr_args) 5865b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 5875b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell int i; 5881597176f7090eea73f41b3114ae2a02a50ac7a12Vinson Lee for(i = 0; i < nr_args; ++i) 5895b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if(LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) 5905b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMAddAttribute(LLVMGetParam(function, i), 5915b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMNoAliasAttribute); 5925b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 5935b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 5945b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 595efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paulinit_args(struct gallivm_state *gallivm, 5963469715a8a171512cf9b528702e70393f01c6041José Fonseca const struct lp_setup_variant_key *key, 5973469715a8a171512cf9b528702e70393f01c6041José Fonseca struct lp_setup_args *args) 5985b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 599efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul LLVMBuilderRef b = gallivm->builder; 6003469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMTypeRef shuf_type = LLVMInt32TypeInContext(gallivm->context); 6013469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef onef = lp_build_const_float(gallivm, 1.0); 6023469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef onei = lp_build_const_int32(gallivm, 1); 6033469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef zeroi = lp_build_const_int32(gallivm, 0); 6043469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef pixel_center, xy0_center, dxy01, dxy20, dyx20; 6053469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef e, f, ef, ooa; 6063469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef shuffles[4]; 6073469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMValueRef attr_pos[3]; 6083469715a8a171512cf9b528702e70393f01c6041José Fonseca struct lp_type typef4 = lp_type_float_vec(32, 128); 6093469715a8a171512cf9b528702e70393f01c6041José Fonseca 6103469715a8a171512cf9b528702e70393f01c6041José Fonseca /* The internal position input is in slot zero: 6113469715a8a171512cf9b528702e70393f01c6041José Fonseca */ 6123469715a8a171512cf9b528702e70393f01c6041José Fonseca load_attribute(gallivm, args, key, 0, attr_pos); 6133469715a8a171512cf9b528702e70393f01c6041José Fonseca 6143469715a8a171512cf9b528702e70393f01c6041José Fonseca pixel_center = lp_build_const_vec(gallivm, typef4, 6153469715a8a171512cf9b528702e70393f01c6041José Fonseca key->pixel_center_half ? 0.5 : 0.0); 6165b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6173469715a8a171512cf9b528702e70393f01c6041José Fonseca /* 6183469715a8a171512cf9b528702e70393f01c6041José Fonseca * xy are first two elems in v0a/v1a/v2a but just use vec4 arit 6193469715a8a171512cf9b528702e70393f01c6041José Fonseca * also offset_tri uses actually xyz in them 6203469715a8a171512cf9b528702e70393f01c6041José Fonseca */ 6213469715a8a171512cf9b528702e70393f01c6041José Fonseca xy0_center = LLVMBuildFSub(b, attr_pos[0], pixel_center, "xy0_center" ); 6225b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6233469715a8a171512cf9b528702e70393f01c6041José Fonseca dxy01 = LLVMBuildFSub(b, attr_pos[0], attr_pos[1], "dxy01"); 6243469715a8a171512cf9b528702e70393f01c6041José Fonseca dxy20 = LLVMBuildFSub(b, attr_pos[2], attr_pos[0], "dxy20"); 6255b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6263469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[0] = onei; 6273469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[1] = zeroi; 6283469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[2] = LLVMGetUndef(shuf_type); 6293469715a8a171512cf9b528702e70393f01c6041José Fonseca shuffles[3] = LLVMGetUndef(shuf_type); 630efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul 6313469715a8a171512cf9b528702e70393f01c6041José Fonseca dyx20 = LLVMBuildShuffleVector(b, dxy20, dxy20, LLVMConstVector(shuffles, 4), ""); 6325b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6333469715a8a171512cf9b528702e70393f01c6041José Fonseca ef = LLVMBuildFMul(b, dxy01, dyx20, "ef"); 6343469715a8a171512cf9b528702e70393f01c6041José Fonseca e = LLVMBuildExtractElement(b, ef, zeroi, ""); 6353469715a8a171512cf9b528702e70393f01c6041José Fonseca f = LLVMBuildExtractElement(b, ef, onei, ""); 6365b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6373469715a8a171512cf9b528702e70393f01c6041José Fonseca ooa = LLVMBuildFDiv(b, onef, LLVMBuildFSub(b, e, f, ""), "ooa"); 6385b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6393469715a8a171512cf9b528702e70393f01c6041José Fonseca ooa = vec4f_from_scalar(gallivm, ooa, ""); 6403469715a8a171512cf9b528702e70393f01c6041José Fonseca 6413469715a8a171512cf9b528702e70393f01c6041José Fonseca /* tri offset calc shares a lot of arithmetic, do it here */ 6423469715a8a171512cf9b528702e70393f01c6041José Fonseca if (key->scale != 0.0f || key->units != 0.0f) { 6433469715a8a171512cf9b528702e70393f01c6041José Fonseca lp_do_offset_tri(gallivm, args, key, ooa, dxy01, dxy20, attr_pos); 6443469715a8a171512cf9b528702e70393f01c6041José Fonseca } 6455b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6463469715a8a171512cf9b528702e70393f01c6041José Fonseca dxy20 = LLVMBuildFMul(b, dxy20, ooa, ""); 6473469715a8a171512cf9b528702e70393f01c6041José Fonseca dxy01 = LLVMBuildFMul(b, dxy01, ooa, ""); 6485b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6493469715a8a171512cf9b528702e70393f01c6041José Fonseca args->dy20_ooa = lp_build_extract_broadcast(gallivm, typef4, typef4, dxy20, onei); 6503469715a8a171512cf9b528702e70393f01c6041José Fonseca args->dy01_ooa = lp_build_extract_broadcast(gallivm, typef4, typef4, dxy01, onei); 6515b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6523469715a8a171512cf9b528702e70393f01c6041José Fonseca args->dx20_ooa = lp_build_extract_broadcast(gallivm, typef4, typef4, dxy20, zeroi); 6533469715a8a171512cf9b528702e70393f01c6041José Fonseca args->dx01_ooa = lp_build_extract_broadcast(gallivm, typef4, typef4, dxy01, zeroi); 6543469715a8a171512cf9b528702e70393f01c6041José Fonseca 6553469715a8a171512cf9b528702e70393f01c6041José Fonseca args->x0_center = lp_build_extract_broadcast(gallivm, typef4, typef4, xy0_center, zeroi); 6563469715a8a171512cf9b528702e70393f01c6041José Fonseca args->y0_center = lp_build_extract_broadcast(gallivm, typef4, typef4, xy0_center, onei); 6573469715a8a171512cf9b528702e70393f01c6041José Fonseca 6583469715a8a171512cf9b528702e70393f01c6041José Fonseca /* might want to merge that with other coef emit in the future */ 6593469715a8a171512cf9b528702e70393f01c6041José Fonseca emit_position_coef(gallivm, args, 0, attr_pos); 6605b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 6615b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6625b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell/** 6635b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * Generate the runtime callable function for the coefficient calculation. 6645b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * 6655b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 6665b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic struct lp_setup_variant * 6673469715a8a171512cf9b528702e70393f01c6041José Fonsecagenerate_setup_variant(struct lp_setup_variant_key *key, 6687f0dc5ea1bb330c6589125baf4017c51a14dce8eHui Qi Tay struct llvmpipe_context *lp) 6695b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 6705b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_variant *variant = NULL; 6713469715a8a171512cf9b528702e70393f01c6041José Fonseca struct gallivm_state *gallivm; 6725b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_args args; 6735b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell char func_name[256]; 6745b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMTypeRef vec4f_type; 6755b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMTypeRef func_type; 6769da17fed2e7645a401a378ae690eb23513948e18Keith Whitwell LLVMTypeRef arg_types[7]; 6775b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMBasicBlockRef block; 6783469715a8a171512cf9b528702e70393f01c6041José Fonseca LLVMBuilderRef builder; 679f1e160f89b5557f5d64455f4d2735a64a7e289bbMarek Olšák int64_t t0 = 0, t1; 6805b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6815b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (0) 6825b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell goto fail; 6835b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6845b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell variant = CALLOC_STRUCT(lp_setup_variant); 6855b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (variant == NULL) 6865b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell goto fail; 6875b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6883469715a8a171512cf9b528702e70393f01c6041José Fonseca variant->gallivm = gallivm = gallivm_create(); 6893469715a8a171512cf9b528702e70393f01c6041José Fonseca if (!variant->gallivm) { 6903469715a8a171512cf9b528702e70393f01c6041José Fonseca goto fail; 6913469715a8a171512cf9b528702e70393f01c6041José Fonseca } 6923469715a8a171512cf9b528702e70393f01c6041José Fonseca 6933469715a8a171512cf9b528702e70393f01c6041José Fonseca builder = gallivm->builder; 6943469715a8a171512cf9b528702e70393f01c6041José Fonseca 6955b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (LP_DEBUG & DEBUG_COUNTERS) { 6965b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell t0 = os_time_get(); 6975b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 6985b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 6995b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell memcpy(&variant->key, key, key->size); 7005b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell variant->list_item_global.base = variant; 7015b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7025b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell util_snprintf(func_name, sizeof(func_name), "fs%u_setup%u", 7035b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 0, 7045b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell variant->no); 7055b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7065b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell /* Currently always deal with full 4-wide vertex attributes from 7075b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * the vertices. 7085b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 7095b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 710efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul vec4f_type = LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4); 7115b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7125b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell arg_types[0] = LLVMPointerType(vec4f_type, 0); /* v0 */ 7135b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell arg_types[1] = LLVMPointerType(vec4f_type, 0); /* v1 */ 7145b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell arg_types[2] = LLVMPointerType(vec4f_type, 0); /* v2 */ 715efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul arg_types[3] = LLVMInt32TypeInContext(gallivm->context); /* facing */ 7165b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell arg_types[4] = LLVMPointerType(vec4f_type, 0); /* a0, aligned */ 7175b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell arg_types[5] = LLVMPointerType(vec4f_type, 0); /* dadx, aligned */ 7185b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell arg_types[6] = LLVMPointerType(vec4f_type, 0); /* dady, aligned */ 7195b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 720efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul func_type = LLVMFunctionType(LLVMVoidTypeInContext(gallivm->context), 721efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul arg_types, Elements(arg_types), 0); 7225b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 723efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul variant->function = LLVMAddFunction(gallivm->module, func_name, func_type); 7245b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (!variant->function) 7255b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell goto fail; 7265b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7275b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMSetFunctionCallConv(variant->function, LLVMCCallConv); 7285b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7295b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell args.v0 = LLVMGetParam(variant->function, 0); 7305b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell args.v1 = LLVMGetParam(variant->function, 1); 7315b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell args.v2 = LLVMGetParam(variant->function, 2); 7325b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell args.facing = LLVMGetParam(variant->function, 3); 7335b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell args.a0 = LLVMGetParam(variant->function, 4); 7345b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell args.dadx = LLVMGetParam(variant->function, 5); 7355b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell args.dady = LLVMGetParam(variant->function, 6); 7365b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7375b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell lp_build_name(args.v0, "in_v0"); 7385b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell lp_build_name(args.v1, "in_v1"); 7395b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell lp_build_name(args.v2, "in_v2"); 7405b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell lp_build_name(args.facing, "in_facing"); 7415b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell lp_build_name(args.a0, "out_a0"); 7425b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell lp_build_name(args.dadx, "out_dadx"); 7435b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell lp_build_name(args.dady, "out_dady"); 7445b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7455b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell /* 7465b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * Function body 7475b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 748efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul block = LLVMAppendBasicBlockInContext(gallivm->context, 749efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul variant->function, "entry"); 7505b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMPositionBuilderAtEnd(builder, block); 7515b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7525b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell set_noalias(builder, variant->function, arg_types, Elements(arg_types)); 7533469715a8a171512cf9b528702e70393f01c6041José Fonseca init_args(gallivm, &variant->key, &args); 754efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul emit_tri_coef(gallivm, &variant->key, &args); 7555b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7565b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LLVMBuildRetVoid(builder); 7575b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7583469715a8a171512cf9b528702e70393f01c6041José Fonseca gallivm_verify_function(gallivm, variant->function); 7593469715a8a171512cf9b528702e70393f01c6041José Fonseca 7603469715a8a171512cf9b528702e70393f01c6041José Fonseca gallivm_compile_module(gallivm); 7613469715a8a171512cf9b528702e70393f01c6041José Fonseca 7623469715a8a171512cf9b528702e70393f01c6041José Fonseca variant->jit_function = (lp_jit_setup_triangle) 7633469715a8a171512cf9b528702e70393f01c6041José Fonseca gallivm_jit_function(gallivm, variant->function); 7645b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (!variant->jit_function) 7655b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell goto fail; 7665b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7675b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell /* 7685b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * Update timing information: 7695b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 7705b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (LP_DEBUG & DEBUG_COUNTERS) { 7715b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell t1 = os_time_get(); 7725b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LP_COUNT_ADD(llvm_compile_time, t1 - t0); 7735b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell LP_COUNT_ADD(nr_llvm_compiles, 1); 7745b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 7755b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7765b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell return variant; 7775b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7785b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellfail: 7795b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (variant) { 7805b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (variant->function) { 7813469715a8a171512cf9b528702e70393f01c6041José Fonseca gallivm_free_function(gallivm, 7823469715a8a171512cf9b528702e70393f01c6041José Fonseca variant->function, 7833469715a8a171512cf9b528702e70393f01c6041José Fonseca variant->jit_function); 7843469715a8a171512cf9b528702e70393f01c6041José Fonseca } 7853469715a8a171512cf9b528702e70393f01c6041José Fonseca if (variant->gallivm) { 7863469715a8a171512cf9b528702e70393f01c6041José Fonseca gallivm_destroy(variant->gallivm); 7875b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 7885b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell FREE(variant); 7895b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 7905b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7915b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell return NULL; 7925b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 7935b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7945b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7955b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 7965b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 7975b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwelllp_make_setup_variant_key(struct llvmpipe_context *lp, 7985b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_variant_key *key) 7995b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 8005b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_fragment_shader *fs = lp->fs; 8015b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell unsigned i; 8025b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 803f34e2f484bb73bba79a5b3fa7cff2e79bc7f0cf9James Benton assert(sizeof key->inputs[0] == sizeof(uint)); 8045b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8050072acd447dc6be652e63752e50215c3105322c8Keith Whitwell key->num_inputs = fs->info.base.num_inputs; 8065b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell key->flatshade_first = lp->rasterizer->flatshade_first; 8075b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell key->pixel_center_half = lp->rasterizer->gl_rasterization_rules; 8087f0dc5ea1bb330c6589125baf4017c51a14dce8eHui Qi Tay key->twoside = lp->rasterizer->light_twoside; 8095b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell key->size = Offset(struct lp_setup_variant_key, 810546c5ffa11d70631348e5776df7a4168f07600f6Keith Whitwell inputs[key->num_inputs]); 8110cbb49aff59f7a671d153d9baa70aa78b07da538José Fonseca 8120cbb49aff59f7a671d153d9baa70aa78b07da538José Fonseca key->color_slot = lp->color_slot [0]; 813546c5ffa11d70631348e5776df7a4168f07600f6Keith Whitwell key->bcolor_slot = lp->bcolor_slot[0]; 8140cbb49aff59f7a671d153d9baa70aa78b07da538José Fonseca key->spec_slot = lp->color_slot [1]; 8150cbb49aff59f7a671d153d9baa70aa78b07da538José Fonseca key->bspec_slot = lp->bcolor_slot[1]; 8160cbb49aff59f7a671d153d9baa70aa78b07da538José Fonseca assert(key->color_slot == lp->color_slot [0]); 8170cbb49aff59f7a671d153d9baa70aa78b07da538José Fonseca assert(key->bcolor_slot == lp->bcolor_slot[0]); 8180cbb49aff59f7a671d153d9baa70aa78b07da538José Fonseca assert(key->spec_slot == lp->color_slot [1]); 8190cbb49aff59f7a671d153d9baa70aa78b07da538José Fonseca assert(key->bspec_slot == lp->bcolor_slot[1]); 8200cbb49aff59f7a671d153d9baa70aa78b07da538José Fonseca 821315f8daab1598a4de709d469a559b5aa09107404Hui Qi Tay key->units = (float) (lp->rasterizer->offset_units * lp->mrd); 822315f8daab1598a4de709d469a559b5aa09107404Hui Qi Tay key->scale = lp->rasterizer->offset_scale; 8235b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell key->pad = 0; 8245b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell memcpy(key->inputs, fs->inputs, key->num_inputs * sizeof key->inputs[0]); 8255b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell for (i = 0; i < key->num_inputs; i++) { 8265b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (key->inputs[i].interp == LP_INTERP_COLOR) { 8277f0dc5ea1bb330c6589125baf4017c51a14dce8eHui Qi Tay if (lp->rasterizer->flatshade) 8285b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell key->inputs[i].interp = LP_INTERP_CONSTANT; 8295b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell else 830982df3c1a5e99e43f28f849419d4379e6e5d5d05Olivier Galibert key->inputs[i].interp = LP_INTERP_PERSPECTIVE; 8315b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 8325b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 8335b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8345b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 8355b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8365b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8375b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 8385b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellremove_setup_variant(struct llvmpipe_context *lp, 8395b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_variant *variant) 8405b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 8415b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (gallivm_debug & GALLIVM_DEBUG_IR) { 8425b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell debug_printf("llvmpipe: del setup_variant #%u total %u\n", 8435b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell variant->no, lp->nr_setup_variants); 8445b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 8455b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8465b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (variant->function) { 8473469715a8a171512cf9b528702e70393f01c6041José Fonseca gallivm_free_function(variant->gallivm, 8483469715a8a171512cf9b528702e70393f01c6041José Fonseca variant->function, 8493469715a8a171512cf9b528702e70393f01c6041José Fonseca variant->jit_function); 8503469715a8a171512cf9b528702e70393f01c6041José Fonseca } 8513469715a8a171512cf9b528702e70393f01c6041José Fonseca 8523469715a8a171512cf9b528702e70393f01c6041José Fonseca if (variant->gallivm) { 8533469715a8a171512cf9b528702e70393f01c6041José Fonseca gallivm_destroy(variant->gallivm); 8545b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 8555b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8565b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell remove_from_list(&variant->list_item_global); 8575b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell lp->nr_setup_variants--; 8585b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell FREE(variant); 8595b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 8605b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8615b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8625b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8635b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell/* When the number of setup variants exceeds a threshold, cull a 8645b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * fraction (currently a quarter) of them. 8655b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 8665b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellstatic void 8675b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellcull_setup_variants(struct llvmpipe_context *lp) 8685b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 8695b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct pipe_context *pipe = &lp->pipe; 8705b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell int i; 8715b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8725b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell /* 8735b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * XXX: we need to flush the context until we have some sort of reference 8745b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * counting in fragment shaders as they may still be binned 8755b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * Flushing alone might not be sufficient we need to wait on it too. 8765b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 8775b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell llvmpipe_finish(pipe, __FUNCTION__); 8785b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8795b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell for (i = 0; i < LP_MAX_SETUP_VARIANTS / 4; i++) { 880f32c7232a8a16887af710a11f025381bc73640f0José Fonseca struct lp_setup_variant_list_item *item; 881f32c7232a8a16887af710a11f025381bc73640f0José Fonseca if (is_empty_list(&lp->setup_variants_list)) { 882f32c7232a8a16887af710a11f025381bc73640f0José Fonseca break; 883f32c7232a8a16887af710a11f025381bc73640f0José Fonseca } 884f32c7232a8a16887af710a11f025381bc73640f0José Fonseca item = last_elem(&lp->setup_variants_list); 885f32c7232a8a16887af710a11f025381bc73640f0José Fonseca assert(item); 886f32c7232a8a16887af710a11f025381bc73640f0José Fonseca assert(item->base); 8875b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell remove_setup_variant(lp, item->base); 8885b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 8895b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 8905b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8915b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 8925b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell/** 8935b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * Update fragment/vertex shader linkage state. This is called just 8945b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * prior to drawing something when some fragment-related state has 8955b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell * changed. 8965b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell */ 8975b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellvoid 8985b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellllvmpipe_update_setup(struct llvmpipe_context *lp) 8995b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 9005b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_variant_key *key = &lp->setup_variant.key; 9015b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_variant *variant = NULL; 9025b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_variant_list_item *li; 9035b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 9045b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell lp_make_setup_variant_key(lp, key); 9055b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 9065b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell foreach(li, &lp->setup_variants_list) { 9075b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if(li->base->key.size == key->size && 9085b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell memcmp(&li->base->key, key, key->size) == 0) { 9095b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell variant = li->base; 9105b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell break; 9115b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 9125b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 9135b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 9145b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (variant) { 9155b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell move_to_head(&lp->setup_variants_list, &variant->list_item_global); 9165b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 9175b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell else { 9185b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (lp->nr_setup_variants >= LP_MAX_SETUP_VARIANTS) { 9195b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell cull_setup_variants(lp); 9205b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 9215b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 9223469715a8a171512cf9b528702e70393f01c6041José Fonseca variant = generate_setup_variant(key, lp); 923c88f3e0374620f18cf38d9fc3c45d14bc53f62b2José Fonseca if (variant) { 924c88f3e0374620f18cf38d9fc3c45d14bc53f62b2José Fonseca insert_at_head(&lp->setup_variants_list, &variant->list_item_global); 925c88f3e0374620f18cf38d9fc3c45d14bc53f62b2José Fonseca lp->nr_setup_variants++; 926c88f3e0374620f18cf38d9fc3c45d14bc53f62b2José Fonseca llvmpipe_variant_count++; 927c88f3e0374620f18cf38d9fc3c45d14bc53f62b2José Fonseca } 9285b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 9295b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 9305b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell lp_setup_set_setup_variant(lp->setup, 9315b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell variant); 9325b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 9335b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 9345b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellvoid 9355b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwelllp_delete_setup_variants(struct llvmpipe_context *lp) 9365b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 9375b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_variant_list_item *li; 9385b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell li = first_elem(&lp->setup_variants_list); 9395b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell while(!at_end(&lp->setup_variants_list, li)) { 9405b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell struct lp_setup_variant_list_item *next = next_elem(li); 9415b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell remove_setup_variant(lp, li->base); 9425b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell li = next; 9435b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 9445b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 9455b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 9465b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwellvoid 9475b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwelllp_dump_setup_coef( const struct lp_setup_variant_key *key, 9485b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell const float (*sa0)[4], 9495b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell const float (*sdadx)[4], 9505b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell const float (*sdady)[4]) 9515b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell{ 9525b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell int i, slot; 9535b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 9546b63e25b3d7a6ac0bd738c139ead0c7e7ad84368Tom Stellard for (i = 0; i < TGSI_NUM_CHANNELS; i++) { 9555b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell float a0 = sa0 [0][i]; 9565b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell float dadx = sdadx[0][i]; 9575b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell float dady = sdady[0][i]; 9585b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 9595b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell debug_printf("POS.%c: a0 = %f, dadx = %f, dady = %f\n", 9605b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell "xyzw"[i], 9615b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell a0, dadx, dady); 9625b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 9635b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 9645b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell for (slot = 0; slot < key->num_inputs; slot++) { 9655b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell unsigned usage_mask = key->inputs[slot].usage_mask; 9666b63e25b3d7a6ac0bd738c139ead0c7e7ad84368Tom Stellard for (i = 0; i < TGSI_NUM_CHANNELS; i++) { 9675b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell if (usage_mask & (1 << i)) { 9685b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell float a0 = sa0 [1 + slot][i]; 9695b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell float dadx = sdadx[1 + slot][i]; 9705b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell float dady = sdady[1 + slot][i]; 9715b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell 9725b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell debug_printf("IN[%u].%c: a0 = %f, dadx = %f, dady = %f\n", 9735b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell slot, 9745b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell "xyzw"[i], 9755b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell a0, dadx, dady); 9765b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 9775b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 9785b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell } 9795b4c43d98556c5a4806757513bcb196a724518c5Keith Whitwell} 980