Post not yet marked as solved
For running my app on devices with P3 screens, to take advantage of the P3 colors, which MTLPixelFormat is the proper one to use for my MTKView? My app is for iOS, but it’s nice to be able to debug on the simulator or as a Catalyst app.
Right now, I’ve been using MTLPixelFormatBGR10_XR_sRGB and MTLPixelFormatBGRA10_XR_sRGB per the recommendation in WWDC 2016 session 605 for iOS support of P3 color. This appears to work fine on iOS devices, but on my Apple silicon Mac, when running as a Catalyst app, the colors sometimes look lighter than they should, but it doesn’t always happen. When I run in the simulator on this Mac, my app crashes. When I was using an Intel Mac (with an sRGB screen), I was having my app render in sRGB-only with MTLPixelFormat.bgra8Unorm. MTLPixelFormat.bgra8Unorm also works fine on my Apple silicon Mac, but of course, I’m not getting the P3 color space.
For Apple Silicon Macs, as the GPU hardware is more like an iOS device than like an Intel Mac, should the same pixel formats work on an Apple silicon Mac as on iOS devices? Or are the pixel formats that work with Apple Silicon Macs the same as those on Intel Macs?
I posted a question in the playgrounds section of the forums regarding an issue with Metal in Swift playgrounds: Metal playground not setting up MTLLibrary with makeDefaultLibrary(), but I figured someone might better know on a general level whether Metal even works in playgrounds or not. I'm not sure if I'm putting my shader file in the correct location in the playground - I tried putting it in both the sources and resources folders of the playground.
When an object that contains a semaphore gets automatically deallocated by ARC in my app, I get an EXC_BREAKPOINT or an EXC_BAD_INSTRUCTION with the message "BUG IN CLIENT OF LIBDISPATCH: Semaphore object deallocated while in use". The call stack shows libdispatch.dylib`_dispatch_semaphore_dispose.cold.1 as the location of the crash. I noticed that this error doesn't occur when the semaphore's wait/signal calls are balanced such that the semaphore's counter is at the value I initially passed in (see code below). In my actual code (which is in Metal rendering code that's getting called every frame), I don't know what the semaphore's counter is at at any given moment (and because of the abstraction of a semaphore, I shouldn't have to know what the counter is at), so how do I make sure that the object that holds the semaphore (and the semaphore itself) is deallocated properly so I don't get this crash on deallocation?class Test {
var semaphore = DispatchSemaphore(value: 3)
}
var test = Test()
// will not crash
test.semaphore.wait(timeout: DispatchTime.distantFuture)
test.semaphore.signal()
test = Test()
// will crash
test.semaphore.wait(timeout: DispatchTime.distantFuture)
test = Test()
Post not yet marked as solved
For the color values I'm passing to my Metal shaders, I'm wanting to have them in P3 since the content of my MTKView is something that makes sense to have in P3. My question is that because these values are stored in extended sRGB (as recommended in WWDC 2016 605 What's New In Metal Part 2), what's the range (below 0.0 and above 1.0) that these values can fall in? Does it vary based on the color channel since the red and green channels are more affected by P3's wider space? Does it vary depending on whether the MTKView's colorPixelFormat is MTLPixelFormat.bgra10_xr or MTLPixelFormat.bgra10_xr_srgb, since the second one is doing gamma correction?Also, how does this affect getting to a UIColor equivalent to the value (and vice versa)? I'm having to convert between UIColor and a Metal-readable form (a float4) because the colors appearing in the MTKView are user-selected, thereby involving UIKit controls that need the color as a UIColor value.
I've got a .h file set up just like in the Metal Xcode template (I copy and pasted the relevant content from one of those files into another existing project not made from the template), but when I define a struct in there similar to how it's defined in the template, my Swift code isn't finding the definition - so the compiler says the struct type doesn't exist. The shaders seem to be fine since I've got the #include statement for the shared .h file in the shader file, so it's just the Swift code that seems to have an issue. Any suggestions on how to fix this? Below is a sample of what I tried.#ifndef ShaderTypes_h
#define ShaderTypes_h
#ifdef __METAL_VERSION__
#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
#define NSInteger metal::int32_t
#else
#import <Foundation/Foundation.h>
#endif
#include <simd/simd.h>
typedef struct {
int test;
} Test;
#endif /* ShaderTypes_h */In the Swift code I just tried defining an instance of the struct type, and then I get use of undefined type/identifier errors.
Post not yet marked as solved
I have a UIViewController subclass that I made in a regular app, and I tried moving it over to my playground, but there are some layout issues happening in the playground that weren't in the regular app (and there were no Auto Layout-related messages in the console when running the app). I've got a UIStackView pinned to the bottom of the view controller's view (meaning the UIView at the highest level of the view hierarchy), and that seems to be where most of the issues are happening.In the playground, the live view is set to an instance of my UIViewController subclass. I'm also seeing different (but both incorrect) results when running the playground on my Mac vs. in the Swift Playgrounds app. When running on the Mac in Xcode, the live view looks more like the size of an iPhone, but the size reported by the view controller instance's view's bounds is 768x1024, which is an iPad screen size.All of this UI setup is done in code - no Interface Builder files involved since I've had so many issues with external files in playgrounds.Any suggestions on how to fix this and/or if this is a common issue?
Post not yet marked as solved
Some of my playground's code is in a public repo I have on my GitHub - will this cause that part of my playground to look like copied code even though it's my own? Should I make a note in one of the written responses on my submission that this code is from a repo of my own?
Post not yet marked as solved
I'm thinking about porting an existing Xcode project (so meaning a regular app) to a playground, and I used one of Xcode's templates as a starting point. Is this an ok template to have used since it's one from Apple? In this case, it's the Metal app template. Would I just need to note that I used open source code?
Post not yet marked as solved
When I call makeDefaultLibrary() on the MTLDevice instance in my Mac playground (which I confirmed is set up correctly), it returns nil. Is this some weird playground setup thing? It doesn't matter whether my shaders (.metal) file is in the sources or resources folder for the playground. I also tried MTLDevice's makeLibrary(source:options:) just to see if it was an issue with the playground finding the Metal shader file, and that didn't help.This Metal setup code was in a regular Mac app before, and it worked fine in there.I'm putting this code in a playground because it's my WWDC scholarship submission.
Post not yet marked as solved
What Mac hardware and macOS version will the Mac playgrounds be run on?
Post not yet marked as solved
I have some textures in my Metal app that have areas where the alpha is less than 1, and Metal is putting a black background on the texture when it's mapped to an object in my shader. Totall transparent areas of the texture are black, and semi-transparent areas of the textures look like they're getting blended with the black background rather than what's truly behind them. How do I fix this?
When I click the capture GPU frame button in the debug bar when running my iOS app, Xcode says No capture boundary detected and therefore isn't able to show me a frame capture. This happens even though draw is getting called for my MTKView before I click the capture GPU frame button, and a call to the command buffer's present() method is in the draw method (I haven't done anything to set a default capture scope since this method call is there). What's causing this? I can provide more details on my implementation if necessary. I tried this in Xcode 9.4.1 and Xcode 10.0 beta 6 running the app on two iOS devices, both of which are running iOS 11.4.1.Thanks!
I'm trying to extend UICollectionViewDataSource to add a method of my own via a protocol extension, but when I override the default implementation in the class that adopts UICollectionViewDataSource, the protocol extension's implementation is still called. Due to this being for some custom UI, I'm having to call the method using the collection view's dataSource property. A simplified version of what I'm trying to do is like this:// here's where I'm trying to define a method of my own
extension UICollectionViewDataSource {
func myMethod() {
print("myMethod called in default implementation")
}
}
class ViewController: UIViewController {
// yes, I'm deliberately defining a collection view property separately since I need multiple collection views in the same view controller and don't want them to use the entire window size
var collectionView = UICollectionView(frame: CGRect(x: 20, y: 20, width: 20, height: 20))
override func viewDidLoad() {
collectionView.dataSource = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "identifier")
}
}
extension ViewController: UICollectionViewDataSource {
// these 2 methods are only here so the compiler doesn't complain
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath)
}
// here's where I'm overriding it with the specific implementation
func myMethod() {
print("myMethod called in overridden implementation")
}
}
// then in my custom UI code (like a UICollectionView subclass), something like this would be called, and this would call the default implementation in the protocol extension
dataSource?.myMethod()Of course, if I'm overriding the default protocol implementation, the expection would be that the overridden implementation is what would get called. Why is this happening, and what can be done to get the desired behavior (if possible)?Thanks!
Post not yet marked as solved
I was watching the WWDC 2018 session 225 A Tour of UICollectionView, and in the last section (starts around 28:30 in the video), it walked through how to animate changes to the collection view. One thing it didn't show is how to handle the animation for inserting an element in a part of the collection view that isn't currently visible (in my case, at the end of it). In saying that, I followed the session's recommendations, but I also added a call to scrollToItem(at:at:animated) at the end of the update code since without it, the collection view updates but doesn't scroll/show the newly added element. With that, it does smoothly show the newly added element at the end.Is this the proper thing to do to get it to show the newly added element with proper animation?Here's the relevant code from the action method of the button that triggers this:// (update the model's data - not shown)
let newItemIndexPath = IndexPath(item: collectionView.numberOfItems(inSection: 0), section: 0)
let update = { self.collectionView.insertItems(at: [newItemIndexPath]) }
collectionView.performBatchUpdates(update)
collectionView.scrollToItem(at: newItemIndexPath, at: .right, animated: true) // the line I'm wondering aboutThanks!
Is an enrollment verification document from my school a sufficient proof of enrollment?