check for pixel format support

This commit is contained in:
Samuliak 2024-08-19 16:40:36 +02:00
parent 69a36246fb
commit ca256eb764
7 changed files with 64 additions and 11 deletions

View File

@ -73,8 +73,8 @@ LatteTextureMtl::LatteTextureMtl(class MetalRenderer* mtlRenderer, Latte::E_DIM
desc->setArrayLength(effectiveBaseDepth); desc->setArrayLength(effectiveBaseDepth);
} }
auto formatInfo = GetMtlPixelFormatInfo(format, isDepth); auto pixelFormat = GetMtlPixelFormat(format, isDepth, m_mtlr->GetPixelFormatSupport());
desc->setPixelFormat(formatInfo.pixelFormat); desc->setPixelFormat(pixelFormat);
// HACK: even though the textures are never written to from a shader, we still need to use `ShaderWrite` usage to prevent pink lines over the screen // HACK: even though the textures are never written to from a shader, we still need to use `ShaderWrite` usage to prevent pink lines over the screen
MTL::TextureUsage usage = MTL::TextureUsageShaderRead | MTL::TextureUsageShaderWrite; MTL::TextureUsage usage = MTL::TextureUsageShaderRead | MTL::TextureUsageShaderWrite;

View File

@ -127,8 +127,8 @@ MTL::Texture* LatteTextureViewMtl::CreateSwizzledView(uint32 gpuSamplerSwizzle)
swizzle.blue = GetMtlTextureSwizzle(compSelB); swizzle.blue = GetMtlTextureSwizzle(compSelB);
swizzle.alpha = GetMtlTextureSwizzle(compSelA); swizzle.alpha = GetMtlTextureSwizzle(compSelA);
auto formatInfo = GetMtlPixelFormatInfo(format, m_baseTexture->IsDepth()); auto pixelFormat = GetMtlPixelFormat(format, m_baseTexture->IsDepth(), m_mtlr->GetPixelFormatSupport());
MTL::Texture* texture = m_baseTexture->GetTexture()->newTextureView(formatInfo.pixelFormat, textureType, NS::Range::Make(baseLevel, levelCount), NS::Range::Make(baseLayer, layerCount), swizzle); MTL::Texture* texture = m_baseTexture->GetTexture()->newTextureView(pixelFormat, textureType, NS::Range::Make(baseLevel, levelCount), NS::Range::Make(baseLayer, layerCount), swizzle);
return texture; return texture;
} }

View File

@ -1,6 +1,7 @@
#include "Cafe/HW/Latte/Renderer/Metal/LatteToMtl.h" #include "Cafe/HW/Latte/Renderer/Metal/LatteToMtl.h"
#include "Common/precompiled.h" #include "Common/precompiled.h"
#include "Metal/MTLDepthStencil.hpp" #include "Metal/MTLDepthStencil.hpp"
#include "Metal/MTLPixelFormat.hpp"
#include "Metal/MTLRenderCommandEncoder.hpp" #include "Metal/MTLRenderCommandEncoder.hpp"
#include "Metal/MTLRenderPipeline.hpp" #include "Metal/MTLRenderPipeline.hpp"
#include "Metal/MTLSampler.hpp" #include "Metal/MTLSampler.hpp"
@ -74,7 +75,6 @@ std::map<Latte::E_GX2SURFFMT, MetalPixelFormatInfo> MTL_COLOR_FORMAT_TABLE = {
}; };
std::map<Latte::E_GX2SURFFMT, MetalPixelFormatInfo> MTL_DEPTH_FORMAT_TABLE = { std::map<Latte::E_GX2SURFFMT, MetalPixelFormatInfo> MTL_DEPTH_FORMAT_TABLE = {
// TODO: one of these 2 formats is not supported on Apple silicon
{Latte::E_GX2SURFFMT::D24_S8_UNORM, {MTL::PixelFormatDepth24Unorm_Stencil8, MetalDataType::NONE, 4, {1, 1}, true}}, {Latte::E_GX2SURFFMT::D24_S8_UNORM, {MTL::PixelFormatDepth24Unorm_Stencil8, MetalDataType::NONE, 4, {1, 1}, true}},
{Latte::E_GX2SURFFMT::D24_S8_FLOAT, {MTL::PixelFormatDepth32Float_Stencil8, MetalDataType::NONE, 4, {1, 1}, true}}, {Latte::E_GX2SURFFMT::D24_S8_FLOAT, {MTL::PixelFormatDepth32Float_Stencil8, MetalDataType::NONE, 4, {1, 1}, true}},
{Latte::E_GX2SURFFMT::D32_S8_FLOAT, {MTL::PixelFormatDepth32Float_Stencil8, MetalDataType::NONE, 5, {1, 1}, true}}, {Latte::E_GX2SURFFMT::D32_S8_FLOAT, {MTL::PixelFormatDepth32Float_Stencil8, MetalDataType::NONE, 5, {1, 1}, true}},
@ -105,6 +105,37 @@ const MetalPixelFormatInfo GetMtlPixelFormatInfo(Latte::E_GX2SURFFMT format, boo
return formatInfo; return formatInfo;
} }
MTL::PixelFormat GetMtlPixelFormat(Latte::E_GX2SURFFMT format, bool isDepth, const MetalPixelFormatSupport& pixelFormatSupport)
{
auto pixelFormat = GetMtlPixelFormatInfo(format, isDepth).pixelFormat;
if (!pixelFormatSupport.m_supportsR8Unorm_sRGB && pixelFormat == MTL::PixelFormatR8Unorm_sRGB)
pixelFormat = MTL::PixelFormatRGBA8Unorm_sRGB;
if (!pixelFormatSupport.m_supportsRG8Unorm_sRGB && pixelFormat == MTL::PixelFormatRG8Unorm_sRGB)
pixelFormat = MTL::PixelFormatRGBA8Unorm_sRGB;
if (!pixelFormatSupport.m_supportsPacked16BitFormats)
{
switch (pixelFormat)
{
case MTL::PixelFormatB5G6R5Unorm:
case MTL::PixelFormatA1BGR5Unorm:
case MTL::PixelFormatABGR4Unorm:
case MTL::PixelFormatBGR5A1Unorm:
pixelFormat = MTL::PixelFormatRGBA8Unorm;
break;
default:
break;
}
}
if (!pixelFormatSupport.m_supportsDepth24Unorm_Stencil8 && pixelFormat == MTL::PixelFormatDepth24Unorm_Stencil8)
pixelFormat = MTL::PixelFormatDepth32Float_Stencil8;
return pixelFormat;
}
inline uint32 CeilDivide(uint32 a, uint32 b) { inline uint32 CeilDivide(uint32 a, uint32 b) {
return (a + b - 1) / b; return (a + b - 1) / b;
} }

View File

@ -1,15 +1,11 @@
#pragma once #pragma once
#include <Metal/Metal.hpp> #include "Cafe/HW/Latte/Renderer/Metal/MetalCommon.h"
#include "Cafe/HW/Latte/ISA/LatteReg.h" #include "Cafe/HW/Latte/ISA/LatteReg.h"
#include "Cafe/HW/Latte/Core/LatteConst.h" #include "Cafe/HW/Latte/Core/LatteConst.h"
//#include "Cafe/HW/Latte/Core/FetchShader.h" //#include "Cafe/HW/Latte/Core/FetchShader.h"
#include "Cafe/HW/Latte/Renderer/Renderer.h" #include "Cafe/HW/Latte/Renderer/Renderer.h"
#include "Metal/MTLDepthStencil.hpp"
#include "Metal/MTLRenderPipeline.hpp"
#include "Metal/MTLSampler.hpp"
#include "Metal/MTLTexture.hpp"
struct Uvec2 { struct Uvec2 {
uint32 x; uint32 x;
@ -34,6 +30,8 @@ struct MetalPixelFormatInfo {
const MetalPixelFormatInfo GetMtlPixelFormatInfo(Latte::E_GX2SURFFMT format, bool isDepth); const MetalPixelFormatInfo GetMtlPixelFormatInfo(Latte::E_GX2SURFFMT format, bool isDepth);
MTL::PixelFormat GetMtlPixelFormat(Latte::E_GX2SURFFMT format, bool isDepth, const MetalPixelFormatSupport& pixelFormatSupport);
size_t GetMtlTextureBytesPerRow(Latte::E_GX2SURFFMT format, bool isDepth, uint32 width); size_t GetMtlTextureBytesPerRow(Latte::E_GX2SURFFMT format, bool isDepth, uint32 width);
size_t GetMtlTextureBytesPerImage(Latte::E_GX2SURFFMT format, bool isDepth, uint32 height, size_t bytesPerRow); size_t GetMtlTextureBytesPerImage(Latte::E_GX2SURFFMT format, bool isDepth, uint32 height, size_t bytesPerRow);

View File

@ -1,8 +1,25 @@
#pragma once #pragma once
#include "Foundation/NSString.hpp" #include <Foundation/Foundation.hpp>
#include <Metal/Metal.hpp> #include <Metal/Metal.hpp>
struct MetalPixelFormatSupport
{
bool m_supportsR8Unorm_sRGB;
bool m_supportsRG8Unorm_sRGB;
bool m_supportsPacked16BitFormats;
bool m_supportsDepth24Unorm_Stencil8;
MetalPixelFormatSupport() = default;
MetalPixelFormatSupport(MTL::Device* device)
{
m_supportsR8Unorm_sRGB = device->supportsFamily(MTL::GPUFamilyApple1);
m_supportsRG8Unorm_sRGB = device->supportsFamily(MTL::GPUFamilyApple1);
m_supportsPacked16BitFormats = device->supportsFamily(MTL::GPUFamilyApple1);
m_supportsDepth24Unorm_Stencil8 = device->supportsFamily(MTL::GPUFamilyMac2);
}
};
#define MAX_MTL_BUFFERS 31 #define MAX_MTL_BUFFERS 31
// Buffer index 30 is reserved for the support buffer, buffer indices 27-29 are reserved for the helper shaders // Buffer index 30 is reserved for the support buffer, buffer indices 27-29 are reserved for the helper shaders
#define GET_MTL_VERTEX_BUFFER_INDEX(index) (MAX_MTL_BUFFERS - index - 5) #define GET_MTL_VERTEX_BUFFER_INDEX(index) (MAX_MTL_BUFFERS - index - 5)

View File

@ -36,6 +36,7 @@ MetalRenderer::MetalRenderer()
// Feature support // Feature support
m_hasUnifiedMemory = m_device->hasUnifiedMemory(); m_hasUnifiedMemory = m_device->hasUnifiedMemory();
m_isAppleGPU = m_device->supportsFamily(MTL::GPUFamilyApple1); m_isAppleGPU = m_device->supportsFamily(MTL::GPUFamilyApple1);
m_pixelFormatSupport = MetalPixelFormatSupport(m_device);
// Resources // Resources
MTL::SamplerDescriptor* samplerDescriptor = MTL::SamplerDescriptor::alloc()->init(); MTL::SamplerDescriptor* samplerDescriptor = MTL::SamplerDescriptor::alloc()->init();

View File

@ -371,6 +371,11 @@ public:
return m_isAppleGPU; return m_isAppleGPU;
} }
const MetalPixelFormatSupport& GetPixelFormatSupport() const
{
return m_pixelFormatSupport;
}
MTL::StorageMode GetOptimalTextureStorageMode() const MTL::StorageMode GetOptimalTextureStorageMode() const
{ {
return (m_isAppleGPU ? MTL::StorageModeShared : MTL::StorageModePrivate); return (m_isAppleGPU ? MTL::StorageModeShared : MTL::StorageModePrivate);
@ -397,6 +402,7 @@ private:
// Feature support // Feature support
bool m_hasUnifiedMemory; bool m_hasUnifiedMemory;
bool m_isAppleGPU; bool m_isAppleGPU;
MetalPixelFormatSupport m_pixelFormatSupport;
// Managers and caches // Managers and caches
class MetalMemoryManager* m_memoryManager; class MetalMemoryManager* m_memoryManager;