Skip to content

Commit e6a5db3

Browse files
committed
✨ Add a way to handle error with CUDA and more generally with the interop plugin #5
1 parent 890b615 commit e6a5db3

22 files changed

+320
-157
lines changed

InteropUnityCUDA/Assets/Actions/InteropHandler.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
namespace ActionUnity
88
{
9+
[Flags]
10+
public enum ErrorBehavior
11+
{
12+
DO_NOTHING = 1,
13+
ASSERT = 2,
14+
DISABLE_ACTION = 4,
15+
};
16+
17+
918
/// <summary>
1019
/// This class will handle interoperability between Unity/Graphics API/GPGPU Technology
1120
/// It is used to register and call particular action (other unity plugin
@@ -20,6 +29,11 @@ public abstract class InteropHandler : MonoBehaviour
2029
[DllImport(_dllPluginInterop)]
2130
private static extern void StartLog();
2231

32+
[DllImport(_dllPluginInterop)]
33+
protected static extern int GetErrorBehavior();
34+
35+
[DllImport(_dllPluginInterop)]
36+
private static extern void SetErrorBehavior(int behavior);
2337

2438
[DllImport(_dllPluginInterop)]
2539
public static extern bool IsSupported();
@@ -51,6 +65,8 @@ private void Awake()
5165
{
5266
Debug.LogError("Interoperability is not supported.");
5367
}
68+
69+
SetErrorBehavior(ErrorBehavior.DISABLE_ACTION);
5470
}
5571

5672
public void InitializeInteropHandler()
@@ -101,6 +117,11 @@ protected virtual void OnDestroyActions()
101117

102118
}
103119

120+
protected void SetErrorBehavior(ErrorBehavior errorBehavior)
121+
{
122+
SetErrorBehavior((int)errorBehavior);
123+
}
124+
104125
#region ACTION_CALLER
105126

106127
/// <summary>

InteropUnityCUDA/Assets/Actions/InteropHandlerSample.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class InteropHandlerSample : InteropHandler
2626
[SerializeField] private int _sizeBuffer = 256;
2727

2828
private Texture2D _texture;
29+
// private RenderTexture _texture;
2930
private Texture2DArray _textureArray;
3031
private Texture2D _textureForDisplay0;
3132
private Texture2D _textureForDisplay1;
@@ -38,6 +39,8 @@ private void CreateTexture()
3839
{
3940
_texture = new Texture2D(_sizeTexture, _sizeTexture, TextureFormat.RGBAFloat, false, true);
4041
_texture.Apply();
42+
// _texture = new RenderTexture(_sizeTexture, _sizeTexture, 1, RenderTextureFormat.ARGB32);
43+
// _texture.Create();
4144
_rawImageOneTexture.texture = _texture;
4245
}
4346

Plugin/PluginInteropUnityCUDA/include/Action.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ class UNITY_INTERFACE_EXPORT Action
2020
virtual int Start() = 0;
2121
virtual int Update() = 0;
2222
virtual int OnDestroy() = 0;
23+
bool IsActive = true;
2324
};

Plugin/PluginInteropUnityCUDA/include/Buffer/vertex_buffer.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,37 @@ class VertexBuffer
1919
/**
2020
* Register the buffer in CUDA, this has to be override because it depends on the graphics api
2121
*/
22-
UNITY_INTERFACE_EXPORT virtual void registerBufferInCUDA() = 0;
22+
UNITY_INTERFACE_EXPORT virtual int registerBufferInCUDA() = 0;
2323

2424
/**
2525
* Unregister the buffer in CUDA, this has to be override because it depends on the graphics api
2626
*/
27-
UNITY_INTERFACE_EXPORT virtual void unregisterBufferInCUDA() = 0;
27+
UNITY_INTERFACE_EXPORT virtual int unregisterBufferInCUDA() = 0;
2828

2929

3030
/**
3131
* Map resources to CUDA
3232
* return an array of T* defined on device memory and which can be edited in cuda
3333
*/
3434
template <typename T>
35-
UNITY_INTERFACE_EXPORT T* mapResources()
35+
UNITY_INTERFACE_EXPORT int mapResources(T** vertexPtr)
3636
{
3737
// map resource
38-
CUDA_CHECK(cudaGraphicsMapResources(1, &_graphicsResource, 0));
38+
CUDA_CHECK_RETURN(cudaGraphicsMapResources(1, &_graphicsResource, 0));
3939
// pointer toward an array of float4 on device memory : the compute buffer
40-
T* vertexPtr;
4140
// number of bytes that has been readed
4241
size_t numBytes;
4342
// map the resources on a float4 array that can be modify on device
44-
CUDA_CHECK(cudaGraphicsResourceGetMappedPointer((void**)&vertexPtr, &numBytes,
43+
CUDA_CHECK_RETURN(cudaGraphicsResourceGetMappedPointer((void**)vertexPtr, &numBytes,
4544
_graphicsResource));
46-
return vertexPtr;
45+
return SUCCESS_INTEROP_CODE;
4746
}
4847

4948
/**
5049
* Unmap resources from CUDA
5150
* This function will wait for all previous GPU activity to complete
5251
*/
53-
UNITY_INTERFACE_EXPORT void unmapResources();
52+
UNITY_INTERFACE_EXPORT int unmapResources();
5453

5554
/**
5655
* Get the default dimension block (8,1,1)

Plugin/PluginInteropUnityCUDA/include/Buffer/vertex_buffer_D3D11.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class VertexBuffer_D3D11 : public VertexBuffer
1414
public:
1515
VertexBuffer_D3D11(void* bufferHandle, int size);
1616
~VertexBuffer_D3D11();
17-
virtual void registerBufferInCUDA();
18-
virtual void unregisterBufferInCUDA();
17+
virtual int registerBufferInCUDA();
18+
virtual int unregisterBufferInCUDA();
1919

2020
};
2121

Plugin/PluginInteropUnityCUDA/include/Buffer/vertex_buffer_OpenGLCoreES.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class VertexBuffer_OpenGLCoreES : public VertexBuffer
1313
public:
1414
VertexBuffer_OpenGLCoreES(void* bufferHandle, int size);
1515
~VertexBuffer_OpenGLCoreES();
16-
virtual void registerBufferInCUDA();
17-
virtual void unregisterBufferInCUDA();
16+
virtual int registerBufferInCUDA();
17+
virtual int unregisterBufferInCUDA();
1818

1919
};
2020

Plugin/PluginInteropUnityCUDA/include/Texture/texture.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class Texture
2727
* Register the texture in CUDA, this has to be override because it depends
2828
* on the graphics api
2929
*/
30-
UNITY_INTERFACE_EXPORT virtual void registerTextureInCUDA() = 0;
30+
UNITY_INTERFACE_EXPORT virtual int registerTextureInCUDA() = 0;
3131

3232
/**
3333
* Unregistered the texture in CUDA, this has to be override because it
3434
* depends on the graphics api
3535
*/
36-
UNITY_INTERFACE_EXPORT virtual void unregisterTextureInCUDA() = 0;
36+
UNITY_INTERFACE_EXPORT virtual int unregisterTextureInCUDA() = 0;
3737

3838
/**
3939
* For some API (DX11) CUDA cannot edit the texture created by Unity
@@ -46,7 +46,7 @@ class Texture
4646
* texture has not been modify in Unity. Tips : not necessary for write only
4747
* in CUDA or read only in Unity
4848
*/
49-
UNITY_INTERFACE_EXPORT virtual void copyUnityTextureToAPITexture() = 0;
49+
UNITY_INTERFACE_EXPORT virtual int copyUnityTextureToAPITexture() = 0;
5050

5151
/**
5252
* For some API (DX11) CUDA cannot edit the texture created by Unity
@@ -59,20 +59,20 @@ class Texture
5959
* texture in CUDA, or if the texture is only write only in Unity. Tips :
6060
* not necessary for read only in CUDA or write only in Unity
6161
*/
62-
UNITY_INTERFACE_EXPORT virtual void copyAPITextureToUnityTexture() = 0;
62+
UNITY_INTERFACE_EXPORT virtual int copyAPITextureToUnityTexture() = 0;
6363

6464
/**
6565
* Map the cuda array from the graphics resources and create a surface
6666
* object from it. To write into the surface object use the getter of
6767
* _surfObjArray.
6868
*/
69-
UNITY_INTERFACE_EXPORT void mapTextureToSurfaceObject();
69+
UNITY_INTERFACE_EXPORT int mapTextureToSurfaceObject();
7070

7171
/**
7272
* Unmap the cuda array from graphics resources and destroy surface object
7373
* This function will wait for all previous GPU activity to complete
7474
*/
75-
UNITY_INTERFACE_EXPORT void unmapTextureToSurfaceObject();
75+
UNITY_INTERFACE_EXPORT int unmapTextureToSurfaceObject();
7676

7777
/**
7878
* Get the default dimension block (8,8,1)

Plugin/PluginInteropUnityCUDA/include/Texture/texture_D3D11.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ class Texture_D3D11 : public Texture
2626
int textureDepth, RenderAPI *renderAPI);
2727
~Texture_D3D11();
2828

29-
virtual void registerTextureInCUDA();
30-
virtual void unregisterTextureInCUDA();
29+
virtual int registerTextureInCUDA();
30+
virtual int unregisterTextureInCUDA();
3131

3232
protected:
33-
virtual void copyUnityTextureToAPITexture();
34-
virtual void copyAPITextureToUnityTexture();
33+
virtual int copyUnityTextureToAPITexture();
34+
virtual int copyAPITextureToUnityTexture();
3535

3636
private:
3737
int copyUnityTextureToBuffer();

Plugin/PluginInteropUnityCUDA/include/Texture/texture_OpenGLCoreES.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class Texture_OpenGLCoreES : public Texture
1515
public:
1616
Texture_OpenGLCoreES(void* textureHandle, int textureWidth, int textureHeight, int textureDepth);
1717
~Texture_OpenGLCoreES();
18-
virtual void registerTextureInCUDA();
19-
virtual void unregisterTextureInCUDA();
18+
virtual int registerTextureInCUDA();
19+
virtual int unregisterTextureInCUDA();
2020
protected:
21-
virtual void copyUnityTextureToAPITexture();
22-
virtual void copyAPITextureToUnityTexture();
21+
virtual int copyUnityTextureToAPITexture();
22+
virtual int copyAPITextureToUnityTexture();
2323
};
2424

2525
#endif

0 commit comments

Comments
 (0)