Repository navigation
Add Metal Renderer and native macOS/iOS support for TIC-80 - #2973
Conversation
|
Can you elaborate a bit more on what this supports... does it support zoom factors, background border, etc? Haven't had a change to compile it yet for a look-see. What is the big advantage over SDL (on Mac OS)? Are you hoping to revive the iOS port? |
| rightChannel?[writeIdx + i] = sampleR | ||
| } | ||
| } else { | ||
| for i in 0..<samplesToCopy { |
There was a problem hiding this comment.
Think we could use memset here, yes?
There was a problem hiding this comment.
Mentioned in the above comment that this is not the main bottleneck
| for i in 0..<samplesToCopy { | ||
| let sampleL = Float(samplesBuffer[(self.bufferReadIdx + i) * 2]) / 32768.0 | ||
| let sampleR = Float(samplesBuffer[(self.bufferReadIdx + i) * 2 + 1]) / 32768.0 | ||
|
|
||
| leftChannel[writeIdx + i] = sampleL | ||
| rightChannel?[writeIdx + i] = sampleR | ||
| } |
There was a problem hiding this comment.
AI suggestion:
I’d also consider replacing the per-sample copy loop (lines 51–57) with an interleaved→planar bulk conversion (e.g., vDSP) later,
Do you understand the suggestion? I did imagine there would be a more optimize way to do this transfer, and sounds like there is.
There was a problem hiding this comment.
I am specialized in audio programming (see for instance my previous project https://github.com/Wang-Yue/cdsp). vDSP is only tremendously better when doing bulk conversion for thousands of samples. In this paticular case if we pull every frame, we only get 44100/60 = 735 samples. vDSP performs badly when you have small chunk size, as there are cost calling vDSP functions --- they are not inline functions. Also the code here and below is not the major concern unless we
- solve the main bottleneck, which is the software renderer.
- add api to start/stop audio, as currently we are still draining battery when there's nothing playing.
There was a problem hiding this comment.
Makes sense. I'll take your word for it. :-)
as currently we are still draining battery when there's nothing playing.
Sounds like something worth investigating one day in a future PR maybe. (after other things as you mention)
| } | ||
|
|
||
| func windowWillClose(_ notification: Notification) { | ||
| NSApp.terminate(nil) |
There was a problem hiding this comment.
Is this a final notice or something we could interact with? Remember sometimes we might have editor changes we need to save.
There was a problem hiding this comment.
No. SDL backend also don't check this. I just tried, no final notice or anything.
I also don't think TIC-80 expose functions that let us check if editor have changes.
|
I did my best to review this a bit. Looks pretty awesome though overall (the functionality - getting this back in core)... |
|
how does loading/saving cartridges on IOS work? Do you use |
Thank you for the detailed review!
as mentioned above, there are still many functionalities I have not implemented/tested, and while I was playing on iPads myself, I already found a few bugs. mac app feels far more ready than iOS, btw. so I am constantly fixing them in my main branch. Once the PR is accepted I will send those incremental fixes to here. I also believe when the users test this on their apple device they will notice bugs I didn't notice, as they may have a different usage pattern compared to mine. So I can address bugs/missing features in later PRs for incremental fixes whenever I have time. Or other interested party may contribute their PRs. It's impossible for me to get to close to 100% ready in the first PR. The good thing is we now have the full app running on iPhone and iPad, and playing games work end to end. so iterative improvements become possible now. |
zoom --- yes. On a mac I think we support almost everything SDL supports. iOS is less ready than Mac. iOS window cannot be set with fixed aspect ratio, so I do have commits to draw the background border as well (https://github.com/Wang-Yue/TIC-80/commit/c7027c2f9510587a93ba102f865025e795ae939b) when it's rendered in a window. I can send those incremental improvements later when this PR is merged.
Higher performance for both audio and graphics. Cleaner callbacks. Here's TIC-80 running the supernova game (60fps 3D car race) profile: You can see in the SDL case only 70% of the run time is leaving for TIC-80's For close to static screens, things are even more extraordinary. This is TIC-80's music editor when music is not playing:
You see SDL is very busy and takes 55% of the run time
with the help of the draw cache, our new backend spend most time doing _platform_memcmp, then find all of them hit the cache, and all the rendering functions become no-op. CPU is almost idle in this case. This is super crucial for mobile and low powered devices. I also want to add that the biggest _platform_memcmp on the right (around 23% of the running time) can be eliminated once TIC-80 decide to accept my draw cache patch. This PR is written with even draw cache not being accepted in mind, so we still compare final buffer every single frame when shipping rendered buffer to GPU. With Draw Cache PR accepted, we will know without memcmp if the buffer is dirty or not, and fully eliminate the 23% memcmp time, saving even more CPU cycles.
Yes. Before working on this port I spent a long time to fix the SDL port for iOS, but even after a lot of work, it's still almost not usable --- Existing user interface is very buggy, and it is running very slow. At this stage, I believe starting from scratch is much easier than fixing that port. So I did it. |
|
@nesbox I'm not sure how you want to merge/release this, but this looks pretty awesome to me. Obviously would be room for you to make an official iOS release in the app store in the future as well if you were so interested... this looks like a pretty solid foundation for that. |
|
Merged pr/skip-clean-screen-blit and pr/draw-cache here, as they provide API to indicate if the buffer is updated or not, which is used to skip rendering for metal backend, instead of using memcmp. NOTE: #2974 is not merged in this as that is an independent change. To test performance please also merge that which will benefit CPU utilization in code mode. |
|
Thanks for folding the two branches in — that makes this the single thread for the mechanism, and #3003 and #2975 are now closed into it. On the cache itself: all three points raised on #2975 are addressed here, which I checked in the tree rather than the description — On the Apple backend, the direction is: no iOS plans, and it stays a build-it-yourself path rather than a platform we ship. Three things before this can merge:
And the rendering optimisation wants to sit behind one build flag. Merging this must not change behaviour for anyone: the default build stays exactly as it is today, and whoever wants the optimisation builds it in.
Today only half of it is gated, and that is the part to fix: |
- Add native Apple Metal backend for macOS with Swift, Metal, AppKit, AVFoundation, and GameController - Gate draw cache and clean screen skip rendering optimizations behind CMake option BUILD_RENDER_CACHE (OFF by default) - Add -DBUILD_APPLE=ON compile check to macOS CI matrix in build.yml - Add macOS Metal build instructions to README.md - Optimize mouse cursor stationary CPU usage in Studio
302057b to
68be9ca
Compare
|
Thanks, PTAL. |
nesbox
left a comment
There was a problem hiding this comment.
Requesting changes. The gating of the optimisation itself is complete and correct — config field, menu entry, the blit skip, all eight bindings — so the flag work is done properly. Three things, though, changed the default build with both flags off, and those have to come back behind the flags first: the condition is that merging changes behaviour for nobody.
Default path — before merge
studio.c:1467,:2323-2332,:2410-2413— the cursor reordering is not under#if defined(BUILD_RENDER_CACHE), so it lands on every platform. Two consequences:vram.vars.cursornow lags a frame, because the tick runs before the sync where it used to run after; and the write goes throughVBANK(tic, 0)whileblitCursorreads the active bank, so a cart that leavesvbank(1)selected loses the studio cursor (bank 1 is{sprite=0, system=false}andblitCursorreturns early). Dropping theVBANKwrapper and writing to the sameram->vramthatblitCursorreads is the small fix.cmake/core.cmake:48-50,CMakeLists.txt:7-9—enable_language(OBJC)andfft.cas Objective-C are gated onAPPLE, not onBUILD_APPLE. Every macOS build compilessrc/ext/fft.cas ObjC, and since onlyCMAKE_C_FLAGS_RELEASEis set, the OBJC language misses the-O2 -fltoits C siblings get. Nothing needs it: there is no.m/.mmin the tree, and Swift takes the bridging header through-import-objc-header.sdl/main.c:560-562—SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal")under__APPLE__overrides the"opengl"hint set at:2090, switching shipped macOS SDL builds to Metal. The Apple backend does not compile this file at all, so the line buys this PR nothing.
The cache, with the flag on
- A false "clean" freezes the screen: the cache verdict (
core.c:673) returns before the vrammemcmp(core.c:679), so a mutation through an API that is not hooked is invisible —function TIC() mset(1,1,t%16) map(0,0,30,17) enddraws frame 1 forever. cache_api_pixreturns 0 to the cartridge on a cache hit (draw_cache.c:529) —cls(3) print(pix(0,0),0,0,12)prints 3 once and then zeroes.map()with a remap function: uncheckedmallocand a truncated size (draw_cache.c:749-782) overflow the buffer onmap(0,0,100000,100000,0,0,nil,nil,1,function(t) return t end).has_scn/has_bdrare never set by the wren, miniscript and forth bindings, so scanline/border effects can disappear for those languages.- The replay truncates
print/fonttext at 511 characters (draw_cache.c:341-354).
Apple backend
main.swift:127-128subtracts the letterbox a second time, somouse()is off by (−16,−8) and the cursor cannot reach the left 8 / top 4 pixels.AudioEngine.swift:42-53copies the samples after releasing the lock; SDL holds its mutex across the whole copy.- The documented build (
README:350,build.yml:712) omits-DBUILD_STATIC=ON, which is off by default on macOS — as written it links no script runtimes, so the binary cannot load a cartridge.
What would make this mergeable
- the three default-path items fixed, or moved under the same flags;
-DBUILD_RENDER_CACHE=ONand-DBUILD_STATIC=ONadded to the Apple CI step, so this code compiles somewhere — nothing in CI buildsdraw_cache.ctoday, which is why all of the above is green;- the cache defects above, at least the ones that produce a wrong picture rather than a slow one.
|
Thank you so much @nesbox for the detail review! I made a commit to address your review comments. All requested changes have been addressed across the default path, render cache, and Apple backend. lemme know how to further improve this. 1. Default Path (Zero Behavioral Changes with Flags OFF)
2. Cache Optimization Fixes (
|




This commit introduces native macOS and iOS support using Metal, AVAudioEngine, GCController.