MetalDevice: Fix incorrect mipmap texture creation

This commit is contained in:
Stenzek 2023-12-18 18:17:41 +10:00
parent 2d4191f8ce
commit 6fe2177ee7
No known key found for this signature in database

View file

@ -22,9 +22,6 @@ Log_SetChannel(MetalDevice);
// TODO: Disable hazard tracking and issue barriers explicitly. // 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 // 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. // to be between 1 (AMD/NV) and 64 (Intel). So, we'll go with 64 here.
static constexpr u32 TEXTURE_UPLOAD_ALIGNMENT = 64; static constexpr u32 TEXTURE_UPLOAD_ALIGNMENT = 64;
@ -56,8 +53,6 @@ static constexpr std::array<MTLPixelFormat, static_cast<u32>(GPUTexture::Format:
MTLPixelFormatBGR10A2Unorm, // RGB10A2 MTLPixelFormatBGR10A2Unorm, // RGB10A2
}; };
static constexpr std::array<float, 4> s_clear_color = {};
static unsigned s_next_bad_shader_id = 1; static unsigned s_next_bad_shader_id = 1;
static NSString* StringViewToNSString(const std::string_view& str) static NSString* StringViewToNSString(const std::string_view& str)
@ -700,7 +695,7 @@ id<MTLDepthStencilState> MetalDevice::GetDepthState(const GPUPipeline::DepthStat
MTLCompareFunctionEqual, // Equal MTLCompareFunctionEqual, // Equal
}}; }};
MTLDepthStencilDescriptor* desc = [[[MTLDepthStencilDescriptor alloc] init] autorelease]; MTLDepthStencilDescriptor* desc = [[MTLDepthStencilDescriptor new] autorelease];
desc.depthCompareFunction = func_mapping[static_cast<u8>(ds.depth_test.GetValue())]; desc.depthCompareFunction = func_mapping[static_cast<u8>(ds.depth_test.GetValue())];
desc.depthWriteEnabled = ds.depth_write ? TRUE : FALSE; desc.depthWriteEnabled = ds.depth_write ? TRUE : FALSE;
@ -778,7 +773,7 @@ std::unique_ptr<GPUPipeline> MetalDevice::CreatePipeline(const GPUPipeline::Grap
MTLBlendOperationMax, // Max MTLBlendOperationMax, // Max
}}; }};
MTLRenderPipelineDescriptor* desc = [[[MTLRenderPipelineDescriptor alloc] init] autorelease]; MTLRenderPipelineDescriptor* desc = [[MTLRenderPipelineDescriptor new] autorelease];
desc.vertexFunction = static_cast<const MetalShader*>(config.vertex_shader)->GetFunction(); desc.vertexFunction = static_cast<const MetalShader*>(config.vertex_shader)->GetFunction();
desc.fragmentFunction = static_cast<const MetalShader*>(config.fragment_shader)->GetFunction(); desc.fragmentFunction = static_cast<const MetalShader*>(config.fragment_shader)->GetFunction();
@ -1054,12 +1049,13 @@ std::unique_ptr<GPUTexture> MetalDevice::CreateTexture(u32 width, u32 height, u3
@autoreleasepool @autoreleasepool
{ {
MTLTextureDescriptor* desc = [[[MTLTextureDescriptor alloc] init] autorelease]; MTLTextureDescriptor* desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pixel_format
desc.width = width; width:width
desc.height = height; height:height
desc.depth = levels; mipmapped:(levels > 1)];
desc.pixelFormat = pixel_format;
desc.mipmapLevelCount = levels; desc.mipmapLevelCount = levels;
desc.storageMode = MTLStorageModeShared;
if (samples > 1) if (samples > 1)
{ {
desc.textureType = (layers > 1) ? MTLTextureType2DMultisampleArray : MTLTextureType2DMultisample; desc.textureType = (layers > 1) ? MTLTextureType2DMultisampleArray : MTLTextureType2DMultisample;
@ -1068,6 +1064,7 @@ std::unique_ptr<GPUTexture> MetalDevice::CreateTexture(u32 width, u32 height, u3
else if (layers > 1) else if (layers > 1)
{ {
desc.textureType = MTLTextureType2DArray; desc.textureType = MTLTextureType2DArray;
desc.arrayLength = layers;
} }
switch (type) switch (type)
@ -1153,7 +1150,7 @@ std::unique_ptr<GPUSampler> MetalDevice::CreateSampler(const GPUSampler::Config&
{0xFFFFFFFFu, MTLSamplerBorderColorOpaqueWhite}, {0xFFFFFFFFu, MTLSamplerBorderColorOpaqueWhite},
}; };
MTLSamplerDescriptor* desc = [[[MTLSamplerDescriptor alloc] init] autorelease]; MTLSamplerDescriptor* desc = [[MTLSamplerDescriptor new] autorelease];
desc.normalizedCoordinates = true; desc.normalizedCoordinates = true;
desc.sAddressMode = ta[static_cast<u8>(config.address_u.GetValue())]; desc.sAddressMode = ta[static_cast<u8>(config.address_u.GetValue())];
desc.tAddressMode = ta[static_cast<u8>(config.address_v.GetValue())]; desc.tAddressMode = ta[static_cast<u8>(config.address_v.GetValue())];
@ -1245,7 +1242,7 @@ bool MetalDevice::CheckDownloadBufferSize(u32 required_size)
if (m_download_buffer != nil) if (m_download_buffer != nil)
[m_download_buffer release]; [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]; m_download_buffer = [[m_device newBufferWithLength:required_size options:options] retain];
if (m_download_buffer == nil) if (m_download_buffer == nil)
{ {
@ -1408,10 +1405,9 @@ void MetalDevice::InvalidateRenderTarget(GPUTexture* t)
void MetalDevice::CommitClear(MetalTexture* tex) void MetalDevice::CommitClear(MetalTexture* tex)
{ {
DebugAssert(tex->IsRenderTargetOrDepthStencil());
if (tex->GetState() == GPUTexture::State::Cleared) if (tex->GetState() == GPUTexture::State::Cleared)
{ {
DebugAssert(tex->IsRenderTargetOrDepthStencil());
tex->SetState(GPUTexture::State::Dirty); tex->SetState(GPUTexture::State::Dirty);
// TODO: We could combine it with the current render pass. // TODO: We could combine it with the current render pass.