Thursday, February 6, 2020

c++ - SRV from UAV on the same texture in directx


I'm programming gpgpu raymarching (volumetric raytracing) in directx11. I succesfully perform compute shader and save raymarched volume data to texture. Then i want to use same texture as SRV in normal graphic pipeline. But it doesnt work, texture is not visible.


Texture is ok, when i save it file it is what i expect. Texture rendering is ok too, when i render another SRV, it is ok. So problem is only in UAV->SRV. I also triple checked if pointers are ok. Please help, i'm getting mad about this.


Here is some code:


//before dispatch

D3D11_TEXTURE2D_DESC textureDesc;
ZeroMemory( &textureDesc, sizeof( textureDesc ) );
textureDesc.Width = xr;
textureDesc.Height = yr;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE ;

textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
D3D->CreateTexture2D( &textureDesc, NULL, &pTexture );

D3D11_UNORDERED_ACCESS_VIEW_DESC viewDescUAV;
ZeroMemory( &viewDescUAV, sizeof( viewDescUAV ) );
viewDescUAV.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
viewDescUAV.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
viewDescUAV.Texture2D.MipSlice = 0;
D3DD->CreateUnorderedAccessView( pTexture, &viewDescUAV, &pTextureUAV );


//the getSRV function after dispatch.
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc ;
ZeroMemory( &srvDesc, sizeof( srvDesc ) );
srvDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = 1;
D3DD->CreateShaderResourceView( pTexture, &srvDesc, &pTextureSRV);

Answer



I solved this. Problem was in releasing. You can't access compute shader's resources until it is fully unbind and released.


Edit you dont have to release it at all. Only "unplug" UAV from from gpu.



//code is shortened
//create UAV
D3D->CreateTexture2D( &textureDesc, NULL, &pTexture );
D3DD->CreateUnorderedAccessView( pTexture, &viewDescUAV, &pTextureUAV );

D3DC->dispatch(...);

//pTextureUAV->release(); // You dont have to release (if you want to use UAV again)
outputTexH->SetUnorderedAccessView(NULL); // This is what is important


effpass->Apply(0,D3DC); // unbind compute shader

//create SRV on the texture, use it.

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...