Implementation of the builder pattern#856
Conversation
wgpu-types/src/lib.rs
Outdated
| pub fn new() -> Self { | ||
| Self { | ||
| index_format: IndexFormat::default(), | ||
| vertex_buffers: Cow::Borrowed(&[]), | ||
| } | ||
| } |
There was a problem hiding this comment.
error: you should consider adding a Default implementation for VertexStateDescriptor<'a>
|
1201 | / pub fn new() -> Self {
1202 | | Self {
1203 | | index_format: IndexFormat::default(),
1204 | | vertex_buffers: Cow::Borrowed(&[]),
1205 | | }
1206 | | }
| |_____^
|
note: `-D clippy::new-without-default` implied by `-D warnings`
help: [refer to the official clippy documentation](https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default)
help: try this
|
1200 | impl Default for VertexStateDescriptor<'a> {
1201 | fn default() -> Self {
1202 | Self::new()
1203 | }
1204 | }
|
generated by rust-clippy
wgpu-types/src/lib.rs
Outdated
| pub fn new() -> Self { | ||
| Self { | ||
| usage: TextureUsage::OUTPUT_ATTACHMENT, | ||
| format: TextureFormat::Bgra8UnormSrgb, | ||
| width: 0, | ||
| height: 0, | ||
| present_mode: PresentMode::Fifo, | ||
| } | ||
| } |
There was a problem hiding this comment.
error: you should consider adding a Default implementation for SwapChainDescriptor
|
1514 | / pub fn new() -> Self {
1515 | | Self {
1516 | | usage: TextureUsage::OUTPUT_ATTACHMENT,
1517 | | format: TextureFormat::Bgra8UnormSrgb,
... |
1521 | | }
1522 | | }
| |_____^
|
help: [refer to the official clippy documentation](https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default)
help: try this
|
1513 | impl Default for SwapChainDescriptor {
1514 | fn default() -> Self {
1515 | Self::new()
1516 | }
1517 | }
|
generated by rust-clippy
kvark
left a comment
There was a problem hiding this comment.
Thank you for this grand work! I understand it was a bit tedious to write this up, but this looks great to me as way forward. I don't think we want to auto-derive this, since we need more flexibility on how we construct the types, and we want compile times to be fast. In particular, there are places where we know fields are to be set together, and we can take advantage of that.
wgpu-types/src/lib.rs
Outdated
| impl DeviceDescriptor { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| features: Features::default(), |
There was a problem hiding this comment.
for things that derive Default we should just do Self::default() here
wgpu-types/src/lib.rs
Outdated
| self | ||
| } | ||
|
|
||
| pub fn shader_validation(&mut self, validation: bool) -> &mut Self { |
There was a problem hiding this comment.
let's have no arguments and just assign this to true, to be in line with compatible_surface
There was a problem hiding this comment.
Should the function name be enable_shader_validation in your opinion?
There was a problem hiding this comment.
uh, that's a hard one... this choice affects all the boolean entry points.
My feeling is that just shader_validation() without prefixes or postfixes is fine. @cwfitzgerald @grovesNL please share your thoughts if you feel strongly
There was a problem hiding this comment.
I don't have particular feelings on the positive side, either thing() or enable_thing() work fine. Going to negative (which we don't right now) it should be disable_thing().
wgpu-types/src/lib.rs
Outdated
| Self::default() | ||
| } | ||
|
|
||
| pub fn src_factor(&mut self, factor: BlendFactor) -> &mut Self { |
There was a problem hiding this comment.
let's take advantage of the manual builder pattern here and combine the src and dst factors: pub fn factors(&mut self, src: BlendFactor, dst: BlendFactor) -> &mut Self. I don't think it's useful to only set one of them ever.
There was a problem hiding this comment.
I agree that it's probably not useful to only set one of them, but this breaks consistency and expectations. When looking for these functions you generally expect the function name to match the field 1:1. I'd argue against using factors(..) simply to keep everything consistent.
wgpu-types/src/lib.rs
Outdated
| impl<S> RequestAdapterOptions<S> { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| power_preference: PowerPreference::default(), |
There was a problem hiding this comment.
S: Default is required. I will implement default for RequestAdapterOptions
wgpu-types/src/lib.rs
Outdated
| self | ||
| } | ||
|
|
||
| pub fn alpha_to_coverage_enabled(&mut self, enabled: bool) -> &mut Self { |
There was a problem hiding this comment.
let's remove the argument, and strip the "_enabled" suffix from the method
wgpu-types/src/lib.rs
Outdated
| } | ||
|
|
||
| impl CommandBufferDescriptor { | ||
| pub fn new(todo: u32) -> Self { |
There was a problem hiding this comment.
doesn't need the argument here, should just call Self::default() in the impl
wgpu-types/src/lib.rs
Outdated
|
|
||
| impl<'a> RenderBundleDescriptor<Option<Cow<'a, str>>> { | ||
| pub fn new() -> Self { | ||
| Self { label: None } |
There was a problem hiding this comment.
similarly, let's call Self::default() for consistency
wgpu-types/src/lib.rs
Outdated
| } | ||
|
|
||
| impl TextureDataLayout { | ||
| pub fn new(offset: BufferAddress, bytes_per_row: u32, rows_per_image: u32) -> Self { |
There was a problem hiding this comment.
let's remove the offset and rows_per_image from here (since they are optional) and have separate methods for them
wgpu-types/src/lib.rs
Outdated
| } | ||
|
|
||
| impl<T> TextureCopyView<T> { | ||
| pub fn new(texture: T, origin: Origin3d) -> Self { |
There was a problem hiding this comment.
let's remove the origin from here (defaulting to ZERO), and have a method for it
cae8a65 to
d8c6822
Compare
wgpu-types/src/lib.rs
Outdated
| depth_write_enabled: false, | ||
| depth_compare: CompareFunction::Undefined, |
There was a problem hiding this comment.
Should depth_compare be initialized as CompareFunction::Undefined? My worry is that this might fail silently.
There was a problem hiding this comment.
Sorry, I should have said. We need to accept it as a new() parameter instead of having a separate method.
kvark
left a comment
There was a problem hiding this comment.
This looks great, thank you!
Just one last fix to make.
wgpu-types/src/lib.rs
Outdated
| depth_write_enabled: false, | ||
| depth_compare: CompareFunction::Undefined, |
There was a problem hiding this comment.
Sorry, I should have said. We need to accept it as a new() parameter instead of having a separate method.
d8c6822 to
8a2fb4b
Compare
| pub fn new(width: u32, height: u32) -> Self { | ||
| Self { | ||
| usage: TextureUsage::OUTPUT_ATTACHMENT, | ||
| format: TextureFormat::Bgra8UnormSrgb, |
There was a problem hiding this comment.
This default value is ok for texture format for the swap chain? I noticed that for web it usually take value TextureFormat::Bgra8Unorm.
There was a problem hiding this comment.
At least here it uses TextureFormat::Bgra8Unorm but it also states // TODO: Allow srgb unconditionally
There was a problem hiding this comment.
I think it's ok defaulting to Bgra8UnormSrgb here. WASM users can always work around by using non-default, and we want to fix this in WASM too
|
Made the (hopefully) last change. Could you check my last comment? |
3d1048e to
24cef68
Compare
68c4ca0 to
42f3b93
Compare
| impl<'a> TextureViewDescriptor<Option<Cow<'a, str>>> { | ||
| pub fn new(format: TextureFormat, dimension: TextureViewDimension) -> Self { | ||
| Self { | ||
| label: None, | ||
| format, | ||
| dimension, | ||
| aspect: TextureAspect::default(), | ||
| base_mip_level: 0, | ||
| level_count: None, | ||
| base_array_layer: 0, | ||
| array_layer_count: None, | ||
| } | ||
| } | ||
|
|
||
| pub fn label(&mut self, label: impl IntoCow<'a, str>) -> &mut Self { | ||
| self.label = Some(label.into_cow()); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl<L> TextureViewDescriptor<L> { | ||
| pub fn mip_levels( | ||
| &mut self, | ||
| base_mip_level: u32, | ||
| level_count: Option<u32>, | ||
| ) -> &mut Self { | ||
| self.base_mip_level = base_mip_level; | ||
| self.level_count = level_count; | ||
| self | ||
| } | ||
|
|
||
| pub fn array_layers( | ||
| &mut self, | ||
| base_array_layer: u32, | ||
| array_layer_count: Option<u32>, | ||
| ) -> &mut Self { | ||
| self.base_array_layer = base_array_layer; | ||
| self.array_layer_count = array_layer_count; | ||
| self | ||
| } | ||
|
|
There was a problem hiding this comment.
@kvark Could you double check this? I accidentally missed its implementation previously. Also, should level_count and array_layer_count checked to be different from 0?
There was a problem hiding this comment.
sorry, I should have spotted that. Please separate these both into methods that change base vs count.
thank you for the heads up!
| impl<'a> TextureViewDescriptor<Option<Cow<'a, str>>> { | ||
| pub fn new(format: TextureFormat, dimension: TextureViewDimension) -> Self { | ||
| Self { | ||
| label: None, | ||
| format, | ||
| dimension, | ||
| aspect: TextureAspect::default(), | ||
| base_mip_level: 0, | ||
| level_count: None, | ||
| base_array_layer: 0, | ||
| array_layer_count: None, | ||
| } | ||
| } | ||
|
|
||
| pub fn label(&mut self, label: impl IntoCow<'a, str>) -> &mut Self { | ||
| self.label = Some(label.into_cow()); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl<L> TextureViewDescriptor<L> { | ||
| pub fn mip_levels( | ||
| &mut self, | ||
| base_mip_level: u32, | ||
| level_count: Option<u32>, | ||
| ) -> &mut Self { | ||
| self.base_mip_level = base_mip_level; | ||
| self.level_count = level_count; | ||
| self | ||
| } | ||
|
|
||
| pub fn array_layers( | ||
| &mut self, | ||
| base_array_layer: u32, | ||
| array_layer_count: Option<u32>, | ||
| ) -> &mut Self { | ||
| self.base_array_layer = base_array_layer; | ||
| self.array_layer_count = array_layer_count; | ||
| self | ||
| } | ||
|
|
There was a problem hiding this comment.
sorry, I should have spotted that. Please separate these both into methods that change base vs count.
thank you for the heads up!
| pub fn new(width: u32, height: u32) -> Self { | ||
| Self { | ||
| usage: TextureUsage::OUTPUT_ATTACHMENT, | ||
| format: TextureFormat::Bgra8UnormSrgb, |
There was a problem hiding this comment.
I think it's ok defaulting to Bgra8UnormSrgb here. WASM users can always work around by using non-default, and we want to fix this in WASM too
Signed-off-by: Andrea Nardi <buongiorno19972@gmail.com>
42f3b93 to
acfbbb4
Compare
kvark
left a comment
There was a problem hiding this comment.
Great stuff, thank you for doing this!
I think we may discover some parts needing changes, but that's ok. Most of the content here is good, and we need it ASAP.
bors r+
|
@Andful thank you! Would you be interested to update wgpu-rs based on this PR? |
|
Yes, I would be. It is only the example code, is it not? |
likely, yes |
Connections
This pull request addresses the issue #851
Description
Add a builder for wgpu types.
Testing
TODO