Skip to content

Commit 808906a

Browse files
committed
✨🚧 ♻️ Update texture to support only 2D array with map texture to surface object array
Rename method to make it more clear Update comment of texture to be more c++ compatible
1 parent 439ee07 commit 808906a

File tree

12 files changed

+208
-222
lines changed

12 files changed

+208
-222
lines changed

Plugin/PluginInteropUnityCUDA/include/Buffer/vertex_buffer.h

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,64 @@ class VertexBuffer
88
{
99
public:
1010

11-
/// <summary>
12-
/// Constructor of vertex buffer
13-
/// </summary>
14-
/// <param name="bufferHandle">A pointer of computeBuffer with float4 that has been generated with Unity (see function
15-
/// GetNativeBufferPtr https://docs.unity3d.com/ScriptReference/ComputeBuffer.GetNativeBufferPtr.html) </param>
16-
/// <param name="size">the size of the computeBuffer</param>
11+
/**
12+
* Constructor of vertex buffer
13+
* @param bufferHandle A pointer of computeBuffer with float4 that has been generated with Unity (see function
14+
* GetNativeBufferPtr https://docs.unity3d.com/ScriptReference/ComputeBuffer.GetNativeBufferPtr.html)
15+
* @param size the size of the computeBuffer
16+
*/
1717
UNITY_INTERFACE_EXPORT VertexBuffer(void* bufferHandle, int size);
1818

19-
/// <summary>
20-
/// Register the buffer in CUDA, this has to be override because it depends on the graphics api
21-
/// </summary>
19+
/**
20+
* Register the buffer in CUDA, this has to be override because it depends on the graphics api
21+
*/
2222
UNITY_INTERFACE_EXPORT virtual void registerBufferInCUDA() = 0;
2323

24-
/// <summary>
25-
/// Unregisteregister the buffer in CUDA, this has to be override because it depends on the graphics api
26-
/// </summary>
27-
UNITY_INTERFACE_EXPORT virtual void unRegisterBufferInCUDA() = 0;
24+
/**
25+
* Unregister the buffer in CUDA, this has to be override because it depends on the graphics api
26+
*/
27+
UNITY_INTERFACE_EXPORT virtual void unregisterBufferInCUDA() = 0;
2828

2929

30-
/// <summary>
31-
/// Map resources to CUDA
32-
/// </summary>
33-
/// <returns>an array of float4* defined on device memory and which can be edited in cuda</returns>
30+
/**
31+
* Map resources to CUDA
32+
* return an array of T* defined on device memory and which can be edited in cuda
33+
*/
3434
template <typename T>
3535
UNITY_INTERFACE_EXPORT T* mapResources()
3636
{
3737
// map resource
38-
CUDA_CHECK(cudaGraphicsMapResources(1, &_pGraphicsResource, 0));
38+
CUDA_CHECK(cudaGraphicsMapResources(1, &_graphicsResource, 0));
3939
// pointer toward an array of float4 on device memory : the compute buffer
4040
T* vertexPtr;
4141
// number of bytes that has been readed
4242
size_t numBytes;
4343
// map the resources on a float4 array that can be modify on device
4444
CUDA_CHECK(cudaGraphicsResourceGetMappedPointer((void**)&vertexPtr, &numBytes,
45-
_pGraphicsResource));
45+
_graphicsResource));
4646
return vertexPtr;
4747
}
4848

49-
/// <summary>
50-
/// Unmap resources from CUDA
51-
/// This function will wait for all previous GPU activity to complete
52-
/// </summary>
49+
/**
50+
* Unmap resources from CUDA
51+
* This function will wait for all previous GPU activity to complete
52+
*/
5353
UNITY_INTERFACE_EXPORT void unmapResources();
5454

55-
/// <summary>
56-
/// Get the default dimension block (8,1,1)
57-
/// </summary>
55+
/**
56+
* Get the default dimension block (8,1,1)
57+
*/
5858
UNITY_INTERFACE_EXPORT dim3 getDimBlock() const;
5959

6060

61-
/// <summary>
62-
/// Get the default dimension grid ((sizeBuffer + 7)/8,1,1)
63-
/// </summary>
61+
/**
62+
* Get the default dimension grid ((sizeBuffer + 7)/8,1,1)
63+
*/
6464
UNITY_INTERFACE_EXPORT dim3 getDimGrid() const;
6565

66-
/// <summary>
67-
/// Get the size of the buffer
68-
/// </summary>
66+
/**
67+
* Get the size of the buffer
68+
*/
6969
UNITY_INTERFACE_EXPORT int getSize() const;
7070

7171

@@ -77,7 +77,7 @@ class VertexBuffer
7777
// size of the buffer
7878
int _size;
7979
// Resource that can be used to retrieve buffer for CUDA
80-
cudaGraphicsResource* _pGraphicsResource;
80+
cudaGraphicsResource* _graphicsResource;
8181

8282

8383
private:

Plugin/PluginInteropUnityCUDA/include/Buffer/vertex_buffer_D3D11.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class VertexBuffer_D3D11 : public VertexBuffer
1515
VertexBuffer_D3D11(void* bufferHandle, int size);
1616
~VertexBuffer_D3D11();
1717
virtual void registerBufferInCUDA();
18-
virtual void unRegisterBufferInCUDA();
18+
virtual void unregisterBufferInCUDA();
1919

2020
};
2121

Plugin/PluginInteropUnityCUDA/include/Buffer/vertex_buffer_OpenGLCoreES.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class VertexBuffer_OpenGLCoreES : public VertexBuffer
1414
VertexBuffer_OpenGLCoreES(void* bufferHandle, int size);
1515
~VertexBuffer_OpenGLCoreES();
1616
virtual void registerBufferInCUDA();
17-
virtual void unRegisterBufferInCUDA();
17+
virtual void unregisterBufferInCUDA();
1818

1919
};
2020

Plugin/PluginInteropUnityCUDA/include/Texture/texture.h

Lines changed: 103 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -6,119 +6,123 @@
66
class Texture
77
{
88
public:
9-
/// <summary>
10-
/// Constructor of texture
11-
/// </summary>
12-
/// <param name="textureHandle">A pointer of texture with float4 that has
13-
/// been generated with Unity (see function GetNativeTexturePtr
14-
/// https://docs.unity3d.com/ScriptReference/Texture.GetNativeTexturePtr.html)
15-
/// </param> <param name="textureWidth">the width of the texture</param>
16-
/// <param name="textureHeight">the height of the texture</param>
17-
/// <param name="textureDepth">the depth of the texture</param>
9+
/**
10+
* Constructor of texture
11+
* @param textureHandle A pointer of texture with float4 that has
12+
* been generated with Unity (see function GetNativeTexturePtr
13+
* https://docs.unity3d.com/ScriptReference/Texture.GetNativeTexturePtr.html)
14+
* @param textureWidth the width of the texture
15+
* @param textureHeight the height of the texture
16+
* @param textureDepth the depth of the texture (should be greater than 0)
17+
*/
1818
UNITY_INTERFACE_EXPORT Texture(void *textureHandle, int textureWidth,
19-
int textureHeight,
20-
int textureDepth);
19+
int textureHeight, int textureDepth);
2120

22-
/// <summary>
23-
/// Register the texture in CUDA, this has to be override because it depends
24-
/// on the graphics api
25-
/// </summary>
21+
/**
22+
* Destructor of texture, will free _surfObjArray
23+
*/
24+
UNITY_INTERFACE_EXPORT ~Texture();
25+
26+
/**
27+
* Register the texture in CUDA, this has to be override because it depends
28+
* on the graphics api
29+
*/
2630
UNITY_INTERFACE_EXPORT virtual void registerTextureInCUDA() = 0;
2731

28-
/// <summary>
29-
/// Unregister the texture in CUDA, this has to be override because it
30-
/// depends on the graphics api
31-
/// </summary>
32-
UNITY_INTERFACE_EXPORT virtual void unRegisterTextureInCUDA() = 0;
33-
34-
35-
/// <summary>
36-
/// For some API (DX11) CUDA cannot edit the texture created by Unity
37-
/// therefore, we have to create a new texture that will used as a buffer
38-
/// between the unity texture and the surface object that is modify by CUDA
39-
/// For these API, this function will copy the content of the unity texture
40-
/// to this buffer, for the other API. It'll do nothing.
41-
/// If Unity texture has been modify in Unity, you have to do the copy before
42-
/// reading it in CUDA. It's not necessary if you only write into the texture
43-
/// in CUDA, or if the texture has not been modify in Unity.
44-
/// Tips : not necessary for write only in CUDA or read only in Unity
45-
/// </summary>
32+
/**
33+
* Unregistered the texture in CUDA, this has to be override because it
34+
* depends on the graphics api
35+
*/
36+
UNITY_INTERFACE_EXPORT virtual void unregisterTextureInCUDA() = 0;
37+
38+
/**
39+
* For some API (DX11) CUDA cannot edit the texture created by Unity
40+
* therefore, we have to create a new texture that will used as a buffer
41+
* between the unity texture and the surface object that is modify by CUDA
42+
* For these API, this function will copy the content of the unity texture
43+
* to this buffer, for the other API. It'll do nothing. If Unity texture has
44+
* been modify in Unity, you have to do the copy before reading it in CUDA.
45+
* It's not necessary if you only write into the texture in CUDA, or if the
46+
* texture has not been modify in Unity. Tips : not necessary for write only
47+
* in CUDA or read only in Unity
48+
*/
4649
UNITY_INTERFACE_EXPORT virtual void copyUnityTextureToAPITexture() = 0;
4750

48-
49-
/// <summary>
50-
/// For some API (DX11) CUDA cannot edit the texture created by Unity
51-
/// therefore, we have to create a new texture that will used as a buffer
52-
/// between the unity texture and the surface object that is modify by CUDA
53-
/// For these API, this function will copy the content of the buffer texture
54-
/// to the unity texture, for the other API. It'll do nothing.
55-
/// If API texture has been modify by, you have to do the copy before
56-
/// reading it in Unity. It's not necessary if you only read into the texture
57-
/// in CUDA, or if the texture is only write only in Unity.
58-
/// Tips : not necessary for read only in CUDA or write only in Unity
59-
/// </summary>
51+
/**
52+
* For some API (DX11) CUDA cannot edit the texture created by Unity
53+
* therefore, we have to create a new texture that will used as a buffer
54+
* between the unity texture and the surface object that is modify by CUDA
55+
* For these API, this function will copy the content of the buffer texture
56+
* to the unity texture, for the other API. It'll do nothing.
57+
* If API texture has been modify by, you have to do the copy before
58+
* reading it in Unity. It's not necessary if you only read into the
59+
* texture in CUDA, or if the texture is only write only in Unity. Tips :
60+
* not necessary for read only in CUDA or write only in Unity
61+
*/
6062
UNITY_INTERFACE_EXPORT virtual void copyAPITextureToUnityTexture() = 0;
6163

62-
/// <summary>
63-
/// Map a cuda array to the graphics resources and wrap it into a surface
64-
/// object of cuda
65-
/// </summary>
66-
/// <param name="indexInArray"> Array index for array textures or cubemap
67-
/// face index as defined by cudaGraphicsCubeFace for cubemap textures for
68-
/// the subresource to access </param> <returns>a cuda surface object on
69-
/// device memory and which can be edited in cuda</returns>
70-
UNITY_INTERFACE_EXPORT cudaSurfaceObject_t mapTextureToSurfaceObject(
71-
int indexInArray = 0);
72-
73-
74-
/// <summary>
75-
/// Unmap the cuda array from graphics resources and destroy surface object
76-
/// This function will wait for all previous GPU activity to complete
77-
/// </summary>
78-
/// <param name="inputSurfObj">the surface object that has been created with
79-
/// <c>mapTextureToSurfaceObject</c> function</param>
80-
UNITY_INTERFACE_EXPORT void unMapTextureToSurfaceObject(
81-
cudaSurfaceObject_t &inputSurfObj);
82-
83-
UNITY_INTERFACE_EXPORT cudaSurfaceObject_t* mapTextureArrayToSurfaceObject();
84-
85-
UNITY_INTERFACE_EXPORT cudaTextureObject_t mapTextureToTextureObject(
86-
int indexInArray = 0);
87-
88-
UNITY_INTERFACE_EXPORT void unMapTextureToTextureObject(
89-
cudaTextureObject_t &texObj);
90-
91-
/// <summary>
92-
/// Get the default dimension block (8,8,1)
93-
/// </summary>
64+
/**
65+
* Map the cuda array from the graphics resources and create a surface
66+
* object from it. To write into the surface object use the getter of
67+
* _surfObjArray.
68+
*/
69+
UNITY_INTERFACE_EXPORT void mapTextureToSurfaceObject();
70+
71+
/**
72+
* Unmap the cuda array from graphics resources and destroy surface object
73+
* This function will wait for all previous GPU activity to complete
74+
*/
75+
UNITY_INTERFACE_EXPORT void unmapTextureToSurfaceObject();
76+
77+
/**
78+
* Get the default dimension block (8,8,1)
79+
*/
9480
UNITY_INTERFACE_EXPORT dim3 getDimBlock() const;
9581

96-
/// <summary>
97-
/// Get the default dimension grid ((sizeBuffer + 7)/8,((sizeBuffer +
98-
/// 7)/8,1)
99-
/// </summary>
82+
/**
83+
* Get the default dimension grid ((sizeBuffer + 7)/8,((sizeBuffer +
84+
* 7)/8,1)
85+
*/
10086
UNITY_INTERFACE_EXPORT dim3 getDimGrid() const;
10187

102-
/// <summary>
103-
/// Get the width of the texture
104-
/// </summary>
88+
/**
89+
* Get the width of the texture
90+
*/
10591
UNITY_INTERFACE_EXPORT int getWidth() const;
10692

107-
/// <summary>
108-
/// Get the height of the texture
109-
/// </summary>
93+
/**
94+
* Get the height of the texture
95+
*/
11096
UNITY_INTERFACE_EXPORT int getHeight() const;
11197

112-
/// <summary>
113-
/// Get the depth of the texture
114-
/// </summary>
98+
/**
99+
* Get the depth of the texture
100+
*/
115101
UNITY_INTERFACE_EXPORT int getDepth() const;
116102

117-
/// <summary>
118-
/// Get the native texture pointer
119-
/// </summary>
103+
/**
104+
* Get the native texture pointer
105+
*/
120106
UNITY_INTERFACE_EXPORT void *getNativeTexturePtr() const;
121107

108+
/**
109+
* Get the pointer of _surfObjArray
110+
* This array of surface object is necessary
111+
* to write or read into a texture
112+
*/
113+
UNITY_INTERFACE_EXPORT cudaSurfaceObject_t *getSurfaceObjectArray() const;
114+
115+
/**
116+
* Get the surface object associated to the given indexInArray
117+
* This array of surface object is necessary
118+
* to write or read into a texture
119+
* @param indexInArray index of the texture to get the surface object (must
120+
* be between 0 and textureDepth)
121+
* @return the surface object associated to it
122+
*/
123+
UNITY_INTERFACE_EXPORT cudaSurfaceObject_t
124+
getSurfaceObject(int indexInArray = 0) const;
125+
122126
protected:
123127
// Pointer to the texture created in Unity
124128
void *_textureHandle;
@@ -129,9 +133,14 @@ class Texture
129133
// depth of the texture <2 <=> to Texture2D; >1 <=> Texture2DArray
130134
int _textureDepth;
131135
// Resource that can be used to retrieve the surface object for CUDA
132-
cudaGraphicsResource *_pGraphicsResource;
136+
cudaGraphicsResource *_graphicsResource;
133137

134138
private:
139+
// An array of surface object that will be of the size of texture depth
140+
// the surface object is the object that you can used to write into texture
141+
// from cuda api (eg. with surf2DWrite)
142+
cudaSurfaceObject_t *_surfObjArray;
143+
135144
dim3 _dimBlock;
136145
dim3 _dimGrid;
137146
};

Plugin/PluginInteropUnityCUDA/include/Texture/texture_D3D11.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Texture_D3D11 : public Texture
2525
int textureDepth, RenderAPI *renderAPI);
2626
~Texture_D3D11();
2727
virtual void registerTextureInCUDA();
28-
virtual void unRegisterTextureInCUDA();
28+
virtual void unregisterTextureInCUDA();
2929

3030
protected:
3131
virtual void copyUnityTextureToAPITexture();

Plugin/PluginInteropUnityCUDA/include/Texture/texture_OpenGLCoreES.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Texture_OpenGLCoreES : public Texture
1616
Texture_OpenGLCoreES(void* textureHandle, int textureWidth, int textureHeight, int textureDepth);
1717
~Texture_OpenGLCoreES();
1818
virtual void registerTextureInCUDA();
19-
virtual void unRegisterTextureInCUDA();
19+
virtual void unregisterTextureInCUDA();
2020
protected:
2121
virtual void copyUnityTextureToAPITexture();
2222
virtual void copyAPITextureToUnityTexture();

Plugin/PluginInteropUnityCUDA/src/Buffer/vertex_buffer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ VertexBuffer::VertexBuffer(void* bufferHandle, int size)
1010
_dimBlock = { 8, 1, 1 };
1111
_dimGrid = { (size + _dimBlock.x - 1) / _dimBlock.x,
1212
1, 1};
13-
_pGraphicsResource = nullptr;
13+
_graphicsResource = nullptr;
1414

1515
}
1616

@@ -19,7 +19,7 @@ VertexBuffer::VertexBuffer(void* bufferHandle, int size)
1919
void VertexBuffer::unmapResources()
2020
{
2121
// unmap the resources
22-
cudaGraphicsUnmapResources(1, &_pGraphicsResource, 0);
22+
cudaGraphicsUnmapResources(1, &_graphicsResource, 0);
2323
}
2424

2525
int VertexBuffer::getSize() const

0 commit comments

Comments
 (0)