fixed issue #524 ("Sprites are disappearing after some frames")

This commit is contained in:
Glatzemann 2011-12-12 08:50:49 +00:00
parent 7b9a23b1b6
commit 5eaba32696
7 changed files with 66 additions and 54 deletions

View File

@ -445,7 +445,7 @@
<Compile Include="NonXNA\RenderSystem\INativeGraphicsDevice.cs" /> <Compile Include="NonXNA\RenderSystem\INativeGraphicsDevice.cs" />
<Compile Include="NonXNA\RenderSystem\IRenderSystemCreator.cs" /> <Compile Include="NonXNA\RenderSystem\IRenderSystemCreator.cs" />
<Compile Include="NonXNA\ResourceTracker\GraphicsResourceTracker.cs" /> <Compile Include="NonXNA\ResourceTracker\GraphicsResourceTracker.cs" />
<Compile Include="NonXNA\ResourceTracker\WeakReference.cs" /> <None Include="NonXNA\ResourceTracker\WeakReference.cs" />
<Compile Include="NonXNA\SoundSystem\ISoundSystemCreator.cs" /> <Compile Include="NonXNA\SoundSystem\ISoundSystemCreator.cs" />
<Compile Include="Plane.cs" /> <Compile Include="Plane.cs" />
<Compile Include="PlaneIntersectionType.cs" /> <Compile Include="PlaneIntersectionType.cs" />

View File

@ -60,7 +60,7 @@ namespace ANX.Framework.Graphics
public class Effect : GraphicsResource, IGraphicsResource public class Effect : GraphicsResource, IGraphicsResource
{ {
#region Private Members #region Private Members
private WeakReference<INativeEffect> nativeEffect; private INativeEffect nativeEffect;
private EffectTechniqueCollection techniqueCollection; private EffectTechniqueCollection techniqueCollection;
private EffectTechnique currentTechnique; private EffectTechnique currentTechnique;
private EffectParameterCollection parameterCollection; private EffectParameterCollection parameterCollection;
@ -96,9 +96,10 @@ namespace ANX.Framework.Graphics
private void GraphicsDevice_ResourceCreated(object sender, ResourceCreatedEventArgs e) private void GraphicsDevice_ResourceCreated(object sender, ResourceCreatedEventArgs e)
{ {
if (nativeEffect.IsAlive) if (nativeEffect != null)
{ {
nativeEffect.Target.Dispose(); nativeEffect.Dispose();
nativeEffect = null;
} }
CreateNativeEffect(); CreateNativeEffect();
@ -106,9 +107,10 @@ namespace ANX.Framework.Graphics
private void GraphicsDevice_ResourceDestroyed(object sender, ResourceDestroyedEventArgs e) private void GraphicsDevice_ResourceDestroyed(object sender, ResourceDestroyedEventArgs e)
{ {
if (nativeEffect.IsAlive) if (nativeEffect != null)
{ {
nativeEffect.Target.Dispose(); nativeEffect.Dispose();
nativeEffect = null;
} }
} }
@ -121,12 +123,12 @@ namespace ANX.Framework.Graphics
{ {
get get
{ {
if (!nativeEffect.IsAlive) if (nativeEffect == null)
{ {
CreateNativeEffect(); CreateNativeEffect();
} }
return this.nativeEffect.Target; return this.nativeEffect;
} }
} }
@ -160,9 +162,10 @@ namespace ANX.Framework.Graphics
public override void Dispose() public override void Dispose()
{ {
if (nativeEffect.IsAlive) if (nativeEffect != null)
{ {
nativeEffect.Target.Dispose(); nativeEffect.Dispose();
nativeEffect = null;
} }
} }
@ -173,10 +176,10 @@ namespace ANX.Framework.Graphics
private void CreateNativeEffect() private void CreateNativeEffect()
{ {
this.nativeEffect = new WeakReference<INativeEffect>(AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateEffect(GraphicsDevice, this, new MemoryStream(this.byteCode, false))); this.nativeEffect = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateEffect(GraphicsDevice, this, new MemoryStream(this.byteCode, false));
this.techniqueCollection = new EffectTechniqueCollection(this, this.nativeEffect.Target); this.techniqueCollection = new EffectTechniqueCollection(this, this.nativeEffect);
this.parameterCollection = new EffectParameterCollection(this, this.nativeEffect.Target); this.parameterCollection = new EffectParameterCollection(this, this.nativeEffect);
} }
} }
} }

View File

@ -61,7 +61,7 @@ namespace ANX.Framework.Graphics
private int indexCount; private int indexCount;
private BufferUsage bufferUsage; private BufferUsage bufferUsage;
private IndexElementSize indexElementSize; private IndexElementSize indexElementSize;
private WeakReference<INativeBuffer> nativeIndexBuffer; private INativeBuffer nativeIndexBuffer;
public IndexBuffer(GraphicsDevice graphicsDevice, IndexElementSize indexElementSize, int indexCount, BufferUsage usage) public IndexBuffer(GraphicsDevice graphicsDevice, IndexElementSize indexElementSize, int indexCount, BufferUsage usage)
: base(graphicsDevice) : base(graphicsDevice)
@ -107,17 +107,19 @@ namespace ANX.Framework.Graphics
private void GraphicsDevice_ResourceDestroyed(object sender, ResourceDestroyedEventArgs e) private void GraphicsDevice_ResourceDestroyed(object sender, ResourceDestroyedEventArgs e)
{ {
if (this.nativeIndexBuffer.IsAlive) if (this.nativeIndexBuffer != null)
{ {
this.nativeIndexBuffer.Target.Dispose(); this.nativeIndexBuffer.Dispose();
this.nativeIndexBuffer = null;
} }
} }
private void GraphicsDevice_ResourceCreated(object sender, ResourceCreatedEventArgs e) private void GraphicsDevice_ResourceCreated(object sender, ResourceCreatedEventArgs e)
{ {
if (nativeIndexBuffer.IsAlive) if (nativeIndexBuffer != null)
{ {
nativeIndexBuffer.Target.Dispose(); nativeIndexBuffer.Dispose();
nativeIndexBuffer = null;
} }
CreateNativeBuffer(); CreateNativeBuffer();
@ -125,7 +127,7 @@ namespace ANX.Framework.Graphics
private void CreateNativeBuffer() private void CreateNativeBuffer()
{ {
this.nativeIndexBuffer = new WeakReference<INativeBuffer>(AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateIndexBuffer(GraphicsDevice, indexElementSize, indexCount, bufferUsage)); this.nativeIndexBuffer = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateIndexBuffer(GraphicsDevice, indexElementSize, indexCount, bufferUsage);
} }
public void GetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount) where T : struct public void GetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount) where T : struct
@ -145,22 +147,22 @@ namespace ANX.Framework.Graphics
public void SetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount) where T : struct public void SetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount) where T : struct
{ {
if (!nativeIndexBuffer.IsAlive) if (nativeIndexBuffer == null)
{ {
CreateNativeBuffer(); CreateNativeBuffer();
} }
this.nativeIndexBuffer.Target.SetData<T>(GraphicsDevice, offsetInBytes, data, startIndex, elementCount); this.nativeIndexBuffer.SetData<T>(GraphicsDevice, offsetInBytes, data, startIndex, elementCount);
} }
public void SetData<T>(T[] data) where T : struct public void SetData<T>(T[] data) where T : struct
{ {
if (!nativeIndexBuffer.IsAlive) if (nativeIndexBuffer == null)
{ {
CreateNativeBuffer(); CreateNativeBuffer();
} }
this.nativeIndexBuffer.Target.SetData<T>(GraphicsDevice, data); this.nativeIndexBuffer.SetData<T>(GraphicsDevice, data);
} }
public void SetData<T>(T[] data, int startIndex, int elementCount) where T : struct public void SetData<T>(T[] data, int startIndex, int elementCount) where T : struct
@ -170,9 +172,10 @@ namespace ANX.Framework.Graphics
public override void Dispose() public override void Dispose()
{ {
if (this.nativeIndexBuffer.IsAlive) if (this.nativeIndexBuffer != null)
{ {
this.nativeIndexBuffer.Target.Dispose(); this.nativeIndexBuffer.Dispose();
this.nativeIndexBuffer = null;
} }
} }
@ -182,12 +185,12 @@ namespace ANX.Framework.Graphics
{ {
get get
{ {
if (!nativeIndexBuffer.IsAlive) if (nativeIndexBuffer == null)
{ {
CreateNativeBuffer(); CreateNativeBuffer();
} }
return this.nativeIndexBuffer.Target; return this.nativeIndexBuffer;
} }
} }

View File

@ -86,7 +86,7 @@ namespace ANX.Framework.Graphics
this.usage = RenderTargetUsage.DiscardContents; this.usage = RenderTargetUsage.DiscardContents;
this.nativeRenderTarget = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateRenderTarget(graphicsDevice, width, height, false, SurfaceFormat.Color, this.depthStencilFormat, this.multiSampleCount, this.usage); this.nativeRenderTarget = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateRenderTarget(graphicsDevice, width, height, false, SurfaceFormat.Color, this.depthStencilFormat, this.multiSampleCount, this.usage);
base.nativeTexture = new WeakReference<INativeTexture>(this.nativeRenderTarget as INativeTexture2D); base.nativeTexture = this.nativeRenderTarget as INativeTexture2D;
} }
public RenderTarget2D(GraphicsDevice graphicsDevice, int width, int height, bool mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat) public RenderTarget2D(GraphicsDevice graphicsDevice, int width, int height, bool mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat)
@ -97,7 +97,7 @@ namespace ANX.Framework.Graphics
this.usage = RenderTargetUsage.DiscardContents; this.usage = RenderTargetUsage.DiscardContents;
this.nativeRenderTarget = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateRenderTarget(graphicsDevice, width, height, false, SurfaceFormat.Color, this.depthStencilFormat, this.multiSampleCount, this.usage); this.nativeRenderTarget = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateRenderTarget(graphicsDevice, width, height, false, SurfaceFormat.Color, this.depthStencilFormat, this.multiSampleCount, this.usage);
base.nativeTexture = new WeakReference<INativeTexture>(this.nativeRenderTarget as INativeTexture2D); base.nativeTexture = this.nativeRenderTarget as INativeTexture2D;
} }
public RenderTarget2D(GraphicsDevice graphicsDevice, int width, int height, bool mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat, int preferredMultiSampleCount, RenderTargetUsage usage) public RenderTarget2D(GraphicsDevice graphicsDevice, int width, int height, bool mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat, int preferredMultiSampleCount, RenderTargetUsage usage)
@ -108,7 +108,7 @@ namespace ANX.Framework.Graphics
this.usage = usage; this.usage = usage;
this.nativeRenderTarget = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateRenderTarget(graphicsDevice, width, height, false, SurfaceFormat.Color, this.depthStencilFormat, this.multiSampleCount, this.usage); this.nativeRenderTarget = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateRenderTarget(graphicsDevice, width, height, false, SurfaceFormat.Color, this.depthStencilFormat, this.multiSampleCount, this.usage);
base.nativeTexture = new WeakReference<INativeTexture>(this.nativeRenderTarget as INativeTexture2D); base.nativeTexture = this.nativeRenderTarget as INativeTexture2D;
} }
#endregion // Constructors #endregion // Constructors

View File

@ -58,7 +58,7 @@ namespace ANX.Framework.Graphics
{ {
protected internal int levelCount; protected internal int levelCount;
protected internal SurfaceFormat format; protected internal SurfaceFormat format;
protected internal WeakReference<INativeTexture> nativeTexture; protected internal INativeTexture nativeTexture;
public Texture(GraphicsDevice graphicsDevice) public Texture(GraphicsDevice graphicsDevice)
: base(graphicsDevice) : base(graphicsDevice)
@ -93,12 +93,12 @@ namespace ANX.Framework.Graphics
{ {
get get
{ {
if (!this.nativeTexture.IsAlive) if (this.nativeTexture == null)
{ {
ReCreateNativeTextureSurface(); ReCreateNativeTextureSurface();
} }
return this.nativeTexture.Target; return this.nativeTexture;
} }
} }
@ -109,9 +109,10 @@ namespace ANX.Framework.Graphics
protected override void Dispose(bool disposeManaged) protected override void Dispose(bool disposeManaged)
{ {
if (disposeManaged && nativeTexture.IsAlive) if (disposeManaged && nativeTexture != null)
{ {
nativeTexture.Target.Dispose(); nativeTexture.Dispose();
nativeTexture = null;
} }
} }
@ -119,17 +120,19 @@ namespace ANX.Framework.Graphics
private void GraphicsDevice_ResourceDestroyed(object sender, ResourceDestroyedEventArgs e) private void GraphicsDevice_ResourceDestroyed(object sender, ResourceDestroyedEventArgs e)
{ {
if (nativeTexture.IsAlive) if (nativeTexture != null)
{ {
nativeTexture.Target.Dispose(); nativeTexture.Dispose();
nativeTexture = null;
} }
} }
private void GraphicsDevice_ResourceCreated(object sender, ResourceCreatedEventArgs e) private void GraphicsDevice_ResourceCreated(object sender, ResourceCreatedEventArgs e)
{ {
if (nativeTexture.IsAlive) if (nativeTexture != null)
{ {
nativeTexture.Target.Dispose(); nativeTexture.Dispose();
nativeTexture = null;
} }
ReCreateNativeTextureSurface(); ReCreateNativeTextureSurface();

View File

@ -184,7 +184,7 @@ namespace ANX.Framework.Graphics
internal void CreateNativeTextureSurface(GraphicsDevice device, SurfaceFormat format, int width, int height, int levelCount) internal void CreateNativeTextureSurface(GraphicsDevice device, SurfaceFormat format, int width, int height, int levelCount)
{ {
base.nativeTexture = new WeakReference<INativeTexture>(AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateTexture(device, format, width, height, levelCount)); base.nativeTexture = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateTexture(device, format, width, height, levelCount);
} }
} }
} }

View File

@ -61,7 +61,7 @@ namespace ANX.Framework.Graphics
private VertexDeclaration vertexDeclaration; private VertexDeclaration vertexDeclaration;
private int vertexCount; private int vertexCount;
private BufferUsage bufferUsage; private BufferUsage bufferUsage;
private WeakReference<INativeBuffer> nativeVertexBuffer; private INativeBuffer nativeVertexBuffer;
public VertexBuffer(GraphicsDevice graphicsDevice, Type vertexType, int vertexCount, BufferUsage usage) public VertexBuffer(GraphicsDevice graphicsDevice, Type vertexType, int vertexCount, BufferUsage usage)
: this(graphicsDevice, VertexBuffer.TypeToVertexDeclaration(vertexType), vertexCount, usage) : this(graphicsDevice, VertexBuffer.TypeToVertexDeclaration(vertexType), vertexCount, usage)
@ -90,17 +90,19 @@ namespace ANX.Framework.Graphics
private void GraphicsDevice_ResourceDestroyed(object sender, ResourceDestroyedEventArgs e) private void GraphicsDevice_ResourceDestroyed(object sender, ResourceDestroyedEventArgs e)
{ {
if (nativeVertexBuffer.IsAlive) if (nativeVertexBuffer != null)
{ {
nativeVertexBuffer.Target.Dispose(); nativeVertexBuffer.Dispose();
nativeVertexBuffer = null;
} }
} }
private void GraphicsDevice_ResourceCreated(object sender, ResourceCreatedEventArgs e) private void GraphicsDevice_ResourceCreated(object sender, ResourceCreatedEventArgs e)
{ {
if (nativeVertexBuffer.IsAlive) if (nativeVertexBuffer != null)
{ {
nativeVertexBuffer.Target.Dispose(); nativeVertexBuffer.Dispose();
nativeVertexBuffer = null;
} }
CreateNativeBuffer(); CreateNativeBuffer();
@ -108,7 +110,7 @@ namespace ANX.Framework.Graphics
private void CreateNativeBuffer() private void CreateNativeBuffer()
{ {
this.nativeVertexBuffer = new WeakReference<INativeBuffer>(AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateVertexBuffer(GraphicsDevice, vertexDeclaration, vertexCount, bufferUsage)); this.nativeVertexBuffer = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateVertexBuffer(GraphicsDevice, vertexDeclaration, vertexCount, bufferUsage);
} }
public BufferUsage BufferUsage public BufferUsage BufferUsage
@ -158,22 +160,22 @@ namespace ANX.Framework.Graphics
public void SetData<T>(T[] data) where T : struct public void SetData<T>(T[] data) where T : struct
{ {
if (!this.nativeVertexBuffer.IsAlive) if (this.nativeVertexBuffer == null)
{ {
CreateNativeBuffer(); CreateNativeBuffer();
} }
this.nativeVertexBuffer.Target.SetData<T>(GraphicsDevice, data); this.nativeVertexBuffer.SetData<T>(GraphicsDevice, data);
} }
public void SetData<T>(T[] data, int startIndex, int elementCount) where T : struct public void SetData<T>(T[] data, int startIndex, int elementCount) where T : struct
{ {
if (!this.nativeVertexBuffer.IsAlive) if (this.nativeVertexBuffer == null)
{ {
CreateNativeBuffer(); CreateNativeBuffer();
} }
this.nativeVertexBuffer.Target.SetData<T>(GraphicsDevice, data, startIndex, elementCount); this.nativeVertexBuffer.SetData<T>(GraphicsDevice, data, startIndex, elementCount);
} }
private static VertexDeclaration TypeToVertexDeclaration(Type t) private static VertexDeclaration TypeToVertexDeclaration(Type t)
@ -189,9 +191,10 @@ namespace ANX.Framework.Graphics
public override void Dispose() public override void Dispose()
{ {
if (nativeVertexBuffer.IsAlive) if (nativeVertexBuffer != null)
{ {
nativeVertexBuffer.Target.Dispose(); nativeVertexBuffer.Dispose();
nativeVertexBuffer = null;
} }
if (vertexDeclaration != null) if (vertexDeclaration != null)
@ -207,12 +210,12 @@ namespace ANX.Framework.Graphics
{ {
get get
{ {
if (!this.nativeVertexBuffer.IsAlive) if (this.nativeVertexBuffer == null)
{ {
CreateNativeBuffer(); CreateNativeBuffer();
} }
return this.nativeVertexBuffer.Target; return this.nativeVertexBuffer;
} }
} }