Reputation: 1552
I am very new to Objective-c so bear with me I am still trying to make sense of the multitude of places you can put properties and class variables in objective c.
I am an experienced Metal developer but I have barely done anything with objective-c.
I am trying to create a MetalCore class that will hold the MTLDevice, MTLCommandQueue and facilitate the creation of the apps core pipelines etc.
I therefore defined the following
@property (readonly, nonatomic, assign) MTLDevice* devicePtr;
In my class however I get the error Unknown type name MTLDevice; did you mean...
even though I #import <Metal/Metal.h>
so what is going on here?
I looked on stack overflow and found examples where people were defining function like this
- (<MTLDevice> *)device;
or
- (instancetype)initWithDevice:(id<MTLDevice>) device;
What is going on with this whole id and <> thing? In what cases can you omit the id part?
What I find especially weird is while id seems to be required for most Metal types there are some like MTLRenderPipelineDescriptor
that dont need it and in fact only work in the Type*
mode. Why is this and how do I determine what is needed from the documentation?
Upvotes: 0
Views: 1416
Reputation: 157
If you know java ,id<MTLDevice>
is like a object that inherited the Interface named ‘MTLDevice’.
Mainly due to different iPhone has different GPU architectures. For the maximum efficiency,different GPU has different implementation of metal.
Such as in iPhoneX the real MTLDevice is a class named 'AGXA11Device' , while in iPhone 6 plus is 'AGXA9Device'.
Upvotes: 0
Reputation: 31782
In Objective-C, id
basically means "any object type". If you want to ensure that an object conforms to a protocol (such as MTLDevice
), you specify the protocol in angle brackets after id
when stating its type: id<MTLDevice>
means "a type that conforms to the MTLDevice
protocol".
Unlike with concrete classes (such as MTLRenderPipelineDescriptor
), you don't use a *
after id, so you wouldn't write id<MTLDevice> *
(unless you were taking a pointer to a device, which is uncommon). MTLDevice *
doesn't make sense because MTLDevice
isn't a concrete type. <MTLDevice>
by itself is nonsense, and a syntax error, as is <MTLDevice> *
.
Coming from Swift, you might be used to conflating between protocols and concrete types, since Swift doesn't make a syntactic distinction between "some type that conforms to a protocol" and "some concrete type." To tell whether something is a protocol, consult the documentation; it'll explicitly note when something is a protocol. In Metal, all "descriptor" types are concrete (e.g., MTLRenderPipelineDescriptor
), while most other object types are abstracted behind protocols (MTLDevice
, MTLCommandQueue
, MTLLibrary
, MTLFunction
, MTLTexture
, MTLRenderPipelineState
, MTLCommandBuffer
, etc.)
Upvotes: 6