Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions llvm/lib/Target/DirectX/DXIL.td
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,23 @@ def CBufferLoadLegacy : DXILOp<59, cbufferLoadLegacy> {
let attributes = [Attributes<DXIL1_0, [ReadOnly]>];
}

def SampleBias : DXILOp<61, sampleBias> {
let Doc = "samples a texture after applying the input bias to the mip level";
// Handle, Sampler, Coord0, Coord1, Coord2, Coord3,
// Offset0, Offset1, Offset2, Bias, Clamp
let arguments = [HandleTy, HandleTy, FloatTy, FloatTy, FloatTy, FloatTy,
Int32Ty, Int32Ty, Int32Ty, FloatTy, FloatTy];
let result = OverloadTy;
let overloads =
[Overloads<DXIL1_0, [ResRetHalfTy, ResRetFloatTy]>,
Overloads<DXIL1_7,
[ResRetHalfTy, ResRetFloatTy, ResRetInt16Ty, ResRetInt32Ty]>];
let stages = [Stages<DXIL1_0, [library, pixel]>,
Stages<DXIL1_6, [library, pixel, compute, amplification, mesh,
Comment thread
Icohedron marked this conversation as resolved.
node]>];
let attributes = [Attributes<DXIL1_0, [ReadOnly]>];
}

def TextureLoad : DXILOp<66, textureLoad> {
let Doc = "reads from a texture resource";
// Handle, MipLevelOrSampleCount, Coord0, Coord1, Coord2, Offset0, Offset1, Offset2
Expand Down
63 changes: 61 additions & 2 deletions llvm/lib/Target/DirectX/DXILOpLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,18 @@ class OpLowerer {
}
}

/// Copy offsets into the argument list at the given index, unless
/// the offsets are known to be zero (i.e., a null constant).
static void extractNonZeroOffsets(IRBuilder<> &IRB,
MutableArrayRef<Value *> Args,
unsigned ArgIdx, Value *Offsets,
unsigned MaxElements) {
auto *COff = dyn_cast<Constant>(Offsets);
bool OffsetsAreZero = COff && COff->isNullValue();
if (!OffsetsAreZero)
extractElementsIntoArgs(IRB, Args, ArgIdx, Offsets, MaxElements);
}

[[nodiscard]] bool lowerTextureLoad(Function &F) {
IRBuilder<> &IRB = OpBuilder.getIRB();
Type *Int32Ty = IRB.getInt32Ty();
Expand All @@ -630,8 +642,7 @@ class OpLowerer {

// Copy coordinates and offsets into Args.
extractElementsIntoArgs(IRB, Args, 2, Coords, 3);
if (auto *C = dyn_cast<Constant>(Offsets); !C || !C->isNullValue())
extractElementsIntoArgs(IRB, Args, 5, Offsets, 3);
extractNonZeroOffsets(IRB, Args, 5, Offsets, 3);

Expected<CallInst *> OpCall = OpBuilder.tryCreateOp(
OpCode::TextureLoad, Args, CI->getName(), NewRetTy);
Expand All @@ -644,6 +655,48 @@ class OpLowerer {
});
}

[[nodiscard]] bool lowerSampleBias(Function &F, bool HasClamp) {
IRBuilder<> &IRB = OpBuilder.getIRB();
Type *Int32Ty = IRB.getInt32Ty();
Type *FloatTy = IRB.getFloatTy();

return replaceFunction(F, [&](CallInst *CI) -> Error {
IRB.SetInsertPoint(CI);

Value *Handle =
createTmpHandleCast(CI->getArgOperand(0), OpBuilder.getHandleType());
Value *Sampler =
createTmpHandleCast(CI->getArgOperand(1), OpBuilder.getHandleType());
Value *Coords = CI->getArgOperand(2);
Value *Bias = CI->getArgOperand(3);
Value *Offsets = CI->getArgOperand(4);
Value *Clamp = HasClamp ? CI->getArgOperand(5) : UndefValue::get(FloatTy);

Type *OldTy = CI->getType();
Type *NewRetTy = OpBuilder.getResRetType(OldTy->getScalarType());

Value *UndefF = UndefValue::get(FloatTy);
Value *UndefI = UndefValue::get(Int32Ty);
// Args: Handle, Sampler, Coord0..3, Offset0..2, Bias, Clamp
std::array<Value *, 11> Args{Handle, Sampler, UndefF, UndefF,
UndefF, UndefF, UndefI, UndefI,
UndefI, Bias, Clamp};

// Copy coordinates and offsets into Args.
extractElementsIntoArgs(IRB, Args, 2, Coords, 4);
extractNonZeroOffsets(IRB, Args, 6, Offsets, 3);

Expected<CallInst *> OpCall = OpBuilder.tryCreateOp(
OpCode::SampleBias, Args, CI->getName(), NewRetTy);
if (Error E = OpCall.takeError())
return E;
if (Error E = replaceResRetUses(CI, *OpCall, /*HasCheckBit=*/false))
return E;

return Error::success();
});
}

[[nodiscard]] bool lowerRawBufferLoad(Function &F) {
const DataLayout &DL = F.getDataLayout();
IRBuilder<> &IRB = OpBuilder.getIRB();
Expand Down Expand Up @@ -1061,6 +1114,12 @@ class OpLowerer {
case Intrinsic::dx_resource_load_level:
HasErrors |= lowerTextureLoad(F);
break;
case Intrinsic::dx_resource_samplebias:
HasErrors |= lowerSampleBias(F, /*HasClamp=*/false);
break;
case Intrinsic::dx_resource_samplebias_clamp:
HasErrors |= lowerSampleBias(F, /*HasClamp=*/true);
break;
case Intrinsic::dx_resource_store_typedbuffer:
HasErrors |= lowerBufferStore(F, /*IsRaw=*/false);
break;
Expand Down
Loading
Loading