在c11中用clang编译的程序中调用DX12函数时出现问题

为了学习,我尝试使用纯c11中的clang正确调用DirectX12 API。我设法将所有内容进行编译,但有时vtable指针似乎已损坏,并在不应该更改的地方更改了它们指向的位置。

ID3D12DeviceVtbl* tbl; //tracking the vtable pointer for debugging purposes

D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
if (D3D12CreateDevice(NULL,featureLevel,&IID_ID3D12Device,(void**)&g_pd3dDevice) != S_OK)
{
    return false;
}

{
    tbl = g_pd3dDevice->lpVtbl;

    D3D12_DESCRIPTOR_HEAP_DESC desc ;
    desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
    desc.NumDescriptors = NUM_BACK_BUFFERS;
    desc.flags = D3D12_DESCRIPTOR_HEAP_flaG_NONE;
    desc.NodeMask = 1;

    // works fine
    if (g_pd3dDevice->lpVtbl->CreateDescriptorHeap(g_pd3dDevice,&desc,&IID_ID3D12DescriptorHeap,(void**)&g_pd3dRtvDescHeap) != S_OK)
        return false;

    // works fine
    SIZE_T rtvDescriptorSize = g_pd3dDevice->lpVtbl->GetDescriptorHandleIncrementSize(g_pd3dDevice,D3D12_DESCRIPTOR_HEAP_TYPE_RTV);

    // works fine
    D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = g_pd3dRtvDescHeap->lpVtbl->getcPUDescriptorHandleForHeapStart(g_pd3dRtvDescHeap);

    // after the line above executes,g_pd3dDevice->lpVtbl now points to somewhere new and invalid
}

{
    D3D12_DESCRIPTOR_HEAP_DESC desc ;
    desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
    desc.NumDescriptors = 1;
    desc.flags = D3D12_DESCRIPTOR_HEAP_flaG_SHADER_VISIBLE;

    // g_pd3dDevice->lpVtbl can't find CreateDescriptorHeap
    if (g_pd3dDevice->lpVtbl->CreateDescriptorHeap(g_pd3dDevice,(void**)&g_pd3dSrvDescHeap) != S_OK)
    {
        return false;
    }

    // tbl->CreateDescriptorHeap is still a valid pointer however.
}

正如评论中的解释,在D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = g_pd3dRtvDescHeap->lpVtbl->getcPUDescriptorHandleForHeapStart(g_pd3dRtvDescHeap);行之后,g_pd3dDevice->lpVtbl指针改变并指向无效的地方,我不明白为什么。

我正在使用以下选项进行编译:

clang.exe -std=c11 -pedantic-errors -g -D CINTERFACE .\main.c
nihaozhuzijian 回答:在c11中用clang编译的程序中调用DX12函数时出现问题

您在C绑定中遇到一个已知的错误...请参阅this thread,其中解释了GetCPUDescriptorHandleForHeapStart中存在的错误。

  

Direct3D9Ex的C绑定也存在类似的问题。基本问题是几乎所有用户都将C ++用于DirectX,因为它自然会映射到COM。 C绑定大部分是自动生成的;他们没有经过良好的测试或维护。

本文链接:https://www.f2er.com/3111920.html

大家都在问