Skip to content

Commit

Permalink
馃悰 Add error when trying mips generation on texture2D for DX11 see #6
Browse files Browse the repository at this point in the history
  • Loading branch information
davidAlgis committed Apr 18, 2023
1 parent f784f62 commit 253c12e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Plugin/PluginInteropUnityCUDA/include/Texture/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class Texture

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

Expand Down
36 changes: 32 additions & 4 deletions Plugin/PluginInteropUnityCUDA/src/RenderAPI/renderAPI_D3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,45 @@ int RenderAPI_D3D11::createShaderResource(
"before.");
return -1;
}
ID3D11Texture2D *texUnityDX11 = (ID3D11Texture2D *)resource;
D3D11_TEXTURE2D_DESC texDesc;
texUnityDX11->GetDesc(&texDesc);
// see #6 https://github.com/davidAlgis/InteropUnityCUDA/issues/6
if (((texDesc.BindFlags & D3D11_BIND_RENDER_TARGET) == false ||
(texDesc.BindFlags & D3D11_BIND_SHADER_RESOURCE) == false) &&
(texDesc.MiscFlags & D3D11_RESOURCE_MISC_GENERATE_MIPS) == false)
{
// see remarks
// https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-id3d11devicecontext-generatemips
Log::log().debugLogError(
"The texture wasn't created with the correct bind or misc flags. "
"Mips cannot be generated. You may have this error, because with "
"DX11, Texture2D mips cannot be generated");
return -2;
}

D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc{};
srvDesc.Format = texDesc.Format;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = -1;

HRESULT hr =
_device->CreateShaderResourceView(resource, &srvDesc, shaderResource);
if (SUCCEEDED(hr) == false)
{
Log::log().debugLogError(
"There has been an error " + std::to_string((int)(hr)) +
"when creating shader resource view associated to the resource.");
return -3;
}

_device->CreateShaderResourceView(resource, NULL, shaderResource);
return 0;
}

void RenderAPI_D3D11::copyTextures2D(ID3D11Texture2D *dest,
ID3D11Texture2D *src)
{
_device->GetImmediateContext(&_context);
_context->CopyResource(dest, src);
getCurrentContext()->CopyResource(dest, src);
}

ID3D11DeviceContext *RenderAPI_D3D11::getCurrentContext()
Expand All @@ -80,7 +109,6 @@ ID3D11DeviceContext *RenderAPI_D3D11::getCurrentContext()
return _context;
}


void RenderAPI_D3D11::ProcessDeviceEvent(UnityGfxDeviceEventType type,
IUnityInterfaces *interfaces)
{
Expand Down
5 changes: 3 additions & 2 deletions Plugin/PluginInteropUnityCUDA/src/Texture/texture_D3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ int Texture_D3D11::registerTextureInCUDA()
D3D11_TEXTURE2D_DESC texDesc;
_texUnityDX11->GetDesc(&texDesc);
DXGI_FORMAT format = texDesc.Format;

// We check if the format is correct see
// https://github.com/davidAlgis/InteropUnityCUDA/issues/2
if (format == DXGI_FORMAT_R8G8B8A8_TYPELESS ||
Expand All @@ -46,8 +47,6 @@ int Texture_D3D11::registerTextureInCUDA()
return -1;
}

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

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

int Texture_D3D11::generateMips()
{
// we generate the shader resource in case the user want to use them (eg. for mips generation)
GRUMBLE(_renderAPI->createShaderResource(_texUnityDX11, &_shaderResources), "Could not create shader resources. Cancel mips genereation");
_renderAPI->getCurrentContext()->GenerateMips(_shaderResources);
return SUCCESS_INTEROP_CODE;
}
Expand Down

0 comments on commit 253c12e

Please sign in to comment.