Search code examples
c++uwptexturesloadingwic

How to map a texture to the default cube utilising the Direct X11 XAML UWP, C++?


I'm relatively new C++/DX11- although I've been researching a lot on the subject to get myself up to speed. Anyway, my issue is that with the default DX11 XAML UWP Template (I'm using VS 2017)- I'm having trouble applying a texture onto the mesh. I've done some digging and found really useful resources on the official MS documentation, however I'm not terribly sure how to implement my texture loading code in my Sample3DSceneRenderer.cpp.

    // After the vertex shader file is loaded, create the shader and input layout.
auto createVSTask = loadVSTask.then([this](const std::vector<byte>& fileData) {
    DX::ThrowIfFailed(
        m_deviceResources->GetD3DDevice()->CreateVertexShader(
            &fileData[0],
            fileData.size(),
            nullptr,
            &m_vertexShader
            )
        );

    static const D3D11_INPUT_ELEMENT_DESC vertexDesc [] =
    {
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "COLOR", 0, DXGI_FORMAT_AYUV, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    };

    DX::ThrowIfFailed(
        m_deviceResources->GetD3DDevice()->CreateInputLayout(
            vertexDesc,
            ARRAYSIZE(vertexDesc),
            &fileData[0],
            fileData.size(),
            &m_inputLayout
            )
        );
});

I figured out by choosing certain values from the specific enum list (The value here is set to "DXGI_FORMAT_AYUV") I can change the colours of the cube, but I am aware usually the process of UV mapping is needed for this kind of thing, but I'm not sure if I have to define the process, or if it's already taken care of in the shader code. (Apologies for any mis-information, I'm still learning DX11)

Here is the documentation that I'm getting the logic from: https://learn.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-resources-textures-how-to

I'm specifically trying to Create WIC Texture From File.

Hopefully anyone familiar with the default DX11 UWP template can see where I'm coming from, all contributions welcome! Thanks.


Solution

  • If you set a break-point on the throw in ThrowIfFailed, you'd see the HRESULT from CreateInputLayout is E_INVALIDARG. The Direct3D Debug Layer will tell you that DXGI_FORMAT_AYUV isn't a supported format for a vertex color:

    D3D11 ERROR: ID3D11Device::CreateInputLayout: Element[1]'s format (AYUV)
    cannot be used with the Input Assembler.
    [ STATE_CREATION ERROR #153: CREATEINPUTLAYOUT_INCOMPATIBLEFORMAT]
    

    DXGI_FORMAT_AYUV is a DirectX Video format, which has special rules for how they are used and you need to keep in mind the Direct3D Hardware Feature level.

    IOW: Don't mess with video formats until you have a handle on basic DirectX 11.

    As for creating a texture using WIC, see WICTextureLoader module.

    You should look at DirectX Tool Kit for DirectX11 which supports UWP for both the DirectX and DirectX+XAML appmodels.