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

[icrate] Help with the creation of a spinning triangle example #483

Closed
PicoLeaf opened this issue Aug 21, 2023 · 9 comments
Closed

[icrate] Help with the creation of a spinning triangle example #483

PicoLeaf opened this issue Aug 21, 2023 · 9 comments

Comments

@PicoLeaf
Copy link
Contributor

I was trying to use icrate for metal context creation, but the complete lack of any guidance on the topic was rough especially since it isn't something I've done before (not on macos), so I'd be interested in contributing an example for this, I'm making this issue to get help.

The goal is a spinning triangle against a black background with each vertex assigned a different color.
The "spinning triangle part" would be done in the vertex shader instead of the main thread.
From what I gathered, there needs to be done a few things for this to work:

  • Create a window (that's not too bad)
  • Get the default metal device (that's also easy)
  • Create a MTKView within the window (I havent figured that one out)
  • load the shaders
  • prepare the pipeline state
  • setup a triangle buffer, aswell as a data buffer for communicating the timestep for the spinning
  • enter the main event loop, this require creating a delegate of the MTKView if I'm not mistaken
  • Handle all of the drawable logic and the buffer changes for the timestep

PLEASE tell me everything I've gotten wrong, I have very little clue of what I'm doing, I don't even know if all of this is possible with the current version of icrate!

@silvanshade
Copy link
Contributor

silvanshade commented Aug 22, 2023

@PicoLeaf A metal example with some animation would be really interesting to have!

I think it should be possible to implement this with icrate in its current state.

I don't really know much about Metal but I was able to make a very minimal template which you should be able to use to create something more advanced with animation.

I uploaded it at my local fork under the metal-example branch under examples/metal.rs

You can run the example with the following command from the objc2 project root:

cargo run --package=icrate --example=metal --features=unstable-example-metal

This should create a window with a static triangle that looks like this:
Screenshot 2023-08-22 at 3 37 32 PM

There's no logic for creating an app menu or quitting through the UI without ctrl-c but you can look at the browser.rs example for how to implement that.

Hope that helps.

@PicoLeaf
Copy link
Contributor Author

Woah, this is awesome, it sure helps, thank you so much!

@silvanshade
Copy link
Contributor

By the way, one small thing I missed: currentDrawable is nullable, so it should return an Option. I've updated the example with the fix.

@madsmtm
Copy link
Owner

madsmtm commented Aug 25, 2023

Note that the plan is to move the metal crate to use icrate, so in the future we'll probably have some consolidation with their examples.

For now though, I think the example that @silvanshade provided will do nicely, could you put up a PR?

@PicoLeaf
Copy link
Contributor Author

Nice, I actually have the spinning animation part mostly written out, would you be intrested in seing that in your example?

@silvanshade
Copy link
Contributor

Nice, I actually have the spinning animation part mostly written out, would you be intrested in seing that in your example?

Sure! If you'd like to create a PR with your modified version I think that would be great.

@PicoLeaf
Copy link
Contributor Author

All right, i've made a pull request on your local fork :),
I'm not sure it's really perfect, a few things come to mind, like that creating two buffers doesn't sound super reasonable performance wise and the fact that there is two separate buffers in the first place bothers me, I couldn't figure out anything better though.
I also had to use NSDate for time keeping, I didn't really knew how to store a date for later use inside drawInMTKView other than using the delegate, but, precise time keeping might not even be neccesary if the draw function is called at regular interval anyways.

@silvanshade
Copy link
Contributor

All right, i've made a pull request on your local fork :), I'm not sure it's really perfect, a few things come to mind, like that creating two buffers doesn't sound super reasonable performance wise and the fact that there is two separate buffers in the first place bothers me, I couldn't figure out anything better though. I also had to use NSDate for time keeping, I didn't really knew how to store a date for later use inside drawInMTKView other than using the delegate, but, precise time keeping might not even be neccesary if the draw function is called at regular interval anyways.

Thanks! I think it's okay to use multiple buffers but maybe there is a better way to do it. One thing I did notice though is that we didn't really need to set up the additional encoder in this case (although it could be useful for an example with more complex composite data). I went ahead and merged your commits and did some refactoring and created the PR for this repo at #489

@PicoLeaf
Copy link
Contributor Author

Now that your PR is open, further discussion can continue over there and I can close this issue :)

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

No branches or pull requests

3 participants