如何创建一个Texture2D格式为YV12(DXGI_FORMAT_420_OPAQUE)。

huangapple go评论58阅读模式
英文:

How to create a Texture2D format YV12 (DXGI_FORMAT_420_OPAQUE)

问题

我想创建一个Texture2D格式为DXGI_FORMAT_420_OPAQUE的输入视图,用作VideoProcessor的输入,然后调用VideoProcessorBlt将其转换为RGBA格式,最后在窗口中渲染它。如msdn所述,应用程序使用YUY 4:2:0格式必须分别映射亮度(Y)平面和色度(UV)平面。开发人员可以通过两次调用ID3D12Device::CreateShaderResourceView为相同的纹理传递1通道和2通道格式来实现这一点。传递与Y平面兼容的1通道格式仅映射Y平面。传递与UV平面(一起)兼容的2通道格式仅将U和V平面映射为单个资源视图。

但我找不到关于这种方法的示例代码。我尝试了以下方法,但失败了。

D3D11_TEXTURE2D_DESC desc = {0};
desc.Width = xres;
desc.Height = yres;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_420_OPAQUE;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = 0;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;

D3D11_SUBRESOURCE_DATA SubResource2D;
ZeroMemory(&SubResource2D, sizeof(SubResource2D));
SubResource2D.pSysMem = (void*)data;
SubResource2D.SysMemPitch = xres;
HRESULT hr = device->CreateTexture2D(&desc, &SubResource2D, &texture);
if (FAILED(hr))
    return false;

然后我尝试了UpdateSubresource方法,但又失败了。

ctx->UpdateSubresource(texture, 0, nullptr, data, xres, 0);

我真的想知道如何使用CreateShaderResourceView方法创建一个Texture2D,并将YUV420p数据传输到它,求帮忙,非常感谢!

英文:

I want create a Texture2D format DXGI_FORMAT_420_OPAQUE as input_view of VideoProcessor, then call VideoProcessorBlt convert it to RGBA format, render it in window lastly. As said in msdn,

>An app using the YUY 4:2:0 formats must map the luma (Y) plane separately from the chroma (UV) planes. Developers do this by calling ID3D12Device::CreateShaderResourceView twice for the same texture and passing in 1-channel and 2-channel formats. Passing in a 1-channel format compatible with the Y plane maps only the Y plane. Passing in a 2-channel format compatible with the UV planes (together) maps only the U and V planes as a single resource view.

But i can't find example code about this method. I tried the following mehod, it failed.

> D3D11 ERROR: ID3D11Device::CreateTexture2D: Initial data cannot be specified for DXGI_FORMAT_420_OPAQUE resources. [ STATE_CREATION ERROR #83: CREATETEXTURE1D_INVALIDINITIALDATA]

D3D11_TEXTURE2D_DESC desc = {0};
desc.Width = xres;
desc.Height = yres;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_420_OPAQUE;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = 0;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;

D3D11_SUBRESOURCE_DATA SubResource2D;
ZeroMemory(&SubResource2D, sizeof(SubResource2D));
SubResource2D.pSysMem = (void*)data;
SubResource2D.SysMemPitch = xres;
HRESULT hr = device->CreateTexture2D(&desc, &SubResource2D, &texture);
if (FAILED(hr))
    return false;

Then i tried UpdateSubresource method, it failed again.

> D3D11 ERROR: ID3D11DeviceContext::UpdateSubresource: UpdateSubresource cannot be called on DXGI_FORMAT_420_OPAQUE. [ RESOURCE_MANIPULATION ERROR #289: UPDATESUBRESOURCE_INVALIDDESTINATIONSTATE]

ctx->UpdateSubresource(texture, 0, nullptr, data, xres, 0);

I really want to know how to use CreateShaderResourceView method to create a Texture2D and transfer YUV420p data to it, please help me out, really thanks!

答案1

得分: 1

如在Microsoft Docs上讨论的:

该格式与DXGI_FORMAT_NV12不同,因为资源内部的数据布局对应用程序是完全不透明的。应用程序不能使用CPU映射资源然后访问资源内的数据。您不能在此格式上使用着色器。

如果您想要进行操作,必须使用DXGI_FORMAT_NV12

英文:

As discussed on Microsoft Docs:

> This format differs from DXGI_FORMAT_NV12 in that the layout of the data within the resource is completely opaque to applications. Applications cannot use the CPU to map the resource and then access the data within the resource. You cannot use shaders with this format.

If you want to manipulate it, you have to use DXGI_FORMAT_NV12.

huangapple
  • 本文由 发表于 2023年3月9日 16:11:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定