• Advertisement
Sign in to follow this  

DX11 DX11 - Write to Texture2D UAV, read from Texture2D SRV

Recommended Posts

Hello,
If anybody could please offer me some advice, I would be very grateful.

Currently, if it worked ideally: I write to a UAV Texture2D in the Pixel Shader. I then pass that Texture2D as an SRV into another shader to be read.
However currently, all that displays is the colour white that I clear the UAV with when I call the following line:

const float zero[4] = { 1, 1, 1, 1 };
DX11AppHelper::_pImmediateContext->ClearUnorderedAccessViewFloat(mUAV, zero);
 
I am currently rendering a fullscreen quad for the testing of this. Apologies if this forum post is done badly, I've never done one before.
 
Here is the the initialisation code for the Texture2D, the UAV and the SRV:
 
D3D11_TEXTURE2D_DESC dstex;
ZeroMemory(&dstex, sizeof(dstex));
dstex.Width = DX11AppHelper::_pRenderWidth;
dstex.Height = DX11AppHelper::_pRenderHeight;
dstex.MipLevels = 1;
dstex.ArraySize = 1;
dstex.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
dstex.SampleDesc.Count = 1;
dstex.Usage = D3D11_USAGE_DEFAULT;
dstex.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
dstex.CPUAccessFlags = 0;
dstex.MiscFlags = 0;
 
if (FAILED(DX11AppHelper::_pd3dDevice->CreateTexture2D(&dstex, nullptr, &mOctreeStructure)))
{
return E_FAIL;
}
 
// Create the render target views.
D3D11_UNORDERED_ACCESS_VIEW_DESC  UAVDesc;
ZeroMemory(&UAVDesc, sizeof(UAVDesc));
UAVDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
UAVDesc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
UAVDesc.Texture2D.MipSlice = 0;
 
if (FAILED(DX11AppHelper::_pd3dDevice->CreateUnorderedAccessView(mOctreeStructure, &UAVDesc, &mUAV)))
{
return E_FAIL;
}
 
 
// Create the resource view.
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
ZeroMemory(&SRVDesc, sizeof(SRVDesc));
SRVDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MostDetailedMip = 0;
SRVDesc.Texture2D.MipLevels = 1;
 
if (FAILED(DX11AppHelper::_pd3dDevice->CreateShaderResourceView(mOctreeStructure, &SRVDesc, &mSRV)))
{
return E_FAIL;
}
 
 
Here is my Pixel Shader which I write to the UAV with:
 
RWTexture2D<float4> octree;
 
//--------------------------------------------------------------------------------------
// Pixel Shader Write
//--------------------------------------------------------------------------------------
void PS(VS_OUTPUT input)
{
//float4 finalColour = float4(0.0f, 0.0f, 0.0f, 1.0f);
 
octree[uint2(input.Tex.x, input.Tex.y)] = float4(0.0f, 1.0f, 0.0f, 0.0f);
 
//return finalColour;
}
 
Here is my Pixel Shader that I read the SRV with:
 
Texture2D txDiffuse : register(t0);
 
//--------------------------------------------------------------------------------------
// Pixel Shader Read
//--------------------------------------------------------------------------------------
float4 PS(VS_OUTPUT input) : SV_Target
{
float4 textureColour = txDiffuse.Sample(samLinear, input.Tex);
 
return textureColour;
}
Edited by AlisdairTW

Share this post


Link to post
Share on other sites
Advertisement

Whenever you have any D3D problems, first check what the D3D debug layer has to say.
Create your D3D11 device using the D3D11_CREATE_DEVICE_DEBUG flag, and then enable break-on-error and break-on-warning, and then fix all the D3D errors/warnings:

ID3D11InfoQueue* debugInfoQueue = 0;
m_device->QueryInterface(IID_ID3D11InfoQueue, (void**)&debugInfoQueue);
if(debugInfoQueue) 
{
  m_debugInfoQueue->SetBreakOnSeverity( D3D11_MESSAGE_SEVERITY_CORRUPTION, TRUE );
  m_debugInfoQueue->SetBreakOnSeverity( D3D11_MESSAGE_SEVERITY_ERROR, TRUE );
  m_debugInfoQueue->SetBreakOnSeverity( D3D11_MESSAGE_SEVERITY_WARNING, TRUE );
  m_debugInfoQueue->Release();
}

For most of development you should have the debug layer enabled, and break-on-error / break-on-corruption enabled. Break-on-warning is less important, but still worth checking.

I also recommend using renderdoc to investigate why any particular D3D command isn't doing what you think it should be doing.

Apologies if this forum post is done badly, I've never done one before

Use:

[code][/code]

To format code sections properly :)

 

Do you un-bind the UAV before you bind the SRV? You're not allowed to have any particular resource simultaneously bound for writing (UAV/RTV/DSV) and for reading (SRV).

Share this post


Link to post
Share on other sites

Hello, it's AlisdairTW, I had to create another account as gamedev didn't seem to respond to me signing in with Google today for some reason.

Anywho, I have the debug layer enabled, and I am not getting any errors or warnings from it.

I believe I am correctly unbinding and binding the SRV's and UAV's each time I use them, here is the code for when I call the full screen quad, which is the shader I use to write to the Texture2D with the UAV:

const float zero[4] = { 1, 1, 1, 1 }; 
DX11AppHelper::_pImmediateContext->ClearUnorderedAccessViewFloat(mUAV, zero); 
DX11AppHelper::_pImmediateContext->OMSetRenderTargetsAndUnorderedAccessViews(0, NULL, NULL, 0, 1, &mTexture2DUAV, 0);  // Bind the Texture2D UAV 
DX11AppHelper::_pImmediateContext->IASetInputLayout(mInputLayout);   
DX11AppHelper::_pImmediateContext->VSSetShader(mVertexShader, nullptr, 0); 
DX11AppHelper::_pImmediateContext->PSSetShader(mPixelShader, nullptr, 0);   
DX11AppHelper::_pImmediateContext->PSSetSamplers(0, 1, &mSamplerLinear);   
DX11AppHelper::_pImmediateContext->IASetVertexBuffers(0, 1, &mQuadVertexBuffer, &mQuadStride, &mQuadOffset); 
DX11AppHelper::_pImmediateContext->IASetIndexBuffer(mQuadIndexBuffer, DXGI_FORMAT_R16_UINT, 0);   
DX11AppHelper::_pImmediateContext->DrawIndexed(6, 0, 0);   ID3D11UnorderedAccessView* nullUAV = nullptr; 
DX11AppHelper::_pImmediateContext->OMSetRenderTargetsAndUnorderedAccessViews(0, nullptr, nullptr, 0, 1, &nullUAV, 0);  // UnBind the Texture2D UAV

Here is the code to then execute the second shader, and I am also binding and then unbinding the SRV before and after.

DX11AppHelper::_pImmediateContext->OMSetRenderTargets(1, &_pRenderTargetViews[0], nullptr);
DX11AppHelper::_pImmediateContext->ClearRenderTargetView(_pRenderTargetViews[0], _pClearColour);
DX11AppHelper::_pImmediateContext->IASetInputLayout(,InputLayout);
DX11AppHelper::_pImmediateContext->VSSetShader(mVertexShader, nullptr, 0);
 
DX11AppHelper::_pImmediateContext->PSSetShader(mPixelShader, nullptr, 0);
DX11AppHelper::_pImmediateContext->VSSetConstantBuffers(0, 1, &,ConstantBuffer);
DX11AppHelper::_pImmediateContext->PSSetConstantBuffers(0, 1, &,ConstantBuffer);
 
DX11AppHelper::_pImmediateContext->PSSetShaderResources(0, 1, &mTexture2DSRV)); // Bind SRV
DX11AppHelper::_pImmediateContext->IASetVertexBuffers(0, 1, &_pQuadVertexBuffer, &_pQuadStride, &_pQuadOffset);
DX11AppHelper::_pImmediateContext->IASetIndexBuffer(_pQuadIndexBuffer, DXGI_FORMAT_R16_UINT, 0);
DX11AppHelper::_pImmediateContext->UpdateSubresource(DX11AppHelper::_pConstantBuffer, 0, nullptr, &cb, 0, 0);
DX11AppHelper::_pImmediateContext->DrawIndexed(6, 0, 0);
ID3D11ShaderResourceView* nullShaderResourceView nullShaderResourceView = nullptr;
DX11AppHelper::_pImmediateContext->PSSetShaderResources(0, 1, &nullShaderResourceView); // Unbind SRV

I believe I am doing it correctly?

 

All I am getting on my screen is the cleared white colour of the UAV from the line:

 

const float zero[4] = { 1, 1, 1, 1 };
DX11AppHelper::_pImmediateContext->ClearUnorderedAccessViewFloat(mUAV, zero); 

 

Which makes me think I am setting the UAV incorrectly for the first shader perhaps?

Edited by Alisdair101

Share this post


Link to post
Share on other sites

That is amazing, thank you.

It is working in that regard now, however the endgame of my work is to selectively pick which pixel I want to write to, as I want to store non-image data in the Texture2D, I want to store quad tree data, and I am bit confused as to why I cannot just tell it to write to pixel (x, y) of my choosing.

e.g. octree[0, 0] = float4(0.0f, 1.0f, 0.0f, 0.0f);

then octree[0, 1] = float4(1.0f, 1.0f, 0.0f, 0.0f); and so on

How would I go about doing that, or is that not posssible?

Share this post


Link to post
Share on other sites

That is amazing, thank you.

It is working in that regard now, however the endgame of my work is to selectively pick which pixel I want to write to, as I want to store non-image data in the Texture2D, I want to store quad tree data, and I am bit confused as to why I cannot just tell it to write to pixel (x, y) of my choosing.

e.g. octree[0, 0] = float4(0.0f, 1.0f, 0.0f, 0.0f);

then octree[0, 1] = float4(1.0f, 1.0f, 0.0f, 0.0f); and so on

How would I go about doing that, or is that not posssible?

You can. What he is saying is that there's a very high chance that input.Tex.x & input.Tex.y are floats in range [0; 1] when they should be uint in range [0; texWidth) and range [0; texHeight).

So basically when you write to pixel location (0.75; 0.75) you end up writing to (0, 0) (the first pixel) instead of writing to (768; 384) of a 1024x512 texture.

Share this post


Link to post
Share on other sites

Cheers for all the help guys, one last question.

I am writing to the UAV correctly, thanks again everyone. Is there any way of effectively debugging the data in the Texture3D?

Edited by Alisdair101

Share this post


Link to post
Share on other sites

No problem :) The main question has been solved, is it possible for someone else to mark it as solved as I cannot gain access to my original account as for some reason, as GameDev doesn't like my logging in with Google.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Stewie.G
      Hi,
       
      I've been trying to implement a basic gaussian blur using the gaussian formula, and here is what it looks like so far:
      float gaussian(float x, float sigma)
      {
          float pi = 3.14159;
          float sigma_square = sigma * sigma;
          float a = 1 / sqrt(2 * pi*sigma_square);
          float b = exp(-((x*x) / (2 * sigma_square)));
          return a * b;
      }
      My problem is that I don't quite know what sigma should be.
      It seems that if I provide a random value for sigma, weights in my kernel won't add up to 1.
      So I ended up calling my gaussian function with sigma == 1, which gives me weights adding up to 1, but also a very subtle blur.
      Here is what my kernel looks like with sigma == 1
              [0]    0.0033238872995488885    
              [1]    0.023804742479357766    
              [2]    0.09713820127276819    
              [3]    0.22585307043511713    
              [4]    0.29920669915475656    
              [5]    0.22585307043511713    
              [6]    0.09713820127276819    
              [7]    0.023804742479357766    
              [8]    0.0033238872995488885    
       
      I would have liked it to be more "rounded" at the top, or a better spread instead of wasting [0], [1], [2] with values bellow 0.1.
      Based on my experiments, the key to this is to provide a different sigma, but if I do, my kernel values no longer adds up to 1, which results to a darker blur.
      I've found this post 
      ... which helped me a bit, but I am really confused with this the part where he divide sigma by 3.
      Can someone please explain how sigma works? How is it related to my kernel size, how can I balance my weights with different sigmas, ect...
       
      Thanks :-)
    • By mc_wiggly_fingers
      Is it possible to asynchronously create a Texture2D using DirectX11?
      I have a native Unity plugin that downloads 8K textures from a server and displays them to the user for a VR application. This works well, but there's a large frame drop when calling CreateTexture2D. To remedy this, I've tried creating a separate thread that creates the texture, but the frame drop is still present.
      Is there anything else that I could do to prevent that frame drop from occuring?
    • By cambalinho
      i'm trying draw a circule using math:
      class coordenates { public: coordenates(float x=0, float y=0) { X = x; Y = y; } float X; float Y; }; coordenates RotationPoints(coordenates ActualPosition, double angle) { coordenates NewPosition; NewPosition.X = ActualPosition.X*sin(angle) - ActualPosition.Y*sin(angle); NewPosition.Y = ActualPosition.Y*cos(angle) + ActualPosition.X*cos(angle); return NewPosition; } but now i know that these have 1 problem, because i don't use the orign.
      even so i'm getting problems on how i can rotate the point.
      these coordinates works between -1 and 1 floating points.
      can anyone advice more for i create the circule?
    • By isu diss
      I managed convert opengl code on http://john-chapman-graphics.blogspot.co.uk/2013/02/pseudo-lens-flare.html to hlsl, but unfortunately I don't know how to add it to my atmospheric scattering code (Sky - first image). Can anyone help me?
      I tried to bind the sky texture as SRV and implement lens flare code in pixel shader, I don't know how to separate them (second image)


    • By jonwil
      I have some code (not written by me) that is creating a window to draw stuff into using these:
      CreateDXGIFactory1 to create an IDXGIFactory1
      dxgi_factory->CreateSwapChain to create an IDXGISwapChain
      D3D11CreateDevice to create an ID3D11Device and an ID3D11DeviceContext
      Other code (that I dont quite understand) that creates various IDXGIAdapter1 and IDXGIOutput instances
      Still other code (that I dont quite understand) that is creating some ID3D11RenderTargetView and ID3D11DepthStencilView instances and is doing something with those as well (possibly loading them into the graphics context somewhere although I cant quite see where)
      What I want to do is to create a second window and draw stuff to that as well as to the main window (all drawing would happen on the one thread with all the drawing to the sub-window happening in one block and outside of any rendering being done to the main window). Do I need to create a second IDXGISwapChain for my new window? Do I need to create a second ID3D11Device or different IDXGIAdapter1 and IDXGIOutput interfaces? How do I tell Direct3D which window I want to render to? Are there particular d3d11 functions I should be looking for that are involved in this?
      I am good with Direct3D9 but this is the first time I am working with Direct3D11 (and the guy who wrote the code has left our team so I cant ask him for help
       
  • Advertisement