在像素着色器中写入无人机

时间:2014-04-03 13:55:39

标签: directx shader hlsl directx-11

我在像素着色器中通过写一些值来做一些纹理作为无人机的实验,但是当我再次将相同的纹理绑定为SRV时,我没有在下一个绘制调用中看到它的效果。

示例着色器:

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;   
}

如果需要,我可以提供应用源代码。

1 个答案:

答案 0 :(得分:0)

我启用了调试层。这是无人机格式不匹配错误。在UAV描述中,我将R8G8B8A8_UNORM声明为格式,并且我在着色器中将该元素作为UINT访问。

description :D3D11错误:ID3D11DeviceContext :: Draw:着色器代码(UINT)中声明的组件0的资源返回类型与绑定到Unordered Access View插槽1的资源类型不兼容像素着色器单元(UNORM)。如果着色器实际使用视图[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); 

通过在临时资源中转储纹理来确认。多谢你们。