Skip to content
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

Add wgsl missing float image format inference #5716

Merged
merged 13 commits into from
Dec 5, 2024
34 changes: 34 additions & 0 deletions source/slang/slang-emit-wgsl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "slang-emit-wgsl.h"

#include "slang-ir-util.h"

// A note on row/column "terminology reversal".
//
// This is an "terminology reversing" implementation in the sense that
Expand Down Expand Up @@ -343,6 +345,38 @@ static const char* getWgslImageFormat(IRTextureTypeBase* type)
//
ImageFormat imageFormat =
type->hasFormat() ? (ImageFormat)type->getFormat() : ImageFormat::unknown;

if (imageFormat == ImageFormat::unknown)
Copy link
Collaborator

@jkwak-work jkwak-work Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the switch statement right after this if, there is a case for ImageFormat::unknown.
This code change could be placed inside of the switch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally had it inside the switch statement. Having it outside makes it a bit easier to mentally parse imo. It's the difference between

handle edge cases
do main operation

and

do main operation except in the case of edge cases where you handle them

{
// WGSL doesn't have a texel format for "unknown" so we try to infer float types that
// normally just resolve to unknown.
auto elementType = type->getElementType();
Int vectorWidth = 1;
if (auto vectorType = as<IRVectorType>(elementType);
auto intLitVal = as<IRIntLit>(vectorType->getElementCount()))
{
vectorWidth = intLitVal->getValue();
}
elementType = getVectorElementType((IRType*)elementType);
if (auto basicType = as<IRBasicType>(elementType))
{
switch (basicType->getBaseType())
{
case BaseType::Float:
switch (vectorWidth)
{
case 1:
return "r32float";
case 2:
return "rg32float";
case 4:
return "rgba32float";
}
break;
}
}
}

switch (imageFormat)
{
case ImageFormat::rgba8:
Expand Down