AMF icon indicating copy to clipboard operation
AMF copied to clipboard

No DX12 encode examples or documentation - unclear synchronisation mechanism

Open twhittock opened this issue 4 years ago • 4 comments

Hi.

I'm trying to integrate the encoder into a DX12 application, initialising the AMFContext2 with DX12, and providing DX12 surfaces to the encoder. But there is no reference material available for the AMFContext2 class, nor are there any examples showing its usage.

Specifically, there doesn't seem to be any information about how to ensure the resource has been completely rendered to - there doesn't seem to be any support for fences, for example.

twhittock avatar Feb 15 '21 13:02 twhittock

Hi, we should update documentation. Currently there is no encoder running via DX12 - you have to interop into DX11. To do this, provide both, DX12 and DX11 devices or use defaults and call surface->Convert(AMF_MEMORY_DX11) before submitting to the encoder.
For DX12 synchronization: I've pushed D3D12AMF.h with information about fences attached or expected to be attached to resources.

MikhailAMD avatar Feb 16 '21 15:02 MikhailAMD

Hi. Thanks for the quick reply!

I think you're referring to the changes in c7f58ee7853a37. Until there is more documentation or example code, could you confirm questions the below?

  1. Does the following code look correct for initialising an externally allocated DX12 render target for consumption by a DX11 encoder?
amf::AMFSurfacePtr makeCompatibleSurface(amf::AMFContext2* context, ID3D12Resource* resource, ID3D12Fence* resourceFence, uint64_t resourceFenceValue)
{
    amf::AMFSurfacePtr surface;
    D3D12_RESOURCE_STATES currentState = D3D12_RESOURCE_STATE_RENDER_TARGET;
    resource->SetPrivateData(AMFResourceStateGUID, sizeof(D3D12_RESOURCE_STATES), currentState);
    resource->SetPrivateDataInterface(AMFFenceGUID, resourceFence);
    resource->SetPrivateData(AMFFenceValueGUID, sizeof(uint64_t), &resourceFenceValue);
    context->CreateSurfaceFromDX12Native(textureResource, &surface, &observer);

    surface->Convert(amf::AMF_MEMORY_DX11);
    return surface;
}

  1. Is the observer intended to be used to signal fences to inform the rest of the application the DX12 resource is available again? If not, when should that be done?
  2. Is it true that users must create an AMFContext which is possible to convert to AMFContext2, but initialise it with InitDX11?
  3. Are there specific heap flags for allocating DX12 resources? I'm expecting they must be allocated shared? How about the fence?
  4. For the decode case, are users expected to use the same private data GUIDs to access an AMF-allocated fence on DX12 surfaces? Or would you recommend a DX11 based decode context in native DX12 apps, too?

twhittock avatar Feb 16 '21 22:02 twhittock

  1. The code looks OK to me but DX12 texture has to to be allocated in the heap with D3D12_HEAP_FLAG_SHARED flag.
  2. Observer callback informs listener when AMFSurface object is destroyed (reference count becomes zero) and wrapped texture could be reused. In case of encoder this can be used for synchronization as it happens when encoder job is complete and compressed frame is retrieved. Note, that the observer callback will be called from the thread which polls the encoder output (inside QueryOutput() call).
  3. Yes. best practice is that application creates all DX devices and ensures that they run on the same GPU adapter or interop will not work.
  4. Yes, D3D12_HEAP_FLAG_SHARED for texture. At this point AMF does not use shared fences but it is good idea to create them as shared (D3D12_FENCE_FLAG_SHARED) so some optimizations could be possible.
  5. DX12 decoder is fully supported so interop to DX11 is not needed. Yes, you should expect properties with the same GUIDs set by AMF decoder on output textures for synchronization at application level.

MikhailAMD avatar Feb 16 '21 22:02 MikhailAMD

Thank you very much for the help. I won't close this in case you want the issue to remain open until documentation is available. Feel free to close if desired.

Thanks again!

twhittock avatar Feb 16 '21 22:02 twhittock

AMF native DX12 encoding support has been added in AMF v1.4.33

rhutsAMD avatar Apr 12 '24 14:04 rhutsAMD