Skip to content

Commit

Permalink
Refactor parsing of optional args
Browse files Browse the repository at this point in the history
  • Loading branch information
cassiebeckley committed Aug 15, 2024
1 parent 8da031c commit 9de329f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 45 deletions.
79 changes: 42 additions & 37 deletions tools/clang/lib/SPIRV/SpirvEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5472,6 +5472,35 @@ SpirvInstruction *SpirvEmitter::createImageSample(
return retVal;
}

void SpirvEmitter::handleOptionalTextureSampleArgs(
const CXXMemberCallExpr *expr, uint32_t index,
SpirvInstruction **constOffset, SpirvInstruction **varOffset,
SpirvInstruction **clamp, SpirvInstruction **status) {
uint32_t numArgs = expr->getNumArgs();

bool hasOffsetArg = index < numArgs &&
(expr->getArg(index)->getType()->isSignedIntegerType() ||
hlsl::IsHLSLVecType(expr->getArg(index)->getType()));
if (hasOffsetArg) {
handleOffsetInMethodCall(expr, index, constOffset, varOffset);
index++;
}

bool hasClampArg =
index < numArgs && expr->getArg(index)->getType()->isFloatingType();
if (hasClampArg) {
*clamp = doExpr(expr->getArg(index));
index++;
}

const bool hasStatusArg =
index < numArgs &&
expr->getArg(index)->getType()->isUnsignedIntegerType();
if (hasStatusArg) {
*status = doExpr(expr->getArg(index));
}
}

SpirvInstruction *
SpirvEmitter::processTextureSampleGather(const CXXMemberCallExpr *expr,
const bool isSample) {
Expand Down Expand Up @@ -5782,32 +5811,20 @@ SpirvEmitter::processTextureSampleCmpBias(const CXXMemberCallExpr *expr) {
// [, out uint Status]
// );

const auto numArgs = expr->getNumArgs();
const bool hasStatusArg =
expr->getArg(numArgs - 1)->getType()->isUnsignedIntegerType();
auto *status = hasStatusArg ? doExpr(expr->getArg(numArgs - 1)) : nullptr;

SpirvInstruction *clamp = nullptr;
if (numArgs > 4 && expr->getArg(4)->getType()->isFloatingType())
clamp = doExpr(expr->getArg(4));
else if (numArgs > 5 && expr->getArg(5)->getType()->isFloatingType())
clamp = doExpr(expr->getArg(5));
const bool hasClampArg = clamp != nullptr;

const auto *imageExpr = expr->getImplicitObjectArgument();
auto *image = loadIfGLValue(imageExpr);

auto *sampler = doExpr(expr->getArg(0));
auto *coordinate = doExpr(expr->getArg(1));
auto *bias = doExpr(expr->getArg(2));
auto *compareVal = doExpr(expr->getArg(3));
// If offset is present in .SampleCmpBias(), it will be the fifth argument.

SpirvInstruction *constOffset = nullptr, *varOffset = nullptr;
SpirvInstruction *clamp = nullptr;
SpirvInstruction *status = nullptr;

// Subtract 1 for clamp (if it exists), 1 for status (if it exists),
// and 4 for sampler_state, location, bias, and compare_value.
const bool hasOffsetArg = numArgs - hasStatusArg - hasClampArg - 4 > 0;
if (hasOffsetArg)
handleOffsetInMethodCall(expr, 4, &constOffset, &varOffset);
handleOptionalTextureSampleArgs(expr, 4, &constOffset, &varOffset, &clamp,
&status);

const auto retType = expr->getDirectCallee()->getReturnType();
const auto imageType = imageExpr->getType();
Expand Down Expand Up @@ -5845,34 +5862,22 @@ SpirvEmitter::processTextureSampleCmpGrad(const CXXMemberCallExpr *expr) {
// [, float Clamp]
// [, out uint Status]);

const auto numArgs = expr->getNumArgs();
const bool hasStatusArg =
expr->getArg(numArgs - 1)->getType()->isUnsignedIntegerType();
auto *status = hasStatusArg ? doExpr(expr->getArg(numArgs - 1)) : nullptr;

SpirvInstruction *clamp = nullptr;
if (numArgs > 5 && expr->getArg(5)->getType()->isFloatingType())
clamp = doExpr(expr->getArg(5));
else if (numArgs > 6 && expr->getArg(6)->getType()->isFloatingType())
clamp = doExpr(expr->getArg(6));
const bool hasClampArg = clamp != nullptr;

// Subtract 1 for clamp (if it exists), 1 for status (if it exists),
// and 5 for sampler_state, location, DDX, DDY, and compare_value;
const bool hasOffsetArg = numArgs - hasClampArg - hasStatusArg - 5 > 0;

const auto *imageExpr = expr->getImplicitObjectArgument();
const QualType imageType = imageExpr->getType();
auto *image = loadIfGLValue(imageExpr);

auto *sampler = doExpr(expr->getArg(0));
auto *coordinate = doExpr(expr->getArg(1));
auto *compareVal = doExpr(expr->getArg(2));
auto *ddx = doExpr(expr->getArg(3));
auto *ddy = doExpr(expr->getArg(4));
// If offset is present in .SampleGrad(), it is the sixth argument.

SpirvInstruction *constOffset = nullptr, *varOffset = nullptr;
if (hasOffsetArg)
handleOffsetInMethodCall(expr, 5, &constOffset, &varOffset);
SpirvInstruction *clamp = nullptr;
SpirvInstruction *status = nullptr;

handleOptionalTextureSampleArgs(expr, 5, &constOffset, &varOffset, &clamp,
&status);

const auto retType = expr->getDirectCallee()->getReturnType();
return createImageSample(
Expand Down
7 changes: 7 additions & 0 deletions tools/clang/lib/SPIRV/SpirvEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,13 @@ class SpirvEmitter : public ASTConsumer {
SpirvInstruction **constOffset,
SpirvInstruction **varOffset);

void handleOptionalTextureSampleArgs(const CXXMemberCallExpr *expr,
uint32_t index,
SpirvInstruction **constOffset,
SpirvInstruction **varOffset,
SpirvInstruction **clamp,
SpirvInstruction **status);

/// \brief Processes .Load() method call for Buffer/RWBuffer and texture
/// objects.
SpirvInstruction *processBufferTextureLoad(const CXXMemberCallExpr *);
Expand Down
8 changes: 4 additions & 4 deletions tools/clang/test/CodeGenSPIRV/texture.sample-cmp-bias.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ void main() {
// CHECK-NEXT: {{%[0-9]+}} = OpImageSampleDrefImplicitLod %float [[sampledImg]] [[v2fc]] [[cmpVal]] Bias|ConstOffset [[bias]] %int_n5
float val2 = t1_array.SampleCmpBias(s, float2(1, 2), bias, cmpVal, -5);

// CHECK: [[clamp:%[0-9]+]] = OpLoad %float %clamp
// CHECK-NEXT: [[t2:%[0-9]+]] = OpLoad %type_2d_image %t2
// CHECK: [[t2:%[0-9]+]] = OpLoad %type_2d_image %t2
// CHECK-NEXT: [[sampler:%[0-9]+]] = OpLoad %type_sampler %s
// CHECK-NEXT: [[bias:%[0-9]+]] = OpLoad %float %bias
// CHECK-NEXT: [[cmpVal:%[0-9]+]] = OpLoad %float %cmpVal
// CHECK-NEXT: [[clamp:%[0-9]+]] = OpLoad %float %clamp
// CHECK-NEXT: [[sampledImg:%[0-9]+]] = OpSampledImage %type_sampled_image_1 [[t2]] [[sampler]]
// CHECK-NEXT: {{%[0-9]+}} = OpImageSampleDrefImplicitLod %float [[sampledImg]] [[v2fc]] [[cmpVal]] Bias|ConstOffset|MinLod [[bias]] [[v2ic]] [[clamp]]
float val3 = t2.SampleCmpBias(s, float2(1, 2), bias, cmpVal, uint2(-5, 7), clamp);

uint status;

// CHECK: [[clamp:%[0-9]+]] = OpLoad %float %clamp
// CHECK-NEXT: [[tcube:%[0-9]+]] = OpLoad %type_cube_image %tcube
// CHECK: [[tcube:%[0-9]+]] = OpLoad %type_cube_image %tcube
// CHECK-NEXT: [[sampler:%[0-9]+]] = OpLoad %type_sampler %s
// CHECK-NEXT: [[bias:%[0-9]+]] = OpLoad %float %bias
// CHECK-NEXT: [[cmpVal:%[0-9]+]] = OpLoad %float %cmpVal
// CHECK-NEXT: [[clamp:%[0-9]+]] = OpLoad %float %clamp
// CHECK-NEXT: [[sampledImg:%[0-9]+]] = OpSampledImage %type_sampled_image_2 [[tcube]] [[sampler]]
// CHECK-NEXT: [[structResult:%[0-9]+]] = OpImageSparseSampleDrefImplicitLod %SparseResidencyStruct [[sampledImg]] [[v3fc]] [[cmpVal]] Bias|MinLod [[bias]] [[clamp]]
// CHECK-NEXT: [[status:%[0-9]+]] = OpCompositeExtract %uint [[structResult]] 0
Expand Down
9 changes: 5 additions & 4 deletions tools/clang/test/CodeGenSPIRV/texture.sample-cmp-grad.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,21 @@ void main() {
// CHECK-NEXT: {{%[0-9]+}} = OpImageSampleDrefExplicitLod %float [[sampledImg]] [[v2fc]] [[cmpVal]] Grad|ConstOffset %float_2 %float_3 %int_n5
float val2 = t1_array.SampleCmpGrad(s, float2(1, 2), cmpVal, 2, 3, -5);

// CHECK: [[clamp:%[0-9]+]] = OpLoad %float %clamp
// CHECK-NEXT: [[t2:%[0-9]+]] = OpLoad %type_2d_image %t2

// CHECK: [[t2:%[0-9]+]] = OpLoad %type_2d_image %t2
// CHECK-NEXT: [[sampler:%[0-9]+]] = OpLoad %type_sampler %s
// CHECK-NEXT: [[cmpVal:%[0-9]+]] = OpLoad %float %cmpVal
// CHECK-NEXT: [[clamp:%[0-9]+]] = OpLoad %float %clamp
// CHECK-NEXT: [[sampledImg:%[0-9]+]] = OpSampledImage %type_sampled_image_1 [[t2]] [[sampler]]
// CHECK-NEXT: {{%[0-9]+}} = OpImageSampleDrefExplicitLod %float [[sampledImg]] [[v2fc]] [[cmpVal]] Grad|ConstOffset|MinLod [[v2f_2]] [[v2f_3]] [[v2ic]] [[clamp]]
float val3 = t2.SampleCmpGrad(s, float2(1, 2), cmpVal, float2(2, 2), float2(3, 3), uint2(-5, 7), clamp);

uint status;

// CHECK: [[clamp:%[0-9]+]] = OpLoad %float %clamp
// CHECK-NEXT: [[tcube:%[0-9]+]] = OpLoad %type_cube_image %tcube
// CHECK: [[tcube:%[0-9]+]] = OpLoad %type_cube_image %tcube
// CHECK-NEXT: [[sampler:%[0-9]+]] = OpLoad %type_sampler %s
// CHECK-NEXT: [[cmpVal:%[0-9]+]] = OpLoad %float %cmpVal
// CHECK-NEXT: [[clamp:%[0-9]+]] = OpLoad %float %clamp
// CHECK-NEXT: [[sampledImg:%[0-9]+]] = OpSampledImage %type_sampled_image_2 [[tcube]] [[sampler]]
// CHECK-NEXT: [[structResult:%[0-9]+]] = OpImageSparseSampleDrefExplicitLod %SparseResidencyStruct [[sampledImg]] [[v3fc]] [[cmpVal]] Grad|MinLod [[v3f_1]] [[v3f_2]] [[clamp]]
// CHECK-NEXT: [[status:%[0-9]+]] = OpCompositeExtract %uint [[structResult]] 0
Expand Down

0 comments on commit 9de329f

Please sign in to comment.