Hi,
I’m trying to implement a game with multiple windows and created A graphics device and a spritebatch for each window. So far everything works smoothly, The thing is that i need to create a Texture for each device for this to work.
is there a way to share the textures between the GraphicsDevice instances?
I’ve manged to solve this problem:
First i did some reading here: 1 and discovered that shared resource must be a RenderTarget and cannot be mipmapped.
then i added a protected Texture2D constructor that has only GraphicsDevice as an argument:
protected Texture2D(GraphicsDevice graphicsDevice)
{
if (graphicsDevice == null)
{
throw new ArgumentNullException("graphicsDevice",FrameworkResources.ResourceCreationWhenDeviceIsNull);
}
this.GraphicsDevice = graphicsDevice;
this._levelCount = 1;
this.ArraySize = 1;
}
Then added a new RenderTarget2D constructor (in RenderTarget2D.Directx.cs):
public RenderTarget2D(GraphicsDevice graphicsDevice, IntPtr sharedPtr)
:base(graphicsDevice)
{
SharpDX.Direct3D11.Texture2D dxTexture = GraphicsDevice._d3dDevice.OpenSharedResource<SharpDX.Direct3D11.Texture2D>(sharedPtr);
this.width = dxTexture.Description.Width;
this.height = dxTexture.Description.Height;
this._format = SharpDXHelper.FromFormat(dxTexture.Description.Format);
this._texture = dxTexture;
}
and a new RenderRarget2D constructor (in RenderTarget2D.cs):
public RenderTarget2D(GraphicsDevice graphicsDevice, int width, int height, bool shared)
: base(graphicsDevice, width, height, false, SurfaceFormat.Color , SurfaceType.RenderTarget, shared, 1)
{ }
also added SharpDXHelper.FromFormat(SharpDX.DXGI.Format) Which does the exact opposite of ToFormat.
in my game i created a render target using like this:
texture = new RenderTarget2D(GraphicsDevice, width, height, true);
and added a new instance for each GraphicsDevice as follows:
IntPtr sharedPtr = texture.GetSharedHandle();
RenderTarget2D[] renderTargets = new RenderTarget2D[GraphicsDevices.Count];
for (int i=0; i< GraphicsDevices.Count;i++)
{
renderTargets[i] = new RenderTarget2D(GraphicsDevices[i], sharedPtr);
}
1 Like