Skip to content

Commit

Permalink
MetalDevice: Fix incorrect mipmap texture creation
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Dec 18, 2023
1 parent 2d4191f commit 6fe2177
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/util/metal_device.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,8 +53,6 @@
MTLPixelFormatBGR10A2Unorm, // RGB10A2
};

static constexpr std::array<float, 4> s_clear_color = {};

static unsigned s_next_bad_shader_id = 1;

static NSString* StringViewToNSString(const std::string_view& str)
Expand Down Expand Up @@ -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<u8>(ds.depth_test.GetValue())];
desc.depthWriteEnabled = ds.depth_write ? TRUE : FALSE;

Expand Down Expand Up @@ -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<const MetalShader*>(config.vertex_shader)->GetFunction();
desc.fragmentFunction = static_cast<const MetalShader*>(config.fragment_shader)->GetFunction();

Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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<u8>(config.address_u.GetValue())];
desc.tAddressMode = ta[static_cast<u8>(config.address_v.GetValue())];
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6fe2177

Please sign in to comment.