microsoft / DirectX-Graphics-Samples

This repo contains the DirectX Graphics samples that demonstrate how to build graphics intensive applications on Windows.
MIT License
6.06k stars 2.03k forks source link

Direct3D 12 Motion Estimation (sample ?) #748

Open DTL2020 opened 2 years ago

DTL2020 commented 2 years ago

I trying to add hardware Motion Estimation support to freeware Avisynth plugin mvtoots based on new Microsoft API Direct3D Motion Estimation. Unfortunately the description at https://docs.microsoft.com/en-us/windows/win32/medfound/direct3d-video-motion-estimation is very incomplete and do not shows how to load source and ref frames to Motion Estimator input resources and get data back.

Current progress - the InitD3D12 and each pairs frames processing is mostly done (in this commit - https://github.com/DTL2020/mvtools/commit/ed95355629cd294181b90747e32ac261b683ade9 ) : Init is function https://github.com/DTL2020/mvtools/blob/ed95355629cd294181b90747e32ac261b683ade9/Sources/MVAnalyse.cpp#L1046

and processing is inside block: https://github.com/DTL2020/mvtools/blob/ed95355629cd294181b90747e32ac261b683ade9/Sources/MVAnalyse.cpp#L701

Current state: the all called from Direct3D functions returns non-error HRs but the final mapping of the readback buffer https://github.com/DTL2020/mvtools/blob/ed95355629cd294181b90747e32ac261b683ade9/Sources/MVAnalyse.cpp#L883 return error 'D3D device disconnected' (DXGI_ERROR_DEVICE_REMOVED error 0x887A0005) and error-reason https://github.com/DTL2020/mvtools/blob/ed95355629cd294181b90747e32ac261b683ade9/Sources/MVAnalyse.cpp#L886 'application have bugs and need debug' (DXGI_ERROR_INVALID_CALL 0x887A0001 | The application provided invalid parameter data; this must be debugged and fixed before the application is released.) Very unclear where may be an error in many of API calls.

Also of really visible differences from the described Motion Estimation API: I still do not found how to switch source resources state from 'resource write' (required to copy input data into resource) to state 'D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ required before call to EstimateMotion().

When I try to add resourcebarrier https://github.com/DTL2020/mvtools/blob/ed95355629cd294181b90747e32ac261b683ade9/Sources/MVAnalyse.cpp#L722 to change resource state from D3D12_RESOURCE_STATE_COPY_DEST to D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ the CommandList->Close(); function returns error indicating in the command sequence there is an error somewhere.

Are there any working sample of feeding frames data in the D3D12 video encoder or motion estimator available ? Or what is the preffered way to get an support on Direct3D 12 video API if possible ?

walbourn commented 2 years ago

You may find this sample useful.

sivileri commented 2 years ago

Hi,

Thanks for reaching out. Unfortunately, we don't have a good sample for sharing on the D3D12 Video Motion Estimation API. For Video Decode/Processing which are similar, you could refer to VideoDecode.cpp or VideoProcess.cpp .

Based on the issues you describe:

  1. The D3D12 resources that are in D3D12_RESOURCE_STATE_COPY_DESThave to be transitioned to D3D12_RESOURCE_STATE_COMMONafter the copy finishes and then transitioned from D3D12_RESOURCE_STATE_COMMONto D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ. Using the D3D12 debug layer will help with these issues by printing messages with a debugger attached regarding state transitions, among other issues/errors.
  2. I believe the device is being removed on ExecuteCommandListsbut it's being hit as an issue in the immediate next D3D12 call, which coincidentally is to Map(). using GetDeviceRemovedReason() and checking it's result is S_OKafter executing command lists (or to validate other operations too) can help detect this issues.