113 lines
2.6 KiB
Plaintext
Raw Normal View History

2024-11-01 16:55:46 +08:00
#include "RenderAPI.h"
#include "PlatformBase.h"
// Metal implementation of RenderAPI.
#if SUPPORT_METAL
#include "Unity/IUnityGraphicsMetal.h"
#import <Metal/Metal.h>
2024-11-22 12:09:31 +08:00
#import <Foundation/Foundation.h>
#import <MetalFX/MTLFXSpatialScaler.h>
2024-11-01 16:55:46 +08:00
class RenderAPI_Metal : public RenderAPI
{
public:
RenderAPI_Metal();
virtual ~RenderAPI_Metal() { }
2024-11-22 12:09:31 +08:00
virtual void processDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces);
2024-11-01 16:55:46 +08:00
2024-11-22 12:09:31 +08:00
virtual bool getUsesReverseZ() { return true; }
2024-11-01 16:55:46 +08:00
2024-11-22 12:09:31 +08:00
virtual void initSupportFeature();
2024-11-01 16:55:46 +08:00
2024-12-17 20:50:36 +08:00
virtual void spatialUpScale(void*data);
2024-11-01 16:55:46 +08:00
private:
2024-11-22 12:09:31 +08:00
id <MTLFXSpatialScaler> mfx_spatial_scaler;
IUnityGraphicsMetal* metal_graphics;
2024-11-01 16:55:46 +08:00
};
RenderAPI* CreateRenderAPI_Metal()
{
return new RenderAPI_Metal();
}
RenderAPI_Metal::RenderAPI_Metal()
{
}
2024-11-22 12:09:31 +08:00
void RenderAPI_Metal::processDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces)
2024-11-01 16:55:46 +08:00
{
if (type == kUnityGfxDeviceEventInitialize)
{
2024-11-22 12:09:31 +08:00
metal_graphics = interfaces->Get<IUnityGraphicsMetal>();
2024-11-01 16:55:46 +08:00
}
else if (type == kUnityGfxDeviceEventShutdown)
{
//@TODO: release resources
}
}
2024-11-22 12:09:31 +08:00
void RenderAPI_Metal::initSupportFeature()
2024-11-01 16:55:46 +08:00
{
2024-12-17 20:35:00 +08:00
if (@available(iOS 16.0, macOS 13.0, *))
2024-11-22 12:09:31 +08:00
{
support_features[GraphicsFeature::METAL_FX_SPATIAL_SR] = true;
support_features[GraphicsFeature::METAL_FX_TEMPORAL_SR] = true;
}
2024-11-01 16:55:46 +08:00
}
2024-12-17 20:50:36 +08:00
void RenderAPI_Metal::spatialUpScale(void* data)
2024-11-01 16:55:46 +08:00
{
2024-12-17 20:35:00 +08:00
if (@available(iOS 16.0, macOS 13.0, *))
2024-11-22 12:09:31 +08:00
{
2024-12-17 20:50:36 +08:00
struct DataPack
{
void* src;
void* dst;
};
DataPack* data_pack = static_cast<DataPack*>(data);
id<MTLTexture> srctex = (__bridge id<MTLTexture>)data_pack->src;
id<MTLTexture> dsttex = (__bridge id<MTLTexture>)data_pack->dst;
2024-11-22 12:09:31 +08:00
id<MTLDevice> _device = metal_graphics->MetalDevice();
id<MTLCommandBuffer> cmd = (id<MTLCommandBuffer>)metal_graphics->CurrentCommandBuffer();
cmd.label = @"Upscale Command Buffer";
if (mfx_spatial_scaler == nil)
{
MTLFXSpatialScalerDescriptor* desc = [[MTLFXSpatialScalerDescriptor alloc]init];
desc.inputWidth = [srctex width];
desc.inputHeight = [srctex height];
desc.outputWidth = [dsttex width];
desc.outputHeight = [dsttex height];
desc.colorTextureFormat = [srctex pixelFormat];
desc.outputTextureFormat = [dsttex pixelFormat];
desc.colorProcessingMode = MTLFXSpatialScalerColorProcessingModeLinear;
mfx_spatial_scaler = [desc newSpatialScalerWithDevice:_device];
if (mfx_spatial_scaler == nil)
{
return;
}
}
mfx_spatial_scaler.colorTexture = srctex;
mfx_spatial_scaler.outputTexture = dsttex;
[mfx_spatial_scaler encodeToCommandBuffer:cmd];
[cmd commit];
}
2024-11-01 16:55:46 +08:00
}
#endif // #if SUPPORT_METAL