Macos 使用NSView的图层备份图形未调用drawRect
我正在尝试将Opengl应用程序迁移到Metal,我正在使用Macos 使用NSView的图层备份图形未调用drawRect,macos,calayer,nsview,metal,Macos,Calayer,Nsview,Metal,我正在尝试将Opengl应用程序迁移到Metal,我正在使用CametLayer实现Metal。 我已在继承的NSView类对象上设置了以下属性: self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize; [self setWantsLayer:YES]; 我查过其他答案,比如 所以,我试着按照上面答案中的建议分配代表,类似这样 [self.layer setDelegate:self]; 我得到以
CametLayer
实现Metal。
我已在继承的NSView
类对象上设置了以下属性:
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
[self setWantsLayer:YES];
我查过其他答案,比如
所以,我试着按照上面答案中的建议分配代表,类似这样
[self.layer setDelegate:self];
我得到以下编译错误:
Cannot initialize a parameter of type 'id<CALayerDelegate> _Nullable' with an lvalue of type 'MyNSView *'
初始化函数
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];
}
我已经实现了所有的draw函数来检查它们是否被击中,但是我没有收到任何函数的调用
为了将内容绘制到NSView,我绘制到屏幕外资源,从NSView的CALayer获取
drawable
,并在提交commandBuffer
之前调用presentDrawable
,因为这里没有太多我可以调试的内容来找到bug的引入位置,所以我开始玩指令执行的顺序。事实证明,你打电话的顺序是:
[self setWantsLayer:YES];
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
这是问题的根源。在交换这两条语句时,我开始接收对我的displayLayer
函数的调用。我仍然不明白为什么层支持的NSView没有调用drawRect
发布此消息,以防此交换也会为其他人解决问题。为什么不使用
MTKView的子类
?您是如何安排为图层使用CametLayer
?您是否覆盖了-makeBackingLayer
?如果是,请显示该方法以及您尝试配置该层的任何其他位置。@Kenthomass仅在使用金属渲染时使用NSView是一种限制。是的,我正在使用makeBackingLayer
生成层。我将用相关的代码片段更新问题。@Kenthomass更新了问题中的详细信息。NSView
的其他方法有哪些?你还设置了哪些其他属性?例如,您是否覆盖-wantsUpdateLayer
以返回true?@kenthomase我已尝试覆盖-wantsUpdateLayer
以返回true,然后覆盖UpdateLayer
。但是,显然没有人打电话给他。在命令缓冲区上调用commit
后,我还尝试将layerContentsRedrawPolicy
更改为NSViewLayerContentsRedrawOnSetNeedsDisplay
,并调用[nsView setNeedsDisplay:YES]
,但这似乎也不会触发绘图。
[self setWantsLayer:YES];
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;