I have a winforms application which uses xna to act as a 2d editor of sorts. I basically have a custom control which wraps up a newly created GraphicsDevice (using the panels handle and width/height to create the viewport) and draw loop.
Now every time I try to use this control I keep getting an exception saying:
The viewport is invalid. The viewport cannot be larger than or outside the bounds of the current render target.The MinDepth and MaxDepth but be between 0 and 1
I am a bit baffled as I an creating a GraphicsDevice like so:
public class GraphicsDeviceBuilder
{
private Viewport viewport;
private PresentationParameters presentationParameters;
private GraphicsProfile graphicsProfile;
private GraphicsAdapter graphicsAdapter;
private void ResetBuilder()
{
viewport = new Viewport(0, 0, 128, 128);
presentationParameters = new PresentationParameters();
presentationParameters.BackBufferFormat = SurfaceFormat.Color;
presentationParameters.DepthStencilFormat = DepthFormat.Depth24;
presentationParameters.PresentationInterval = PresentInterval.Immediate;
presentationParameters.IsFullScreen = false;
graphicsProfile = GraphicsProfile.Reach;
graphicsAdapter = GraphicsAdapter.DefaultAdapter;
}
public GraphicsDeviceBuilder Create()
{
ResetBuilder();
return this;
}
public GraphicsDeviceBuilder WithViewport(Viewport viewport)
{
this.viewport = viewport;
return this;
}
public GraphicsDeviceBuilder WithPresentationParameters(PresentationParameters presentationParameters)
{
this.presentationParameters = presentationParameters;
return this;
}
public GraphicsDeviceBuilder WithGraphicsProfile(GraphicsProfile graphicsProfile)
{
this.graphicsProfile = graphicsProfile;
return this;
}
public GraphicsDeviceBuilder WithGraphicsAdapter(GraphicsAdapter graphicsAdapter)
{
this.graphicsAdapter = graphicsAdapter;
return this;
}
public GraphicsDevice Build(IntPtr handle)
{
presentationParameters.DeviceWindowHandle = handle;
presentationParameters.BackBufferWidth = viewport.Width;
presentationParameters.BackBufferHeight = viewport.Height;
return new GraphicsDevice(graphicsAdapter, graphicsProfile, presentationParameters)
{
Viewport = viewport
};
}
}
Then in the panel that encapsulates this created device:
private void SetupGraphics(GraphicsDeviceBuilder graphicsDeviceBuilder)
{
var viewport = new Viewport()
{
X = 0,
Y = 0,
Width = Math.Max(Width, 1),
Height = Math.Max(Height, 1),
MinDepth = 0.0f,
MaxDepth = 1.0f
};
GraphicsDevice = graphicsDeviceBuilder.Create().WithViewport(viewport).Build(Handle);
}
I have also got a method which will Reset() the GraphicsDevice on Resizing the form, it also updates the height/width if required, which looks like this:
private void ResetDevice()
{
if (GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.Lost)
{ throw new DeviceLostException("Cannot regain access to video hardware"); }
var safeWidth = Math.Max(Width, 1);
var safeHeight = Math.Max(Height, 1);
var newViewport = new Viewport(0, 0, safeWidth, safeHeight) { MinDepth = 0.0f, MaxDepth = 1.0f };
GraphicsDevice.Viewport = newViewport;
GraphicsDevice.PresentationParameters.BackBufferWidth = newViewport.Width;
GraphicsDevice.PresentationParameters.BackBufferHeight = newViewport.Height;
GraphicsDevice.PresentationParameters.DeviceWindowHandle = Handle;
GraphicsDevice.Reset();
}
The error implies that the viewport's size is invalid (which upon debugging seems false as it matches the size of the panel) and also has an invalid depth, but as you can see it is set to 0 and 1, making it within the range (debugging also proves this to be true).
When running at runtime I do not receive an error like in the designer, but I do just get a red cross up on the panel making me think it is not working there either.