76 WaitForSingleObjectEx(
ctx, INFINITE, FALSE);
86 uint64_t completion = ID3D12Fence_GetCompletedValue(psync_ctx->
fence);
87 if (completion < psync_ctx->fence_value) {
88 if (FAILED(ID3D12Fence_SetEventOnCompletion(psync_ctx->
fence, psync_ctx->
fence_value, psync_ctx->
event)))
91 WaitForSingleObjectEx(psync_ctx->
event, INFINITE, FALSE);
107 ID3D12Resource **ppResource,
int download)
111 D3D12_HEAP_PROPERTIES props = { .Type = download ? D3D12_HEAP_TYPE_READBACK : D3D12_HEAP_TYPE_UPLOAD };
112 D3D12_RESOURCE_DESC
desc = {
113 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
115 .Width =
s->luma_component_size + (
s->luma_component_size >> 1),
117 .DepthOrArraySize = 1,
119 .Format = DXGI_FORMAT_UNKNOWN,
120 .SampleDesc = { .Count = 1, .Quality = 0 },
121 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
122 .Flags = D3D12_RESOURCE_FLAG_NONE,
125 if (FAILED(ID3D12Device_CreateCommittedResource(device_hwctx->
device, &props, D3D12_HEAP_FLAG_NONE, &
desc,
126 states,
NULL, &IID_ID3D12Resource, (
void **)ppResource))) {
140 D3D12_COMMAND_QUEUE_DESC queue_desc = {
141 .Type = D3D12_COMMAND_LIST_TYPE_COPY,
146 s->luma_component_size =
FFALIGN(
ctx->width * (frames_hwctx->
format == DXGI_FORMAT_P010 ? 2 : 1),
147 D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) *
ctx->height;
149 DX_CHECK(ID3D12Device_CreateFence(device_hwctx->
device, 0, D3D12_FENCE_FLAG_NONE,
150 &IID_ID3D12Fence, (
void **)&
s->sync_ctx.fence));
152 s->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
153 if (!
s->sync_ctx.event)
156 DX_CHECK(ID3D12Device_CreateCommandQueue(device_hwctx->
device, &queue_desc,
157 &IID_ID3D12CommandQueue, (
void **)&
s->command_queue));
159 DX_CHECK(ID3D12Device_CreateCommandAllocator(device_hwctx->
device, queue_desc.Type,
160 &IID_ID3D12CommandAllocator, (
void **)&
s->command_allocator));
162 DX_CHECK(ID3D12Device_CreateCommandList(device_hwctx->
device, 0, queue_desc.Type,
163 s->command_allocator,
NULL, &IID_ID3D12GraphicsCommandList, (
void **)&
s->command_list));
165 DX_CHECK(ID3D12GraphicsCommandList_Close(
s->command_list));
167 ID3D12CommandQueue_ExecuteCommandLists(
s->command_queue, 1, (ID3D12CommandList **)&
s->command_list);
180 if (
s->sync_ctx.event)
181 CloseHandle(
s->sync_ctx.event);
193 int nb_sw_formats = 0;
202 D3D12_FEATURE_DATA_FORMAT_SUPPORT format_support = {
supported_formats[
i].d3d_format };
203 hr = ID3D12Device_CheckFeatureSupport(device_hwctx->
device, D3D12_FEATURE_FORMAT_SUPPORT, &format_support,
sizeof(format_support));
204 if (SUCCEEDED(hr) && (format_support.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D))
225 if (
frame->sync_ctx.event)
226 CloseHandle(
frame->sync_ctx.event);
239 D3D12_HEAP_PROPERTIES props = { .Type = D3D12_HEAP_TYPE_DEFAULT };
240 D3D12_RESOURCE_DESC
desc = {
241 .Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D,
244 .Height =
ctx->height,
245 .DepthOrArraySize = 1,
248 .SampleDesc = {.Count = 1, .Quality = 0 },
249 .Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN,
250 .Flags = hwctx->
flags,
257 if (FAILED(ID3D12Device_CreateCommittedResource(device_hwctx->
device, &props, D3D12_HEAP_FLAG_NONE, &
desc,
258 D3D12_RESOURCE_STATE_COMMON,
NULL, &IID_ID3D12Resource, (
void **)&
frame->texture))) {
263 DX_CHECK(ID3D12Device_CreateFence(device_hwctx->
device, 0, D3D12_FENCE_FLAG_NONE,
264 &IID_ID3D12Fence, (
void **)&
frame->sync_ctx.fence));
266 frame->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
267 if (!
frame->sync_ctx.event)
288 if (hwctx->
format != DXGI_FORMAT_UNKNOWN &&
320 D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
342 fmts[0] =
ctx->sw_format;
363 ID3D12Resource *texture = (ID3D12Resource *)
f->texture;
365 uint8_t *mapped_data;
369 D3D12_TEXTURE_COPY_LOCATION staging_y_location = { 0 };
370 D3D12_TEXTURE_COPY_LOCATION staging_uv_location = { 0 };
372 D3D12_TEXTURE_COPY_LOCATION texture_y_location = {
373 .pResource = texture,
374 .Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
375 .SubresourceIndex = 0,
378 D3D12_TEXTURE_COPY_LOCATION texture_uv_location = {
379 .pResource = texture,
380 .Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
381 .SubresourceIndex = 1,
384 D3D12_RESOURCE_BARRIER barrier = {
385 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
386 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
388 .pResource = texture,
389 .StateBefore = D3D12_RESOURCE_STATE_COMMON,
390 .StateAfter = download ? D3D12_RESOURCE_STATE_COPY_SOURCE : D3D12_RESOURCE_STATE_COPY_DEST,
391 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
395 if (
frame->hw_frames_ctx->data != (uint8_t *)
ctx || other->
format !=
ctx->sw_format)
400 if (!
s->command_queue) {
406 for (
int i = 0;
i < 4;
i++)
408 D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
410 staging_y_location = (D3D12_TEXTURE_COPY_LOCATION) {
411 .Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
415 .Format = frames_hwctx->
format == DXGI_FORMAT_P010 ?
416 DXGI_FORMAT_R16_UNORM : DXGI_FORMAT_R8_UNORM,
418 .Height =
ctx->height,
420 .RowPitch = linesizes[0],
425 staging_uv_location = (D3D12_TEXTURE_COPY_LOCATION) {
426 .Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
428 .Offset =
s->luma_component_size,
430 .Format = frames_hwctx->
format == DXGI_FORMAT_P010 ?
431 DXGI_FORMAT_R16G16_UNORM : DXGI_FORMAT_R8G8_UNORM,
432 .Width =
ctx->width >> 1,
433 .Height =
ctx->height >> 1,
435 .RowPitch = linesizes[0],
440 DX_CHECK(ID3D12CommandAllocator_Reset(
s->command_allocator));
442 DX_CHECK(ID3D12GraphicsCommandList_Reset(
s->command_list,
s->command_allocator,
NULL));
445 if (!
s->staging_download_buffer) {
447 &
s->staging_download_buffer, 1);
453 staging_y_location.pResource = staging_uv_location.pResource =
s->staging_download_buffer;
455 ID3D12GraphicsCommandList_ResourceBarrier(
s->command_list, 1, &barrier);
457 ID3D12GraphicsCommandList_CopyTextureRegion(
s->command_list,
458 &staging_y_location, 0, 0, 0,
459 &texture_y_location,
NULL);
461 ID3D12GraphicsCommandList_CopyTextureRegion(
s->command_list,
462 &staging_uv_location, 0, 0, 0,
463 &texture_uv_location,
NULL);
465 barrier.Transition.StateBefore = barrier.Transition.StateAfter;
466 barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COMMON;
467 ID3D12GraphicsCommandList_ResourceBarrier(
s->command_list, 1, &barrier);
469 DX_CHECK(ID3D12GraphicsCommandList_Close(
s->command_list));
471 DX_CHECK(ID3D12CommandQueue_Wait(
s->command_queue,
f->sync_ctx.fence,
f->sync_ctx.fence_value));
473 ID3D12CommandQueue_ExecuteCommandLists(
s->command_queue, 1, (ID3D12CommandList **)&
s->command_list);
479 DX_CHECK(ID3D12Resource_Map(
s->staging_download_buffer, 0,
NULL, (
void **)&mapped_data));
483 ctx->sw_format,
ctx->width,
ctx->height);
485 ID3D12Resource_Unmap(
s->staging_download_buffer, 0,
NULL);
487 if (!
s->staging_upload_buffer) {
489 &
s->staging_upload_buffer, 0);
495 staging_y_location.pResource = staging_uv_location.pResource =
s->staging_upload_buffer;
497 DX_CHECK(ID3D12Resource_Map(
s->staging_upload_buffer, 0,
NULL, (
void **)&mapped_data));
501 ctx->sw_format,
ctx->width,
ctx->height);
503 ID3D12Resource_Unmap(
s->staging_upload_buffer, 0,
NULL);
505 ID3D12GraphicsCommandList_ResourceBarrier(
s->command_list, 1, &barrier);
507 ID3D12GraphicsCommandList_CopyTextureRegion(
s->command_list,
508 &texture_y_location, 0, 0, 0,
509 &staging_y_location,
NULL);
511 ID3D12GraphicsCommandList_CopyTextureRegion(
s->command_list,
512 &texture_uv_location, 0, 0, 0,
513 &staging_uv_location,
NULL);
515 barrier.Transition.StateBefore = barrier.Transition.StateAfter;
516 barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COMMON;
517 ID3D12GraphicsCommandList_ResourceBarrier(
s->command_list, 1, &barrier);
519 DX_CHECK(ID3D12GraphicsCommandList_Close(
s->command_list));
521 ID3D12CommandQueue_ExecuteCommandLists(
s->command_queue, 1, (ID3D12CommandList **)&
s->command_list);
542 priv->
d3d12lib = dlopen(
"d3d12.dll", 0);
543 priv->
dxgilib = dlopen(
"dxgi.dll", 0);
558 priv->
create_device = (PFN_D3D12_CREATE_DEVICE) D3D12CreateDevice;
589 if (
ctx->lock_ctx == INVALID_HANDLE_VALUE) {
597 if (!
ctx->video_device)
598 DX_CHECK(ID3D12Device_QueryInterface(
ctx->device, &IID_ID3D12VideoDevice, (
void **)&
ctx->video_device));
613 CloseHandle(device_hwctx->
lock_ctx);
614 device_hwctx->
lock_ctx = INVALID_HANDLE_VALUE;
626 UINT create_flags = 0;
627 IDXGIAdapter *pAdapter =
NULL;
641 create_flags |= DXGI_CREATE_FACTORY_DEBUG;
642 ID3D12Debug_EnableDebugLayer(pDebug);
649 IDXGIFactory2 *pDXGIFactory =
NULL;
653 int adapter = device ? atoi(device) : 0;
654 if (FAILED(IDXGIFactory2_EnumAdapters(pDXGIFactory, adapter, &pAdapter)))
656 IDXGIFactory2_Release(pDXGIFactory);
660 DXGI_ADAPTER_DESC
desc;
661 hr = IDXGIAdapter2_GetDesc(pAdapter, &
desc);
668 hr = priv->
create_device((IUnknown *)pAdapter, D3D_FEATURE_LEVEL_12_0, &IID_ID3D12Device, (
void **)&
ctx->device);