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

Metal: Texture write fix #4952

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

Dynamitos
Copy link
Contributor

In Metal, there is no overload for different component counts of textures, only 4-component writes are allowed. For textures that do not have four components the unused components get discarded, as far as I can gather from the specs.

@Dynamitos Dynamitos marked this pull request as draft August 28, 2024 19:54
@@ -1797,6 +1797,21 @@ namespace Slang
}
}
}
for(auto inst : block->getChildren())
{
if(auto write = as<IRImageStore>(inst))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we move this if statement to the loop above, instead of doing a fresh loop?

@Dynamitos
Copy link
Contributor Author

for now i implemented a split between ImageStore and ImageStoreArray, because i cant split the array component from the coordinate in hlsl.meta.slang, but also i dont know when to split it off in the legalize because i dont know if the target texture is an array texture or not. this way if the swizzle does work in the future the entire ImageStoreArray can be deleted and the array coordinate passed the aux coord in ImageStore

@Dynamitos Dynamitos marked this pull request as ready for review September 12, 2024 08:30
@csyonghe
Copy link
Collaborator

csyonghe commented Sep 12, 2024

You should be able to check the type of the image operand to get its shape and arrayness.

auto type = imageStoreInst->getOperand(0)->getDataType();
auto imageType = as<IRTextureType>(type);
imageType->xxx //get info about the texture type.

@Dynamitos
Copy link
Contributor Author

Now a much better solution, not needing the ImageStoreArray anymore, but IMO it would be a bit better to swizzle the location in the hlsl.meta.slang instead if doing it in the legalize step

break;
}
// last arg will be replaced with the split off array index
__metalImageStore(this, location, newValue, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you should be able to do this completely in stdlib without implementing any logic for kIROp_ImageStore in the compiler backend.

[require(metal, texture_sm_4_1)]
static void __metalImageStore(This val, vector<uint, Shape.dimensions> location, T value, uint arrayIndex)
{
     __intrinsic_asm "...";
}

//...
case metal:
    __metalImageStore(this, __vectorReshape<Shape.dimensions>(location), newValue, location[Shape.dimensions+isArray-1]);
     return;

@@ -1740,6 +1740,46 @@ namespace Slang
}
};

// metal textures only support writing 4-component values, even if the texture is only 1, 2, or 3-component
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should be able to use vectorReshape<> on the value to write into in stdlib as well? That should remove the need for this leglaization step, right?

@csyonghe
Copy link
Collaborator

@Dynamitos Do you plan to clean up the image store inst with vectorShape to change the incoming value into float4?
If not we can also just merge the PR as is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants