Skip to content

Commit 253c12e

Browse files
committed
🐛 Add error when trying mips generation on texture2D for DX11 see #6
1 parent f784f62 commit 253c12e

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

Plugin/PluginInteropUnityCUDA/include/Texture/texture.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class Texture
7676

7777
/**
7878
* Generate the mips maps of the texture
79+
* For DX11 it doesn't works for Texture2D
80+
* see issue #6 https://github.com/davidAlgis/InteropUnityCUDA/issues/6
7981
*/
8082
UNITY_INTERFACE_EXPORT virtual int generateMips() = 0;
8183

Plugin/PluginInteropUnityCUDA/src/RenderAPI/renderAPI_D3D11.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,45 @@ int RenderAPI_D3D11::createShaderResource(
6262
"before.");
6363
return -1;
6464
}
65+
ID3D11Texture2D *texUnityDX11 = (ID3D11Texture2D *)resource;
66+
D3D11_TEXTURE2D_DESC texDesc;
67+
texUnityDX11->GetDesc(&texDesc);
68+
// see #6 https://github.com/davidAlgis/InteropUnityCUDA/issues/6
69+
if (((texDesc.BindFlags & D3D11_BIND_RENDER_TARGET) == false ||
70+
(texDesc.BindFlags & D3D11_BIND_SHADER_RESOURCE) == false) &&
71+
(texDesc.MiscFlags & D3D11_RESOURCE_MISC_GENERATE_MIPS) == false)
72+
{
73+
// see remarks
74+
// https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-id3d11devicecontext-generatemips
75+
Log::log().debugLogError(
76+
"The texture wasn't created with the correct bind or misc flags. "
77+
"Mips cannot be generated. You may have this error, because with "
78+
"DX11, Texture2D mips cannot be generated");
79+
return -2;
80+
}
81+
82+
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc{};
83+
srvDesc.Format = texDesc.Format;
84+
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
85+
srvDesc.Texture2D.MipLevels = -1;
86+
87+
HRESULT hr =
88+
_device->CreateShaderResourceView(resource, &srvDesc, shaderResource);
89+
if (SUCCEEDED(hr) == false)
90+
{
91+
Log::log().debugLogError(
92+
"There has been an error " + std::to_string((int)(hr)) +
93+
"when creating shader resource view associated to the resource.");
94+
return -3;
95+
}
6596

66-
_device->CreateShaderResourceView(resource, NULL, shaderResource);
6797
return 0;
6898
}
6999

70100
void RenderAPI_D3D11::copyTextures2D(ID3D11Texture2D *dest,
71101
ID3D11Texture2D *src)
72102
{
73-
_device->GetImmediateContext(&_context);
74-
_context->CopyResource(dest, src);
103+
getCurrentContext()->CopyResource(dest, src);
75104
}
76105

77106
ID3D11DeviceContext *RenderAPI_D3D11::getCurrentContext()
@@ -80,7 +109,6 @@ ID3D11DeviceContext *RenderAPI_D3D11::getCurrentContext()
80109
return _context;
81110
}
82111

83-
84112
void RenderAPI_D3D11::ProcessDeviceEvent(UnityGfxDeviceEventType type,
85113
IUnityInterfaces *interfaces)
86114
{

Plugin/PluginInteropUnityCUDA/src/Texture/texture_D3D11.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int Texture_D3D11::registerTextureInCUDA()
2828
D3D11_TEXTURE2D_DESC texDesc;
2929
_texUnityDX11->GetDesc(&texDesc);
3030
DXGI_FORMAT format = texDesc.Format;
31+
3132
// We check if the format is correct see
3233
// https://github.com/davidAlgis/InteropUnityCUDA/issues/2
3334
if (format == DXGI_FORMAT_R8G8B8A8_TYPELESS ||
@@ -46,8 +47,6 @@ int Texture_D3D11::registerTextureInCUDA()
4647
return -1;
4748
}
4849

49-
// we generate the shader resource in case the user want to use them (eg. for mips generation)
50-
_renderAPI->createShaderResource(_texUnityDX11, &_shaderResources);
5150

5251
// register the texture to cuda : it initialize the _pGraphicsResource
5352
CUDA_CHECK_RETURN(cudaGraphicsD3D11RegisterResource(
@@ -65,6 +64,8 @@ int Texture_D3D11::unregisterTextureInCUDA()
6564

6665
int Texture_D3D11::generateMips()
6766
{
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");
6869
_renderAPI->getCurrentContext()->GenerateMips(_shaderResources);
6970
return SUCCESS_INTEROP_CODE;
7071
}

0 commit comments

Comments
 (0)