我花了一些时间,试图了解苹果公司的金属图形API的夜晚。我遇到了一个令人沮丧的问题,因此必须缺少一些基本的东西:仅当禁用深度测试或将深度功能更改为“更大”时,我才能使渲染的对象显示在屏幕上。可能出什么问题了?另外,我可以为了调试这个问题检查什么样的事情?
这是我在做什么:
1)我正在使用SDL创建我的窗口。设置Metal时,我手动创建一个CAMetalLayer并将其插入到图层层次结构中。需要明确的是,我没有使用MTKView,也不想使用MTKView。尽可能远离Objective-C和Cocoa似乎是将应用程序编写为跨平台的最佳策略。目的是使用SDL和可在运行时交换的渲染引擎编写与平台无关的C ++代码。这个界面的背后是所有苹果专用的代码将生活。但是,我强烈怀疑出现问题的部分原因与设置层有关:
SDL_SysWMinfo windowManagerInfo;
SDL_VERSION(&windowManagerInfo.version);
SDL_GetWindowWMInfo(&window, &windowManagerInfo);
// Create a metal layer and add it to the view that SDL created.
NSView *sdlView = windowManagerInfo.info.cocoa.window.contentView;
sdlView.wantsLayer = YES;
CALayer *sdlLayer = sdlView.layer;
CGFloat contentsScale = sdlLayer.contentsScale;
NSSize layerSize = sdlLayer.frame.size;
_metalLayer = [[CAMetalLayer layer] retain];
_metalLayer.contentsScale = contentsScale;
_metalLayer.drawableSize = NSMakeSize(layerSize.width * contentsScale,
layerSize.height * contentsScale);
_metalLayer.device = device;
_metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
_metalLayer.frame = sdlLayer.frame;
_metalLayer.framebufferOnly = true;
[sdlLayer addSublayer:_metalLayer];
Run Code Online (Sandbox Code Playgroud)
2)I创建深度纹理深度缓冲器来使用。我的理解是,此步骤在Metal中是必需的。虽然,在OpenGL中,框架很会自动为我创建一个深度缓冲:
CGSize drawableSize = _metalLayer.drawableSize;
MTLTextureDescriptor *descriptor = …Run Code Online (Sandbox Code Playgroud)