public interface MPSImageAllocator extends NSSecureCoding
Sometimes it is prohibitively costly for MPS to figure out how big an image should be in advance. In addition, you may want to have some say over whether the image is a temporary image or not. In such circumstances, the MPSImageAllocator is used to provide the developer with an opportunity for just in time feedback about how the image should be allocated.
Two standard MPSImageAllocators are provided: MPSImageDefaultAllocator and MPSTemporaryImageDefaultAllocator. You may of course provide your own allocator instead.
Example:
[@code]
// Note: MPSImageDefaultAllocator is already provided
// by the framework under that name. It is provided here
// as sample code for writing your own variant.
-(MPSImage * __nonnull) imageForCommandBuffer: (__nonnull id
// make sure the object sticks around at least as lomg as the command buffer
[result retain];
[cmdBuf addCompletedHandler: ^(id
// return autoreleased result
return [result autorelease];
};
-(BOOL) supportsSecureCoding{ return YES; }
-(void)encodeWithCoder:(NSCoder * __nonnull)aCoder
{
[super encodeWithCoder: aCoder];
// encode any data owned by the class at this level
}
-(nullable instancetype) initWithCoder: (NSCoder*__nonnull) aDecoder
{
self = [super initWithCoder: aDecoder ];
if( nil == self )
return self;
// use coder to load any extra data kept by this object here
return self;
}
[@endcode]
Please see [MPSImage defaultAllocator] and [MPSTemporaryImage defaultAllocator]
for implentations of the protocol already provided by MPS.
When considering whether to write your own MPSImageAllocator, you should know
the existing MPSImage and MPSTemporaryImage default allocators are optimized
to make image batch allocation much faster than one MPSImage at a time in a loop.
When possible, it can be better to use the MPS provided allocators and override
the behavior in a padding policy instead, if the changes can be contained in
the MPSImageDescriptor. This will help reduce CPU encode time. However, custom
padding policies can inhibit optimizations in the MPSNNGraph, particularly node
fusion, resulting in more work for the GPU. In cases where the custom padding method
does not change filter properties but only adjusts the result image (e.g. adjust result
feature channel format) then MPSNNPaddingMethodCustomAllowForNodeFusion may be
used to signal that node fusion is acceptable.
| Modifier and Type | Method and Description |
|---|---|
MPSImage |
imageForCommandBufferImageDescriptorKernel(MTLCommandBuffer cmdBuf,
MPSImageDescriptor descriptor,
MPSKernel kernel)
Create a new MPSImage
|
_supportsSecureCodingencodeWithCoder, initWithCoderMPSImage imageForCommandBufferImageDescriptorKernel(MTLCommandBuffer cmdBuf, MPSImageDescriptor descriptor, MPSKernel kernel)
See class description for sample implementation
cmdBuf - The MTLCommandBuffer on which the image will be initialized.
cmdBuf.device encodes the MTLDevice.descriptor - A MPSImageDescriptor containing the image format to use.
This format is the result of your MPSPadding policy.kernel - The kernel that will overwrite the image returned by the filter.
Note that the MPS implementations of this protocol don't need
this field. It is provided for your convenience.