1

Closed

Get Texture Height\Width

description

To render sprites in d3dx9 you used a texture type, where as dxtk uses a shader resource view type...

Biggest issue here is that i can't get the texture's width and height?

(Although i'm new to DX11 so, not too sure if i'm missing something)
Closed Jan 31, 2013 at 8:00 AM by walbourn

comments

walbourn wrote Jan 31, 2013 at 7:59 AM

In Direct3D 10.x and 11.x, you have both a texture resource and a shader resource view. You can get both back when you call DirectX::CreateDDSTextureFromFile() or DirectX::CreateWICTextureFromFile().
Microsoft::WRL::ComPtr<ID3D11ID3D11Resource> res;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv;
HRESULT hr = DirectXCreateDDSTextureFromFile( d3dDevice.Get(), L"SEAFLOOR.DDS",
    &res, &srv )
ThrowIfFailed(hr);

// You use srv.Get() when you are rendering with SpriteBatch

UINT width, height;
GetTextureSize( res.Get(), &width, &height );


void GetTextureSize( ID3D11Resource* res, UINT* width, UINT* height )
{
    assert( res != 0 );

    // This is the most generic solution. you can make it a lot
    // simpler if you know it will always be a 2D texture file
    D3D11_RESOURCE_DIMENSION dim;
    res->GetType( &dim );
    switch(dim)
    {
        case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
            { 
                 auto txt = reinterpret_cast<ID3D11Texture1D*>( res );
                 D3D11_TEXTURE1D_DESC desc;
                 txt->GetDesc( &txt );
                 if ( width ) *width = txt.Width;
                 if ( height ) *height = 1;
            }
            break;
        case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
            {
                 auto txt = reinterpret_cast<ID3D11Texture2D*>( res );
                 D3D11_TEXTURE2D_DESC desc;
                 txt->GetDesc( &txt );
                 if ( width ) *width = txt.Width;
                 if ( height ) *height = txt.Height;
            }
            break;
        case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
            {
                 auto txt = reinterpret_cast<ID3D11Texture3D*>( res );
                 D3D11_TEXTURE3D_DESC desc;
                 txt->GetDesc( &txt );
                 if ( width ) *width = txt.Width;
                 if ( height ) *height = txt.Height;
            }
            break;
        default:
             if ( width ) *width = 0;
             if ( height ) *height = 0;
            break;
    }
}

walbourn wrote Jan 31, 2013 at 8:09 AM

The 'resource' is essentially the object that holds the actual data (a vertex buffer, index buffer, a texture, etc.). The 'shader resource view' is the object that tells the rendering pipeline how to interpret the data. This split provides some additional capabilities. For example, you could have a single 'resource' which is an array of 6 2D textures. You can then have two different 'shader resource views' both pointing to the same 'resource': one is a srv that interprets it as 6 2D textures as an array, another is an srv that interprets it as one cubemap.

It does make the 'simple' case a little more complicated which is why you can just keep the srv around after loading a texture and not worry about the specific 'resource' most of the time.

Speewave wrote Feb 1, 2013 at 6:36 AM

Oh. i see... thanks for that, Didn't know exactly how SRVs worked and how to get the data out of them.
Sorry, still trying to get the hang of DX11 ...

DirectXTutorials wrote Nov 28, 2013 at 1:41 AM

txt->GetDesc(&txt) should be txt->GetDesc(&desc)