77 WaitForSingleObjectEx(
ctx, INFINITE, FALSE);
87 uint64_t completion = ID3D12Fence_GetCompletedValue(psync_ctx->
fence);
88 if (completion < psync_ctx->fence_value) {
89 if (FAILED(ID3D12Fence_SetEventOnCompletion(psync_ctx->
fence, psync_ctx->
fence_value, psync_ctx->
event)))
92 WaitForSingleObjectEx(psync_ctx->
event, INFINITE, FALSE);
108 ID3D12Resource **ppResource,
int download)
112 D3D12_HEAP_PROPERTIES props = { .Type = download ? D3D12_HEAP_TYPE_READBACK : D3D12_HEAP_TYPE_UPLOAD };
113 D3D12_RESOURCE_DESC
desc = {
114 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
116 .Width =
s->luma_component_size + (
s->luma_component_size >> 1),
118 .DepthOrArraySize = 1,
120 .Format = DXGI_FORMAT_UNKNOWN,
121 .SampleDesc = { .Count = 1, .Quality = 0 },
122 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
123 .Flags = D3D12_RESOURCE_FLAG_NONE,
126 if (FAILED(ID3D12Device_CreateCommittedResource(device_hwctx->
device, &props, D3D12_HEAP_FLAG_NONE, &
desc,
127 states,
NULL, &IID_ID3D12Resource, (
void **)ppResource))) {
141 D3D12_COMMAND_QUEUE_DESC queue_desc = {
142 .Type = D3D12_COMMAND_LIST_TYPE_COPY,
147 s->luma_component_size =
FFALIGN(
ctx->width * (frames_hwctx->
format == DXGI_FORMAT_P010 ? 2 : 1),
148 D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) *
ctx->height;
150 DX_CHECK(ID3D12Device_CreateFence(device_hwctx->
device, 0, D3D12_FENCE_FLAG_NONE,
151 &IID_ID3D12Fence, (
void **)&
s->sync_ctx.fence));
153 s->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
154 if (!
s->sync_ctx.event)
157 DX_CHECK(ID3D12Device_CreateCommandQueue(device_hwctx->
device, &queue_desc,
158 &IID_ID3D12CommandQueue, (
void **)&
s->command_queue));
160 DX_CHECK(ID3D12Device_CreateCommandAllocator(device_hwctx->
device, queue_desc.Type,
161 &IID_ID3D12CommandAllocator, (
void **)&
s->command_allocator));
163 DX_CHECK(ID3D12Device_CreateCommandList(device_hwctx->
device, 0, queue_desc.Type,
164 s->command_allocator,
NULL, &IID_ID3D12GraphicsCommandList, (
void **)&
s->command_list));
166 DX_CHECK(ID3D12GraphicsCommandList_Close(
s->command_list));
168 ID3D12CommandQueue_ExecuteCommandLists(
s->command_queue, 1, (ID3D12CommandList **)&
s->command_list);
182 if (
s->sync_ctx.event)
183 CloseHandle(
s->sync_ctx.event);
198 int nb_sw_formats = 0;
207 D3D12_FEATURE_DATA_FORMAT_SUPPORT format_support = {
supported_formats[
i].d3d_format };
208 hr = ID3D12Device_CheckFeatureSupport(device_hwctx->
device, D3D12_FEATURE_FORMAT_SUPPORT, &format_support,
sizeof(format_support));
209 if (SUCCEEDED(hr) && (format_support.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D))
232 if (
frame->sync_ctx.event)
233 CloseHandle(
frame->sync_ctx.event);
248 DX_CHECK(ID3D12Device_CreateFence(device_hwctx->
device, 0, D3D12_FENCE_FLAG_NONE,
249 &IID_ID3D12Fence, (
void **)&
frame->sync_ctx.fence));
250 frame->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
251 if (!
frame->sync_ctx.event)
254 if (
s->nb_surfaces_used >=
ctx->initial_pool_size) {
260 frame->subresource_index =
s->nb_surfaces_used;
267 s->nb_surfaces_used++;
286 D3D12_HEAP_PROPERTIES props = { .Type = D3D12_HEAP_TYPE_DEFAULT };
287 D3D12_RESOURCE_DESC
desc = {
288 .Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D,
291 .Height =
ctx->height,
292 .DepthOrArraySize = 1,
295 .SampleDesc = {.Count = 1, .Quality = 0 },
296 .Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN,
304 if (FAILED(ID3D12Device_CreateCommittedResource(device_hwctx->
device, &props, hwctx->
heap_flags, &
desc,
305 D3D12_RESOURCE_STATE_COMMON,
NULL, &IID_ID3D12Resource, (
void **)&
frame->texture))) {
310 DX_CHECK(ID3D12Device_CreateFence(device_hwctx->
device, 0, D3D12_FENCE_FLAG_NONE,
311 &IID_ID3D12Fence, (
void **)&
frame->sync_ctx.fence));
313 frame->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
314 if (!
frame->sync_ctx.event)
333 D3D12_HEAP_PROPERTIES props = { .Type = D3D12_HEAP_TYPE_DEFAULT };
335 D3D12_RESOURCE_DESC
desc = {
336 .Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D,
339 .Height =
ctx->height,
340 .DepthOrArraySize =
ctx->initial_pool_size,
343 .SampleDesc = {.Count = 1, .Quality = 0 },
344 .Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN,
348 if (FAILED(ID3D12Device_CreateCommittedResource(device_hwctx->
device, &props, hwctx->
heap_flags, &
desc,
349 D3D12_RESOURCE_STATE_COMMON,
NULL, &IID_ID3D12Resource, (
void **)&hwctx->
texture_array))) {
364 if (hwctx->
format != DXGI_FORMAT_UNKNOWN &&
406 D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
428 fmts[0] =
ctx->sw_format;
449 ID3D12Resource *texture = (ID3D12Resource *)
f->texture;
451 uint8_t *mapped_data;
455 D3D12_TEXTURE_COPY_LOCATION staging_y_location = { 0 };
456 D3D12_TEXTURE_COPY_LOCATION staging_uv_location = { 0 };
458 D3D12_TEXTURE_COPY_LOCATION texture_y_location = {
459 .pResource = texture,
460 .Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
461 .SubresourceIndex = 0,
464 D3D12_TEXTURE_COPY_LOCATION texture_uv_location = {
465 .pResource = texture,
466 .Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
467 .SubresourceIndex = 1,
470 D3D12_RESOURCE_BARRIER barrier = {
471 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
472 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
474 .pResource = texture,
475 .StateBefore = D3D12_RESOURCE_STATE_COMMON,
476 .StateAfter = download ? D3D12_RESOURCE_STATE_COPY_SOURCE : D3D12_RESOURCE_STATE_COPY_DEST,
477 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
481 if (
frame->hw_frames_ctx->data != (uint8_t *)
ctx || other->
format !=
ctx->sw_format)
486 if (!
s->command_queue) {
492 for (
int i = 0;
i < 4;
i++)
494 D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
496 staging_y_location = (D3D12_TEXTURE_COPY_LOCATION) {
497 .Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
501 .Format = frames_hwctx->
format == DXGI_FORMAT_P010 ?
502 DXGI_FORMAT_R16_UNORM : DXGI_FORMAT_R8_UNORM,
504 .Height =
ctx->height,
506 .RowPitch = linesizes[0],
511 staging_uv_location = (D3D12_TEXTURE_COPY_LOCATION) {
512 .Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
514 .Offset =
s->luma_component_size,
516 .Format = frames_hwctx->
format == DXGI_FORMAT_P010 ?
517 DXGI_FORMAT_R16G16_UNORM : DXGI_FORMAT_R8G8_UNORM,
518 .Width =
ctx->width >> 1,
519 .Height =
ctx->height >> 1,
521 .RowPitch = linesizes[0],
526 DX_CHECK(ID3D12CommandAllocator_Reset(
s->command_allocator));
528 DX_CHECK(ID3D12GraphicsCommandList_Reset(
s->command_list,
s->command_allocator,
NULL));
531 if (!
s->staging_download_buffer) {
533 &
s->staging_download_buffer, 1);
539 staging_y_location.pResource = staging_uv_location.pResource =
s->staging_download_buffer;
541 ID3D12GraphicsCommandList_ResourceBarrier(
s->command_list, 1, &barrier);
543 ID3D12GraphicsCommandList_CopyTextureRegion(
s->command_list,
544 &staging_y_location, 0, 0, 0,
545 &texture_y_location,
NULL);
547 ID3D12GraphicsCommandList_CopyTextureRegion(
s->command_list,
548 &staging_uv_location, 0, 0, 0,
549 &texture_uv_location,
NULL);
551 barrier.Transition.StateBefore = barrier.Transition.StateAfter;
552 barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COMMON;
553 ID3D12GraphicsCommandList_ResourceBarrier(
s->command_list, 1, &barrier);
555 DX_CHECK(ID3D12GraphicsCommandList_Close(
s->command_list));
557 DX_CHECK(ID3D12CommandQueue_Wait(
s->command_queue,
f->sync_ctx.fence,
f->sync_ctx.fence_value));
559 ID3D12CommandQueue_ExecuteCommandLists(
s->command_queue, 1, (ID3D12CommandList **)&
s->command_list);
565 DX_CHECK(ID3D12Resource_Map(
s->staging_download_buffer, 0,
NULL, (
void **)&mapped_data));
569 ctx->sw_format,
ctx->width,
ctx->height);
571 ID3D12Resource_Unmap(
s->staging_download_buffer, 0,
NULL);
573 if (!
s->staging_upload_buffer) {
575 &
s->staging_upload_buffer, 0);
581 staging_y_location.pResource = staging_uv_location.pResource =
s->staging_upload_buffer;
583 DX_CHECK(ID3D12Resource_Map(
s->staging_upload_buffer, 0,
NULL, (
void **)&mapped_data));
587 ctx->sw_format,
ctx->width,
ctx->height);
589 ID3D12Resource_Unmap(
s->staging_upload_buffer, 0,
NULL);
591 ID3D12GraphicsCommandList_ResourceBarrier(
s->command_list, 1, &barrier);
593 ID3D12GraphicsCommandList_CopyTextureRegion(
s->command_list,
594 &texture_y_location, 0, 0, 0,
595 &staging_y_location,
NULL);
597 ID3D12GraphicsCommandList_CopyTextureRegion(
s->command_list,
598 &texture_uv_location, 0, 0, 0,
599 &staging_uv_location,
NULL);
601 barrier.Transition.StateBefore = barrier.Transition.StateAfter;
602 barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COMMON;
603 ID3D12GraphicsCommandList_ResourceBarrier(
s->command_list, 1, &barrier);
605 DX_CHECK(ID3D12GraphicsCommandList_Close(
s->command_list));
607 ID3D12CommandQueue_ExecuteCommandLists(
s->command_queue, 1, (ID3D12CommandList **)&
s->command_list);
628 priv->
d3d12lib = dlopen(
"d3d12.dll", 0);
629 priv->
dxgilib = dlopen(
"dxgi.dll", 0);
644 priv->
create_device = (PFN_D3D12_CREATE_DEVICE) D3D12CreateDevice;
675 if (
ctx->lock_ctx == INVALID_HANDLE_VALUE) {
683 if (!
ctx->video_device)
684 DX_CHECK(ID3D12Device_QueryInterface(
ctx->device, &IID_ID3D12VideoDevice, (
void **)&
ctx->video_device));
699 CloseHandle(device_hwctx->
lock_ctx);
700 device_hwctx->
lock_ctx = INVALID_HANDLE_VALUE;
712 UINT create_flags = 0;
713 IDXGIAdapter *pAdapter =
NULL;
727 create_flags |= DXGI_CREATE_FACTORY_DEBUG;
728 ID3D12Debug_EnableDebugLayer(pDebug);
735 IDXGIFactory2 *pDXGIFactory =
NULL;
739 int adapter = device ? atoi(device) : 0;
740 if (FAILED(IDXGIFactory2_EnumAdapters(pDXGIFactory, adapter, &pAdapter)))
742 IDXGIFactory2_Release(pDXGIFactory);
746 DXGI_ADAPTER_DESC
desc;
747 hr = IDXGIAdapter2_GetDesc(pAdapter, &
desc);
754 hr = priv->
create_device((IUnknown *)pAdapter, D3D_FEATURE_LEVEL_12_0, &IID_ID3D12Device, (
void **)&
ctx->device);
763 ctx->resource_flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
766 ctx->resource_flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
769 ctx->heap_flags |= D3D12_HEAP_FLAG_SHARED;