I am debugging some rendering issues on Windows (DirectX) and am trying to save a rendertarget as a file. But the rendertarget is multisampled, and the call throws an “Invalid arguments” exception. The rendertarget is not set as the current target for draw operations.
HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments]
ved SharpDX.Result.CheckError()
ved SharpDX.Direct3D11.Device.CreateTexture2D(Texture2DDescription& descRef, DataBox[] initialDataRef, Texture2D texture2DOut)
ved SharpDX.Direct3D11.Texture2D..ctor(Device device, Texture2DDescription description)
ved Microsoft.Xna.Framework.Graphics.Texture2D.PlatformGetData[T](Int32 level, Int32 arraySlice, Rectangle rect, T[] data, Int32 startIndex, Int32 elementCount) i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Graphics\Texture2D.DirectX.cs:linje 138
ved Microsoft.Xna.Framework.Graphics.Texture2D.GetData[T](Int32 level, Int32 arraySlice, Nullable`1 rect, T[] data, Int32 startIndex, Int32 elementCount) i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Graphics\Texture2D.cs:linje 206
ved Microsoft.Xna.Framework.Graphics.Texture2D.GetData[T](Int32 level, Nullable`1 rect, T[] data, Int32 startIndex, Int32 elementCount) i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Graphics\Texture2D.cs:linje 221
ved Microsoft.Xna.Framework.Graphics.Texture2D.GetData[T](T[] data) i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Graphics\Texture2D.cs:linje 247
ved MonoGame.Utilities.Png.PngWriter.GetColorData(Texture2D texture2D) i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Utilities\Png\PngWriter.cs:linje 227
ved MonoGame.Utilities.Png.PngWriter.Write(Texture2D texture2D, Stream outputStream) i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Utilities\Png\PngWriter.cs:linje 36
ved Microsoft.Xna.Framework.Graphics.Texture2D.PlatformSaveAsPng(Stream stream, Int32 width, Int32 height) i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Graphics\Texture2D.DirectX.cs:linje 311
ved Microsoft.Xna.Framework.Graphics.Texture2D.SaveAsPng(Stream stream, Int32 width, Int32 height) i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Graphics\Texture2D.cs:linje 294
This is the place in MonoGame that throws the exception:
private void PlatformGetData<T>(int level, int arraySlice, Rectangle rect, T[] data, int startIndex, int elementCount) where T : struct
{
// Create a temp staging resource for copying the data.
//
// TODO: We should probably be pooling these staging resources
// and not creating a new one each time.
//
var min = _format.IsCompressedFormat() ? 4 : 1;
var levelWidth = Math.Max(width >> level, min);
var levelHeight = Math.Max(height >> level, min);
if (_cachedStagingTexture == null)
{
var desc = new Texture2DDescription();
desc.Width = levelWidth;
desc.Height = levelHeight;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = SharpDXHelper.ToFormat(_format);
desc.BindFlags = BindFlags.None;
desc.CpuAccessFlags = CpuAccessFlags.Read;
desc.SampleDescription = CreateSampleDescription();
desc.Usage = ResourceUsage.Staging;
desc.OptionFlags = ResourceOptionFlags.None;
// Save sampling description.
_sampleDescription = desc.SampleDescription;
_cachedStagingTexture = new SharpDX.Direct3D11.Texture2D(GraphicsDevice._d3dDevice, desc); <- line that crashes
}