Skip to content

Commit 439ee07

Browse files
committed
✨ Add a function in texture.cpp to map texture array to an array of surface object
1 parent de72382 commit 439ee07

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

Plugin/PluginInteropUnityCUDA/include/Texture/texture.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class Texture
8080
UNITY_INTERFACE_EXPORT void unMapTextureToSurfaceObject(
8181
cudaSurfaceObject_t &inputSurfObj);
8282

83+
UNITY_INTERFACE_EXPORT cudaSurfaceObject_t* mapTextureArrayToSurfaceObject();
84+
8385
UNITY_INTERFACE_EXPORT cudaTextureObject_t mapTextureToTextureObject(
8486
int indexInArray = 0);
8587

Plugin/PluginInteropUnityCUDA/src/Texture/texture.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,33 @@ cudaSurfaceObject_t Texture::mapTextureToSurfaceObject(int indexInArray)
4646
return inputSurfObj;
4747
}
4848

49+
cudaSurfaceObject_t* Texture::mapTextureArrayToSurfaceObject()
50+
{
51+
cudaSurfaceObject_t* surf = new cudaSurfaceObject_t[_textureDepth];
52+
Log::log().debugLog("map");
53+
// map the resource to cuda
54+
CUDA_CHECK(cudaGraphicsMapResources(1, &_pGraphicsResource));
55+
for(int i=0; i<_textureDepth;i++)
56+
{
57+
// cuda array on which the resources will be sended
58+
cudaArray *arrayPtr;
59+
// https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__INTEROP.html#group__CUDART__INTEROP_1g0dd6b5f024dfdcff5c28a08ef9958031
60+
CUDA_CHECK(cudaGraphicsSubResourceGetMappedArray(
61+
&arrayPtr, _pGraphicsResource, i, 0));
62+
63+
// Wrap the cudaArray in a surface object
64+
cudaResourceDesc resDesc;
65+
memset(&resDesc, 0, sizeof(resDesc));
66+
resDesc.resType = cudaResourceTypeArray;
67+
resDesc.res.array.array = arrayPtr;
68+
surf[i] = 0;
69+
CUDA_CHECK(cudaCreateSurfaceObject(&surf[i], &resDesc));
70+
CUDA_CHECK(cudaGetLastError());
71+
}
72+
73+
return surf;
74+
}
75+
4976
cudaTextureObject_t Texture::mapTextureToTextureObject(int indexInArray)
5077
{
5178
// map the resource to cuda

Plugin/SampleBasic/include/action_sample_texture_array.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace SampleBasic {
1515

1616
private:
1717
Texture* _texture;
18-
cudaSurfaceObject_t _surf;
18+
cudaSurfaceObject_t* _surf;
1919
};
2020
} // namespace SampleBasic
2121

Plugin/SampleBasic/src/action_sample_texture_array.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,29 @@ ActionSampleTextureArray::ActionSampleTextureArray(void *texturePtr, int width,
1010
: Action()
1111
{
1212
_texture = CreateTextureInterop(texturePtr, width, height, depth);
13-
_surf = 0;
13+
_surf = new cudaSurfaceObject_t[depth];
14+
for(int i=0; i<depth; i++)
15+
{
16+
_surf[i] = 0;
17+
}
1418
}
1519

1620
inline int ActionSampleTextureArray::Start()
1721
{
1822
_texture->registerTextureInCUDA();
19-
_surf = _texture->mapTextureToSurfaceObject(1);
23+
_surf = _texture->mapTextureArrayToSurfaceObject();
2024
return 0;
2125
}
2226

2327
int ActionSampleTextureArray::Update()
2428
{
2529
kernelCallerWriteTextureArray(
2630
_texture->getDimGrid(), _texture->getDimBlock(),
27-
_surf, GetTime(), _texture->getWidth(),
31+
_surf[0], GetTime(), _texture->getWidth(),
32+
_texture->getHeight(), _texture->getDepth());
33+
kernelCallerWriteTextureArray(
34+
_texture->getDimGrid(), _texture->getDimBlock(),
35+
_surf[1], 2*GetTime(), _texture->getWidth(),
2836
_texture->getHeight(), _texture->getDepth());
2937

3038
cudaDeviceSynchronize();
@@ -33,8 +41,9 @@ int ActionSampleTextureArray::Update()
3341

3442
inline int ActionSampleTextureArray::OnDestroy()
3543
{
36-
_texture->unMapTextureToSurfaceObject(_surf);
44+
_texture->unMapTextureToSurfaceObject(_surf[0]);
3745
_texture->unRegisterTextureInCUDA();
46+
delete(_surf);
3847
return 0;
3948
}
4049

0 commit comments

Comments
 (0)