ios oc 插件 metal fx 测试

This commit is contained in:
connan 2024-12-20 16:10:04 +08:00
parent a5d0976a8a
commit a8e24d20c8
12 changed files with 232 additions and 43 deletions

View File

@ -0,0 +1,62 @@
// Unity Native Plugin API copyright © 2015 Unity Technologies ApS
//
// Licensed under the Unity Companion License for Unity - dependent projects--see[Unity Companion License](http://www.unity3d.com/legal/licenses/Unity_Companion_License).
//
// Unless expressly provided otherwise, the Software under this license is made available strictly on an “AS IS” BASIS WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.Please review the license for details on these and other terms and conditions.
#pragma once
#include "IUnityInterface.h"
// Has to match the GfxDeviceRenderer enum
typedef enum UnityGfxRenderer
{
//kUnityGfxRendererOpenGL = 0, // Legacy OpenGL, removed
//kUnityGfxRendererD3D9 = 1, // Direct3D 9, removed
kUnityGfxRendererD3D11 = 2, // Direct3D 11
kUnityGfxRendererNull = 4, // "null" device (used in batch mode)
//kUnityGfxRendererOpenGLES20 = 8, // OpenGL ES 2.0, removed
kUnityGfxRendererOpenGLES30 = 11, // OpenGL ES 3.0
//kUnityGfxRendererGXM = 12, // PlayStation Vita, removed
kUnityGfxRendererPS4 = 13, // PlayStation 4
kUnityGfxRendererXboxOne = 14, // Xbox One
kUnityGfxRendererMetal = 16, // iOS Metal
kUnityGfxRendererOpenGLCore = 17, // OpenGL core
kUnityGfxRendererD3D12 = 18, // Direct3D 12
kUnityGfxRendererVulkan = 21, // Vulkan
kUnityGfxRendererNvn = 22, // Nintendo Switch NVN API
kUnityGfxRendererXboxOneD3D12 = 23, // MS XboxOne Direct3D 12
kUnityGfxRendererGameCoreXboxOne = 24, // GameCore Xbox One
kUnityGfxRendererGameCoreXboxSeries = 25, // GameCore XboxSeries
kUnityGfxRendererPS5 = 26, // PS5
kUnityGfxRendererPS5NGGC = 27 // PS5 NGGC
} UnityGfxRenderer;
typedef enum UnityGfxDeviceEventType
{
kUnityGfxDeviceEventInitialize = 0,
kUnityGfxDeviceEventShutdown = 1,
kUnityGfxDeviceEventBeforeReset = 2,
kUnityGfxDeviceEventAfterReset = 3,
} UnityGfxDeviceEventType;
typedef void (UNITY_INTERFACE_API * IUnityGraphicsDeviceEventCallback)(UnityGfxDeviceEventType eventType);
// Should only be used on the rendering thread unless noted otherwise.
UNITY_DECLARE_INTERFACE(IUnityGraphics)
{
UnityGfxRenderer(UNITY_INTERFACE_API * GetRenderer)(); // Thread safe
// This callback will be called when graphics device is created, destroyed, reset, etc.
// It is possible to miss the kUnityGfxDeviceEventInitialize event in case plugin is loaded at a later time,
// when the graphics device is already created.
void(UNITY_INTERFACE_API * RegisterDeviceEventCallback)(IUnityGraphicsDeviceEventCallback callback);
void(UNITY_INTERFACE_API * UnregisterDeviceEventCallback)(IUnityGraphicsDeviceEventCallback callback);
int(UNITY_INTERFACE_API * ReserveEventIDRange)(int count); // reserves 'count' event IDs. Plugins should use the result as a base index when issuing events back and forth to avoid event id clashes.
};
UNITY_REGISTER_INTERFACE_GUID(0x7CBA0A9CA4DDB544ULL, 0x8C5AD4926EB17B11ULL, IUnityGraphics)
// Certain Unity APIs (GL.IssuePluginEvent, CommandBuffer.IssuePluginEvent) can callback into native plugins.
// Provide them with an address to a function of this signature.
typedef void (UNITY_INTERFACE_API * UnityRenderingEvent)(int eventId);
typedef void (UNITY_INTERFACE_API * UnityRenderingEventAndData)(int eventId, void* data);

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: DnxM43+uBi5PTN+VjLUhJiqWT35vZ3X/qYGSh1s9ygOCPHX7gp770Po=
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings:
AddToEmbeddedBinaries: false
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,21 @@
#import "UnityAppController.h"
#include "IUnityGraphics.h"
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces);
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload();
@interface MyAppController : UnityAppController
{
}
- (void)shouldAttachRenderDelegate;
@end
@implementation MyAppController
- (void)shouldAttachRenderDelegate
{
// unlike desktops where plugin dynamic library is automatically loaded and registered
// we need to do that manually on iOS
UnityRegisterRenderingPluginV5(&UnityPluginLoad, &UnityPluginUnload);
}
@end
IMPL_APP_CONTROLLER_SUBCLASS(MyAppController);

View File

@ -0,0 +1,73 @@
fileFormatVersion: 2
guid: BykYsCOpBX+d2b+9FYpH4sRtQQWfzWt//qzo8usCE85bX6WDY31ZgjI=
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 1
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
: Any
second:
enabled: 0
settings:
Exclude Editor: 1
Exclude Linux64: 1
Exclude OSXUniversal: 1
Exclude Win: 1
Exclude Win64: 1
Exclude iOS: 0
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
CPU: AnyCPU
DefaultValueInitialized: true
OS: AnyOS
- first:
Standalone: Linux64
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
Standalone: OSXUniversal
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
Standalone: Win
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
Standalone: Win64
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
iPhone: iOS
second:
enabled: 1
settings:
AddToEmbeddedBinaries: false
CPU: AnyCPU
CompileFlags:
FrameworkDependencies:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -67,7 +67,7 @@ PluginImporter:
AddToEmbeddedBinaries: false AddToEmbeddedBinaries: false
CPU: ARM64 CPU: ARM64
CompileFlags: CompileFlags:
FrameworkDependencies: FrameworkDependencies: Metal;MetalKit;
userData: userData:
assetBundleName: assetBundleName:
assetBundleVariant: assetBundleVariant:

View File

@ -83,7 +83,7 @@ MonoBehaviour:
- {fileID: 1342351342872651138} - {fileID: 1342351342872651138}
- {fileID: 6446168921458335383} - {fileID: 6446168921458335383}
m_RendererFeatureMap: bc3f630842f2e70dd6a559c442a94bfd4529d15534f2d3de228858dca8d12222f0f17d10860a28157820480586dae7578265b12b6ffda01297deaf157b647559 m_RendererFeatureMap: bc3f630842f2e70dd6a559c442a94bfd4529d15534f2d3de228858dca8d12222f0f17d10860a28157820480586dae7578265b12b6ffda01297deaf157b647559
m_UseNativeRenderPass: 0 m_UseNativeRenderPass: 1
postProcessData: {fileID: 11400000, guid: 41439944d30ece34e96484bdb6645b55, type: 2} postProcessData: {fileID: 11400000, guid: 41439944d30ece34e96484bdb6645b55, type: 2}
shaders: shaders:
blitPS: {fileID: 4800000, guid: c17132b1f77d20942aa75f8429c0f8bc, type: 3} blitPS: {fileID: 4800000, guid: c17132b1f77d20942aa75f8429c0f8bc, type: 3}

View File

@ -78,7 +78,7 @@ void RenderAPI_Metal::initSupportFeature()
void RenderAPI_Metal::spatialUpScale(void* data) void RenderAPI_Metal::spatialUpScale(void* data)
{ {
if (@available(iOS 16, macOS 13, *)) // if (@available(iOS 16, macOS 13, *))
{ {
struct DataPack struct DataPack
{ {
@ -93,9 +93,8 @@ void RenderAPI_Metal::spatialUpScale(void* data)
id<MTLDevice> device = metal_graphics->MetalDevice(); id<MTLDevice> device = metal_graphics->MetalDevice();
id<MTLCommandBuffer> cmd = (id<MTLCommandBuffer>)metal_graphics->CurrentCommandBuffer(); id<MTLCommandBuffer> cmd = (id<MTLCommandBuffer>)metal_graphics->CurrentCommandBuffer();
metal_graphics->EndCurrentCommandEncoder(); // metal_graphics->EndCurrentCommandEncoder();
cmd.label = @"Upscale Command Buffer"; cmd.label = @"Upscale Command Buffer";
if (mfx_spatial_scaler == nil ) if (mfx_spatial_scaler == nil )
{ {
MTLFXSpatialScalerDescriptor* desc = [[MTLFXSpatialScalerDescriptor alloc]init]; MTLFXSpatialScalerDescriptor* desc = [[MTLFXSpatialScalerDescriptor alloc]init];
@ -128,34 +127,34 @@ void RenderAPI_Metal::spatialUpScale(void* data)
return; return;
} }
mfx_spatial_scaler.colorTexture = srctex; mfx_spatial_scaler.colorTexture = srctex;
mfx_spatial_scaler.outputTexture = outTexture; mfx_spatial_scaler.outputTexture = dsttex;
mfx_spatial_scaler.inputContentWidth = [srctex width]; // mfx_spatial_scaler.inputContentWidth = [srctex width];
mfx_spatial_scaler.inputContentHeight = [srctex height]; // mfx_spatial_scaler.inputContentHeight = [srctex height];
// if(!cmd) // if(!cmd)
// { // {
// id <MTLCommandBuffer> upscaleCommandBuffer = [commandQueue commandBuffer]; id <MTLCommandBuffer> upscaleCommandBuffer = [commandQueue commandBuffer];
// upscaleCommandBuffer.label = @"Upscale Command Buffer"; upscaleCommandBuffer.label = @"Upscale Command Buffer";
// [mfx_spatial_scaler encodeToCommandBuffer:upscaleCommandBuffer]; [mfx_spatial_scaler encodeToCommandBuffer:upscaleCommandBuffer];
// [upscaleCommandBuffer commit]; [upscaleCommandBuffer commit];
// id <MTLCommandBuffer> textureCommandBuffer = [commandQueue commandBuffer]; // id <MTLCommandBuffer> textureCommandBuffer = [commandQueue commandBuffer];
// id <MTLBlitCommandEncoder> _mfxSpatialEncoder =[textureCommandBuffer blitCommandEncoder]; // id <MTLBlitCommandEncoder> _mfxSpatialEncoder =[textureCommandBuffer blitCommandEncoder];
// [_mfxSpatialEncoder copyFromTexture: outTexture toTexture: dsttex]; // [_mfxSpatialEncoder copyFromTexture: outTexture toTexture: dsttex];
// [_mfxSpatialEncoder endEncoding]; // [_mfxSpatialEncoder endEncoding];
// [textureCommandBuffer commit]; // [textureCommandBuffer commit];
// } // }
// else // else
{ {
[mfx_spatial_scaler encodeToCommandBuffer:cmd]; // [mfx_spatial_scaler encodeToCommandBuffer:cmd];
// [cmd commit]; // [cmd commit];
// id <MTLCommandBuffer> textureCommandBuffer = [commandQueue commandBuffer]; // id <MTLCommandBuffer> textureCommandBuffer = [commandQueue commandBuffer];
id <MTLBlitCommandEncoder> _mfxSpatialEncoder =[textureCommandBuffer blitCommandEncoder]; // id <MTLBlitCommandEncoder> _mfxSpatialEncoder =[textureCommandBuffer blitCommandEncoder];
[_mfxSpatialEncoder copyFromTexture: outTexture toTexture: dsttex]; // [_mfxSpatialEncoder copyFromTexture: outTexture toTexture: dsttex];
[_mfxSpatialEncoder endEncoding]; // [_mfxSpatialEncoder endEncoding];
// [textureCommandBuffer commit]; // [textureCommandBuffer commit];
} }

View File

@ -71,6 +71,7 @@ static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType ev
if (eventType == kUnityGfxDeviceEventInitialize) if (eventType == kUnityGfxDeviceEventInitialize)
{ {
assert(s_current_api == NULL); assert(s_current_api == NULL);
unityLog("kUnityGfxDeviceEventInitialize");
s_device_type = s_graphics->GetRenderer(); s_device_type = s_graphics->GetRenderer();
s_current_api = createRenderAPI(s_device_type); s_current_api = createRenderAPI(s_device_type);
} }
@ -114,10 +115,10 @@ enum NativeRenderingEvent
static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void *data) static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void *data)
{ {
// unityLog("OnRenderEventAndData0");
// Unknown / unsupported graphics device type? Do nothing // Unknown / unsupported graphics device type? Do nothing
if (s_current_api == NULL) if (s_current_api == NULL)
return; return;
switch ((NativeRenderingEvent)eventID) switch ((NativeRenderingEvent)eventID)
{ {
case NativeRenderingEvent::EnableVRS: case NativeRenderingEvent::EnableVRS:

View File

@ -69,18 +69,17 @@ namespace X.Rendering.Feature
lastQuality = quality; lastQuality = quality;
} }
ptr->dst = destination.rt.GetNativeTexturePtr(); ptr->dst = destination.rt.GetNativeTexturePtr();
[DllImport("__Internal", EntryPoint = "callMetalFX_SpatialScaling")] //[DllImport("__Internal", EntryPoint = "callMetalFX_SpatialScaling")]
static extern void CallNativeMethod(IntPtr srcTexture, IntPtr dstTexture); //static extern void CallNativeMethod(IntPtr srcTexture, IntPtr dstTexture);
// cmd.SetRenderTarget(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.DontCare); //CallNativeMethod(source.rt.GetNativeTexturePtr(), destination.rt.GetNativeTexturePtr());
CallNativeMethod(source.rt.GetNativeTexturePtr(), destination.rt.GetNativeTexturePtr()); cmd.IssuePluginEventAndData(RenderingPlugin.GetRenderEventAndDataFunc(), (int)RenderingPlugin.NativeRenderingEvent.SpatialUpScale, dataPtr);
//cmd.Blit(dstRT, destination);
//cmd.IssuePluginEventAndData(RenderingPlugin.GetRenderEventAndDataFunc(), (int)RenderingPlugin.NativeRenderingEvent.SpatialUpScale, dataPtr);
//cmd.Blit(dstRT, destination); //cmd.Blit(dstRT, destination);
} }
//cmd.SetRenderTarget(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store); //cmd.SetRenderTarget(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
//cmd.IssuePluginEventAndData(RenderingPlugin.GetRenderEventAndDataFunc(), (int)RenderingPlugin.NativeRenderingEvent.SpatialUpScale, dataPtr); //cmd.IssuePluginEventAndData(RenderingPlugin.GetRenderEventAndDataFunc(), (int)RenderingPlugin.NativeRenderingEvent.SpatialUpScale, dataPtr);
//SuperResolutionParamSets.Instance.RenderContext.ExecuteCommandBuffer(cmd); SuperResolutionParamSets.Instance.RenderContext.ExecuteCommandBuffer(cmd);
cmd.Clear();
//CommandBufferPool.Release(cmd); //CommandBufferPool.Release(cmd);
} }

View File

@ -10,7 +10,7 @@ PlayerSettings:
AndroidFilterTouchesWhenObscured: 0 AndroidFilterTouchesWhenObscured: 0
AndroidEnableSustainedPerformanceMode: 0 AndroidEnableSustainedPerformanceMode: 0
defaultScreenOrientation: 3 defaultScreenOrientation: 3
targetDevice: 2 targetDevice: 0
useOnDemandResources: 0 useOnDemandResources: 0
accelerometerFrequency: 60 accelerometerFrequency: 60
companyName: DefaultCompany companyName: DefaultCompany
@ -1088,7 +1088,8 @@ PlayerSettings:
Standalone: 0 Standalone: 0
il2cppCompilerConfiguration: {} il2cppCompilerConfiguration: {}
il2cppCodeGeneration: {} il2cppCodeGeneration: {}
managedStrippingLevel: {} managedStrippingLevel:
iPhone: 1
incrementalIl2cppBuild: {} incrementalIl2cppBuild: {}
suppressCommonWarnings: 1 suppressCommonWarnings: 1
allowUnsafeCode: 1 allowUnsafeCode: 1

View File

@ -43,7 +43,7 @@ MonoBehaviour:
width: 1920 width: 1920
height: 954 height: 954
m_ShowMode: 4 m_ShowMode: 4
m_Title: Console m_Title: Game
m_RootView: {fileID: 13} m_RootView: {fileID: 13}
m_MinSize: {x: 875, y: 321} m_MinSize: {x: 875, y: 321}
m_MaxSize: {x: 10000, y: 10000} m_MaxSize: {x: 10000, y: 10000}
@ -97,7 +97,7 @@ MonoBehaviour:
m_MinSize: {x: 640, y: 601} m_MinSize: {x: 640, y: 601}
m_MaxSize: {x: 4000, y: 4021} m_MaxSize: {x: 4000, y: 4021}
vertical: 0 vertical: 0
controlID: 2099 controlID: 14
draggingID: 0 draggingID: 0
--- !u!114 &5 --- !u!114 &5
MonoBehaviour: MonoBehaviour:
@ -150,7 +150,7 @@ MonoBehaviour:
m_MinSize: {x: 200, y: 50} m_MinSize: {x: 200, y: 50}
m_MaxSize: {x: 16192, y: 8096} m_MaxSize: {x: 16192, y: 8096}
vertical: 0 vertical: 0
controlID: 18 controlID: 123
draggingID: 0 draggingID: 0
--- !u!114 &7 --- !u!114 &7
MonoBehaviour: MonoBehaviour:
@ -202,7 +202,7 @@ MonoBehaviour:
m_MinSize: {x: 200, y: 50} m_MinSize: {x: 200, y: 50}
m_MaxSize: {x: 16192, y: 8096} m_MaxSize: {x: 16192, y: 8096}
vertical: 0 vertical: 0
controlID: 120 controlID: 226
draggingID: 0 draggingID: 0
--- !u!114 &9 --- !u!114 &9
MonoBehaviour: MonoBehaviour:
@ -228,7 +228,7 @@ MonoBehaviour:
m_MinSize: {x: 300, y: 100} m_MinSize: {x: 300, y: 100}
m_MaxSize: {x: 24288, y: 16192} m_MaxSize: {x: 24288, y: 16192}
vertical: 0 vertical: 0
controlID: 16 controlID: 121
draggingID: 0 draggingID: 0
--- !u!114 &10 --- !u!114 &10
MonoBehaviour: MonoBehaviour:
@ -403,7 +403,7 @@ MonoBehaviour:
m_MinSize: {x: 200, y: 100} m_MinSize: {x: 200, y: 100}
m_MaxSize: {x: 16192, y: 16192} m_MaxSize: {x: 16192, y: 16192}
vertical: 1 vertical: 1
controlID: 17 controlID: 122
draggingID: 0 draggingID: 0
--- !u!114 &17 --- !u!114 &17
MonoBehaviour: MonoBehaviour:
@ -958,9 +958,9 @@ MonoBehaviour:
m_IsLocked: 0 m_IsLocked: 0
m_FolderTreeState: m_FolderTreeState:
scrollPos: {x: 0, y: 37} scrollPos: {x: 0, y: 37}
m_SelectedIDs: 76ba0000 m_SelectedIDs: 4ebb0000
m_LastClickedID: 47734 m_LastClickedID: 47950
m_ExpandedIDs: 0000000074b7000076b7000078b7000000ca9a3bffffff7f m_ExpandedIDs: 000000004cb800004eb8000050b8000000ca9a3bffffff7f
m_RenameOverlay: m_RenameOverlay:
m_UserAcceptedRename: 0 m_UserAcceptedRename: 0
m_Name: m_Name:
@ -988,7 +988,7 @@ MonoBehaviour:
scrollPos: {x: 0, y: 0} scrollPos: {x: 0, y: 0}
m_SelectedIDs: m_SelectedIDs:
m_LastClickedID: 0 m_LastClickedID: 0
m_ExpandedIDs: 0000000074b7000076b7000078b70000 m_ExpandedIDs: 000000004cb800004eb8000050b80000
m_RenameOverlay: m_RenameOverlay:
m_UserAcceptedRename: 0 m_UserAcceptedRename: 0
m_Name: m_Name:
@ -1015,7 +1015,7 @@ MonoBehaviour:
m_ListAreaState: m_ListAreaState:
m_SelectedInstanceIDs: m_SelectedInstanceIDs:
m_LastClickedInstanceID: 0 m_LastClickedInstanceID: 0
m_HadKeyboardFocusLastEvent: 1 m_HadKeyboardFocusLastEvent: 0
m_ExpandedInstanceIDs: c6230000000000005aca0000 m_ExpandedInstanceIDs: c6230000000000005aca0000
m_RenameOverlay: m_RenameOverlay:
m_UserAcceptedRename: 0 m_UserAcceptedRename: 0
@ -1130,7 +1130,7 @@ MonoBehaviour:
scrollPos: {x: 0, y: 0} scrollPos: {x: 0, y: 0}
m_SelectedIDs: m_SelectedIDs:
m_LastClickedID: 0 m_LastClickedID: 0
m_ExpandedIDs: f263feff9264feff72d6feffb6ecfeff56edfeff365fffff4cfaffff m_ExpandedIDs: 4cfaffff
m_RenameOverlay: m_RenameOverlay:
m_UserAcceptedRename: 0 m_UserAcceptedRename: 0
m_Name: m_Name: