1

Try using directx12 to render a model, following rendering a model, using its ._obj .mtl .jpg assets, convert these assets to .sdkmesh successfully. I'm using Directx samples HelloTriangle, try to make some changes in HelloTriangle to render a model.

My code is a bit different from rendering a model, which is

m_model = Model::CreateFromSDKMESH(m_device.Get(), GetAssetFullPath(L"cup.sdkmesh").c_str());

m_modelResources = m_model->LoadTextures(m_device.Get(), resourceUpload,
            GetAssetFullPath(L"").c_str());

I got an error in LoadTextures, following the source code I find the error comes from EffectTextureFactory.cpp

HRESULT hr = CreateWICTextureFromFileEx(
                mDevice.Get(),
                mResourceUploadBatch,
                fullName,
                0u,
                D3D12_RESOURCE_FLAG_NONE,
                static_cast<WIC_LOADER_FLAGS>(loadFlags),
                textureEntry.mResource.ReleaseAndGetAddressOf());

the error is

onecore\com\combase\objact\objact.cxx(800)\combase.dll!00007FF8518976C5: (caller: 00007FF8518969D2) ReturnHr(1) tid(1ad0) 800401F0 尚未调用 CoInitialize。
ERROR: CreateWICTextureFromFile failed (80004002) for 'D:\D3D12HelloWorld\src\HelloTriangle\bin\x64\Debug\cup.jpg'
Exception thrown at 0x00007FF8509B4B2C in D3D12HelloTriangle.exe: Microsoft C++ exception: std::runtime_error at memory location 0x00000089456FDEE8.
Unhandled exception at 0x00007FF8509B4B2C in D3D12HelloTriangle.exe: Microsoft C++ exception: std::runtime_error at memory location 0x00000089456FDEE8.

Assertion failed!

Program: ...HelloTriangle\bin\x64\Debug\D3D12HelloTriangle.exe
File: D:\a\_work\1\s\Src\EffectTextureFactory.cpp
Line: 194

Expression: textureEntry.mResource != nullptr

In debugging mode, I found the fullName variable is correct and suffix with .jpg. What's the problem? Am I missing some initialization or something?

this is the changes I made In D3D12HelloTriangle.cpp LoadAssets function

        // Define the geometry for a triangle.
        //Vertex triangleVertices[] =
        //{
        //  { { 0.0f, 0.25f * m_aspectRatio, 0.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } },
        //  { { 0.25f, -0.25f * m_aspectRatio, 0.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } },
        //  { { -0.25f, -0.25f * m_aspectRatio, 0.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }
        //};

        //const UINT vertexBufferSize = sizeof(triangleVertices);

        m_graphicsMemory = std::make_unique<GraphicsMemory>(m_device.Get());
        m_state = std::make_unique<CommonStates>(m_device.Get());
        m_model = Model::CreateFromSDKMESH(m_device.Get(), GetAssetFullPath(L"cup.sdkmesh").c_str());
        ResourceUploadBatch resourceUpload(m_device.Get());
        resourceUpload.Begin();

        m_modelResources = m_model->LoadTextures(m_device.Get(), resourceUpload,
            GetAssetFullPath(L"").c_str());
        m_fxFactory = std::make_unique<EffectFactory>(m_modelResources->Heap(), m_state->Heap());

        auto uploadResourceFinished = resourceUpload.End(m_commandQueue.Get());
        uploadResourceFinished.wait();

        RenderTargetState rtState(DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_D32_FLOAT);
        EffectPipelineStateDescription pd(nullptr, CommonStates::Opaque, CommonStates::DepthDefault, CommonStates::CullClockwise, rtState);
        m_modelNormal = m_model->CreateEffects(*m_fxFactory, pd, pd);
youngh
  • 11
  • 2
  • 1
    In your call to `LoadTextures`, shouldn't you be supplying a valid texture filename?. Another consideration in DX12 is that textures are loaded separately to models. Perhaps review the _DirectX Tool Kit for DX12_ tutorial on texture & model loading to see how this is performed. – Maico De Blasio Aug 06 '23 at 04:52

0 Answers0