为了学习,我尝试使用纯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