diff --git a/native/cocos/renderer/gfx-metal/MTLContext.mm b/native/cocos/renderer/gfx-metal/MTLContext.mm index 8d72fe997f..48aa2df28d 100644 --- a/native/cocos/renderer/gfx-metal/MTLContext.mm +++ b/native/cocos/renderer/gfx-metal/MTLContext.mm @@ -54,9 +54,12 @@ CCMTLContext::CCMTLContext(Device *device) bool CCMTLContext::initialize(const ContextInfo &info) { _vsyncMode = info.vsyncMode; _windowHandle = info.windowHandle; - CAMetalLayer *layer = (CAMetalLayer *)((CCMTLDevice *)_device)->getMTLLayer(); + + CCMTLDevice *device = (CCMTLDevice *)_device; + CAMetalLayer *layer = (CAMetalLayer *)device->getMTLLayer(); _colorFmt = toFormat(layer.pixelFormat); - _depthStencilFmt = toFormat(MTLPixelFormatDepth24Unorm_Stencil8); + id dssTex = (id)device->getDSSTexture(); + _depthStencilFmt = toFormat(dssTex.pixelFormat); return true; } diff --git a/native/cocos/renderer/gfx-metal/MTLDevice.mm b/native/cocos/renderer/gfx-metal/MTLDevice.mm index 12818aaf03..b4f608ea31 100644 --- a/native/cocos/renderer/gfx-metal/MTLDevice.mm +++ b/native/cocos/renderer/gfx-metal/MTLDevice.mm @@ -78,7 +78,11 @@ bool CCMTLDevice::initialize(const DeviceInfo &info) { _inFlightSemaphore = CC_NEW(CCMTLSemaphore(MAX_FRAMES_IN_FLIGHT)); _currentFrameIndex = 0; +#if CC_PLATFORM == CC_PLATFORM_MAC_OSX NSView *view = (NSView *)_windowHandle; +#else + UIView *view = (UIView *)_windowHandle; +#endif CAMetalLayer *layer = static_cast(view.layer); _mtlLayer = layer; id mtlDevice = (id)layer.device; @@ -104,13 +108,13 @@ bool CCMTLDevice::initialize(const DeviceInfo &info) { } // Persistent depth stencil texture - MTLTextureDescriptor *textureDescriptor = [[MTLTextureDescriptor alloc] init]; - textureDescriptor.pixelFormat = mu::getSupportedDepthStencilFormat(mtlDevice, gpuFamily, _depthBits); - textureDescriptor.width = info.width; - textureDescriptor.height = info.height; - textureDescriptor.storageMode = MTLStorageModePrivate; - textureDescriptor.usage = MTLTextureUsageRenderTarget; - _dssTex = [mtlDevice newTextureWithDescriptor:textureDescriptor]; + MTLTextureDescriptor *dssDescriptor = [[MTLTextureDescriptor alloc] init]; + dssDescriptor.pixelFormat = mu::getSupportedDepthStencilFormat(mtlDevice, gpuFamily, _depthBits); + dssDescriptor.width = info.width; + dssDescriptor.height = info.height; + dssDescriptor.storageMode = MTLStorageModePrivate; + dssDescriptor.usage = MTLTextureUsageRenderTarget; + _dssTex = [mtlDevice newTextureWithDescriptor:dssDescriptor]; _stencilBits = 8; ContextInfo contextCreateInfo;