从ID3D11Texture2D获取数据

时间:2014-12-02 22:23:44

标签: windows-runtime c++-cx

我看到了一些类似的帖子,但没有看到NV12格式,所以这是我的代码:

void MyClass::loadPreviewTexture(){

ID3D11Texture2D* textureBuf;
D3D11_TEXTURE2D_DESC textureDesc;
ComPtr<ID3D11Texture2D> tempTexture = this->c_PreviewTexture;
D3D11_TEXTURE2D_DESC tempDesc;

if (tempTexture != nullptr){

    tempTexture->GetDesc(&tempDesc);

    ComPtr<ID3D11Device> device;
    ComPtr<ID3D11DeviceContext> context;

    ZeroMemory(&textureDesc, sizeof(textureDesc));

    textureDesc.Width = tempDesc.Width;
    textureDesc.Height = tempDesc.Height;
    textureDesc.MipLevels = tempDesc.MipLevels;
    textureDesc.ArraySize = tempDesc.ArraySize;
    textureDesc.Format = tempDesc.Format; // NV12
    textureDesc.SampleDesc = tempDesc.SampleDesc;

    textureDesc.Usage = D3D11_USAGE_STAGING;
    textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
    textureDesc.MiscFlags = tempDesc.MiscFlags;

    tempTexture->GetDevice(&device);
    device->GetImmediateContext(&context);
    device->GetImmediateContext(&context);
    device->CreateTexture2D(&textureDesc, NULL, &textureBuf);

    context->CopyResource(textureBuf, tempTexture.Get());

    D3D11_MAPPED_SUBRESOURCE  mapResource;
    HRESULT hr = context->Map(textureBuf, 0, D3D11_MAP_READ, NULL, &mapResource);

    //data = new byte[(mapResource.RowPitch / sizeof(byte)) * textureDesc.Height];
    //this->size = (mapResource.RowPitch / sizeof(byte)) * textureDesc.Height;

    this->size = textureDesc.Width * textureDesc.Height * 3 * sizeof(byte);
    data = new byte[this->size];


    //memcpy(data, mapResource.pData, mapResource.RowPitch* textureDesc.Height);
    memcpy(data, mapResource.pData, this->size);
    printf("");
}

}

c_PreviewTexture是一个ComPtr,包含我需要的数据。  我不确定数据[]的大小和分配。 NV12格式的大小假设是1280 * 720 * 3,分辨率为1280 * 720.我遗漏了一些东西,因为如果我像这样复制它会有效:

memcpy(data, mapResource.pData, mapResource.RowPitch* textureDesc.Height); // 1280*720

0 个答案:

没有答案