@@ -54,11 +54,22 @@ void DynamicTextureFolding::FoldSingleTextureValue(CallInst& I)
5454 bool directIdx = false ;
5555 uint textureIndex = 0 ;
5656 DecodeAS4GFXResource (addrSpace, directIdx, textureIndex);
57-
57+ IRBuilder<> builder (&I);
5858 // if the current texture index is found in modMD as uniform texture, replace the texture load/sample as constant.
5959 auto it = modMD->inlineDynTextures .find (textureIndex);
6060 if (it != modMD->inlineDynTextures .end ())
6161 {
62+ builder.SetInsertPoint (&I);
63+ // check if the array index is out of bound. The array index size is passed in from UMD
64+ // we might check xyz coordinate later but skip for now
65+ Value* cmpInstA = nullptr ;
66+ if (I.getOperand (3 )->getType ()->isIntOrIntVectorTy ())
67+ cmpInstA = builder.CreateICmp (ICmpInst::ICMP_SLE, I.getOperand (3 ), ConstantInt::get (Type::getInt32Ty (I.getContext ()), it->second [7 ]));
68+ else if (I.getOperand (3 )->getType ()->isFPOrFPVectorTy ())
69+ cmpInstA = builder.CreateICmp (ICmpInst::FCMP_ULE, I.getOperand (3 ), ConstantFP::get (Type::getFloatTy (I.getContext ()), it->second [7 ]));
70+ else
71+ return ;
72+
6273 for (auto iter = I.user_begin (); iter != I.user_end (); iter++)
6374 {
6475 if (llvm::ExtractElementInst* pExtract = llvm::dyn_cast<llvm::ExtractElementInst>(*iter))
@@ -67,11 +78,17 @@ void DynamicTextureFolding::FoldSingleTextureValue(CallInst& I)
6778 {
6879 if ((&I)->getType ()->isIntOrIntVectorTy ())
6980 {
70- pExtract->replaceAllUsesWith (ConstantInt::get ((pExtract)->getType (), (it->second [(uint32_t )(pIdx->getZExtValue ())])));
81+ llvm::Value* Zero = ConstantInt::get (Type::getInt32Ty (I.getContext ()), 0 );
82+ Value* IntInst = ConstantInt::get ((pExtract)->getType (), (it->second [(uint32_t )(pIdx->getZExtValue ())]));
83+ Value* newInst = builder.CreateSelect (cmpInstA, IntInst, Zero);
84+ pExtract->replaceAllUsesWith (newInst);
7185 }
7286 else if ((&I)->getType ()->isFPOrFPVectorTy ())
7387 {
74- pExtract->replaceAllUsesWith (ConstantFP::get ((pExtract)->getType (), *(float *)&(it->second [(uint32_t )(pIdx->getZExtValue ())])));
88+ llvm::Value* ZeroFP = ConstantFP::get (Type::getFloatTy (I.getContext ()), 0.0 );
89+ Value* fpInst = ConstantFP::get ((pExtract)->getType (), *(float *)&(it->second [(uint32_t )(pIdx->getZExtValue ())]));
90+ Value* newInst = builder.CreateSelect (cmpInstA, fpInst, ZeroFP);
91+ pExtract->replaceAllUsesWith (newInst);
7592 }
7693 }
7794 }
0 commit comments