rgb32数据资源映射。使用DirectX Memcpy

时间:2019-01-08 03:25:16

标签: ffmpeg mapping directx-11 memcpy texture2d

我一直在使用谷歌搜索解决问题一个月。 但是现在我必须在这里寻求帮助。

我想使用ffmpeg解码帧进行渲染。 并使用帧(将其转换为RGB32格式),我尝试使用DX2D纹理渲染帧。

ZeroMemory(&TextureDesc, sizeof(TextureDesc));

TextureDesc.Height = pFrame->height;
TextureDesc.Width = pFrame->width;
TextureDesc.MipLevels = 1;
TextureDesc.ArraySize = 1;
TextureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;            //size 16
TextureDesc.SampleDesc.Count = 1;
TextureDesc.SampleDesc.Quality = 0;
TextureDesc.Usage = D3D11_USAGE_DYNAMIC;
TextureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
TextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
TextureDesc.MiscFlags = 0;

result = m_device->CreateTexture2D(&TextureDesc, NULL, &m_2DTex);
if (FAILED(result))     return false;

ShaderResourceViewDesc.Format = TextureDesc.Format;
ShaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
ShaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
ShaderResourceViewDesc.Texture2D.MipLevels = 1;

    D3D11_MAPPED_SUBRESOURCE S_mappedResource_tt = { 0, };

ZeroMemory(&S_mappedResource_tt, sizeof(D3D11_MAPPED_SUBRESOURCE));

result = m_deviceContext->Map(m_2DTex, 0, D3D11_MAP_WRITE_DISCARD, 0, &S_mappedResource_tt);
if (FAILED(result)) return false;
BYTE* mappedData = reinterpret_cast<BYTE *>(S_mappedResource_tt.pData);
for (auto i = 0; i < pFrame->height; ++i) {
    memcpy(mappedData, pFrame->data, pFrame->linesize[0]);
    mappedData += S_mappedResource_tt.RowPitch;
    pFrame->data[0] += pFrame->linesize[0];
}

m_deviceContext->Unmap(m_2DTex, 0);

result = m_device->CreateShaderResourceView(m_2DTex, &ShaderResourceViewDesc, &m_ShaderResourceView);
if (FAILED(result))     return false;

    m_deviceContext->PSSetShaderResources(0, 1, &m_ShaderResourceView);

但是它显示的只是黑屏(什么都没有渲染)。 我猜这是错误的memcpy大小。 最大的问题是我不知道是什么问题。

问题1: 创建2D纹理进行贴图有任何问题吗?

问题2: 我应该输入多少大小的memcpy参数(与格式相关)?

我基于下面的链接。

[1] https://www.gamedev.net/forums/topic/667097-copy-2d-array-into-texture2d/ [2] https://www.gamedev.net/forums/topic/645514-directx-11-maping-id3d11texture2d/ [3] https://www.gamedev.net/forums/topic/606100-solved-dx11-updating-texture-data/

感谢您的收看,请回复。

1 个答案:

答案 0 :(得分:0)

没有人回复。我解决了我的问题。 我已经修改了一些代码,但不确定是否可以解决问题。黑屏的原因是我的矩阵。

D3D11_TEXTURE2D_DESC TextureDesc;
D3D11_RENDER_TARGET_VIEW_DESC RenderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC ShaderResourceViewDesc;

ZeroMemory(&TextureDesc, sizeof(TextureDesc));

TextureDesc.Height = pFrame->height;
TextureDesc.Width = pFrame->width;
TextureDesc.MipLevels = 1;
TextureDesc.ArraySize = 1;
TextureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;/*DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;*/            //size 32bit
TextureDesc.SampleDesc.Count = 1;
TextureDesc.SampleDesc.Quality = 0;
TextureDesc.Usage = D3D11_USAGE_DYNAMIC;
TextureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
TextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
TextureDesc.MiscFlags = 0;

DWORD*  pInitImage = new DWORD[pFrame->width*pFrame->height];
memset(pInitImage, 0, sizeof(DWORD)*pFrame->width*pFrame->height);
D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = pInitImage;
InitData.SysMemPitch = pFrame->width*sizeof(DWORD);
InitData.SysMemSlicePitch = 0;
result = m_device->CreateTexture2D(&TextureDesc, &InitData, &m_2DTex);
if (FAILED(result))     return false;

ShaderResourceViewDesc.Format = TextureDesc.Format;
ShaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
ShaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
ShaderResourceViewDesc.Texture2D.MipLevels = 1;

result = m_device->CreateShaderResourceView(m_2DTex, &ShaderResourceViewDesc, &m_ShaderResourceView);
if (FAILED(result))     return false;

D3D11_MAPPED_SUBRESOURCE S_mappedResource_tt;
ZeroMemory(&S_mappedResource_tt, sizeof(S_mappedResource_tt));
DWORD   Stride = pFrame->linesize[0];

result = m_deviceContext->Map(m_2DTex, 0, D3D11_MAP_WRITE_DISCARD, 0, &S_mappedResource_tt);
if (FAILED(result)) return false;

BYTE * pFrameData = pFrame->data[0];   // now we have a pointer that points to begin of the destination buffer
BYTE* mappedData = (BYTE *)S_mappedResource_tt.pData;// +S_mappedResource_tt.RowPitch;
for (auto i = 0; i < pFrame->height; i++) {     
    memcpy(mappedData, pFrameData, Stride);
    mappedData += S_mappedResource_tt.RowPitch;
    pFrameData += Stride;
}

m_deviceContext->Unmap(m_2DTex, 0);

效果很好。我希望这对和我一起做同样事情的人有所帮助。