Green Herb
Green Herb

Reputation: 11

Read pixel data from R32_UINT texture format

im trying to implement ID value on geometry

so i use MRT, first render target is color texture and second render target is R32_UINT format and store unsigned int value

this is how my pixel shader works.

color for first render target and id for second one

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;

and there is issue that is accessing id value from texture

i made same 2DTexture with second rendertarget and based on mouse position i coppied one pixel from texture that is second rendertarget (x,y is mouse position)

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

and through map,unmap i accessed a value and cast it unsigned int *

but i got the weird value. for test i assigned 100 at the rendertarget

i got the 4294967295 which is max value on unsigned int when i drag mouse to empty space i get 0 . (becauce there is nothing)

how can i read unsigned int value from texture? is there any problem in my code?

p.s i tried CopyResource method not copy one pixel data but result was same

Upvotes: 0

Views: 770

Answers (1)

Green Herb
Green Herb

Reputation: 11

my code is correct.. but in my pixel shader there is

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

i didnt assign value at branch because of that i got max unsined int value :(

Upvotes: 1

Related Questions