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

Rustification of Extensions and SamplerDescriptor #696

Merged
merged 1 commit into from
Jun 3, 2020

Conversation

cwfitzgerald
Copy link
Member

@cwfitzgerald cwfitzgerald commented Jun 2, 2020

Connections

This begins the rustificaiton of wgpu-types as discussed in #689, starting with Extensions and SamplerDescriptor.

#691 and the resulting discussion was used to advise the shape of the extension struct.

Description

The PR should be fairly straight forward. As discussed, I have left extensions as a bitflag until the webgpu-native issue can be opened and discussed regarding allocation of extensions.

The most controversial part of this will be the AnisotropyLevel enum. I created it for two reasons, one that matters, one that doesn't:

  • It means that, with the exception of enabling the aniso extension (and lod_bias), the sampler is correct by construction, which is helpful to both beginners and experts. It also better exposes the range of valid values and means less panics in user code.
  • It saves a byte in the Option<u8> 😄

I definitely think that, if at all possible, we should have explicitly typed enums for "numbers" which have a limited amount of valid values (so not lod bias, though that may be better expressed, idk) to make it very explicit which values can be accepted. This also feels more "rusty" and reduces the amount of "magicness" in the interface.

Ofc, I'm not married to the idea.

Testing

Follow up PR into wgpu-rs will include conversion of the examples.

TODO

@cwfitzgerald cwfitzgerald requested a review from kvark June 2, 2020 22:30
Copy link
Member

@kvark kvark left a comment

Choose a reason for hiding this comment

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

Thank you! Expressed some thoughts below

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trace", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub enum AnisotropyLevel {
Copy link
Member

Choose a reason for hiding this comment

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

The advantages here are obvious (i.e. being correct by construction), the disadvantages, however, less so.
We had a lot of iteration on similar aspects of the API in gfx-rs during the years... and quite often the original desire to make the types pretty back-fired in the end. The morale of gfx-rs experience is that we need to strive for simplicity.

In this case, for example, writing this down as anisotropy_clamp: Some(wgpu:AnisotropyLevel::X2) isn't any cleaner than anisotropy_clamp: Some(2). And the "correct by construction" aspect is not helping much in practice(!) since the users gets the error instantly upon creating a sampler.

In another case, say you are writing an engine and you don't have that value predefined. You are loading from disk/configuration. Now you have 2 choices:

  1. make your configuration fully use AnisotropyLevel enum here, which makes it intrusive. Not always applicable, i.e. if you don't control the format
  2. do a manual match aniso_level { 0 | 1 => None, 2 => Some(wgpu::AnisotropyLevel::X2), .. }, which is very annoying.

So in both of these cases the strong enum, despite it being elegant and all that, doesn't bring much to the table in practice.

Also we need to think about what the other parameters are going to be. For example, do you see MSAA level also enumerized in the same way? Unlike unisotropy, MSAA levels have different support on platforms. That means, we don't know at compile time if the value is good anyway. We might as well accept it as an integer without fanciness.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's fair enough. The point about the intrusiveness is an interesting angle that I didn't think about as I'm mainly used to working on end products (where I can see the whole picture). I'll switch it back.

Copy link
Member

@kvark kvark left a comment

Choose a reason for hiding this comment

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

Thank you! just one last thing

@@ -955,10 +956,10 @@ pub struct SamplerDescriptor<L> {
pub lod_max_clamp: f32,
pub compare: CompareFunction,
Copy link
Member

Choose a reason for hiding this comment

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

can we also switch this to be an Option since we are at it?

Copy link
Member

@kvark kvark left a comment

Choose a reason for hiding this comment

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

Thank you!
bors r+

bors bot added a commit that referenced this pull request Jun 3, 2020
696: Rustification of Extensions and SamplerDescriptor r=kvark a=cwfitzgerald

**Connections**

This begins the rustificaiton of `wgpu-types` as discussed in #689, starting with `Extensions` and `SamplerDescriptor`.

#691 and the resulting discussion was used to advise the shape of the extension struct.

**Description**

The PR should be fairly straight forward. As discussed, I have left extensions as a bitflag until the webgpu-native issue can be opened and discussed regarding allocation of extensions.

The most controversial part of this will be the `AnisotropyLevel` enum. I created it for two reasons, one that matters, one that doesn't:
 - It means that, with the exception of enabling the aniso extension (and lod_bias), the sampler is correct by construction, which is helpful to both beginners and experts. It also better exposes the range of valid values and means less panics in user code.
 - It saves a byte in the `Option<u8>` 😄 

I definitely think that, if at all possible, we should have explicitly typed enums for "numbers" which have a limited amount of valid values (so _not_ lod bias, though that may be better expressed, idk) to make it very explicit which values can be accepted. This also feels more "rusty" and reduces the amount of "magicness" in the interface.

Ofc, I'm not married to the idea.

**Testing**

Follow up PR into wgpu-rs will include conversion of the examples.

**TODO**

- [x] wgpu-native pr rolling up this PR and #690 (gfx-rs/wgpu-native#34)
- [x] wgpu-rs pr updating examples with the new structures (gfx-rs/wgpu-rs#345)

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
@cwfitzgerald
Copy link
Member Author

bors retry

@bors
Copy link
Contributor

bors bot commented Jun 3, 2020

Already running a review

@bors
Copy link
Contributor

bors bot commented Jun 3, 2020

Build failed:

@cwfitzgerald
Copy link
Member Author

bors retry

1 similar comment
@kvark
Copy link
Member

kvark commented Jun 3, 2020

bors retry

@bors
Copy link
Contributor

bors bot commented Jun 3, 2020

Already running a review

@bors
Copy link
Contributor

bors bot commented Jun 3, 2020

@bors bors bot merged commit ac9587e into gfx-rs:master Jun 3, 2020
bors bot added a commit to gfx-rs/wgpu-rs that referenced this pull request Jun 3, 2020
345: Rustify the example's use of Extensions and SamplerDescriptor r=kvark a=cwfitzgerald

Implements gfx-rs/wgpu#696.

This didn't change any content from #338, just interface, but I tested a couple examples and it seems to work okay.

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
bors bot added a commit to gfx-rs/wgpu-rs that referenced this pull request Jun 3, 2020
345: Rustify the example's use of Extensions and SamplerDescriptor r=kvark a=cwfitzgerald

Implements gfx-rs/wgpu#696.

This didn't change any content from #338, just interface, but I tested a couple examples and it seems to work okay.

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
@cwfitzgerald cwfitzgerald deleted the rustification branch June 3, 2020 01:53
kvark pushed a commit to kvark/wgpu that referenced this pull request Jun 3, 2021
345: Rustify the example's use of Extensions and SamplerDescriptor r=kvark a=cwfitzgerald

Implements gfx-rs#696.

This didn't change any content from gfx-rs#338, just interface, but I tested a couple examples and it seems to work okay.

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
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