经过将近两周的搜索和尝试,我终于找到了解决我的问题的方法,如下所示。
然而,这确实显示了图像,但不是我预期的那样,但我相信这对我来说是一个开始,因为下面的代码回答了我最初提出的问题。
Device.ImmediateContext.ClearRenderTargetView(this.m_RenderTargetView, Color4.Black);
Texture2DDescription colordesc = new Texture2DDescription
{
BindFlags = BindFlags.ShaderResource,
Format = m_PixelFormat,
Width = iWidth,
Height = iHeight,
MipLevels = 1,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Dynamic,
OptionFlags = ResourceOptionFlags.None,
CpuAccessFlags = CpuAccessFlags.Write,
ArraySize = 1
};
Texture2D newFrameTexture = new Texture2D(this.Device, colordesc);
DataStream dtStream = null;
DataBox dBox = Device.ImmediateContext.MapSubresource(newFrameTexture, 0, MapMode.WriteDiscard, 0, out dtStream);
if (dtStream != null)
{
int iRowPitch = dBox.RowPitch;
for (int iHeightIndex = 0; iHeightIndex < iHeight; iHeightIndex++)
{
dtStream.Position = iHeightIndex * iRowPitch;
Marshal.Copy(decodedData, iHeightIndex * iWidth * 4, new IntPtr(dtStream.DataPointer.ToInt64() + iHeightIndex * iRowPitch), iWidth * 4);
}
}
Device.ImmediateContext.UnmapSubresource(newFrameTexture, 0);
Device.ImmediateContext.CopySubresourceRegion(newFrameTexture, 0, null, this.RenderTarget, 0);
var shaderRescVw = new ShaderResourceView(this.Device, this.RenderTarget);
Device.ImmediateContext.PixelShader.SetShaderResource(0, shaderRescVw);
Device.ImmediateContext.Draw(6, 0);
Device.ImmediateContext.Flush();
this.D3DSurface.InvalidateD3DImage();
Disposer.SafeDispose(ref newFrameTexture);
使用上述代码,我现在能够使用接收到的新图像数据填充纹理,但是如下图中红框所示,图像的颜色/像素没有正确呈现。
渲染图像的截图:
![enter image description here](https://istack.dev59.com/vlwmO.webp)
图像字节以BGRA32像素格式通过解码器接收。
如何解决这个问题,任何建议都将非常有帮助。