【问题标题】:Read pixel data from R32_UINT texture format从 R32_UINT 纹理格式读取像素数据
【发布时间】:2021-02-01 18:57:53
【问题描述】:

我试图在几何上实现 ID 值

所以我使用 MRT,第一个渲染目标是颜色纹理,第二个渲染目标是 R32_UINT 格式并存储 unsigned int 值

这就是我的像素着色器的工作原理。

第一个渲染目标的颜色和第二个渲染目标的 id

struct PS_OUT
{
    float4 color : SV_TARGET0;
    uint id : SV_TARGET1;
};
....
...
..
.

output.color = tex[0].Sample(splr, tc * tilingFactor) * color;
output.id = 100;
return output;

并且存在从纹理访问 id 值的问题

我用第二个渲染目标制作了相同的 2DTexture,并基于鼠标位置,我从纹理中复制了一个像素,即第二个渲染目标 (x,y 是鼠标位置)

D3D11_TEXTURE2D_DESC textureDesc;
    textureDesc.Width = 1;
    textureDesc.Height = 1;
    textureDesc.MipLevels = 1;
    textureDesc.ArraySize = 1;
    textureDesc.Format = (DXGI_FORMAT_R32_UINT);
    textureDesc.SampleDesc.Count = 1;
    textureDesc.SampleDesc.Quality = 0;
    textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
    textureDesc.Usage = D3D11_USAGE_STAGING;
    textureDesc.BindFlags = 0;
    textureDesc.MiscFlags = 0;

gfx.GetDevice()->CreateTexture2D(&textureDesc, nullptr, &pTexTemp);

D3D11_BOX srcBox;
srcBox.left = x;
srcBox.right = x + 1;
srcBox.bottom = y + 1;
srcBox.top = y;
srcBox.front = 0;
srcBox.back = 1;
gfx.GetContext()->CopySubresourceRegion(pTexTemp.Get(), 0, 0, 0, 0, pTexSource.Get(), 0, &srcBox);
D3D11_MAPPED_SUBRESOURCE msr = {};
gfx.GetContext()->Map(pTexTemp.Get(), 0, D3D11_MAP::D3D11_MAP_READ, 0, &msr);
    if (x >= 0 && x <= m_width && y >= 0 && y <= m_height)
    {

        uint32_t* data = reinterpret_cast<uint32_t*>(msr.pData);
    }
gfx.GetContext()->Unmap(pTexTemp.Get(), 0);

通过 map,unmap 我访问了一个值并将其转换为 unsigned int *

但我得到了奇怪的价值。对于测试,我在渲染目标分配了 100

当我将鼠标拖到空白空间时,我得到了 4294967295,这是 unsigned int 上的最大值,我得到 0 。 (因为什么都没有)

如何从纹理中读取 unsigned int 值?我的代码有什么问题吗?

p.s 我试过 CopyResource 方法没有复制一个像素数据但结果是一样的

【问题讨论】:

  • 我认为您的程序没有任何问题。所有的值和参数似乎都是正确的。您能否确保使用例如正确填充您的渲染目标? renderdoc 或 Visual Studio 图形调试器?你启用调试层了吗?您能否在创建渲染目标并使用 OMSetRenderTargets 设置它们的位置显示您的代码?
  • @zezanjee 哎呀..我的像素着色器代码中有错误,如果分支,但我没有分配值,因为分配了第二个渲染目标的最大值:(谢谢你的回复^.^

标签: c++ textures directx-11 hlsl render-to-texture


【解决方案1】:

我的代码是正确的.. 但在我的像素着色器中有

PS_OUT main(float2 tc: Texcoord, float4 color : Color, float texIndex : v_TexIndex, float tilingFactor : v_TilingFactor)
{
    int index = (int)texIndex;
    PS_OUT output;
    [unroll]
    for (int i = 0; i < 32; ++i)
    {
        **if (index == 0)**
        {
            output.color = tex[0].Sample(splr, tc * tilingFactor) * color;
            return output;
        }
        **if (i == index)**
        {
            output.color =  tex[i].Sample(splr, tc * tilingFactor) * color;
            return output;
        }   
    }
    
    output.color = tex[0].Sample(splr, tc * tilingFactor) * color;
    output.id = 100;
    return output;
}

我没有在分支上赋值,因为我得到了最大的 unsined int 值:(

【讨论】:

    猜你喜欢
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    相关资源
    最近更新 更多