From 6fe2177ee79f5d55479e94e5cc0ae04fcbccaf32 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Mon, 18 Dec 2023 18:17:41 +1000 Subject: [PATCH] MetalDevice: Fix incorrect mipmap texture creation --- src/util/metal_device.mm | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/util/metal_device.mm b/src/util/metal_device.mm index 7e87d8c298..02785fc932 100644 --- a/src/util/metal_device.mm +++ b/src/util/metal_device.mm @@ -22,9 +22,6 @@ // TODO: Disable hazard tracking and issue barriers explicitly. -static constexpr MTLPixelFormat LAYER_MTL_PIXEL_FORMAT = MTLPixelFormatRGBA8Unorm; -static constexpr GPUTexture::Format LAYER_TEXTURE_FORMAT = GPUTexture::Format::RGBA8; - // Looking across a range of GPUs, the optimal copy alignment for Vulkan drivers seems // to be between 1 (AMD/NV) and 64 (Intel). So, we'll go with 64 here. static constexpr u32 TEXTURE_UPLOAD_ALIGNMENT = 64; @@ -56,8 +53,6 @@ MTLPixelFormatBGR10A2Unorm, // RGB10A2 }; -static constexpr std::array s_clear_color = {}; - static unsigned s_next_bad_shader_id = 1; static NSString* StringViewToNSString(const std::string_view& str) @@ -700,7 +695,7 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_ MTLCompareFunctionEqual, // Equal }}; - MTLDepthStencilDescriptor* desc = [[[MTLDepthStencilDescriptor alloc] init] autorelease]; + MTLDepthStencilDescriptor* desc = [[MTLDepthStencilDescriptor new] autorelease]; desc.depthCompareFunction = func_mapping[static_cast(ds.depth_test.GetValue())]; desc.depthWriteEnabled = ds.depth_write ? TRUE : FALSE; @@ -778,7 +773,7 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_ MTLBlendOperationMax, // Max }}; - MTLRenderPipelineDescriptor* desc = [[[MTLRenderPipelineDescriptor alloc] init] autorelease]; + MTLRenderPipelineDescriptor* desc = [[MTLRenderPipelineDescriptor new] autorelease]; desc.vertexFunction = static_cast(config.vertex_shader)->GetFunction(); desc.fragmentFunction = static_cast(config.fragment_shader)->GetFunction(); @@ -1054,12 +1049,13 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_ @autoreleasepool { - MTLTextureDescriptor* desc = [[[MTLTextureDescriptor alloc] init] autorelease]; - desc.width = width; - desc.height = height; - desc.depth = levels; - desc.pixelFormat = pixel_format; + MTLTextureDescriptor* desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pixel_format + width:width + height:height + mipmapped:(levels > 1)]; + desc.mipmapLevelCount = levels; + desc.storageMode = MTLStorageModeShared; if (samples > 1) { desc.textureType = (layers > 1) ? MTLTextureType2DMultisampleArray : MTLTextureType2DMultisample; @@ -1068,6 +1064,7 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_ else if (layers > 1) { desc.textureType = MTLTextureType2DArray; + desc.arrayLength = layers; } switch (type) @@ -1153,7 +1150,7 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_ {0xFFFFFFFFu, MTLSamplerBorderColorOpaqueWhite}, }; - MTLSamplerDescriptor* desc = [[[MTLSamplerDescriptor alloc] init] autorelease]; + MTLSamplerDescriptor* desc = [[MTLSamplerDescriptor new] autorelease]; desc.normalizedCoordinates = true; desc.sAddressMode = ta[static_cast(config.address_u.GetValue())]; desc.tAddressMode = ta[static_cast(config.address_v.GetValue())]; @@ -1245,7 +1242,7 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_ if (m_download_buffer != nil) [m_download_buffer release]; - constexpr MTLResourceOptions options = MTLResourceStorageModeShared | MTLResourceOptionCPUCacheModeDefault; + constexpr MTLResourceOptions options = MTLResourceStorageModeShared | MTLResourceCPUCacheModeDefaultCache; m_download_buffer = [[m_device newBufferWithLength:required_size options:options] retain]; if (m_download_buffer == nil) { @@ -1408,10 +1405,9 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_ void MetalDevice::CommitClear(MetalTexture* tex) { - DebugAssert(tex->IsRenderTargetOrDepthStencil()); - if (tex->GetState() == GPUTexture::State::Cleared) { + DebugAssert(tex->IsRenderTargetOrDepthStencil()); tex->SetState(GPUTexture::State::Dirty); // TODO: We could combine it with the current render pass.