Skip to content

Commit 3f4aaf3

Browse files
committed
🐛 Add a null test for texture
1 parent 5bb5d53 commit 3f4aaf3

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

Plugin/PluginInteropUnityCUDA/src/Texture/texture.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
Texture::Texture(void *textureHandle, int textureWidth, int textureHeight,
55
int textureDepth)
66
{
7+
if (textureHandle == nullptr)
8+
{
9+
Log::log().debugLogError(
10+
"The texture ptr is null, please create it in Unity and then send "
11+
"it with GetNativePtr function.");
12+
return;
13+
}
14+
715
_textureHandle = textureHandle;
816
_textureWidth = textureWidth;
917
_textureHeight = textureHeight;
@@ -69,8 +77,8 @@ int Texture::mapTextureToSurfaceObject()
6977
CUDA_CHECK_RETURN(cudaGetLastError());
7078
}
7179
CUDA_CHECK_RETURN(cudaMemcpy(d_surfObjArray, _surfObjArray,
72-
_textureDepth * sizeof(cudaSurfaceObject_t),
73-
cudaMemcpyHostToDevice));
80+
_textureDepth * sizeof(cudaSurfaceObject_t),
81+
cudaMemcpyHostToDevice));
7482
return SUCCESS_INTEROP_CODE;
7583
}
7684

@@ -134,9 +142,9 @@ cudaSurfaceObject_t Texture::getSurfaceObject(int indexInArray) const
134142
}
135143

136144
// to use a single surface object in a kernel
137-
// we can use directly the surface object that
145+
// we can use directly the surface object that
138146
// is on host side, because cudaSurfaceObject_t is a
139147
// typename for unsigned long long which can be directly
140-
// send to kernel as it's managed memory ?
148+
// send to kernel as it's managed memory ?
141149
return _surfObjArray[indexInArray];
142150
}

Plugin/PluginInteropUnityCUDA/src/Texture/texture_D3D11.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ Texture_D3D11::~Texture_D3D11()
2222

2323
int Texture_D3D11::registerTextureInCUDA()
2424
{
25+
if (_textureHandle == nullptr)
26+
{
27+
Log::log().debugLogError(
28+
"The texture ptr is null, please create it in Unity and then send "
29+
"it with GetNativePtr function.");
30+
return -1;
31+
}
2532
// texture2D and texture2D array are ID3D11Texture2D in Unity for DX11
2633
_texUnityDX11 = (ID3D11Texture2D *)_textureHandle;
2734

@@ -39,23 +46,21 @@ int Texture_D3D11::registerTextureInCUDA()
3946
format == DXGI_FORMAT_R32G32_TYPELESS ||
4047
format == DXGI_FORMAT_R8_TYPELESS)
4148
{
42-
Log::log().debugLogError(
43-
"Texture of type " + std::to_string(format) +
44-
" cannot be registered in CUDA." +
45-
" It may comes from the fact that you can\'t used RenderTexture for "
46-
"DX11 but only Texture2D.");
49+
Log::log().debugLogError("Texture of type " + std::to_string(format) +
50+
" cannot be registered in CUDA." +
51+
" It may comes from the fact that you can\'t "
52+
"used RenderTexture for "
53+
"DX11 but only Texture2D.");
4754
return -1;
4855
}
4956

50-
5157
// register the texture to cuda : it initialize the _pGraphicsResource
5258
CUDA_CHECK_RETURN(cudaGraphicsD3D11RegisterResource(
5359
&_graphicsResource, _texUnityDX11, cudaGraphicsRegisterFlagsNone));
5460

5561
return SUCCESS_INTEROP_CODE;
5662
}
5763

58-
5964
int Texture_D3D11::unregisterTextureInCUDA()
6065
{
6166
CUDA_CHECK_RETURN(cudaGraphicsUnregisterResource(_graphicsResource));
@@ -64,13 +69,14 @@ int Texture_D3D11::unregisterTextureInCUDA()
6469

6570
int Texture_D3D11::generateMips()
6671
{
67-
// we generate the shader resource in case the user want to use them (eg. for mips generation)
68-
GRUMBLE(_renderAPI->createShaderResource(_texUnityDX11, &_shaderResources), "Could not create shader resources. Cancel mips genereation");
72+
// we generate the shader resource in case the user want to use them (eg.
73+
// for mips generation)
74+
GRUMBLE(_renderAPI->createShaderResource(_texUnityDX11, &_shaderResources),
75+
"Could not create shader resources. Cancel mips genereation");
6976
_renderAPI->getCurrentContext()->GenerateMips(_shaderResources);
7077
return SUCCESS_INTEROP_CODE;
7178
}
7279

73-
7480
int Texture_D3D11::copyUnityTextureToAPITexture()
7581
{
7682
_renderAPI->copyTextures2D(_texBufferInterop, _texUnityDX11);

Plugin/PluginInteropUnityCUDA/src/Texture/texture_OpenGLCoreES.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ Texture_OpenGLCoreES::~Texture_OpenGLCoreES()
1818

1919
int Texture_OpenGLCoreES::registerTextureInCUDA()
2020
{
21+
if (_textureHandle == nullptr)
22+
{
23+
Log::log().debugLogError(
24+
"The texture ptr is null, please create it in Unity and then send "
25+
"it with GetNativePtr function.");
26+
return -1;
27+
}
2128
// if depth is < 2 it's a texture2D, else it's a texture2DArray
2229
_texTarget = _textureDepth < 2 ? GL_TEXTURE_2D : GL_TEXTURE_2D_ARRAY;
2330

0 commit comments

Comments
 (0)