-
Notifications
You must be signed in to change notification settings - Fork 187
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
Merged
csyonghe
merged 28 commits into
shader-slang:master
from
Dynamitos:feature/metal-texture-write
Oct 9, 2024
Merged
Metal: Texture write fix #4952
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
54bc2cb
Metal: starting work to fix texture writes
Dynamitos 92c0b0f
Hacky changes to metal texture write
Dynamitos 4e0b871
Even more hacky texture write fix
Dynamitos 0bcb27c
Merge branch 'master' into feature/metal-texture-write
Dynamitos 449ab10
Much better solution to metal texture write
Dynamitos b3e5aa9
Merge branch 'master' into feature/metal-texture-write
Dynamitos 2c741bc
Using vector reshape to separate array indices
Dynamitos a845740
Merge branch 'master' into feature/metal-texture-write
Dynamitos 5a96c65
Merge branch 'master' into feature/metal-texture-write
Dynamitos 7d449ae
Merge branch 'master' into feature/metal-texture-write
Dynamitos ae094b1
Merge branch 'master' into feature/metal-texture-write
csyonghe bfc4185
Merge remote-tracking branch 'upstream/master' into feature/metal-tex…
Dynamitos 3040948
Merge branch 'master' into feature/metal-texture-write
Dynamitos d4d54b9
Fixing imagestore operand count
Dynamitos e56e62b
Merge branch 'master' into feature/metal-texture-write
Dynamitos 4a3659f
Merge branch 'master' into feature/metal-texture-write
Dynamitos 5886e4e
Merge branch 'master' into feature/metal-texture-write
jkwak-work 1ade83d
Removing array indices from non-array writes
Dynamitos a77d50b
Merge remote-tracking branch 'origin/feature/metal-texture-write' int…
Dynamitos a1b0adc
Merge branch 'master' into feature/metal-texture-write
Dynamitos 519724b
Removing unused metallib test
Dynamitos 743a651
Merge branch 'master' into feature/metal-texture-write
Dynamitos 373bb9b
Adding metallib checks
Dynamitos 32f9b7c
Merge branch 'master' into feature/metal-texture-write
Dynamitos 9be4e07
Merge branch 'master' into feature/metal-texture-write
Dynamitos 8ca17f6
Merge branch 'master' into feature/metal-texture-write
csyonghe 60a6a3e
Merge branch 'master' into feature/metal-texture-write
csyonghe 0d2433e
Merge branch 'master' into feature/metal-texture-write
csyonghe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
// in this case the other channels get ignored, but the signature still doesnt match | ||
// so now we have to replace the value being written with a 4-component vector where | ||
// the new components get ignored, nice | ||
void legalizeImageStoreValue(IRBuilder& builder, IRImageStore* imageStore) | ||
{ | ||
builder.setInsertBefore(imageStore); | ||
auto originalValue = imageStore->getValue(); | ||
auto valueBaseType = originalValue->getDataType(); | ||
IRType* elementType = nullptr; | ||
List<IRInst*> components; | ||
if(auto valueVectorType = as<IRVectorType>(valueBaseType)) | ||
{ | ||
if(auto originalElementCount = as<IRIntLit>(valueVectorType->getElementCount())) | ||
{ | ||
if(originalElementCount->getValue() == 4) | ||
{ | ||
return; | ||
} | ||
} | ||
elementType = valueVectorType->getElementType(); | ||
auto vectorValue = as<IRMakeVector>(originalValue); | ||
for(UInt i = 0; i < vectorValue->getOperandCount(); i++) | ||
{ | ||
components.add(vectorValue->getOperand(i)); | ||
} | ||
} | ||
else | ||
{ | ||
elementType = valueBaseType; | ||
components.add(originalValue); | ||
} | ||
for(UInt i = components.getCount(); i < 4; i++) | ||
{ | ||
components.add(builder.getIntValue(builder.getIntType(), 0)); | ||
} | ||
auto fourComponentVectorType = builder.getVectorType(elementType, 4); | ||
imageStore->setOperand(2, builder.emitMakeVector(fourComponentVectorType, components)); | ||
} | ||
|
||
void legalizeFuncBody(IRFunc* func) | ||
{ | ||
IRBuilder builder(func); | ||
|
@@ -1796,6 +1836,10 @@ namespace Slang | |
arg->set(temp); | ||
} | ||
} | ||
if(auto write = as<IRImageStore>(inst)) | ||
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. Can we move this if statement to the loop above, instead of doing a fresh loop? |
||
{ | ||
legalizeImageStoreValue(builder, write); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//TEST:SIMPLE(filecheck=METAL): -stage compute -entry computeMain -target metal -DEMIT_SOURCE | ||
//TEST:SIMPLE(filecheck=METALLIB): -stage compute -entry computeMain -target metallib | ||
// for some reason, metal textures dont have an overload for less-than-four component | ||
// writes, they need to be converted to 4-components in a legalize step, as the other components | ||
// get discarded | ||
struct TextureWrite | ||
{ | ||
//TEST_INPUT: RWTexture2D(size=4, content = zero):name t2D_f32 | ||
RWTexture2D<float> tex1; | ||
//TEST_INPUT: RWTexture2D(size=4, content = zero):name t2D_f32v2 | ||
RWTexture2D<float2> tex2; | ||
//TEST_INPUT: RWTexture2D(size=4, content = zero):name t2D_f32v3 | ||
RWTexture2D<float3> tex3; | ||
//TEST_INPUT: RWTexture2D(size=4, content = zero):name t2D_f32v4 | ||
RWTexture2D<float4> tex4; | ||
|
||
//TEST_INPUT: RWTexture2DArray(size=4, content = zero):name t2D_f32 | ||
RWTexture2DArray<float> tex1Array; | ||
//TEST_INPUT: RWTexture2DArray(size=4, content = zero):name t2D_f32v2 | ||
RWTexture2DArray<float2> tex2Array; | ||
//TEST_INPUT: RWTexture2DArray(size=4, content = zero):name t2D_f32v3 | ||
RWTexture2DArray<float3> tex3Array; | ||
//TEST_INPUT: RWTexture2DArray(size=4, content = zero):name t2D_f32v4 | ||
RWTexture2DArray<float4> tex4Array; | ||
} | ||
ParameterBlock<TextureWrite> pWrites; | ||
|
||
[numthreads(1, 1, 1)] | ||
void computeMain() | ||
{ | ||
// TODO: check for the type of first parameter to be a 4-component vector | ||
// METALLIB: call {{.*}}.write_texture_2d.v4f32( | ||
// METAL: .write( | ||
pWrites.tex1[uint2(1, 1)] = 1; | ||
// METALLIB: call {{.*}}.write_texture_2d.v4f32( | ||
// METAL: .write( | ||
pWrites.tex2[uint2(2, 2)] = float2(1, 2); | ||
// METALLIB: call {{.*}}.write_texture_2d.v4f32( | ||
// METAL: .write( | ||
pWrites.tex3[uint2(3, 3)] = float3(1, 2, 3); | ||
// METALLIB: call {{.*}}.write_texture_2d.v4f32( | ||
// METAL: .write( | ||
pWrites.tex4[uint2(4, 4)] = float4(1, 2, 3, 4); | ||
|
||
// METALLIB: call {{.*}}.write_texture_2d_array.v4f32( | ||
// METAL: .write( | ||
pWrites.tex1Array[uint3(1, 1, 1)] = 1; | ||
// METALLIB: call {{.*}}.write_texture_2d_array.v4f32( | ||
// METAL: .write( | ||
pWrites.tex2Array[uint3(2, 2, 2)] = float2(1, 2); | ||
// METALLIB: call {{.*}}.write_texture_2d_array.v4f32( | ||
// METAL: .write( | ||
pWrites.tex3Array[uint3(3, 3, 3)] = float3(1, 2, 3); | ||
// METALLIB: call {{.*}}.write_texture_2d_array.v4f32( | ||
// METAL: .write( | ||
pWrites.tex4Array[uint3(4, 4, 4)] = float4(1, 2, 3, 4); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 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?