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 IRWArray interface, and make StructuredBuffer conform to them. #5097

Merged
merged 5 commits into from
Sep 18, 2024

Conversation

csyonghe
Copy link
Collaborator

@csyonghe csyonghe commented Sep 18, 2024

Closes #5098.
Closes #5099.

This adds IRWArray interface to generic functions to read/write arrays.

Makes StructuredBuffer conform to IArray, and RWStructuredBuffer conform to IRWArray.

Most part of this PR is about adding the ability to synthesize a __subscript implementation from an interface requirement.
For example,

interface IArray<T> { __subscript(int index) -> T {get;}}

struct Array<T> : IArray<T>
{
     // case 1:
      // compiler should be able to automatically synthesize IArray::__subscript without
     // specifying it explicitly here, because arrays already has a builtin `[]` syntax.
}

struct MyArray<T>
{
     // case 2:
     // the signature of subscript doesn't exactly match the interface
    // but the compiler should be able to synthesize a matching one
    // from the provided subscript operator.
     __subscript(uint index)->T {ref{...}}
}

The PR fills in the missing pieces in trySynthesizeMemberRequirement to handle the subscript requirement case, just like how properties are handled. In fact a lot of the logic in synthesizing the accessors of a property decl is refactored into a shared sub-routine and reused for the subscript case.

This PR also removes the warning on unmodified inout parameter since it frequently gets in the way if there is an interface that requires inout parameter and the implementation doesn't actually modify it. There is adhoc logic to silence the warning if the method is implementing an interface method, but that doesn't really work for generic functions. Overall I think this warning is kind of annoying and not easy to get it right, so removing.

@csyonghe csyonghe added the pr: non-breaking PRs without breaking changes label Sep 18, 2024
Copy link
Collaborator

@jkwak-work jkwak-work left a comment

Choose a reason for hiding this comment

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

Code looks good to me.

Comment on lines +19 to +23
writeToBuffer(outputBuffer, 0, 1.0f);

// CHECK: 4.0
writeToArray(arr, 0, 4.0f);
outputBuffer[1] = readFromArray(arr, 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 am little confused by the inconsistency with writeToBuffer.
If I do writeToBuffer(outputBuffer, ...), it will modify outputBuffer.
But if I do writeToBuffer(arr, ...), it wouldn't modify arr.

When I look at the function signature alone, it shouldn't modify the first argument because it doesn't have out nor inout modifier.

void writeToBuffer<U, T : IRWArray<U>>(T array, int index, U value)

When used with RWStructuredBuffer, T array works as if it is inout T array, which seems inconsistent.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, this is a little unfortunate, but at the moment there isn't a better solution.

RWStructuredBuffer itself should be viewed as T*, that is a handle to an array. Therefore, The structuredbuffer itself is a non-mutable object (represented as a uniform parameter), and therefore you can't pass it into a function that expects an inout parameter, because you can't assign buffer to something else, it is readonly.

In contract, for arrays you do want to mutate its content, so inout is needed there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The essence in this is that RWStructuredBuffer is Array<T>* and not an array, but there is no correponding interface to represent a "reference to an array" right now, and even if we do, we can't construct a reference object to a real array in order to satisfy that interface anyways.

@csyonghe csyonghe merged commit 2d83875 into shader-slang:master Sep 18, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: non-breaking PRs without breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make StructuredBuffer conform to IArray. Missing IRWArray in stdlib.
2 participants