Skip to content

Commit 67056c8

Browse files
committed
✨ Add a function to know if the device/graphics api is supported
1 parent 2f6ab48 commit 67056c8

File tree

4 files changed

+147
-88
lines changed

4 files changed

+147
-88
lines changed

InteropUnityCUDA/Assets/Actions/InteropHandler.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Runtime.InteropServices;
44
using UnityEngine;
5+
using UnityEngine.Assertions;
56

67
namespace ActionUnity
78
{
@@ -18,6 +19,10 @@ public abstract class InteropHandler : MonoBehaviour
1819

1920
[DllImport(_dllPluginInterop)]
2021
private static extern void StartLog();
22+
23+
24+
[DllImport(_dllPluginInterop)]
25+
public static extern bool IsSupported();
2126

2227
[DllImport(_dllPluginInterop)]
2328
private static extern void SetTime(float time);
@@ -40,10 +45,19 @@ public abstract class InteropHandler : MonoBehaviour
4045

4146
protected virtual int ReserveCapacity => 16;
4247

48+
private void Awake()
49+
{
50+
if (IsSupported() == false)
51+
{
52+
Debug.LogError("Interoperability is not supported.");
53+
}
54+
}
55+
4356
public void InitializeInteropHandler()
4457
{
4558
// initialize log
4659
StartLog();
60+
4761
InitializeRegisterActions(ReserveCapacity);
4862

4963
//Here create and register your actions

InteropUnityCUDA/Assets/Actions/InteropHandlerSample.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System;
21
using System.Runtime.InteropServices;
32
using Unity.Mathematics;
43
using UnityEngine;
5-
using UnityEngine.Rendering;
64
using UnityEngine.UI;
75
using Utilities;
86

@@ -68,10 +66,12 @@ private void CreateBuffer()
6866
_computeBuffer = new ComputeBuffer(_sizeBuffer, stride);
6967
_particlesDrawer.InitParticlesBuffer(_computeBuffer, _sizeBuffer, 0.1f);
7068
}
69+
7170

7271
private void Start()
7372
{
7473
InitializeInteropHandler();
74+
7575
}
7676

7777
/// <summary>
@@ -90,7 +90,7 @@ protected override void InitializeActions()
9090

9191
InitSampleTexture();
9292
InitSampleTextureArray();
93-
// InitSampleVertexBuffer();
93+
InitSampleVertexBuffer();
9494
}
9595

9696
private void InitSampleTexture()
@@ -131,7 +131,7 @@ protected override void UpdateActions()
131131
base.UpdateActions();
132132
CallFunctionUpdateInAction(_ActionTextureName);
133133
CallFunctionUpdateInAction(_ActionTextureArrayName);
134-
// CallFunctionUpdateInAction(_ActionVertexBufferName);
134+
CallFunctionUpdateInAction(_ActionVertexBufferName);
135135

136136
Graphics.CopyTexture(_textureArray, 0, _textureForDisplay0, 0);
137137
Graphics.CopyTexture(_textureArray, 1, _textureForDisplay1, 0);
Lines changed: 93 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,115 @@
11
#pragma once
22

3-
#include "action.h"
43
#include "IUnityGraphics.h"
5-
#include "renderAPI.h"
4+
#include "action.h"
65
#include "log.h"
7-
#include <memory>
6+
#include "renderAPI.h"
87
#include <map>
8+
#include <memory>
99

1010
class Texture;
1111
class VertexBuffer;
1212

13-
1413
extern "C"
1514
{
16-
// Export function for other native plugin to create interop graphics object
17-
18-
19-
/// <summary>
20-
/// Create a texture object for interoperability from a texture pointer of unity
21-
/// </summary>
22-
/// <param name="textureHandle">A pointer of texture with float4 that has been generated with Unity (see function
23-
/// GetNativeTexturePtr https://docs.unity3d.com/ScriptReference/Texture.GetNativeTexturePtr.html) </param>
24-
/// <param name="width">the width of the texture</param>
25-
/// <param name="height">the height of the texture</param>
26-
/// <param name="depth">depth of 0 or 1 are equivalent to a simple texture 2D when it greater than 1 it will be a texture 2D array </param>
27-
UNITY_INTERFACE_EXPORT Texture* CreateTextureInterop(void* textureHandle, int w, int h, int depth);
28-
29-
/// <summary>
30-
/// Create a vertex buffer object for interoperability from a compute buffer pointer of unity
31-
/// </summary>
32-
/// <param name="bufferHandle">A pointer of computeBuffer with float4 that has been generated with Unity (see function
33-
/// GetNativeBufferPtr https://docs.unity3d.com/ScriptReference/ComputeBuffer.GetNativeBufferPtr.html) </param>
34-
/// <param name="size">the size of the computeBuffer</param>
35-
UNITY_INTERFACE_EXPORT VertexBuffer* CreateVertexBufferInterop(void* bufferHandle, int size);
36-
37-
/// <summary>
38-
/// Set time for interoperability plugin
39-
/// </summary>
40-
/// <param name="time">new value for time</param>
41-
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetTime(float time);
42-
15+
// Export function for other native plugin to create interop graphics object
16+
4317
/// <summary>
44-
/// Get time for interoperability plugin
18+
/// Check if the device and the graphics api are supported
4519
/// </summary>
46-
/// <returns>the current time</returns>
47-
UNITY_INTERFACE_EXPORT float UNITY_INTERFACE_API GetTime();
48-
49-
/// <summary>
50-
/// Return the callback which will be called from render thread
51-
/// </summary>
52-
UNITY_INTERFACE_EXPORT UnityRenderingEvent UNITY_INTERFACE_API GetRenderEventFunc();
53-
54-
/// <summary>
55-
/// Function to call when plugin is unloaded, indeed the function UnityUnload doesn't work
56-
/// see https://unity3d.atlassian.net/servicedesk/customer/portal/2/user/login?destination=portal%2F2%2FIN-13513
57-
/// </summary>
58-
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UnityShutdown();
59-
60-
/// <summary>
61-
/// Initialize log
62-
/// </summary>
63-
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API StartLog();
64-
65-
/// <summary>
66-
/// Register an action in plugin. When an action has been registered these functions (Start, Update, ...)
67-
/// can be called on render thread from Unity with GL.IssuePluginEvent
68-
/// https://docs.unity3d.com/ScriptReference/GL.IssuePluginEvent.html
69-
/// Make sure the register has been initialized with InitializeRegisterActions function
70-
/// </summary>
71-
/// <param name="action">A pointer toward the action that we want to register</param>
72-
/// <returns>the eventId to recover the action that has been registered. To call these functions on render thread you must
73-
/// call GL.IssuePluginEvent with this Id.</returns>
74-
UNITY_INTERFACE_EXPORT size_t UNITY_INTERFACE_API RegisterAction(Action* action);
75-
76-
/// <summary>
77-
/// Initialize the register of actions, this has to be called before register actions
78-
/// </summary>
79-
/// <param name="reserveCapacity">The default capacity for the register (just for optimization)</param>
80-
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API InitializeRegisterActions(int reserveCapacity);
20+
UNITY_INTERFACE_EXPORT bool IsSupported();
21+
22+
/// <summary>
23+
/// Create a texture object for interoperability from a texture pointer of
24+
/// unity
25+
/// </summary>
26+
/// <param name="textureHandle">A pointer of texture with float4 that has
27+
/// been generated with Unity (see function GetNativeTexturePtr
28+
/// https://docs.unity3d.com/ScriptReference/Texture.GetNativeTexturePtr.html)
29+
/// </param> <param name="width">the width of the texture</param> <param
30+
/// name="height">the height of the texture</param> <param
31+
/// name="depth">depth of 0 or 1 are equivalent to a simple texture 2D when
32+
/// it greater than 1 it will be a texture 2D array </param>
33+
UNITY_INTERFACE_EXPORT Texture *CreateTextureInterop(void *textureHandle,
34+
int w, int h,
35+
int depth);
36+
37+
/// <summary>
38+
/// Create a vertex buffer object for interoperability from a compute buffer
39+
/// pointer of unity
40+
/// </summary>
41+
/// <param name="bufferHandle">A pointer of computeBuffer with float4 that
42+
/// has been generated with Unity (see function GetNativeBufferPtr
43+
/// https://docs.unity3d.com/ScriptReference/ComputeBuffer.GetNativeBufferPtr.html)
44+
/// </param> <param name="size">the size of the computeBuffer</param>
45+
UNITY_INTERFACE_EXPORT VertexBuffer *CreateVertexBufferInterop(
46+
void *bufferHandle, int size);
47+
48+
/// <summary>
49+
/// Set time for interoperability plugin
50+
/// </summary>
51+
/// <param name="time">new value for time</param>
52+
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetTime(float time);
53+
54+
/// <summary>
55+
/// Get time for interoperability plugin
56+
/// </summary>
57+
/// <returns>the current time</returns>
58+
UNITY_INTERFACE_EXPORT float UNITY_INTERFACE_API GetTime();
59+
60+
/// <summary>
61+
/// Return the callback which will be called from render thread
62+
/// </summary>
63+
UNITY_INTERFACE_EXPORT UnityRenderingEvent UNITY_INTERFACE_API
64+
GetRenderEventFunc();
65+
66+
/// <summary>
67+
/// Function to call when plugin is unloaded, indeed the function
68+
/// UnityUnload doesn't work see
69+
/// https://unity3d.atlassian.net/servicedesk/customer/portal/2/user/login?destination=portal%2F2%2FIN-13513
70+
/// </summary>
71+
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UnityShutdown();
72+
73+
/// <summary>
74+
/// Initialize log
75+
/// </summary>
76+
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API StartLog();
77+
78+
/// <summary>
79+
/// Register an action in plugin. When an action has been registered these
80+
/// functions (Start, Update, ...) can be called on render thread from Unity
81+
/// with GL.IssuePluginEvent
82+
/// https://docs.unity3d.com/ScriptReference/GL.IssuePluginEvent.html
83+
/// Make sure the register has been initialized with
84+
/// InitializeRegisterActions function
85+
/// </summary>
86+
/// <param name="action">A pointer toward the action that we want to
87+
/// register</param> <returns>the eventId to recover the action that has
88+
/// been registered. To call these functions on render thread you must call
89+
/// GL.IssuePluginEvent with this Id.</returns>
90+
UNITY_INTERFACE_EXPORT size_t UNITY_INTERFACE_API
91+
RegisterAction(Action *action);
92+
93+
/// <summary>
94+
/// Initialize the register of actions, this has to be called before
95+
/// register actions
96+
/// </summary>
97+
/// <param name="reserveCapacity">The default capacity for the register
98+
/// (just for optimization)</param>
99+
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API
100+
InitializeRegisterActions(int reserveCapacity);
81101
}
82102

83-
84-
85103
static float _time;
86104

87105
// current API used by Unity
88-
static RenderAPI* s_CurrentAPI = NULL;
106+
static RenderAPI *s_CurrentAPI = NULL;
89107
static UnityGfxRenderer s_DeviceType = kUnityGfxRendererNull;
90-
static IUnityInterfaces* s_UnityInterfaces = NULL;
91-
static IUnityGraphics* s_Graphics = NULL;
108+
static IUnityInterfaces *s_UnityInterfaces = NULL;
109+
static IUnityGraphics *s_Graphics = NULL;
92110

93111
// the register of action
94-
static std::vector<Action*> _registerActions;
112+
static std::vector<Action *> _registerActions;
95113

96114
// defined the key action to use
97115
static int _keyAction = 0;
@@ -103,4 +121,5 @@ static int _keyAction = 0;
103121
/// <returns></returns>
104122
static void UNITY_INTERFACE_API OnRenderEvent(int eventID);
105123

106-
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType);
124+
static void UNITY_INTERFACE_API
125+
OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType);

Plugin/PluginInteropUnityCUDA/src/unity_plugin.cpp

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ extern "C"
3737

3838
s_DeviceType = s_Graphics->GetRenderer();
3939
// create a buffer in function of graphics API
40-
return Factory::createBuffer(bufferHandle, size, s_DeviceType, s_CurrentAPI);
40+
return Factory::createBuffer(bufferHandle, size, s_DeviceType,
41+
s_CurrentAPI);
4142
}
4243

4344
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetTime(float time)
@@ -50,6 +51,39 @@ extern "C"
5051
return _time;
5152
}
5253

54+
UNITY_INTERFACE_EXPORT bool IsSupported()
55+
{
56+
int deviceCount;
57+
CUDA_CHECK(cudaGetDeviceCount(&deviceCount));
58+
if (deviceCount == 0)
59+
{
60+
Log::log().debugLogError(
61+
"No CUDA device have been found. Interoperability could not "
62+
"work in this case. Use a computer with CUDA device if you "
63+
"want to take advantage of CUDA performance.");
64+
return false;
65+
}
66+
67+
if(s_DeviceType == kUnityGfxRendererNull)
68+
{
69+
70+
Log::log().debugLogError(
71+
"Unknown graphics API.");
72+
return false;
73+
}
74+
75+
if (s_DeviceType != kUnityGfxRendererD3D11 &&
76+
s_DeviceType != kUnityGfxRendererOpenGLCore &&
77+
s_DeviceType != kUnityGfxRendererOpenGLES20 &&
78+
s_DeviceType != kUnityGfxRendererOpenGLES30)
79+
{
80+
Log::log().debugLogError(
81+
"Graphics API is not supported yet.");
82+
return false;
83+
}
84+
return true;
85+
}
86+
5387
/// <summary>
5488
/// Initialize the correct API
5589
/// </summary>
@@ -58,21 +92,13 @@ extern "C"
5892
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API
5993
UnityPluginLoad(IUnityInterfaces *unityInterfaces)
6094
{
61-
//_registerActions.reserve(16);
95+
6296
s_UnityInterfaces = unityInterfaces;
6397

6498
s_Graphics = s_UnityInterfaces->Get<IUnityGraphics>();
6599

66100
s_Graphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent);
67101

68-
#if SUPPORT_VULKAN
69-
if (s_Graphics->GetRenderer() == kUnityGfxRendererNull)
70-
{
71-
extern void RenderAPI_Vulkan_OnPluginLoad(IUnityInterfaces *);
72-
RenderAPI_Vulkan_OnPluginLoad(unityInterfaces);
73-
}
74-
#endif
75-
76102
OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize);
77103
}
78104

0 commit comments

Comments
 (0)