c++ - DXGI_FORMAT_YUY2 纹理在 Windows 8.1 和 Windows 10 下返回不同的 RowPitch

标签 c++ windows-8.1 windows-10 directx-11

我的构建环境如下: Windows 8.1、VS2012、使用 Windows 8.0 SDK 和 C++ 构建的桌面应用程序。

当我在 Windows 8.1 上运行我的程序时,RowPitch 打印 2560,但在 Windows 10 下,同一程序打印 5120。

我在这里做错了什么?

这是代码,感谢大家的回复。

#include <d3d11.h>

static bool init_directx11(ID3D11Device **pDevice, ID3D11DeviceContext **pDeviceContext)
{
    D3D_FEATURE_LEVEL featureLevels[] = {D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_1};

    D3D_FEATURE_LEVEL featureLevel;

    UINT createDeviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
    HRESULT hr = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevels, ARRAYSIZE(featureLevels), D3D11_SDK_VERSION, pDevice,
                                   &featureLevel, pDeviceContext);

    return SUCCEEDED(hr);
}

int _tmain(int argc, _TCHAR* argv[])
{
    ID3D11Device *pDevice = nullptr;
    ID3D11DeviceContext *pDeviceContext= nullptr;
    if (!init_directx11(&pDevice, &pDeviceContext))
    {
        return FALSE;
    }

    D3D11_TEXTURE2D_DESC desc;
    ZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC));

    desc.ArraySize = 1;
    desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    desc.Format = DXGI_FORMAT_YUY2;
    desc.MipLevels = 1;
    desc.MiscFlags = 0;
    desc.SampleDesc.Count = 1;
    desc.SampleDesc.Quality = 0;
    desc.Usage = D3D11_USAGE_DYNAMIC;
    desc.Width = 1280;
    desc.Height = 720;

    ID3D11Texture2D* pTexture2D = nullptr;
    HRESULT hr = pDevice->CreateTexture2D(&desc, NULL, &pTexture2D);

    D3D11_MAPPED_SUBRESOURCE mappedResource;
    ZeroMemory(&mappedResource, sizeof(DXGI_MAPPED_RECT));
    hr = pDeviceContext->Map(pTexture2D, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);

    printf("RowPitch = %d\n", mappedResource.RowPitch);

    pDeviceContext->Unmap(pTexture2D, 0);


    pTexture2D->Release();
    pDeviceContext->Release();
    pDevice->Release();

    getchar();
   }

最佳答案

What am I doing wrong here?

这不一定是错的。 RowPitch 取决于为纹理分配的硬件和驱动程序布局。音高可能会有所不同。一旦资源被映射,你应该读回 pitch,并分别使用它来读取或写入数据。

参见 this thread and message对于推销使用代码片段:

The texture resource will have it's own pitch (the number of bytes in a row), which is probably different than the pitch of your source data. This pitch is given to you as the "RowPitch" member of D3D11_MAPPED_SUBRESOURCE. So typically you do something like this:

BYTE* mappedData = reinterpret_cast<BYTE*>(mappedResource.pData);
for(UINT i = 0; i < height; ++i)
{
  memcpy(mappedData, buffer, rowspan);
  mappedData += mappedResource.RowPitch;
  buffer += rowspan;
}

关于c++ - DXGI_FORMAT_YUY2 纹理在 Windows 8.1 和 Windows 10 下返回不同的 RowPitch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37754135/

相关文章:

c++ - 什么是新的展示位置?

c++ - 什么是非空 STL 删除的安全等价物?

storyboard - 使用 Storyboard 代码在 Windows 通用应用程序中的资源字典中工作

c# - 使用 MobileServiceClient 和 SingleSignOn 身份验证的问题

xaml - 在 App.xaml 上添加基于样式在 App() { InitializeComponent(); }

c++ - 由于 Internet Explorer,VS2017 堆分析不起作用

c++ - 测量重载运算符和成员函数时间的函数

c++ - 帮助插入第一个 BST

powershell - 在 Windows10 上使用 NSIS 启用应用侧载

vb.net - 在 WinRT、Windows8(10) 应用程序中获取 Windows 版本