Skip to content

Add ability to replace all resources of a type#2063

Merged
apazylbe merged 1 commit into
google:masterfrom
apazylbe:replace_all_shaders
Jul 17, 2018
Merged

Add ability to replace all resources of a type#2063
apazylbe merged 1 commit into
google:masterfrom
apazylbe:replace_all_shaders

Conversation

@apazylbe

Copy link
Copy Markdown
Contributor

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.

Comment thread 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"`

Copy link
Copy Markdown
Contributor

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?

Comment thread cmd/gapit/replace_resource.go Outdated

if verb.Handle == "" {
app.Usage(ctx, "-handle argument is required")
if verb.Handle == "" && verb.UpdateResourceBinary == "" {

Copy link
Copy Markdown
Contributor

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 == "")

Comment thread 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() {

Copy link
Copy Markdown
Contributor

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
}

Comment thread 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 != "" {

Copy link
Copy Markdown
Contributor

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 != "":
        ...
}

Comment thread cmd/gapit/replace_resource.go Outdated
return nil
}

func (verb *replaceResourceVerb) getNewResourceData(ctx context.Context, resourceData *api.ResourceData) (string, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Docs please

Comment thread gapis/api/service.proto
}
}

message MultiResourceData {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Docs please

Comment thread gapis/resolve/set.go

cmdIdx := p.After.Indices[0]
// If we change resource data, subcommands do not affect this, so change
// the main comand.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

command

}
}

func (n *Command) ResourcesAfter(ids []*ID) *MultiResourceData {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Docs please

fmt.Fprintf(f, "%v.resource-data<%x>", n.Parent(), n.ID)
}

func (n MultiResourceData) Format(f fmt.State, c rune) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Docs please

)
}

func (n *MultiResourceData) Validate() error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Docs please

@apazylbe apazylbe force-pushed the replace_all_shaders branch 2 times, most recently from 31c9204 to f10168d Compare July 13, 2018 20:53
Comment thread cmd/gapit/replace_resource.go Outdated

if verb.Handle == "" {
app.Usage(ctx, "-handle argument is required")
if verb.Handle == verb.UpdateResourceBinary {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this right?

Copy link
Copy Markdown
Contributor Author

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.

Comment thread 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 {

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, thanks!

@apazylbe apazylbe force-pushed the replace_all_shaders branch from f10168d to 9bd4a7b Compare July 16, 2018 14:26

@ben-clayton ben-clayton left a comment

Copy link
Copy Markdown
Contributor

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!

Comment thread cmd/gapit/replace_resource.go Outdated

if verb.Handle == "" {
app.Usage(ctx, "-handle argument is required")
if (verb.Handle != "" && verb.UpdateResourceBinary != "") || (verb.Handle == "" && verb.UpdateResourceBinary == "") {

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yup, just testing 😉

Comment thread 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)

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread 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)

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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.

Comment thread gapis/api/resource.go
}
}

func NewMultiResourceData(resources []*ResourceData) *MultiResourceData {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still missing docs.

@apazylbe apazylbe force-pushed the replace_all_shaders branch from 9bd4a7b to fc76a51 Compare July 17, 2018 14:02

@ben-clayton ben-clayton left a comment

Copy link
Copy Markdown
Contributor

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
@apazylbe apazylbe force-pushed the replace_all_shaders branch from fc76a51 to 9b89cfb Compare July 17, 2018 15:20
@apazylbe apazylbe merged commit bca43f3 into google:master Jul 17, 2018
@apazylbe apazylbe deleted the replace_all_shaders branch July 17, 2018 18:26
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.

3 participants