Search code examples
c++texturesdirect3d

Why does CreateWICTextureFromFile require ShowWindow?


I'm writing some D3D11 apps and am using DirectXTK's CreateWICTextureFromFile to load a texture file into SRV. I wanted to show my rendering window only when I start to draw the scene (after initializing models, textures, shaders, constant buffers, etc.) so I've created the window early on but I omit the ShowWindow until later.

Unfortunately I get an error unless I show the window prior to creating the texture:

// ShowWindow(hwnd, SW_SHOW); // works
hr = DirectX::CreateWICTextureFromFile(device.Get(), L"../../Common/Resources/Textures/green_grid.png", nullptr, psTexture.GetAddressOf());
ShowWindow(hwnd, SW_SHOW); // fails

HResult error:

No such interface supported

Also it seems to work fine if I show the window at the end of initialization as long as I don't load any textures with this function.

Maybe I don't have a good understanding of how a window works with respect to the D3D API. Looking at CreateWICTextureFromFile's parameters, I only see a dependency on device and the SRV. I'm not sure why there's a dependency on the window visibility?


Solution

  • Before you call WICTextureLoader (which uses the Windows Imaging Component) you need to initialize COM as noted in the documentation.

    In your main entry-point, add:

    if (FAILED(CoInitializeEx(nullptr, COINIT_MULTITHREADED)))
        // error
    

    The fact that ShowWindow happens to initalize COM is an interesting side-effect, but that's definitely not a function you are required to call to use my GitHub libraries.