-
Notifications
You must be signed in to change notification settings - Fork 326
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 ability to replace all resources of a type #2063
Conversation
cmd/gapit/flags.go
Outdated
Handle string `help:"required. handle of the resource to replace"` | ||
ResourcePath string `help:"file path for the new resource"` | ||
At int `help:"command index to replace the resource(s) at"` | ||
UpdateResourceBinary string `help:"binary to run for every shader; consumes resource data from standard input and writes to standard output"` |
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.
Is it just shaders or all resources?
cmd/gapit/replace_resource.go
Outdated
@@ -50,13 +54,13 @@ func (verb *replaceResourceVerb) Run(ctx context.Context, flags flag.FlagSet) er | |||
return nil | |||
} | |||
|
|||
if verb.Handle == "" { | |||
app.Usage(ctx, "-handle argument is required") | |||
if verb.Handle == "" && verb.UpdateResourceBinary == "" { |
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.
Is it acceptable for both be specified? If not, perhaps this should be:
if (verb.Handle == "") != (verb.UpdateResourceBinary == "")
cmd/gapit/replace_resource.go
Outdated
return fmt.Errorf("Multiple resources matched: %s, %s", matchedResource.GetHandle(), v.GetHandle()) | ||
if verb.Handle != "" { | ||
var matchedResource *service.Resource | ||
for _, v := range types.GetResources() { |
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.
We already have a function to look for a resource by type and ID - perhaps we should add a more generic one to search using a predicate? We can then have a version that errors if theres more or less than one found.
Something like:
// FindAll returns all the resources that match the predicate f.
func (r *Resources) FindAll(f func(api.ResourceType, Resource) bool) []*Resource { ... }
// FindSingle returns the single resource that matches the predicate f.
// If there are 0 or multiple resources found, FindSingle returns an error.
func (r *Resources) FindSingle(f func(api.ResourceType, Resource) bool) (*Resource, error) { ... }
// Find looks up a resource by type and identifier.
func (r *Resources) Find(ty api.ResourceType, id id.ID) *Resource {
r, _ := r.FindSingle(func(t api.ResourceType, r Resource) bool {
return t == ty && r.ID.ID() == id
})
return r
}
cmd/gapit/replace_resource.go
Outdated
return log.Errf(ctx, err, "Could not read resource file %s", verb.ResourcePath) | ||
} | ||
resourceData = api.NewResourceData(&api.Shader{Type: api.ShaderType_Spirv, Source: string(newResourceBytes)}) | ||
} else if verb.UpdateResourceBinary != "" { |
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.
Consider a type switch instead of chained if statements:
switch {
case verb.Handle != "":
...
case verb.UpdateResourceBinary != "":
...
}
cmd/gapit/replace_resource.go
Outdated
return nil | ||
} | ||
|
||
func (verb *replaceResourceVerb) getNewResourceData(ctx context.Context, resourceData *api.ResourceData) (string, error) { |
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.
Docs please
gapis/api/service.proto
Outdated
@@ -127,6 +127,10 @@ message ResourceData { | |||
} | |||
} | |||
|
|||
message MultiResourceData { |
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.
Docs please
gapis/resolve/set.go
Outdated
|
||
cmdIdx := p.After.Indices[0] | ||
// If we change resource data, subcommands do not affect this, so change | ||
// the main comand. |
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.
command
gapis/service/path/path.go
Outdated
@@ -602,6 +609,13 @@ func (n *Command) ResourceAfter(id *ID) *ResourceData { | |||
} | |||
} | |||
|
|||
func (n *Command) ResourcesAfter(ids []*ID) *MultiResourceData { |
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.
Docs please
gapis/service/path/path.go
Outdated
@@ -246,6 +249,10 @@ func (n ResourceData) Format(f fmt.State, c rune) { | |||
fmt.Fprintf(f, "%v.resource-data<%x>", n.Parent(), n.ID) | |||
} | |||
|
|||
func (n MultiResourceData) Format(f fmt.State, c rune) { |
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.
Docs please
gapis/service/path/validate.go
Outdated
@@ -249,6 +249,18 @@ func (n *ResourceData) Validate() error { | |||
) | |||
} | |||
|
|||
func (n *MultiResourceData) Validate() error { |
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.
Docs please
31c9204
to
f10168d
Compare
cmd/gapit/replace_resource.go
Outdated
@@ -50,13 +54,13 @@ func (verb *replaceResourceVerb) Run(ctx context.Context, flags flag.FlagSet) er | |||
return nil | |||
} | |||
|
|||
if verb.Handle == "" { | |||
app.Usage(ctx, "-handle argument is required") | |||
if verb.Handle == verb.UpdateResourceBinary { |
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.
Is this right?
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.
Changed, my intention is to error if both are non empty or if both are empty.
cmd/gapit/replace_resource.go
Outdated
return fmt.Errorf("Multiple resources matched: %s, %s", matchedResource.GetHandle(), v.GetHandle()) | ||
switch { | ||
case verb.Handle != "": | ||
matchedResource, err := resources.FindSingle(func(t api.ResourceType, r service.Resource) bool { |
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.
This doesn't need to be in a for loop now, right?
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.
Right, thanks!
f10168d
to
9bd4a7b
Compare
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.
Please reply to review comments with a 'done' if they've been addressed - this way the reviewer knows if things have been missed, or not yet done.
Thanks!
cmd/gapit/replace_resource.go
Outdated
@@ -50,13 +54,13 @@ func (verb *replaceResourceVerb) Run(ctx context.Context, flags flag.FlagSet) er | |||
return nil | |||
} | |||
|
|||
if verb.Handle == "" { | |||
app.Usage(ctx, "-handle argument is required") | |||
if (verb.Handle != "" && verb.UpdateResourceBinary != "") || (verb.Handle == "" && verb.UpdateResourceBinary == "") { |
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.
Which is equivalent to:
if (verb.Handle == "") != (verb.UpdateResourceBinary == "")
Can keep as-is if you find this clearer.
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.
Sorry misread your original suggestion.
Updated, though I added : (verb.Handle == "") == (verb.UpdateResourceBinary == "")
since if condition should be true for error cases
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.
Yup, just testing 😉
cmd/gapit/replace_resource.go
Outdated
switch { | ||
case verb.Handle != "": | ||
matchedResource, err := resources.FindSingle(func(t api.ResourceType, r service.Resource) bool { | ||
return strings.Contains(r.GetHandle(), verb.Handle) |
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.
I think you need to check type here too?
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.
Done
cmd/gapit/replace_resource.go
Outdated
// getNewResourceData runs the update resource binary on the old resource data | ||
// and returns the newly generated resource data | ||
func (verb *replaceResourceVerb) getNewResourceData(ctx context.Context, resourceData string) (string, error) { | ||
updateCmd := exec.Command(verb.UpdateResourceBinary) |
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.
Did my example code not work for you?
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.
It does. Sorry, totally missed that comment.
gapis/api/resource.go
Outdated
@@ -100,3 +100,7 @@ func NewResourceData(data interface{}) *ResourceData { | |||
panic(fmt.Errorf("%T is not a ResourceData type", data)) | |||
} | |||
} | |||
|
|||
func NewMultiResourceData(resources []*ResourceData) *MultiResourceData { |
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.
Still missing docs.
9bd4a7b
to
fc76a51
Compare
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.
Thank you for the changes!
Currently only works for shaders UpdateResourceBinary should read the resource data from standard input and output the modified resource data to standard output. Also modifying replace_resource to consume spir-v disassembly to replace a single shader for consistency
fc76a51
to
9b89cfb
Compare
Currently only works for shaders
UpdateResourceBinary should read the resource data from
standard input and output the modified resource data to
standard output.
Also modifying replace_resource to consume spir-v
disassembly to replace a single shader for consistency
I know MultiResourceData is not the best of names, but couldn't come up with anything better.
Suggestions are welcome.