我正在尝试将我的 Opengl 应用程序迁移到 Metal,并且我正在使用CAMetalLayer
Metal 实现。我在继承的NSView
类对象上设置了以下属性:
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
[self setWantsLayer:YES];
我已经检查了其他答案,比如NSView 子类 - drawRect: not called
所以,我尝试按照上面的答案中的建议分配代表,就像这样
[self.layer setDelegate:self];
我收到以下编译错误:
Cannot initialize a parameter of type 'id<CALayerDelegate> _Nullable' with an lvalue of type 'MyNSView *'
此外,我还在https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/SettingUpLayerObjects/SettingUpLayerObjects.html 上看到了设置图层对象的文档,其中指出:
对于具有自定义内容的图层支持视图,您应该继续覆盖视图的方法来进行绘图。层支持的视图会自动使自己成为其层的委托并实现所需的委托方法,您不应更改该配置。相反,您应该实现视图的 drawRect: 方法来绘制您的内容。
因此,据此,我什至不必在我的情况下设置委托,而且我已经drawRect
在MyNSView
. 仍然没有被调用。
还有什么需要做的吗?
谢谢!
编辑:所以,我通过让我的层遵循协议来修复编译错误CALayerDelegate
,但我仍然没有收到任何对drawLayer
或 的调用drawRect
。
制作支持层
- (CALayer*)makeBackingLayer
{
id <MTLDevice> device = MTLCreateSystemDefaultDevice();
CAMetalLayer *backingLayer = [CAMetalLayer layer];
backingLayer.opaque = YES;
backingLayer.device = device;
backingLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
backingLayer.framebufferOnly = YES;
// [backingLayer setDelegate:self]; //Tried setting the delegate
//here as well
return backingLayer;
}
初始化函数
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
[self setWantsLayer:YES];
[self.layer setDelegate:self];
绘制函数
- (void)displayLayer:(CALayer *)layer
{
[self drawRect:self.bounds];
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
[self drawRect:self.bounds];
}
-(void) drawRect: (NSRect) dirtyRect
{
[super drawRect:dirtyRect];
}
我已经实现了所有的绘制函数来检查它们中是否有任何一个被击中,但我没有收到任何这些函数的调用。
为了将内容绘制到 NSView,我绘制到屏幕外资源并drawable
从 NSView 的 CALayer获取并presentDrawable
在提交commandBuffer
.
由于这里没有太多我可以调试以找到引入错误的位置,因此我开始尝试执行指令的顺序。事实证明,您调用的顺序是:
[self setWantsLayer:YES];
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
导致了这个问题。在交换这两个语句时,我开始接收对我的displayLayer
函数的调用。我仍然无法弄清楚为什么drawRect
没有调用层支持的 NSView。
发布这个以防万一这种交换也解决了其他人的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句