Skip to content

In copying to/from texture, use given aspect instead of hardcoded 'all' - #851

Open
almarklein wants to merge 2 commits into
mainfrom
copy-aspect
Open

almarklein wants to merge 2 commits into
mainfrom
copy-aspect

Conversation

@almarklein

Copy link
Copy Markdown
Member

No description provided.

Comment thread wgpu/backends/wgpu_native/_api.py Outdated
mipLevel=int(destination.get("mip_level", 0)),
origin=c_origin,
aspect=enums.TextureAspect.all,
aspect=destination.get("aspect", "all"),

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 was about to leave a commit preferring the enum instead of the magic value for default. But then I checked what would be better for performance and maybe it's neither. Instead it should be the magic int value... so maybe doing something like

Suggested change
aspect=destination.get("aspect", "all"),
aspect=destination.get("aspect", enummap["TextureAspect.all"]),

but that feels awkward too. (skips a lot of the string manipulation in _new_struct_p, but maybe we should have the enum types in the backend specific map... or add a _to_int method?)

way off topic, perhaps just something to remember should we do a performance pass

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I like the general idea though. I now defined ASPECT_ALL = enummap["TextureAspect.all"] at the top of the module and use that instead. What do you think?

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.

You would end up with something like this for every single entry in the current mapping. So I am not sure that's the way to go. Maybe for a few specific hot cases. Which I don't think this is.
I think Vulkan does this, and it's horrible.

I have thought a bit on how it could be improved... But I need to actually try and measure that. For this specific case, just keep it as it is which will be fine.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This'd only be done for the default enums/flags, right? So the number of them is not that big? Anyway, I guess this is the first one for which we try some approach, we can can easily change that once we/you come up with a better one 😄

@Vipitis Vipitis Nov 6, 2026 •

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.

there is a point to be made that defaults are also handled by the c layer and once more by the rust implementation. so maybe we should not do it ourselfs (function parameter default, and then .get etc).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The fields in the C structs cannot be omitted; omitted means zero, which usually is the default, but not sure if I want to rely on that.

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.

yeah, I remember the point with semantic null in a few places. There is a list of exceptions, but I am not sure if that is exhaustive... especially about the wgpu native only stuff: https://webgpu-native.github.io/webgpu-headers/SentinelValues.html

I would have to lookup the semantics of unwrap_or in rust, since they also have a !map_enum_with_undefined macro somewhere. As in wgpu types there is no undefined values in the enums. https://github.com/gfx-rs/wgpu-native/blob/589e6caa4e9fa5596f601e82ca5d3937a1afc829/src/conv.rs#L808

All of this needs some benchmarking or profiling to reason about. For the user experience being able to use both strings and enums like currently is great. And having readable default arguments is great for the IDE hover experience too. To come back to my original point, I prefer the enum types over magic strings but that is preference. I don't think there is a performance difference - since both end up doing some string manipulation and then the mapping lookup.
Using "undefined" as the default would technically be more correct, but that is less readable and might confuse the user, plus we aren't mapping that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok, so what approach do you prefer for the current PR? Let's get it approved and merged 😉

  • The current approach is a bit more performant (but not sure if its measurable).
  • Using get('aspect', 'all') is more readable and avoids the lookup on the enum.

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.

the 2nd one is my preference. basically the original commit

This branch has not been deployed

No deployments
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