-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[HLSL][DXIL] Implement refract
intrinsic
#147342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2b5c513
7feddb7
aaa8f23
70646d2
9b0ccaf
b46a32f
390a4c7
478ef94
76b6c7f
dbb1dda
2f7d9ce
76950bd
78fcafd
2a7720c
199bda9
86d2f84
17d1fbb
7bdad40
39323c1
729fbf3
322bb3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,59 @@ static bool CheckAllArgsHaveSameType(Sema *S, CallExpr *TheCall) { | |
return false; | ||
} | ||
|
||
static bool CheckAllArgTypesAreCorrect( | ||
Sema *S, CallExpr *TheCall, | ||
llvm::ArrayRef< | ||
llvm::function_ref<bool(Sema *, SourceLocation, int, QualType)>> | ||
Checks) { | ||
unsigned NumArgs = TheCall->getNumArgs(); | ||
assert(Checks.size() == NumArgs && | ||
"Wrong number of checks for Number of args."); | ||
// Apply each check to the corresponding argument | ||
for (unsigned I = 0; I < NumArgs; ++I) { | ||
Expr *Arg = TheCall->getArg(I); | ||
if (Checks[I](S, Arg->getBeginLoc(), I + 1, Arg->getType())) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
static bool CheckAllArgTypesAreCorrect( | ||
raoanag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Sema *S, CallExpr *TheCall, | ||
llvm::function_ref<bool(Sema *, SourceLocation, int, QualType)> Check) { | ||
return CheckAllArgTypesAreCorrect( | ||
S, TheCall, | ||
SmallVector< | ||
llvm::function_ref<bool(Sema *, SourceLocation, int, QualType)>, 4>( | ||
TheCall->getNumArgs(), Check)); | ||
} | ||
|
||
static bool CheckFloatOrHalfRepresentation(Sema *S, SourceLocation Loc, | ||
int ArgOrdinal, | ||
clang::QualType PassedType) { | ||
clang::QualType BaseType = | ||
PassedType->isVectorType() | ||
? PassedType->castAs<clang::VectorType>()->getElementType() | ||
: PassedType; | ||
if (!BaseType->isHalfType() && !BaseType->isFloat32Type()) | ||
return S->Diag(Loc, diag::err_builtin_invalid_arg_type) | ||
<< ArgOrdinal << /* scalar or vector of */ 5 << /* no int */ 0 | ||
<< /* half or float */ 2 << PassedType; | ||
return false; | ||
} | ||
|
||
static bool CheckFloatOrHalfScalarRepresentation(Sema *S, SourceLocation Loc, | ||
int ArgOrdinal, | ||
clang::QualType PassedType) { | ||
const auto *VecTy = PassedType->getAs<VectorType>(); | ||
|
||
if (VecTy || (!PassedType->isHalfType() && !PassedType->isFloat32Type())) | ||
raoanag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return S->Diag(Loc, diag::err_builtin_invalid_arg_type) | ||
<< ArgOrdinal << /* scalar */ 1 << /* no int */ 0 | ||
<< /* half or float */ 2 << PassedType; | ||
return false; | ||
} | ||
|
||
static std::optional<int> | ||
processConstant32BitIntArgument(Sema &SemaRef, CallExpr *Call, int Argument) { | ||
ExprResult Arg = | ||
|
@@ -235,6 +288,30 @@ bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(const TargetInfo &TI, | |
TheCall->setType(RetTy); | ||
break; | ||
} | ||
case SPIRV::BI__builtin_spirv_refract: { | ||
if (SemaRef.checkArgCount(TheCall, 3)) | ||
return true; | ||
|
||
llvm::function_ref<bool(Sema *, SourceLocation, int, QualType)> | ||
ChecksArr[] = {CheckFloatOrHalfRepresentation, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused on if this is meant to handle scalar values as well as vectors? Looking at the code gen, we are asserting that the first two arguments are vectors, but here we allow them to be scalars. @farzonl Does this handle only the case where the first two arguments are vectors? If that is the case 'CheckFloatOrHalfRepresentation' should be updated to only check for vectors of half or float and should probably be renamed to 'CheckFloatOrHalfVecRepresentation'. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to support vector of size 1, which is implicitly converted to scalar. Hence, even though first 2 args are described as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think your explanation is slightly incorrect, but it does seem the __builtin_spirv_refract is reachable with a scalar value. In this case the codegen assertions are wrong and I will leave a comment there about updating them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’ll review this tomorrow, but supporting scalars here seems wrong. I’m almost 100% sure that spirv only supports vectors and that our semantics should match that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Refract spirv op says both scalar and vector are supported. https://registry.khronos.org/SPIR-V/specs/unified1/GLSL.std.450.pdf (search for Refract). |
||
CheckFloatOrHalfRepresentation, | ||
CheckFloatOrHalfScalarRepresentation}; | ||
if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall, | ||
llvm::ArrayRef(ChecksArr))) | ||
return true; | ||
|
||
ExprResult C = TheCall->getArg(2); | ||
QualType ArgTyC = C.get()->getType(); | ||
if (!ArgTyC->isFloatingType()) { | ||
raoanag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SemaRef.Diag(C.get()->getBeginLoc(), diag::err_builtin_invalid_arg_type) | ||
<< 3 << /* scalar*/ 5 << /* no int */ 0 << /* fp */ 1 << ArgTyC; | ||
return true; | ||
} | ||
|
||
QualType RetTy = TheCall->getArg(0)->getType(); | ||
TheCall->setType(RetTy); | ||
break; | ||
} | ||
case SPIRV::BI__builtin_spirv_smoothstep: { | ||
if (SemaRef.checkArgCount(TheCall, 3)) | ||
return true; | ||
|
Uh oh!
There was an error while loading. Please reload this page.