@@ -4551,44 +4551,60 @@ SpirvInstruction *SpirvEmitter::processTextureGatherRGBACmpRGBA(
45514551 // * SamplerState s, float2 location, float compare_value, out uint status
45524552 //
45534553 // Return type is always a 4-component vector.
4554+ //
4555+ // SampledTexture variants have the same signatures without the SamplerState
4556+ // parameter.
45544557 const FunctionDecl *callee = expr->getDirectCallee();
45554558 const auto numArgs = expr->getNumArgs();
45564559 const auto *imageExpr = expr->getImplicitObjectArgument();
45574560 const auto loc = expr->getCallee()->getExprLoc();
45584561 const QualType imageType = imageExpr->getType();
45594562 const QualType retType = callee->getReturnType();
4563+ const bool isImageSampledTexture = isSampledTexture(imageType);
45604564
45614565 // If the last arg is an unsigned integer, it must be the status.
45624566 const bool hasStatusArg =
45634567 expr->getArg(numArgs - 1)->getType()->isUnsignedIntegerType();
45644568
45654569 // Subtract 1 for status arg (if it exists), subtract 1 for compare_value (if
45664570 // it exists), and subtract 2 for SamplerState and location.
4567- const auto numOffsetArgs = numArgs - hasStatusArg - isCmp - 2;
4571+ int offsetStartIndex = (isImageSampledTexture ? 1 : 2) + isCmp;
4572+ const auto numOffsetArgs = numArgs - hasStatusArg - offsetStartIndex;
45684573 // No offset args for TextureCube, 1 or 4 offset args for the rest.
45694574 assert(numOffsetArgs == 0 || numOffsetArgs == 1 || numOffsetArgs == 4);
45704575
4576+ int samplerIndex, coordIndex, compareValIndex;
4577+ if (isImageSampledTexture) {
4578+ samplerIndex = -1; // non-existant
4579+ coordIndex = 0;
4580+ compareValIndex = 1;
4581+ } else {
4582+ samplerIndex = 0;
4583+ coordIndex = 1;
4584+ compareValIndex = 2;
4585+ }
45714586 auto *image = loadIfGLValue(imageExpr);
4572- auto *sampler = doExpr(expr->getArg(0));
4573- auto *coordinate = doExpr(expr->getArg(1));
4574- auto *compareVal = isCmp ? doExpr(expr->getArg(2)) : nullptr;
4587+ auto *sampler =
4588+ samplerIndex >= 0 ? doExpr(expr->getArg(samplerIndex)) : nullptr;
4589+ auto *coordinate = doExpr(expr->getArg(coordIndex));
4590+ auto *compareVal = isCmp ? doExpr(expr->getArg(compareValIndex)) : nullptr;
45754591
45764592 // Handle offsets (if any).
45774593 bool needsEmulation = false;
45784594 SpirvInstruction *constOffset = nullptr, *varOffset = nullptr,
45794595 *constOffsets = nullptr;
45804596 if (numOffsetArgs == 1) {
45814597 // The offset arg is not optional.
4582- handleOffsetInMethodCall(expr, 2 + isCmp , &constOffset, &varOffset);
4598+ handleOffsetInMethodCall(expr, offsetStartIndex , &constOffset, &varOffset);
45834599 } else if (numOffsetArgs == 4) {
4584- auto *offset0 = constEvaluator.tryToEvaluateAsConst(expr->getArg(2 + isCmp),
4585- isSpecConstantMode);
4586- auto *offset1 = constEvaluator.tryToEvaluateAsConst(expr->getArg(3 + isCmp),
4587- isSpecConstantMode);
4588- auto *offset2 = constEvaluator.tryToEvaluateAsConst(expr->getArg(4 + isCmp),
4589- isSpecConstantMode);
4590- auto *offset3 = constEvaluator.tryToEvaluateAsConst(expr->getArg(5 + isCmp),
4591- isSpecConstantMode);
4600+ auto *offset0 = constEvaluator.tryToEvaluateAsConst(
4601+ expr->getArg(offsetStartIndex), isSpecConstantMode);
4602+ auto *offset1 = constEvaluator.tryToEvaluateAsConst(
4603+ expr->getArg(offsetStartIndex + 1), isSpecConstantMode);
4604+ auto *offset2 = constEvaluator.tryToEvaluateAsConst(
4605+ expr->getArg(offsetStartIndex + 2), isSpecConstantMode);
4606+ auto *offset3 = constEvaluator.tryToEvaluateAsConst(
4607+ expr->getArg(offsetStartIndex + 3), isSpecConstantMode);
45924608
45934609 // If any of the offsets is not constant, we then need to emulate the call
45944610 // using 4 OpImageGather instructions. Otherwise, we can leverage the
@@ -4611,7 +4627,7 @@ SpirvInstruction *SpirvEmitter::processTextureGatherRGBACmpRGBA(
46114627
46124628 SpirvInstruction *texels[4];
46134629 for (uint32_t i = 0; i < 4; ++i) {
4614- varOffset = doExpr(expr->getArg(2 + isCmp + i));
4630+ varOffset = doExpr(expr->getArg(offsetStartIndex + i));
46154631 auto *gatherRet = spvBuilder.createImageGather(
46164632 retType, imageType, image, sampler, coordinate,
46174633 spvBuilder.getConstantInt(astContext.IntTy,
@@ -4656,25 +4672,44 @@ SpirvEmitter::processTextureGatherCmp(const CXXMemberCallExpr *expr) {
46564672 // );
46574673 //
46584674 // Other Texture types do not have the GatherCmp method.
4675+ //
4676+ // SampledTexture variants have the same signatures without the SamplerState
4677+ // parameter.
46594678
46604679 const FunctionDecl *callee = expr->getDirectCallee();
46614680 const auto numArgs = expr->getNumArgs();
46624681 const auto loc = expr->getExprLoc();
46634682 const bool hasStatusArg =
46644683 expr->getArg(numArgs - 1)->getType()->isUnsignedIntegerType();
4665- const bool hasOffsetArg = (numArgs == 5) || (numArgs == 4 && !hasStatusArg);
4666-
46674684 const auto *imageExpr = expr->getImplicitObjectArgument();
4685+
4686+ const QualType imageType = imageExpr->getType();
4687+ const bool isImageSampledTexture = isSampledTexture(imageType);
4688+
4689+ int samplerIndex, coordIndex, compareValIndex;
4690+ if (isImageSampledTexture) {
4691+ samplerIndex = -1; // non-existant
4692+ coordIndex = 0;
4693+ compareValIndex = 1;
4694+ } else {
4695+ samplerIndex = 0;
4696+ coordIndex = 1;
4697+ compareValIndex = 2;
4698+ }
4699+
46684700 auto *image = loadIfGLValue(imageExpr);
4669- auto *sampler = doExpr(expr->getArg(0));
4670- auto *coordinate = doExpr(expr->getArg(1));
4671- auto *comparator = doExpr(expr->getArg(2));
4701+ auto *sampler =
4702+ samplerIndex >= 0 ? doExpr(expr->getArg(samplerIndex)) : nullptr;
4703+ auto *coordinate = doExpr(expr->getArg(coordIndex));
4704+ auto *comparator = doExpr(expr->getArg(compareValIndex));
4705+
4706+ const bool hasOffsetArg = numArgs - hasStatusArg - compareValIndex > 1;
46724707 SpirvInstruction *constOffset = nullptr, *varOffset = nullptr;
46734708 if (hasOffsetArg)
4674- handleOffsetInMethodCall(expr, 3, &constOffset, &varOffset);
4709+ handleOffsetInMethodCall(expr, compareValIndex + 1, &constOffset,
4710+ &varOffset);
46754711
46764712 const auto retType = callee->getReturnType();
4677- const auto imageType = imageExpr->getType();
46784713 const auto status =
46794714 hasStatusArg ? doExpr(expr->getArg(numArgs - 1)) : nullptr;
46804715
0 commit comments