-
Notifications
You must be signed in to change notification settings - Fork 920
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
Conversation
3d30ac3
to
33afc6b
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! Expressed some thoughts below
wgpu-types/src/lib.rs
Outdated
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | ||
#[cfg_attr(feature = "trace", derive(Serialize))] | ||
#[cfg_attr(feature = "replay", derive(Deserialize))] | ||
pub enum AnisotropyLevel { |
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.
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:
- make your configuration fully use
AnisotropyLevel
enum here, which makes it intrusive. Not always applicable, i.e. if you don't control the format - 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.
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.
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.
33afc6b
to
fae6b89
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! just one last thing
wgpu-types/src/lib.rs
Outdated
@@ -955,10 +956,10 @@ pub struct SamplerDescriptor<L> { | |||
pub lod_max_clamp: f32, | |||
pub compare: CompareFunction, |
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.
can we also switch this to be an Option
since we are at it?
fae6b89
to
ade7ce1
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!
bors r+
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>
bors retry |
Already running a review |
Build failed: |
bors retry |
1 similar comment
bors retry |
Already running a review |
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>
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>
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>
Connections
This begins the rustificaiton of
wgpu-types
as discussed in #689, starting withExtensions
andSamplerDescriptor
.#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: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