Skip to content

Commit ef6c8f1

Browse files
committed
✨ Make it works with texture array that are wrote at the same time
1 parent 409d4c7 commit ef6c8f1

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

Plugin/PluginInteropUnityCUDA/include/Texture/texture.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Texture
106106
UNITY_INTERFACE_EXPORT void *getNativeTexturePtr() const;
107107

108108
/**
109-
* Get the pointer of _surfObjArray
109+
* Get the pointer of d_surfObjArray
110110
* This array of surface object is necessary
111111
* to write or read into a texture
112112
*/
@@ -137,9 +137,15 @@ class Texture
137137

138138
private:
139139
// An array of surface object that will be of the size of texture depth
140+
// This array is allocate on host side and will be copy to device memory
141+
// when texture is map to it
142+
cudaSurfaceObject_t *_surfObjArray;
143+
144+
// A device array of surface object that will be of the size of texture depth
145+
// This array is allocate on device memory.
140146
// the surface object is the object that you can used to write into texture
141147
// from cuda api (eg. with surf2DWrite)
142-
cudaSurfaceObject_t *_surfObjArray;
148+
cudaSurfaceObject_t *d_surfObjArray;
143149

144150
dim3 _dimBlock;
145151
dim3 _dimGrid;

Plugin/PluginInteropUnityCUDA/src/Texture/texture.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Texture::Texture(void *textureHandle, int textureWidth, int textureHeight,
2929
// initialize surface object
3030
_surfObjArray = new cudaSurfaceObject_t[textureDepth];
3131

32+
CUDA_CHECK(cudaMalloc(&d_surfObjArray,
33+
_textureDepth * sizeof(cudaSurfaceObject_t)));
34+
3235
for (int i = 0; i < textureDepth; i++)
3336
{
3437
_surfObjArray[i] = 0;
@@ -40,6 +43,7 @@ Texture::Texture(void *textureHandle, int textureWidth, int textureHeight,
4043
Texture::~Texture()
4144
{
4245
delete (_surfObjArray);
46+
CUDA_CHECK(cudaFree(d_surfObjArray));
4347
}
4448

4549
void Texture::mapTextureToSurfaceObject()
@@ -63,6 +67,9 @@ void Texture::mapTextureToSurfaceObject()
6367
CUDA_CHECK(cudaCreateSurfaceObject(&_surfObjArray[i], &resDesc));
6468
CUDA_CHECK(cudaGetLastError());
6569
}
70+
CUDA_CHECK(cudaMemcpy(d_surfObjArray, _surfObjArray,
71+
_textureDepth * sizeof(cudaSurfaceObject_t),
72+
cudaMemcpyHostToDevice));
6673
}
6774

6875
void Texture::unmapTextureToSurfaceObject()
@@ -108,7 +115,9 @@ void *Texture::getNativeTexturePtr() const
108115

109116
cudaSurfaceObject_t *Texture::getSurfaceObjectArray() const
110117
{
111-
return _surfObjArray;
118+
// to use a complete array of surface object in a kernel,
119+
// we need to use the array allocate on device memory
120+
return d_surfObjArray;
112121
}
113122

114123
cudaSurfaceObject_t Texture::getSurfaceObject(int indexInArray) const
@@ -121,5 +130,10 @@ cudaSurfaceObject_t Texture::getSurfaceObject(int indexInArray) const
121130
return 0;
122131
}
123132

133+
// to use a single surface object in a kernel
134+
// we can use directly the surface object that
135+
// is on host side, because cudaSurfaceObject_t is a
136+
// typename for unsigned long long which can be directly
137+
// send to kernel as it's managed memory ?
124138
return _surfObjArray[indexInArray];
125139
}

Plugin/SampleBasic/src/sample_kernels.cu

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ void kernelCallerWriteTextureArray(const dim3 dimGrid, const dim3 dimBlock,
5656
const float time, const int width,
5757
const int height, const int depth)
5858
{
59-
60-
writeTex<<<dimGrid, dimBlock>>>(surfObjArray[0], width, height, time);
61-
62-
writeTex<<<dimGrid, dimBlock>>>(surfObjArray[1], width, height, 2*time);
63-
// writeTexArray<<<dimGrid, dimBlock>>>(surfObjArray, width, height, depth, time);
59+
writeTexArray<<<dimGrid, dimBlock>>>(surfObjArray, width, height, depth, time);
6460
}
6561

6662
void kernelCallerWriteBuffer(const dim3 dimGrid, const dim3 dimBlock,

0 commit comments

Comments
 (0)