Dolphin, the GameCube and Wii emulator - Forums

Full Version: Some Metal API-related Questions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi everyone,

I'm back. Already. With what I think might be a slightly shorter novel this time. I've been having some fun stumbling my way through Dolphin's video code, and decided to try to make a rudimentary Metal backend (just as a learning exercise, don't expect anything). Regrettably, I'm currently hung up on the most basic of things: drawing to Dolphin's window using Metal.

I've copied the Null backend, renamed all necessary stuff, linked the Metal and QuartzCore frameworks (unless that one was already linked, I can't remember), added a Metal entry to the APIList enum, added an instance of the backend to the g_available_video_backends vector in VideoBackendBase:TongueopulateList(), and then selected it in the UI.

From what I've gathered, it seems the recommended (or only) way to get realtime graphics on the screen with Metal is through a CAMetalLayer, either with a layer-backed view or a layer-hosted view. I have tried a number of ways to achieve this goal in Dolphin which I won't bore you all with the details of, but the most successful I've been so far is by directly creating a CAMetalLayer and passing it to Dolphin's window_handle (by reinterpret_cast-ing the handle to an NSView), then setting wantsLayer to true in VideoBackend::Initialize(). Also in Initialize(), I wrote some test code to draw a single triangle to the texture contained in the CAMetalLayer, which brings me to the "successful" part of this exercise. Although the screen is still plain black, I know that the Metal code is solid because when I stop emulation, I see the triangle perfectly rendered for a split second before the window gets destroyed/hidden.

So, anyone know what's going on? I've tried really hard to figure out the solution on my own, but I think I just need to be pointed in the right direction and I'll be off to the races.

Thanks again!
I can't help with OSX plattform dependent code, but feel free to ignore dolphin's window handle and to just create a new window. This might be better for the thread localiness, as the window is created on another thread as the context, and we're using this context on another thread again ...
(04-19-2017, 03:50 PM)degasus Wrote: [ -> ]I can't help with OSX plattform dependent code, but feel free to ignore dolphin's window handle and to just create a new window. This might be better for the thread localiness, as the window is created on another thread as the context, and we're using this context on another thread again ...

Thanks, degasus. I tried your idea of creating my own window, and it led to an interesting discovery - the window did not show up until stopping the emulation, at which point it stayed open until the app closes (well that part isn't interesting; I just hadn't destroyed the window yet). Moving the window creation code to Video_Prepare did the same thing. Printing to cout revealed both methods were being called at the correct times, though. So I was incredibly confused.

An hour of googling and testing stuff later, turns out macOS can sometimes be very particular about which thread you run UI code in. So, I tried running the code in the main thread and voila! It worked! Big Grin

And even better, my original code to render directly into dolphin's window handle also works perfectly when I run it on the main thread. Go freaking figure.

Thanks again degasus. You definitely helped point me in the right direction. I'm amazed at the dedication of the dolphin dev community.
I no longer use a Mac, but I used to have one as my daily machine. I can tell you that Mac owners will be immensely grateful if your Metal backend becomes useable. macOS is currently restricted to openGL 4.1/4.2 and performance isn't as good as it could be. Not to mention that newer Macs use AMD GPUs which have very poor openGL performance.

My apologies for not being able to offer anything more than encouragement, but good luck all the same!
(04-20-2017, 07:43 AM)seoulgamer Wrote: [ -> ]I no longer use a Mac, but I used to have one as my daily machine. I can tell you that Mac owners will be immensely grateful if your Metal backend becomes useable. macOS is currently restricted to openGL 4.1/4.2 and performance isn't as good as it could be. Not to mention that newer Macs use AMD GPUs which have very poor openGL performance.

My apologies for not being able to offer anything more than encouragement, but good luck all the same!

Thanks seoulgamer! I'm actually not even the target market for this type of thing either as although I use Macs daily for all of my work stuff, my main PC is a hackintosh I dualboot Windows on, so at any point I can switch to Windows within minutes and just use Vulkan if I so desire to play GC/Wii games. It'll be fun (if I ever get anywhere) to see if my Metal backend can get close to Vulkan's performance on Windows on the same hardware. Smile
I'm very curious if this is still in the works, since I have a few friends with Mac's that are dying to try out a few of their old GC games on their machine without having to worry about buggy OpenGL
OpenGL isn't buggy, it's that Apple doesn't support the new versions so Dolphin has to use the slower, older OpenGL commands or even off-load some things to the CPU to render, making it slower.
All-in-all though, the games should run fine assuming they can run the games at 100%
(05-20-2017, 08:19 AM)mstreurman Wrote: [ -> ]I'm very curious if this is still in the works, since I have a few friends with Mac's that are dying to try out a few of their old GC games on their machine without having to worry about buggy OpenGL
Still inching away at it when I have time. Smile Extremely little progress so far, though. Been spending most of my time reading code from the other backends and VideoCommon. Tell your friends to hold out a few years or use OGL, lol
(05-20-2017, 08:55 AM)zachrwolfe Wrote: [ -> ]Tell your friends to hold out a few years or use OGL, lol


It would be amazing if you complete the Metal backend
I'm a Mac user (And although I can boot on Windows, my main SO is MacOS) and the Mac version of Dolphin hasn't received love in the recent years. Having this backend will for sure revive it. As it is now, most Mac users install Windows to use Dolphin as it performs really bad on MacOS, so not even Mac users (like me) give it any love. Having a competent backend on MacOS will make it finally usable
(06-02-2017, 05:00 AM)ddnava Wrote: [ -> ]so not even Mac users (like me) give it any love
Feel free to just help him: https://github.com/zachrwolfe/dolphin/tree/master/Source/Core/VideoBackends/Metal
Pages: 1 2