-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[HLSL][DXIL] Implement refract
intrinsic
#136026
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
6ce233a
83d69dd
83c4f5a
1853e3d
dff5181
3d87d6d
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
#define _HLSL_HLSL_INTRINSIC_HELPERS_H_ | ||
|
||
namespace hlsl { | ||
namespace __detail { | ||
namespace __dETAil { | ||
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. this looks like you did find replace for eta. Please revert this change. In the future you can use clangd' vscode plugin to apply llvm c++'s stye guide. |
||
|
||
constexpr vector<uint, 4> d3d_color_to_ubyte4_impl(vector<float, 4> V) { | ||
// Use the same scaling factor used by FXC, and DXC for DXIL | ||
|
@@ -71,6 +71,27 @@ constexpr vector<T, L> reflect_vec_impl(vector<T, L> I, vector<T, L> N) { | |
#endif | ||
} | ||
|
||
template <typename T> constexpr T refract_impl(T I, T N, T Eta) { | ||
T K = 1 - Eta * Eta * (1 - (N * I * N * I)); | ||
if (K < 0) | ||
return 0; | ||
else | ||
return (Eta * I - (Eta * N * I + sqrt(K)) * N); | ||
} | ||
|
||
template <typename T, int L> | ||
constexpr vector<T, L> refract_vec_impl(vector<T, L> I, vector<T, L> N, T Eta) { | ||
#if (__has_builtin(__builtin_spirv_refract)) | ||
return __builtin_spirv_refract(I, N, Eta); | ||
#else | ||
vector<T, L> K = 1 - Eta * Eta * (1 - dot(N, I) * dot(N, I)); | ||
if (K < 0) | ||
return 0; | ||
else | ||
return (Eta * I - (Eta * dot(N, I) + sqrt(K)) * N); | ||
#endif | ||
} | ||
|
||
template <typename T> constexpr T fmod_impl(T X, T Y) { | ||
#if !defined(__DIRECTX__) | ||
return __builtin_elementwise_fmod(X, Y); | ||
|
@@ -126,7 +147,7 @@ template <typename T> constexpr vector<T, 4> lit_impl(T NDotL, T NDotH, T M) { | |
return Result; | ||
} | ||
|
||
} // namespace __detail | ||
} // namespace __dETAil | ||
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. fix this too |
||
} // namespace hlsl | ||
|
||
#endif // _HLSL_HLSL_INTRINSIC_HELPERS_H_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(unsigned BuiltinID, | |
QualType ArgTyB = B.get()->getType(); | ||
auto *VTyB = ArgTyB->getAs<VectorType>(); | ||
if (VTyB == nullptr) { | ||
SemaRef.Diag(A.get()->getBeginLoc(), | ||
SemaRef.Diag(B.get()->getBeginLoc(), | ||
diag::err_typecheck_convert_incompatible) | ||
<< ArgTyB | ||
<< SemaRef.Context.getVectorType(ArgTyB, 2, VectorKind::Generic) << 1 | ||
|
@@ -69,6 +69,47 @@ bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(unsigned BuiltinID, | |
TheCall->setType(RetTy); | ||
break; | ||
} | ||
case SPIRV::BI__builtin_spirv_refract: { | ||
if (SemaRef.checkArgCount(TheCall, 3)) | ||
return true; | ||
|
||
ExprResult A = TheCall->getArg(0); | ||
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. you could do |
||
QualType ArgTyA = A.get()->getType(); | ||
auto *VTyA = ArgTyA->getAs<VectorType>(); | ||
if (VTyA == nullptr) { | ||
SemaRef.Diag(A.get()->getBeginLoc(), | ||
diag::err_typecheck_convert_incompatible) | ||
<< ArgTyA | ||
<< SemaRef.Context.getVectorType(ArgTyA, 2, VectorKind::Generic) << 1 | ||
<< 0 << 0; | ||
return true; | ||
} | ||
|
||
ExprResult B = TheCall->getArg(1); | ||
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. Maybe this should be a helper function since reflect is doing the same thing. |
||
QualType ArgTyB = B.get()->getType(); | ||
auto *VTyB = ArgTyB->getAs<VectorType>(); | ||
if (VTyB == nullptr) { | ||
SemaRef.Diag(B.get()->getBeginLoc(), | ||
diag::err_typecheck_convert_incompatible) | ||
<< ArgTyB | ||
<< SemaRef.Context.getVectorType(ArgTyB, 2, VectorKind::Generic) << 1 | ||
<< 0 << 0; | ||
return true; | ||
} | ||
|
||
ExprResult C = TheCall->getArg(2); | ||
QualType ArgTyC = C.get()->getType(); | ||
if (!ArgTyC->hasFloatingRepresentation()) { | ||
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.
|
||
SemaRef.Diag(C.get()->getBeginLoc(), diag::err_builtin_invalid_arg_type) | ||
<< 3 << /* scalar or vector */ 5 << /* no int */ 0 << /* fp */ 1 | ||
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. this wouldn't be scalar or vector needs to just be scalar float\half. |
||
<< ArgTyC; | ||
return true; | ||
} | ||
|
||
QualType RetTy = ArgTyA; | ||
TheCall->setType(RetTy); | ||
break; | ||
} | ||
case SPIRV::BI__builtin_spirv_reflect: { | ||
if (SemaRef.checkArgCount(TheCall, 2)) | ||
return true; | ||
|
@@ -89,7 +130,7 @@ bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(unsigned BuiltinID, | |
QualType ArgTyB = B.get()->getType(); | ||
auto *VTyB = ArgTyB->getAs<VectorType>(); | ||
if (VTyB == nullptr) { | ||
SemaRef.Diag(A.get()->getBeginLoc(), | ||
SemaRef.Diag(B.get()->getBeginLoc(), | ||
raoanag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
diag::err_typecheck_convert_incompatible) | ||
<< ArgTyB | ||
<< SemaRef.Context.getVectorType(ArgTyB, 2, VectorKind::Generic) << 1 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should just check
E->getArg(2)->getType()
is a float or half scalar might be worth putting this in its own assert so you can have a different message.