Question

I was doing some experiment with textures as UAV in pixel shader by writing some value on it, but I'm not seeing its effect in the next draw call when I bind the same texture again as an SRV.

Example shader:

RWTexture2D<unsigned int> uav;
Texture2D tex : register(t0);

// Vertex Shader
float4 VS( float4 Pos : POSITION ) : SV_POSITION
{
      return Pos;
}

// Pixel Shader, draw1 warm up 
float4 PS( float4 Pos : SV_POSITION ) : SV_Target
{
     return float4( 1.0f, 1.0f, 0.0f, 1.0f );    // Yellow, with Alpha = 1
}

// Pixel Shader, we are writing onto the texture by binding it as an UAV, draw2
float4 PS1( float4 Pos : SV_POSITION ) : SV_Target
{
     if((Pos.x %2) && (Pos.y %2))
     {
            uav[Pos.xy]=0xFF000000; //some color
     }
     else
     {
            uav[Pos.xy]=0x00FF0000; //some color
     }

     return float4( 1.0f, 0.0f, 0.0f, 1.0f );
}

// Pixel Shader, here we are accessing texture as an SRV, draw3
float4 PS2( float4 Pos : SV_POSITION ) : SV_Target
{
       float4 x = tex[Pos.xy];
       return x;   
}

I can provide the app source code if required.

Was it helpful?

Solution

I enabled the debug layer. It was UAV format mismatch error. In the UAV description, I declared R8G8B8A8_UNORM as a format and I'm accessing the element as UINT in the shader.

description: D3D11 ERROR: ID3D11DeviceContext::Draw: The resource return type for component 0 declared in the shader code (UINT) is not compatible with the resource type bound to Unordered Access View slot 1 of the Pixel Shader unit (UNORM). This mismatch is invalid if the shader actually uses the view [ EXECUTION ERROR #2097372: DEVICE_UNORDEREDACCESSVIEW_RETURN_TYPE_MISMATCH]

Source code: 

        D3D11_UNORDERED_ACCESS_VIEW_DESC UAVdesc;
        ZeroMemory( &SRVdesc, sizeof(SRVdesc));
        UAVdesc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;
        UAVdesc.ViewDimension=D3D11_UAV_DIMENSION_TEXTURE2D;
        UAVdesc.Texture2D.MipSlice=0;
        g_pd3dDevice->CreateUnorderedAccessView( g_pTexture, &UAVdesc, &g_pUAV);


Texture created : 

    D3D11_TEXTURE2D_DESC TextureData;
    ZeroMemory( &TextureData, sizeof(TextureData) );
    TextureData.ArraySize=1;
    TextureData.Height=height;
    TextureData.Width=width;
    TextureData.Format=DXGI_FORMAT_R8G8B8A8_TYPELESS;
    TextureData.CPUAccessFlags=0;
    TextureData.BindFlags=D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET|D3D11_BIND_UNORDERED_ACCESS;
    TextureData.MipLevels=1;
    TextureData.MiscFlags=0;
    TextureData.SampleDesc.Count=1;
    TextureData.SampleDesc.Quality=0;
    TextureData.Usage=D3D11_USAGE_DEFAULT;


    D3D11_SUBRESOURCE_DATA InitialData;
    ZeroMemory( &InitialData, sizeof(InitialData));
    InitialData.pSysMem=pData;
    InitialData.SysMemPitch=width * sizeof(UINT);
    InitialData.SysMemSlicePitch=width * sizeof(UINT) * height;
    g_pd3dDevice->CreateTexture2D( &TextureData, &InitialData, &g_pTexture);

Shader code is already given above. 

Fix:    
        D3D11_UNORDERED_ACCESS_VIEW_DESC UAVdesc;
        ZeroMemory( &SRVdesc, sizeof(SRVdesc));
        **UAVdesc.Format=DXGI_FORMAT_R32_UINT;**
        UAVdesc.ViewDimension=D3D11_UAV_DIMENSION_TEXTURE2D;
        UAVdesc.Texture2D.MipSlice=0;
        g_pd3dDevice->CreateUnorderedAccessView( g_pTexture, &UAVdesc, &g_pUAV); 

confirmed by dumping texture in staging resource. Thanks guys.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top