Skip to content

Implementation of the builder pattern#856

Merged
bors[bot] merged 1 commit intogfx-rs:masterfrom
Andful:add-builder-pattern
Aug 2, 2020
Merged

Implementation of the builder pattern#856
bors[bot] merged 1 commit intogfx-rs:masterfrom
Andful:add-builder-pattern

Conversation

@Andful
Copy link
Copy Markdown
Contributor

@Andful Andful commented Jul 31, 2020

Connections
This pull request addresses the issue #851

Description
Add a builder for wgpu types.

Testing
TODO

Copy link
Copy Markdown
Contributor

@monocodus monocodus bot left a comment

Choose a reason for hiding this comment

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

This is an autogenerated code review.New suggestions: 2

Checker summary (by rust_clippy):
The tool has found 0 warnings, 2 errors.

Comment on lines +1201 to +1180
pub fn new() -> Self {
Self {
index_format: IndexFormat::default(),
vertex_buffers: Cow::Borrowed(&[]),
}
}
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.

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

Comment on lines +1514 to +1490
pub fn new() -> Self {
Self {
usage: TextureUsage::OUTPUT_ATTACHMENT,
format: TextureFormat::Bgra8UnormSrgb,
width: 0,
height: 0,
present_mode: PresentMode::Fifo,
}
}
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.

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

@Andful Andful changed the title first draft implementation Implementation of the builder pattern Jul 31, 2020
Copy link
Copy Markdown
Contributor

@monocodus monocodus bot left a comment

Choose a reason for hiding this comment

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

This is an autogenerated code review.

Checker summary (by rust_clippy):
The tool has found 0 warnings, 2 errors.

Copy link
Copy Markdown
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 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.

impl DeviceDescriptor {
pub fn new() -> Self {
Self {
features: Features::default(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

for things that derive Default we should just do Self::default() here

self
}

pub fn shader_validation(&mut self, validation: bool) -> &mut Self {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let's have no arguments and just assign this to true, to be in line with compatible_surface

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.

Should the function name be enable_shader_validation in your opinion?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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().

Self::default()
}

pub fn src_factor(&mut self, factor: BlendFactor) -> &mut Self {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

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 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.

impl<S> RequestAdapterOptions<S> {
pub fn new() -> Self {
Self {
power_preference: PowerPreference::default(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let's call Self::Default

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.

S: Default is required. I will implement default for RequestAdapterOptions

self
}

pub fn alpha_to_coverage_enabled(&mut self, enabled: bool) -> &mut Self {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let's remove the argument, and strip the "_enabled" suffix from the method

}

impl CommandBufferDescriptor {
pub fn new(todo: u32) -> Self {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

doesn't need the argument here, should just call Self::default() in the impl


impl<'a> RenderBundleDescriptor<Option<Cow<'a, str>>> {
pub fn new() -> Self {
Self { label: None }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

similarly, let's call Self::default() for consistency

}

impl TextureDataLayout {
pub fn new(offset: BufferAddress, bytes_per_row: u32, rows_per_image: u32) -> Self {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let's remove the offset and rows_per_image from here (since they are optional) and have separate methods for them

}

impl<T> TextureCopyView<T> {
pub fn new(texture: T, origin: Origin3d) -> Self {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let's remove the origin from here (defaulting to ZERO), and have a method for it

@Andful Andful force-pushed the add-builder-pattern branch 3 times, most recently from cae8a65 to d8c6822 Compare August 1, 2020 11:20
Comment on lines +951 to +952
depth_write_enabled: false,
depth_compare: CompareFunction::Undefined,
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.

Should depth_compare be initialized as CompareFunction::Undefined? My worry is that this might fail silently.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, I should have said. We need to accept it as a new() parameter instead of having a separate method.

Copy link
Copy Markdown
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.

This looks great, thank you!
Just one last fix to make.

Comment on lines +951 to +952
depth_write_enabled: false,
depth_compare: CompareFunction::Undefined,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, I should have said. We need to accept it as a new() parameter instead of having a separate method.

@Andful Andful force-pushed the add-builder-pattern branch from d8c6822 to 8a2fb4b Compare August 2, 2020 00:53
pub fn new(width: u32, height: u32) -> Self {
Self {
usage: TextureUsage::OUTPUT_ATTACHMENT,
format: TextureFormat::Bgra8UnormSrgb,
Copy link
Copy Markdown
Contributor Author

@Andful Andful Aug 2, 2020

Choose a reason for hiding this comment

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

This default value is ok for texture format for the swap chain? I noticed that for web it usually take value TextureFormat::Bgra8Unorm.

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.

At least here it uses TextureFormat::Bgra8Unorm but it also states // TODO: Allow srgb unconditionally

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

@Andful
Copy link
Copy Markdown
Contributor Author

Andful commented Aug 2, 2020

Made the (hopefully) last change. Could you check my last comment?

@Andful Andful force-pushed the add-builder-pattern branch 2 times, most recently from 3d1048e to 24cef68 Compare August 2, 2020 02:54
@Andful Andful force-pushed the add-builder-pattern branch 3 times, most recently from 68c4ca0 to 42f3b93 Compare August 2, 2020 03:22
Comment on lines +1762 to +1802
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
}

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.

@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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

sorry, I should have spotted that. Please separate these both into methods that change base vs count.
thank you for the heads up!

Comment on lines +1762 to +1802
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
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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>
@Andful Andful force-pushed the add-builder-pattern branch from 42f3b93 to acfbbb4 Compare August 2, 2020 03:49
Copy link
Copy Markdown
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.

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+

@kvark
Copy link
Copy Markdown
Member

kvark commented Aug 2, 2020

@Andful thank you! Would you be interested to update wgpu-rs based on this PR?

@Andful
Copy link
Copy Markdown
Contributor Author

Andful commented Aug 2, 2020

Yes, I would be. It is only the example code, is it not?

@bors
Copy link
Copy Markdown
Contributor

bors bot commented Aug 2, 2020

@bors bors bot merged commit 5d35936 into gfx-rs:master Aug 2, 2020
@kvark
Copy link
Copy Markdown
Member

kvark commented Aug 2, 2020

Yes, I would be. It is only the example code, is it not?

likely, yes

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.

4 participants