-
Notifications
You must be signed in to change notification settings - Fork 112
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
Add creating and sampling textured quad example #143
Conversation
7e06101
to
d13813b
Compare
d13813b
to
9affa3a
Compare
9affa3a
to
cd25d93
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.
This is some impressive amount of work!
May I ask what is your long-term goal here, and motivation?
Previously, metal-rs
was fairly light, we could quickly build and update it. Most testing was done via gfx-rs.
With the large examples like this, the project becomes more standalone and more serious. It also requires more maintenance. Are you using it for something that's not gfx-rs?
Cargo.toml
Outdated
|
||
[workspace] | ||
members = [ | ||
"examples/creating-and-sampling-textures", |
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.
could just call it texture
?
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.
Absolutely!
My original thinking was to mimic the name of the corresponding tutorial in the Apple docs, but that isn't super important I don't think so I'll change this to texture
.
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.
this still needs to happen :)
Cargo.toml
Outdated
|
||
[workspace] | ||
members = [ | ||
"examples/creating-and-sampling-textures", |
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.
why does it need to be a separate project in the workspace?
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.
This allows the build script to run whenever we compile.
I think that it's valuable to demonstrate that since it can reduce friction when working on a real metal application.
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 makes sense. Perhaps, we'd have all the examples in a separate crate here, like in https://github.com/servo/webrender/tree/master/examples ?
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.
I think that since you get one build script per crate we'd want to be sure that we weren't ever adding any additional build scripts before combining examples into one crate.
Thanks for sharing that webrender structure - that's helpful to visualize.
There's also examples in the wild of having examples live in their own crate, a la wasm-bindgen's examples
Personally I'd like to delay this decision until if/when there are more examples to worry about.
But I'm for sure all ears if you think it's better overall to tackle this right now?
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.
My expectation is that any other examples are going to need shaders, so the need to build them is inevitable. And if you build them, it would be simpler to maintain if it's a single build script for all.
I'm fine to follow up with that as well, if you aren't convinced :)
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.
Gotcha - cool I see where you're coming from.
Valid point
Ok cool - let's table this one then. Not blocking for now anyways (:
Hey! I didn't consider the cost of adding another example when I opened this PR - so apologies for springing this PR on you unannounced. Thanks for taking the time to explain the trade-offs! So my primary motivation here is that I'd like to see the Rust graphics ecosystem be one of the first places that people turn to when working on graphics applications. (For no other reason than I love Rust and I love graphics so it's fun to see the Rust + graphics ecosystem grow and to contribute where I can.) For that to be the case it needs to be easier to write graphics applications with Rust than it is to write them without Rust. To me some of the key ingredients for this are:
I'd like to see all of the libraries in So that's why I PRd this example to My motivation for using metal-rs in the first place is that I'm working on an application where I'd like to use Metal directly when targeting Apple hardware (I currently use WebGL when targeting the web but have my eyes on wgpu-rs's Wasm support!). I prefer to work directly with the different graphics APIs because:
So the above explains why I've been using metal-rs directly. While using
So I wanted to help where I could as I ran into small papercuts. My original longer term vision was that metal-rs would have counterparts for most of the examples projects in Apple's metal docs https://developer.apple.com/documentation/metal . However - now that you've forced me to pause and think for a bit.. What I really hope for is that Rust developers have ~zero friction when getting started with metal. That can be achieved without adding examples to metal-rs. I do think that an example of working with textures should be included in the main repo - but other examples would certainly exist in a different repository if that made things easier for the gfx-rs team. So, here are the potential paths forwards that I see
Personally I think that a texture example should definitely exist in Because metal-rs is just a raw bindings library I think it's possible to have examples that have very low maintenance overhead since the library shouldn't change all that much all that often. But I'm happy with any of the options that you think is best. For both this example as well as any other examples going forwards. Thanks! |
Thank you for the extended reply! I really appreciate the enthusiasm, which drives you in this mission 🎖️ If we ever hit a situation that maintaining this becomes a burden, we'll start looking for alternatives. In the meantime, I included you in the collaborators on this project. |
Gotcha! I've only had one real issue so far fortunately. I've taken your advice and tracked it #144 . Hosting them in the repo for now sounds good. I addressed one crate for all examples vs. crate per example here so I'll pause until whenever you get around to thinking about this #143 (comment) Thanks for the collaborator add! |
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.
A few more notes...
Cargo.toml
Outdated
|
||
[workspace] | ||
members = [ | ||
"examples/creating-and-sampling-textures", |
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.
this still needs to happen :)
|
||
unsafe { | ||
let view = window.ns_view() as cocoa_id; | ||
view.setWantsLayer(YES); |
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.
We should change that to just work with bool
. This would be consistent with other functions and let the examples to avoid linking to obj
directly.
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.
Ah so it looks like BOOL
is just a type alias for i8
. Sweet thanks.
Changed 33cbdac
Alright all feedback so far addressed. I'll rebase on top of master once there's no more new feedback |
examples/textures/src/main.rs
Outdated
|
||
use std::path::PathBuf; | ||
|
||
use cocoa::{appkit::NSView, base::id as cocoa_id}; |
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.
where is NSView
used?
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.
It's a trait that needs to be in scope for these two calls:
view.setWantsLayer(true as _);
view.setLayer(std::mem::transmute(layer.as_ref()));
examples/textures/Cargo.toml
Outdated
[dependencies] | ||
cocoa = "0.20" | ||
core-graphics = "0.19" | ||
image = {version = "0.23", default-features = false, features = ["png"]} |
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.
as I suggested earlier, let's just depend on png
directly
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.
Done!
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+ |
143: Add creating and sampling textured quad example r=kvark a=chinedufn This PR ports the Swift/Objective-C [Creating and Sampling Textures Tutorial](https://developer.apple.com/documentation/metal/creating_and_sampling_textures) to Rust. ![image](https://user-images.githubusercontent.com/2099811/80553340-7db06280-8997-11ea-958f-2ebc647a5464.png) ## Overview - Added the example to a newly created cargo workspace. - I did this because I wanted to add a build script to generate Rust types from metal shader types (C header files) - Instead of using the makefile this example is compiling the shaders in the same `build.rs` that is generating the rust types. I didn't see any reason that this slight deviation from existing patterns would be an issue - but just let me know! Co-authored-by: Chinedu Francis Nwafili <[email protected]>
bah, we need to migrate to GHA (github actions) |
Something scary happening on nightly - https://travis-ci.org/github/gfx-rs/metal-rs/jobs/682192239#L183 |
Timed out. |
This is really solid and really timely work @chinedufn. I've been meaning to figure out how to share types between metal, and rust but I've been putting it off. I'm a big fan of your dual quaternion shader blog post (https://www.chinedufn.com/dual-quaternion-shader-explained/), I've been using it as a demo to show people why dqs are really good. |
@adamnemecek thanks you're way too kind! DQs all the way for sure! |
This PR ports the Swift/Objective-C Creating and Sampling Textures Tutorial to Rust.
Overview
Added the example to a newly created cargo workspace.
Instead of using the makefile this example is compiling the shaders in the same
build.rs
that is generating the rust types. I didn't see any reason that this slight deviation from existing patterns would be an issue - but just let me know!