From 845d8ec716492d53dab3345dec16d6016a39d2f0 Mon Sep 17 00:00:00 2001 From: "SND\\AstrorEnales_cp" Date: Thu, 30 Aug 2012 12:05:40 +0000 Subject: [PATCH] - Further implemented the Dispose chain which now prevents the OpenGL gd from leaking (still some work required) - Logging OpenGL version on Device Reset with GL3 RenderSystem --- ANX.Framework/ANX.Framework.csproj | 1 + ANX.Framework/ANX.Framework_Linux.csproj | 1 + ANX.Framework/ANX.Framework_PSVita.csproj | 1 + .../ANX.Framework_WindowsMetro.csproj | 1 + ANX.Framework/Game.cs | 73 +- ANX.Framework/GameHost.cs | 80 +- ANX.Framework/Graphics/GraphicsDevice.cs | 1366 ++++++++--------- ANX.Framework/Graphics/VertexDeclaration.cs | 131 +- ANX.Framework/GraphicsDeviceManager.cs | 19 +- ANX.Framework/NonXNA/AddIn.cs | 8 +- ANX.Framework/NonXNA/Logger.cs | 11 + .../NonXNA/Reflection/AssemblyLoader.cs | 11 +- ANX.Framework/NonXNA/Reflection/TypeHelper.cs | 7 + .../RenderSystem/INativeGraphicsDevice.cs | 50 +- ANX.Framework/NonXNA/ThreadHelper.cs | 17 + .../ANX.InputDevices.OpenTK.csproj | 4 - .../ANX.InputDevices.OpenTK_Linux.csproj | 4 - .../ANX.InputDevices.OpenTK_PSVita.csproj | 4 - ...NX.InputDevices.OpenTK_WindowsMetro.csproj | 4 - .../ANX.Framework.Windows.GL3/Creator.cs | 19 +- .../GraphicsDeviceWindowsGL3.cs | 17 +- .../ANX.SoundSystem.OpenAL.csproj | 4 - .../ANX.SoundSystem.OpenAL_Linux.csproj | 4 - .../ANX.SoundSystem.OpenAL_PSVita.csproj | 4 - ...ANX.SoundSystem.OpenAL_WindowsMetro.csproj | 4 - .../OpenALSoundEffect.cs | 4 +- .../OpenALSoundEffectInstance.cs | 2 +- 27 files changed, 930 insertions(+), 921 deletions(-) create mode 100644 ANX.Framework/NonXNA/ThreadHelper.cs diff --git a/ANX.Framework/ANX.Framework.csproj b/ANX.Framework/ANX.Framework.csproj index db392460..8c78fd0f 100644 --- a/ANX.Framework/ANX.Framework.csproj +++ b/ANX.Framework/ANX.Framework.csproj @@ -442,6 +442,7 @@ + diff --git a/ANX.Framework/ANX.Framework_Linux.csproj b/ANX.Framework/ANX.Framework_Linux.csproj index 8961406a..758baee9 100644 --- a/ANX.Framework/ANX.Framework_Linux.csproj +++ b/ANX.Framework/ANX.Framework_Linux.csproj @@ -442,6 +442,7 @@ + diff --git a/ANX.Framework/ANX.Framework_PSVita.csproj b/ANX.Framework/ANX.Framework_PSVita.csproj index 2d4fb8d6..4072afa4 100644 --- a/ANX.Framework/ANX.Framework_PSVita.csproj +++ b/ANX.Framework/ANX.Framework_PSVita.csproj @@ -444,6 +444,7 @@ + diff --git a/ANX.Framework/ANX.Framework_WindowsMetro.csproj b/ANX.Framework/ANX.Framework_WindowsMetro.csproj index c1d9c1d6..2577c67f 100644 --- a/ANX.Framework/ANX.Framework_WindowsMetro.csproj +++ b/ANX.Framework/ANX.Framework_WindowsMetro.csproj @@ -445,6 +445,7 @@ + diff --git a/ANX.Framework/Game.cs b/ANX.Framework/Game.cs index 713cc4bb..c4e1294a 100644 --- a/ANX.Framework/Game.cs +++ b/ANX.Framework/Game.cs @@ -86,8 +86,8 @@ namespace ANX.Framework //TODO: implement draw- and update-order handling of GameComponents this.components = new GameComponentCollection(); - this.components.ComponentAdded += new EventHandler(components_ComponentAdded); - this.components.ComponentRemoved += new EventHandler(components_ComponentRemoved); + this.components.ComponentAdded += components_ComponentAdded; + this.components.ComponentRemoved += components_ComponentRemoved; this.drawableGameComponents = new List(); Logger.Info("finished initializing new Game class"); @@ -98,31 +98,25 @@ namespace ANX.Framework this.components.ComponentAdded -= components_ComponentAdded; this.components.ComponentRemoved -= components_ComponentRemoved; - //TODO: implement + Dispose(false); } #region CreateGameHost private void CreateGameHost() { Logger.Info("creating GameHost"); - var creator = - AddInSystemFactory.Instance.GetDefaultCreator(); - if (creator != null) - { - host = creator.CreateGameHost(this); + var creator = AddInSystemFactory.Instance.GetDefaultCreator(); + if (creator == null) + Logger.ErrorAndThrow("Could not fetch PlatformSystem creator to create a game host."); - host.Activated += HostActivated; - host.Deactivated += HostDeactivated; - host.Suspend += HostSuspend; - host.Resume += HostResume; - host.Idle += HostIdle; - host.Exiting += HostExiting; - } - else - { - Logger.Error("could not fetch PlatformSystem creator to create a game host..."); - throw new NullReferenceException("could not fetch PlatformSystem creator"); - } + host = creator.CreateGameHost(this); + + host.Activated += HostActivated; + host.Deactivated += HostDeactivated; + host.Suspend += HostSuspend; + host.Resume += HostResume; + host.Idle += HostIdle; + host.Exiting += HostExiting; } #endregion @@ -233,9 +227,7 @@ namespace ANX.Framework public void Tick() { if (this.ShouldExit) - { return; - } //TODO: calculation of times is wrong //TODO: encapsulate timing stuff in GameTimer class @@ -244,10 +236,7 @@ namespace ANX.Framework { while (elapsedUpdate < targetElapsedTime) { - // TODO: search replacement -#if !WINDOWSMETRO - Thread.Sleep(TargetElapsedTime.Milliseconds - elapsedUpdate.Milliseconds); -#endif + ThreadHelper.Sleep(TargetElapsedTime.Milliseconds - elapsedUpdate.Milliseconds); elapsedUpdate = TimeSpan.FromTicks(clock.Timestamp - elapsedUpdate.Ticks); } @@ -281,9 +270,8 @@ namespace ANX.Framework { this.graphicsDeviceManager = this.Services.GetService(typeof(IGraphicsDeviceManager)) as IGraphicsDeviceManager; if (this.graphicsDeviceManager != null) - { this.graphicsDeviceManager.CreateDevice(); - } + this.Initialize(); this.inRun = true; this.BeginRun(); @@ -406,11 +394,7 @@ namespace ANX.Framework { get { - if (this.host != null) - { - return this.host.Window; - } - return null; + return (host != null) ? host.Window : null; } } @@ -482,12 +466,31 @@ namespace ANX.Framework public void Dispose() { - //TODO: dispose everything to dispose :-) + Dispose(true); + GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { - //TODO: dispose everything to dispose :-) + if (disposing) + { + IDisposable disposable; + var array = new IGameComponent[components.Count]; + components.CopyTo(array, 0); + for (int i = 0; i < array.Length; i++) + { + disposable = (IDisposable)array[i]; + if (disposable != null) + disposable.Dispose(); + } + + disposable = (IDisposable)graphicsDeviceManager; + if (disposable != null) + disposable.Dispose(); + + if (Disposed != null) + Disposed(this, EventArgs.Empty); + } } protected virtual void OnActivated(Object sender, EventArgs args) diff --git a/ANX.Framework/GameHost.cs b/ANX.Framework/GameHost.cs index f762e8bc..1ee3adc1 100644 --- a/ANX.Framework/GameHost.cs +++ b/ANX.Framework/GameHost.cs @@ -6,56 +6,48 @@ using System; namespace ANX.Framework { - public abstract class GameHost - { - internal event EventHandler Activated; - internal event EventHandler Deactivated; - internal event EventHandler Exiting; - internal event EventHandler Idle; - internal event EventHandler Resume; - internal event EventHandler Suspend; + public abstract class GameHost + { + internal event EventHandler Activated; + internal event EventHandler Deactivated; + internal event EventHandler Exiting; + internal event EventHandler Idle; + internal event EventHandler Resume; + internal event EventHandler Suspend; - public GameHost(Game game) - { + public abstract GameWindow Window { get; } - } + public GameHost(Game game) + { + } - public abstract void Run(); + public abstract void Run(); + public abstract void Exit(); - public abstract GameWindow Window { get; } + protected void OnActivated() + { + InvokeIfNotNull(this.Activated); + } - public abstract void Exit(); + protected void OnDeactivated() + { + InvokeIfNotNull(this.Deactivated); + } - protected void OnActivated() - { - if (this.Activated != null) - { - this.Activated(this, EventArgs.Empty); - } - } + protected void OnIdle() + { + InvokeIfNotNull(this.Idle); + } - protected void OnDeactivated() - { - if (this.Deactivated != null) - { - this.Deactivated(this, EventArgs.Empty); - } - } + protected void OnExiting() + { + InvokeIfNotNull(this.Exiting); + } - protected void OnIdle() - { - if (this.Idle != null) - { - this.Idle(this, EventArgs.Empty); - } - } - - protected void OnExiting() - { - if (this.Exiting != null) - { - this.Exiting(this, EventArgs.Empty); - } - } - } + private void InvokeIfNotNull(EventHandler eventHandler) + { + if (eventHandler != null) + eventHandler(this, EventArgs.Empty); + } + } } diff --git a/ANX.Framework/Graphics/GraphicsDevice.cs b/ANX.Framework/Graphics/GraphicsDevice.cs index de2aaf8c..476a71ec 100644 --- a/ANX.Framework/Graphics/GraphicsDevice.cs +++ b/ANX.Framework/Graphics/GraphicsDevice.cs @@ -1,13 +1,6 @@ -#region Using Statements using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using ANX.Framework.NonXNA; -using ANX.Framework.Graphics; using System.Runtime.InteropServices; - -#endregion // Using Statements +using ANX.Framework.NonXNA; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -15,715 +8,722 @@ using System.Runtime.InteropServices; namespace ANX.Framework.Graphics { - public class GraphicsDevice : IDisposable - { - #region Private Members - private INativeGraphicsDevice nativeDevice; - private IndexBuffer indexBuffer; - private SamplerStateCollection samplerStateCollection; - private Viewport viewport; - private BlendState blendState; - private RasterizerState rasterizerState; - private DepthStencilState depthStencilState; - private GraphicsAdapter currentAdapter; - private PresentationParameters currentPresentationParameters; - private bool isDisposed; - private GraphicsProfile graphicsProfile; - private VertexBufferBinding[] currentVertexBufferBindings; - private RenderTargetBinding[] currentRenderTargetBindings; - private TextureCollection vertexTextureCollection; - private TextureCollection textureCollection; + public class GraphicsDevice : IDisposable + { + #region Private Members + private INativeGraphicsDevice nativeDevice; + private IndexBuffer indexBuffer; + private SamplerStateCollection samplerStateCollection; + private Viewport viewport; + private BlendState blendState; + private RasterizerState rasterizerState; + private DepthStencilState depthStencilState; + private GraphicsAdapter currentAdapter; + private PresentationParameters currentPresentationParameters; + private bool isDisposed; + private GraphicsProfile graphicsProfile; + private VertexBufferBinding[] currentVertexBufferBindings; + private RenderTargetBinding[] currentRenderTargetBindings; + private TextureCollection vertexTextureCollection; + private TextureCollection textureCollection; - #endregion // Private Members + #endregion // Private Members - #region Events - public event EventHandler Disposing; - public event EventHandler ResourceDestroyed; - public event EventHandler ResourceCreated; - public event EventHandler DeviceLost; - public event EventHandler DeviceReset; - public event EventHandler DeviceResetting; + #region Events + public event EventHandler Disposing; + public event EventHandler ResourceDestroyed; + public event EventHandler ResourceCreated; + public event EventHandler DeviceLost; + public event EventHandler DeviceReset; + public event EventHandler DeviceResetting; - #endregion // Events + #endregion // Events - #region Constructor & Destructor - public GraphicsDevice(GraphicsAdapter adapter, GraphicsProfile graphicsProfile, PresentationParameters presentationParameters) - { - this.currentAdapter = adapter; - this.graphicsProfile = graphicsProfile; + #region Constructor & Destructor + public GraphicsDevice(GraphicsAdapter adapter, GraphicsProfile graphicsProfile, + PresentationParameters presentationParameters) + { + this.currentAdapter = adapter; + this.graphicsProfile = graphicsProfile; - this.viewport = new Viewport(0, 0, presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight); + this.viewport = new Viewport(0, 0, presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight); - Recreate(presentationParameters); + Recreate(presentationParameters); - this.samplerStateCollection = new SamplerStateCollection(this, 8); //TODO: get maximum number of sampler states from capabilities - this.textureCollection = new TextureCollection(); - this.vertexTextureCollection = new TextureCollection(); + // TODO: get maximum number of sampler states from capabilities + this.samplerStateCollection = new SamplerStateCollection(this, 8); + this.textureCollection = new TextureCollection(); + this.vertexTextureCollection = new TextureCollection(); - this.BlendState = BlendState.Opaque; - this.DepthStencilState = DepthStencilState.Default; - this.RasterizerState = RasterizerState.CullCounterClockwise; - } + this.BlendState = BlendState.Opaque; + this.DepthStencilState = DepthStencilState.Default; + this.RasterizerState = RasterizerState.CullCounterClockwise; + } - ~GraphicsDevice() - { - this.Dispose(false); - } + ~GraphicsDevice() + { + this.Dispose(false); + } + #endregion // Constructor & Destructor - #endregion // Constructor & Destructor + #region Clear + public void Clear(Color color) + { + ClearOptions options = ClearOptions.Target; + if (this.currentPresentationParameters.DepthStencilFormat != DepthFormat.None) + { + options |= ClearOptions.DepthBuffer; + } + if (this.currentPresentationParameters.DepthStencilFormat == DepthFormat.Depth24Stencil8) + { + options |= ClearOptions.Stencil; + } - #region Clear - public void Clear(Color color) - { - ClearOptions options = ClearOptions.Target; - if (this.currentPresentationParameters.DepthStencilFormat != DepthFormat.None) - { - options |= ClearOptions.DepthBuffer; - } - if (this.currentPresentationParameters.DepthStencilFormat == DepthFormat.Depth24Stencil8) - { - options |= ClearOptions.Stencil; - } - - Clear(options, color, 1, 0); - // nativeDevice.Clear(ref color); - } + Clear(options, color, 1, 0); + // nativeDevice.Clear(ref color); + } - public void Clear(ClearOptions options, Color color, float depth, int stencil) - { - Clear(options, color.ToVector4(), depth, stencil); - } + public void Clear(ClearOptions options, Color color, float depth, int stencil) + { + Clear(options, color.ToVector4(), depth, stencil); + } - public void Clear(ClearOptions options, Vector4 color, float depth, int stencil) - { - if ((options & ClearOptions.DepthBuffer) == ClearOptions.DepthBuffer && - this.currentPresentationParameters.DepthStencilFormat == DepthFormat.None) - { - throw new InvalidOperationException("The depth buffer can only be cleared if it exists. The current DepthStencilFormat is DepthFormat.None"); - } + public void Clear(ClearOptions options, Vector4 color, float depth, int stencil) + { + if ((options & ClearOptions.DepthBuffer) == ClearOptions.DepthBuffer && + this.currentPresentationParameters.DepthStencilFormat == DepthFormat.None) + { + throw new InvalidOperationException("The depth buffer can only be cleared if it exists. The current DepthStencilFormat is DepthFormat.None"); + } - if ((options & ClearOptions.Stencil) == ClearOptions.Stencil && - this.currentPresentationParameters.DepthStencilFormat != DepthFormat.Depth24Stencil8) - { - throw new InvalidOperationException("The stencil buffer can only be cleared if it exists. The current DepthStencilFormat is not DepthFormat.Depth24Stencil8"); - } + if ((options & ClearOptions.Stencil) == ClearOptions.Stencil && + this.currentPresentationParameters.DepthStencilFormat != DepthFormat.Depth24Stencil8) + { + throw new InvalidOperationException("The stencil buffer can only be cleared if it exists. The current DepthStencilFormat is not DepthFormat.Depth24Stencil8"); + } - nativeDevice.Clear(options, color, depth, stencil); - } + nativeDevice.Clear(options, color, depth, stencil); + } - #endregion // Clear + #endregion // Clear - #region Present - public void Present() - { - nativeDevice.Present(); - } + #region Present + public void Present() + { + nativeDevice.Present(); + } - public void Present(Nullable sourceRectangle, Nullable destinationRectangle, IntPtr overrideWindowHandle) - { - //TODO: implement - throw new NotImplementedException(); - } + public void Present(Nullable sourceRectangle, Nullable destinationRectangle, IntPtr overrideWindowHandle) + { + //TODO: implement + throw new NotImplementedException(); + } - #endregion // Present + #endregion // Present - #region DrawPrimitives & DrawIndexedPrimitives - public void DrawIndexedPrimitives(PrimitiveType primitiveType, int baseVertex, int minVertexIndex, int numVertices, int startIndex, int primitiveCount) - { - nativeDevice.DrawIndexedPrimitives(primitiveType, baseVertex, minVertexIndex, numVertices, startIndex, primitiveCount); - } + #region DrawPrimitives & DrawIndexedPrimitives + public void DrawIndexedPrimitives(PrimitiveType primitiveType, int baseVertex, int minVertexIndex, int numVertices, + int startIndex, int primitiveCount) + { + nativeDevice.DrawIndexedPrimitives(primitiveType, baseVertex, minVertexIndex, numVertices, startIndex, + primitiveCount); + } - public void DrawPrimitives(PrimitiveType primitiveType, int startVertex, int primitiveCount) - { - nativeDevice.DrawPrimitives(primitiveType, startVertex, primitiveCount); - } + public void DrawPrimitives(PrimitiveType primitiveType, int startVertex, int primitiveCount) + { + nativeDevice.DrawPrimitives(primitiveType, startVertex, primitiveCount); + } + #endregion - #endregion // DrawPrimitives & DrawIndexedPrimitives + #region DrawInstancedPrimitives + public void DrawInstancedPrimitives(PrimitiveType primitiveType, int baseVertex, int minVertexIndex, int numVertices, + int startIndex, int primitiveCount, int instanceCount) + { + nativeDevice.DrawInstancedPrimitives(primitiveType, baseVertex, minVertexIndex, numVertices, startIndex, + primitiveCount, instanceCount); + } + #endregion - #region DrawInstancedPrimitives - public void DrawInstancedPrimitives(PrimitiveType primitiveType, int baseVertex, int minVertexIndex, int numVertices, int startIndex, int primitiveCount, int instanceCount) - { - nativeDevice.DrawInstancedPrimitives(primitiveType, baseVertex, minVertexIndex, numVertices, startIndex, primitiveCount, instanceCount); - } + #region DrawUserIndexedPrimitives + public void DrawUserIndexedPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, + short[] indexData, int indexOffset, int primitiveCount) where T : struct, IVertexType + { + VertexDeclaration vertexDeclaration = GetDeclarationForDraw(); + nativeDevice.DrawUserIndexedPrimitives(primitiveType, vertexData, vertexOffset, numVertices, indexData, + indexOffset, primitiveCount, vertexDeclaration, IndexElementSize.SixteenBits); + } - #endregion // DrawInstancedPrimitives + public void DrawUserIndexedPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, + short[] indexData, int indexOffset, int primitiveCount, VertexDeclaration vertexDeclaration) + where T : struct, IVertexType + { + nativeDevice.DrawUserIndexedPrimitives(primitiveType, vertexData, vertexOffset, numVertices, indexData, + indexOffset, primitiveCount, vertexDeclaration, IndexElementSize.SixteenBits); + } - #region DrawUserIndexedPrimitives - public void DrawUserIndexedPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, short[] indexData, int indexOffset, int primitiveCount) where T : struct, IVertexType - { - //TODO: cache the instances to avoid reflection overhead - IVertexType vertexType = Activator.CreateInstance(); - VertexDeclaration vertexDeclaration = vertexType.VertexDeclaration; + public void DrawUserIndexedPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, + int[] indexData, int indexOffset, int primitiveCount) where T : struct, IVertexType + { + VertexDeclaration vertexDeclaration = GetDeclarationForDraw(); + nativeDevice.DrawUserIndexedPrimitives(primitiveType, vertexData, vertexOffset, numVertices, indexData, + indexOffset, primitiveCount, vertexDeclaration, IndexElementSize.ThirtyTwoBits); + } - nativeDevice.DrawUserIndexedPrimitives(primitiveType, vertexData, vertexOffset, numVertices, indexData, indexOffset, primitiveCount, vertexDeclaration, IndexElementSize.SixteenBits); - } + public void DrawUserIndexedPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, + int[] indexData, int indexOffset, int primitiveCount, VertexDeclaration vertexDeclaration) + where T : struct, IVertexType + { + nativeDevice.DrawUserIndexedPrimitives(primitiveType, vertexData, vertexOffset, numVertices, indexData, + indexOffset, primitiveCount, vertexDeclaration, IndexElementSize.ThirtyTwoBits); + } + #endregion - public void DrawUserIndexedPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, short[] indexData, int indexOffset, int primitiveCount, VertexDeclaration vertexDeclaration) where T : struct, IVertexType - { - nativeDevice.DrawUserIndexedPrimitives(primitiveType, vertexData, vertexOffset, numVertices, indexData, indexOffset, primitiveCount, vertexDeclaration, IndexElementSize.SixteenBits); - } + #region DrawUserPrimitives + public void DrawUserPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int primitiveCount) + where T : struct, IVertexType + { + VertexDeclaration vertexDeclaration = GetDeclarationForDraw(); + nativeDevice.DrawUserPrimitives(primitiveType, vertexData, vertexOffset, primitiveCount, vertexDeclaration); + } - public void DrawUserIndexedPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, int[] indexData, int indexOffset, int primitiveCount) where T : struct, IVertexType - { - //TODO: cache the instances to avoid reflection overhead - IVertexType vertexType = Activator.CreateInstance(); - VertexDeclaration vertexDeclaration = vertexType.VertexDeclaration; + public void DrawUserPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int primitiveCount, + VertexDeclaration vertexDeclaration) where T : struct, IVertexType + { + nativeDevice.DrawUserPrimitives(primitiveType, vertexData, vertexOffset, primitiveCount, vertexDeclaration); + } + #endregion - nativeDevice.DrawUserIndexedPrimitives(primitiveType, vertexData, vertexOffset, numVertices, indexData, indexOffset, primitiveCount, vertexDeclaration, IndexElementSize.ThirtyTwoBits); - } - - public void DrawUserIndexedPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, int[] indexData, int indexOffset, int primitiveCount, VertexDeclaration vertexDeclaration) where T : struct, IVertexType - { - nativeDevice.DrawUserIndexedPrimitives(primitiveType, vertexData, vertexOffset, numVertices, indexData, indexOffset, primitiveCount, vertexDeclaration, IndexElementSize.ThirtyTwoBits); - } - - #endregion // DrawUserIndexedPrimitives - - #region DrawUserPrimitives - public void DrawUserPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int primitiveCount) where T : struct, IVertexType - { - //TODO: cache the instances to avoid reflection overhead - IVertexType vertexType = Activator.CreateInstance(); - VertexDeclaration vertexDeclaration = vertexType.VertexDeclaration; - - nativeDevice.DrawUserPrimitives(primitiveType, vertexData, vertexOffset, primitiveCount, vertexDeclaration); - } - - public void DrawUserPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int primitiveCount, VertexDeclaration vertexDeclaration) where T : struct, IVertexType - { - nativeDevice.DrawUserPrimitives(primitiveType, vertexData, vertexOffset, primitiveCount, vertexDeclaration); - } - - #endregion // DrawUserPrimitives + private VertexDeclaration GetDeclarationForDraw() where T : struct, IVertexType + { + //TODO: cache the instances to avoid reflection overhead + IVertexType vertexType = Activator.CreateInstance(); + return vertexType.VertexDeclaration; + } #if XNAEXT - #region SetConstantBuffer - /// - /// Binds a ConstantBuffer to the current GraphicsDevice - /// - /// The index of the constant buffer used in the shader - /// The managed constant buffer object to bind. - public void SetConstantBuffer(int slot, ConstantBuffer constantBuffer) - { - this.nativeDevice.SetConstantBuffer(slot, constantBuffer); - } + #region SetConstantBuffer + /// + /// Binds a ConstantBuffer to the current GraphicsDevice + /// + /// The index of the constant buffer used in the shader + /// The managed constant buffer object to bind. + public void SetConstantBuffer(int slot, ConstantBuffer constantBuffer) + { + this.nativeDevice.SetConstantBuffer(slot, constantBuffer); + } - /// - /// Binds ConstantBuffer objects to the current GraphicsDevice. - /// - /// The array of managed constant buffer objects to bind. - /// The constant buffer objects are bound in the order found in the passed array. - public void SetConstantBuffers(params ConstantBuffer[] constantBuffers) - { - for (int slot = 0; slot < constantBuffers.Length; slot++) - { - this.nativeDevice.SetConstantBuffer(slot, constantBuffers[slot]); - } - } + /// + /// Binds ConstantBuffer objects to the current GraphicsDevice. + /// + /// The array of managed constant buffer objects to bind. + /// The constant buffer objects are bound in the order found in the passed array. + public void SetConstantBuffers(params ConstantBuffer[] constantBuffers) + { + for (int slot = 0; slot < constantBuffers.Length; slot++) + { + this.nativeDevice.SetConstantBuffer(slot, constantBuffers[slot]); + } + } - #endregion + #endregion #endif - #region SetVertexBuffer - public void SetVertexBuffer(VertexBuffer vertexBuffer) - { - VertexBufferBinding[] bindings = new VertexBufferBinding[] { new VertexBufferBinding(vertexBuffer) }; - this.currentVertexBufferBindings = bindings; - this.nativeDevice.SetVertexBuffers(bindings); - } - - public void SetVertexBuffer(VertexBuffer vertexBuffer, int vertexOffset) - { - VertexBufferBinding[] bindings = new VertexBufferBinding[] { new VertexBufferBinding(vertexBuffer, vertexOffset) }; - this.currentVertexBufferBindings = bindings; - this.nativeDevice.SetVertexBuffers(bindings); - } - - public void SetVertexBuffers(params Graphics.VertexBufferBinding[] vertexBuffers) - { - this.currentVertexBufferBindings = vertexBuffers; - nativeDevice.SetVertexBuffers(vertexBuffers); - } - - #endregion // SetVertexBuffer - - #region SetRenderTarget - public void SetRenderTarget(RenderTarget2D renderTarget) - { - if (renderTarget != null) - { - RenderTargetBinding[] renderTargetBindings = new RenderTargetBinding[] { new RenderTargetBinding(renderTarget) }; - this.currentRenderTargetBindings = renderTargetBindings; - nativeDevice.SetRenderTargets(renderTargetBindings); - } - else - { - nativeDevice.SetRenderTargets(null); - } - } - - public void SetRenderTarget(RenderTargetCube renderTarget, CubeMapFace cubeMapFace) - { - RenderTargetBinding[] renderTargetBindings = new RenderTargetBinding[] { new RenderTargetBinding(renderTarget, cubeMapFace) }; - this.currentRenderTargetBindings = renderTargetBindings; - nativeDevice.SetRenderTargets(renderTargetBindings); - } - - public void SetRenderTargets(params RenderTargetBinding[] renderTargets) - { - this.currentRenderTargetBindings = renderTargets; - nativeDevice.SetRenderTargets(renderTargets); - } - - #endregion // SetRenderTarget - - #region GetBackBufferData - public void GetBackBufferData(Nullable rect, T[] data, int startIndex, int elementCount) where T : struct - { - nativeDevice.GetBackBufferData(rect, data, startIndex, elementCount); - } - - public void GetBackBufferData(T[] data) where T : struct - { - nativeDevice.GetBackBufferData(data); - } - - public void GetBackBufferData(T[] data, int startIndex, int elementCount) where T : struct - { - nativeDevice.GetBackBufferData(data, startIndex, elementCount); - } - - #endregion // GetBackBufferData - - public VertexBufferBinding[] GetVertexBuffers() - { - return this.currentVertexBufferBindings; - } - - public RenderTargetBinding[] GetRenderTargets() - { - return this.currentRenderTargetBindings; - } - - #region Reset - public void Reset() - { - this.Reset(this.currentPresentationParameters, this.currentAdapter); - } - - public void Reset(PresentationParameters presentationParameters) - { - this.Reset(presentationParameters, this.currentAdapter); - } - - public void Reset(PresentationParameters presentationParameters, GraphicsAdapter adapter) - { - if (presentationParameters == null) - { - throw new ArgumentNullException("presentationParameters"); - } - - if (adapter == null) - { - throw new ArgumentNullException("adapter"); - } - - if (this.currentAdapter != adapter) - { - throw new InvalidOperationException("adapter switch not yet implemented"); - } - - raise_DeviceResetting(this, EventArgs.Empty); - - // As it seems that no hardware Depth24 exists we handle Depth24 - // and Depth24Stencil8 the same way. Problem is that the Clear method - // checks for Depth24Stencil8 when trying to clear the stencil buffer - // and the format is set to Depth24. Internally Depth24 is already - // handled as Depth24Stencil8 so it is interchangeable. - if ((this.currentPresentationParameters.DepthStencilFormat == DepthFormat.Depth24 || - this.currentPresentationParameters.DepthStencilFormat == DepthFormat.Depth24Stencil8) && - (presentationParameters.DepthStencilFormat == DepthFormat.Depth24 || - presentationParameters.DepthStencilFormat == DepthFormat.Depth24Stencil8)) - { - this.currentPresentationParameters.DepthStencilFormat = presentationParameters.DepthStencilFormat; - } - - // reset presentation parameters - nativeDevice.ResizeBuffers(presentationParameters); - this.viewport = new Graphics.Viewport(0, 0, presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight); - - raise_DeviceReset(this, EventArgs.Empty); - } - - #endregion // Reset - - public void Dispose() - { - if (this.nativeDevice != null) - { - this.nativeDevice.Dispose(); - this.nativeDevice = null; - } - - //TODO: more to dispose? - } - - protected virtual void Dispose( - [MarshalAs(UnmanagedType.U1)] bool disposeManaged) - { - //TODO: implement - } - - #region Public Properties - public IndexBuffer Indices - { - get - { - return this.indexBuffer; - } - set - { - if (this.indexBuffer != value) - { - this.indexBuffer = value; - NativeDevice.SetIndexBuffer(this.indexBuffer); - } - } - } - - public Viewport Viewport - { - get - { - return this.viewport; - } - set - { - this.viewport = value; - } - } - - public BlendState BlendState - { - get - { - return this.blendState; - } - set - { - if (this.blendState != value) - { - if (this.blendState != null) - { - this.blendState.NativeBlendState.Release(); - } - - this.blendState = value; - - this.blendState.NativeBlendState.Apply(this); - } - } - } - - public DepthStencilState DepthStencilState - { - get - { - return this.depthStencilState; - } - set - { - if (this.depthStencilState != value) - { - if (this.depthStencilState != null) - { - this.depthStencilState.NativeDepthStencilState.Release(); - } - - this.depthStencilState = value; - - this.depthStencilState.NativeDepthStencilState.Apply(this); - } - } - } - - public RasterizerState RasterizerState - { - get - { - return this.rasterizerState; - } - set - { - if (this.rasterizerState != value) - { - if (this.rasterizerState != null) - { - this.rasterizerState.NativeRasterizerState.Release(); - } - - this.rasterizerState = value; - - this.rasterizerState.NativeRasterizerState.Apply(this); - } - } - } - - public SamplerStateCollection SamplerStates - { - get - { - return this.samplerStateCollection; - } - } - - public bool IsDisposed - { - get - { - return this.isDisposed; - } - } - - public DisplayMode DisplayMode - { - get - { - return this.currentAdapter.CurrentDisplayMode; - } - } - - public GraphicsProfile GraphicsProfile - { - get - { - return this.graphicsProfile; - } - } - - public GraphicsAdapter Adapter - { - get - { - return this.currentAdapter; - } - } - - public PresentationParameters PresentationParameters - { - get - { - return this.currentPresentationParameters; - } - } - - public int ReferenceStencil - { - get - { - return DepthStencilState.ReferenceStencil; - } - set - { - if (DepthStencilState.ReferenceStencil != value) - { - DepthStencilState.ReferenceStencil = value; - } - } - } - - public int MultiSampleMask - { - get - { - return BlendState.MultiSampleMask; - } - set - { - if (BlendState.MultiSampleMask != value) - { - BlendState.MultiSampleMask = value; - } - } - } - - public Color BlendFactor - { - get - { - return BlendState.BlendFactor; - } - set - { - if (BlendState.BlendFactor != value) - { - BlendState.BlendFactor = value; - } - } - } - - public TextureCollection VertexTextures - { - get - { - return this.vertexTextureCollection; - } - } - - public TextureCollection Textures - { - get - { - return this.textureCollection; - } - } - - #endregion // Public Properties - - public Rectangle ScissorRectangle - { - get - { - throw new NotImplementedException(); - } - set - { - throw new NotImplementedException(); - } - } - - public GraphicsDeviceStatus GraphicsDeviceStatus - { - get - { - throw new NotImplementedException(); - } - } - - public SamplerStateCollection VertexSamplerStates - { - get - { - throw new NotImplementedException(); - } - } - - internal INativeGraphicsDevice NativeDevice - { - get - { - return this.nativeDevice; - } - set - { - this.nativeDevice = value; - } - } - - internal void Recreate(PresentationParameters presentationParameters) - { - if (nativeDevice != null) - { - nativeDevice.Dispose(); - raise_ResourceDestroyed(this, new ResourceDestroyedEventArgs("NativeGraphicsDevice", nativeDevice)); - nativeDevice = null; - } - - if (nativeDevice == null) - { - this.currentPresentationParameters = presentationParameters; - nativeDevice = AddInSystemFactory.Instance.GetDefaultCreator().CreateGraphicsDevice(presentationParameters); - this.viewport = new Viewport(0, 0, presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight); - - raise_ResourceCreated(this, new ResourceCreatedEventArgs(nativeDevice)); - GraphicsResourceTracker.Instance.UpdateGraphicsDeviceReference(this); - - if (this.indexBuffer != null) - { - NativeDevice.SetIndexBuffer(this.indexBuffer); - } - - if (this.currentVertexBufferBindings != null) - { - NativeDevice.SetVertexBuffers(this.currentVertexBufferBindings); - } - - if (this.blendState != null) - { - this.blendState.NativeBlendState.Apply(this); - } - - if (this.rasterizerState != null) - { - this.rasterizerState.NativeRasterizerState.Apply(this); - } - - if (this.depthStencilState != null) - { - this.depthStencilState.NativeDepthStencilState.Apply(this); - } - - if (this.samplerStateCollection != null) - { - for (int i = 0; i < 8; i++) - { - if (this.samplerStateCollection[i] != null) - { - this.samplerStateCollection[i].NativeSamplerState.Apply(this, i); - } - } - } - } - - } - - protected void raise_Disposing(object sender, EventArgs args) - { - if (Disposing != null) - { - Disposing(sender, args); - } - } - - protected void raise_DeviceResetting(object sender, EventArgs args) - { - if (DeviceResetting != null) - { - DeviceResetting(sender, args); - } - } - - protected void raise_DeviceReset(object sender, EventArgs args) - { - if (DeviceReset != null) - { - DeviceReset(sender, args); - } - } - - protected void raise_DeviceLost(object sender, EventArgs args) - { - if (DeviceLost != null) - { - DeviceLost(sender, args); - } - } - - protected void raise_ResourceCreated(object sender, ResourceCreatedEventArgs args) - { - if (ResourceCreated != null) - { - ResourceCreated(sender, args); - } - } - - protected void raise_ResourceDestroyed(object sender, ResourceDestroyedEventArgs args) - { - if (ResourceDestroyed != null) - { - ResourceDestroyed(sender, args); - } - } - } + #region SetVertexBuffer + public void SetVertexBuffer(VertexBuffer vertexBuffer) + { + VertexBufferBinding[] bindings = new VertexBufferBinding[] { new VertexBufferBinding(vertexBuffer) }; + this.currentVertexBufferBindings = bindings; + this.nativeDevice.SetVertexBuffers(bindings); + } + + public void SetVertexBuffer(VertexBuffer vertexBuffer, int vertexOffset) + { + VertexBufferBinding[] bindings = new VertexBufferBinding[] { new VertexBufferBinding(vertexBuffer, vertexOffset) }; + this.currentVertexBufferBindings = bindings; + this.nativeDevice.SetVertexBuffers(bindings); + } + + public void SetVertexBuffers(params Graphics.VertexBufferBinding[] vertexBuffers) + { + this.currentVertexBufferBindings = vertexBuffers; + nativeDevice.SetVertexBuffers(vertexBuffers); + } + + #endregion // SetVertexBuffer + + #region SetRenderTarget + public void SetRenderTarget(RenderTarget2D renderTarget) + { + if (renderTarget != null) + { + RenderTargetBinding[] renderTargetBindings = new RenderTargetBinding[] { new RenderTargetBinding(renderTarget) }; + this.currentRenderTargetBindings = renderTargetBindings; + nativeDevice.SetRenderTargets(renderTargetBindings); + } + else + { + nativeDevice.SetRenderTargets(null); + } + } + + public void SetRenderTarget(RenderTargetCube renderTarget, CubeMapFace cubeMapFace) + { + RenderTargetBinding[] renderTargetBindings = new RenderTargetBinding[] { new RenderTargetBinding(renderTarget, cubeMapFace) }; + this.currentRenderTargetBindings = renderTargetBindings; + nativeDevice.SetRenderTargets(renderTargetBindings); + } + + public void SetRenderTargets(params RenderTargetBinding[] renderTargets) + { + this.currentRenderTargetBindings = renderTargets; + nativeDevice.SetRenderTargets(renderTargets); + } + + #endregion // SetRenderTarget + + #region GetBackBufferData + public void GetBackBufferData(Nullable rect, T[] data, int startIndex, int elementCount) where T : struct + { + nativeDevice.GetBackBufferData(rect, data, startIndex, elementCount); + } + + public void GetBackBufferData(T[] data) where T : struct + { + nativeDevice.GetBackBufferData(data); + } + + public void GetBackBufferData(T[] data, int startIndex, int elementCount) where T : struct + { + nativeDevice.GetBackBufferData(data, startIndex, elementCount); + } + + #endregion // GetBackBufferData + + public VertexBufferBinding[] GetVertexBuffers() + { + return this.currentVertexBufferBindings; + } + + public RenderTargetBinding[] GetRenderTargets() + { + return this.currentRenderTargetBindings; + } + + #region Reset + public void Reset() + { + this.Reset(this.currentPresentationParameters, this.currentAdapter); + } + + public void Reset(PresentationParameters presentationParameters) + { + this.Reset(presentationParameters, this.currentAdapter); + } + + public void Reset(PresentationParameters presentationParameters, GraphicsAdapter adapter) + { + if (presentationParameters == null) + { + throw new ArgumentNullException("presentationParameters"); + } + + if (adapter == null) + { + throw new ArgumentNullException("adapter"); + } + + if (this.currentAdapter != adapter) + { + throw new InvalidOperationException("adapter switch not yet implemented"); + } + + raise_DeviceResetting(this, EventArgs.Empty); + + // As it seems that no hardware Depth24 exists we handle Depth24 + // and Depth24Stencil8 the same way. Problem is that the Clear method + // checks for Depth24Stencil8 when trying to clear the stencil buffer + // and the format is set to Depth24. Internally Depth24 is already + // handled as Depth24Stencil8 so it is interchangeable. + if ((this.currentPresentationParameters.DepthStencilFormat == DepthFormat.Depth24 || + this.currentPresentationParameters.DepthStencilFormat == DepthFormat.Depth24Stencil8) && + (presentationParameters.DepthStencilFormat == DepthFormat.Depth24 || + presentationParameters.DepthStencilFormat == DepthFormat.Depth24Stencil8)) + { + this.currentPresentationParameters.DepthStencilFormat = presentationParameters.DepthStencilFormat; + } + + // reset presentation parameters + nativeDevice.ResizeBuffers(presentationParameters); + this.viewport = new Graphics.Viewport(0, 0, presentationParameters.BackBufferWidth, + presentationParameters.BackBufferHeight); + + raise_DeviceReset(this, EventArgs.Empty); + } + + #endregion // Reset + + #region Dispose (TODO) + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose([MarshalAs(UnmanagedType.U1)] bool disposeManaged) + { + if (isDisposed == false) + { + if (nativeDevice != null) + { + nativeDevice.Dispose(); + nativeDevice = null; + } + + raise_Disposing(this, EventArgs.Empty); + //TODO: more to dispose? + } + } + #endregion + + #region Public Properties + public IndexBuffer Indices + { + get + { + return this.indexBuffer; + } + set + { + if (this.indexBuffer != value) + { + this.indexBuffer = value; + NativeDevice.SetIndexBuffer(this.indexBuffer); + } + } + } + + public Viewport Viewport + { + get + { + return this.viewport; + } + set + { + this.viewport = value; + } + } + + public BlendState BlendState + { + get + { + return this.blendState; + } + set + { + if (this.blendState != value) + { + if (this.blendState != null) + { + this.blendState.NativeBlendState.Release(); + } + + this.blendState = value; + + this.blendState.NativeBlendState.Apply(this); + } + } + } + + public DepthStencilState DepthStencilState + { + get + { + return this.depthStencilState; + } + set + { + if (this.depthStencilState != value) + { + if (this.depthStencilState != null) + { + this.depthStencilState.NativeDepthStencilState.Release(); + } + + this.depthStencilState = value; + + this.depthStencilState.NativeDepthStencilState.Apply(this); + } + } + } + + public RasterizerState RasterizerState + { + get + { + return this.rasterizerState; + } + set + { + if (this.rasterizerState != value) + { + if (this.rasterizerState != null) + { + this.rasterizerState.NativeRasterizerState.Release(); + } + + this.rasterizerState = value; + + this.rasterizerState.NativeRasterizerState.Apply(this); + } + } + } + + public SamplerStateCollection SamplerStates + { + get + { + return this.samplerStateCollection; + } + } + + public bool IsDisposed + { + get + { + return this.isDisposed; + } + } + + public DisplayMode DisplayMode + { + get + { + return this.currentAdapter.CurrentDisplayMode; + } + } + + public GraphicsProfile GraphicsProfile + { + get + { + return this.graphicsProfile; + } + } + + public GraphicsAdapter Adapter + { + get + { + return this.currentAdapter; + } + } + + public PresentationParameters PresentationParameters + { + get + { + return this.currentPresentationParameters; + } + } + + public int ReferenceStencil + { + get + { + return DepthStencilState.ReferenceStencil; + } + set + { + if (DepthStencilState.ReferenceStencil != value) + { + DepthStencilState.ReferenceStencil = value; + } + } + } + + public int MultiSampleMask + { + get + { + return BlendState.MultiSampleMask; + } + set + { + if (BlendState.MultiSampleMask != value) + { + BlendState.MultiSampleMask = value; + } + } + } + + public Color BlendFactor + { + get + { + return BlendState.BlendFactor; + } + set + { + if (BlendState.BlendFactor != value) + { + BlendState.BlendFactor = value; + } + } + } + + public TextureCollection VertexTextures + { + get + { + return this.vertexTextureCollection; + } + } + + public TextureCollection Textures + { + get + { + return this.textureCollection; + } + } + + #endregion // Public Properties + + public Rectangle ScissorRectangle + { + get + { + throw new NotImplementedException(); + } + set + { + throw new NotImplementedException(); + } + } + + public GraphicsDeviceStatus GraphicsDeviceStatus + { + get + { + throw new NotImplementedException(); + } + } + + public SamplerStateCollection VertexSamplerStates + { + get + { + throw new NotImplementedException(); + } + } + + internal INativeGraphicsDevice NativeDevice + { + get + { + return this.nativeDevice; + } + set + { + this.nativeDevice = value; + } + } + + internal void Recreate(PresentationParameters presentationParameters) + { + if (nativeDevice != null) + { + nativeDevice.Dispose(); + raise_ResourceDestroyed(this, new ResourceDestroyedEventArgs("NativeGraphicsDevice", nativeDevice)); + nativeDevice = null; + } + + if (nativeDevice == null) + { + this.currentPresentationParameters = presentationParameters; + var creator = AddInSystemFactory.Instance.GetDefaultCreator(); + nativeDevice = creator.CreateGraphicsDevice(presentationParameters); + this.viewport = new Viewport(0, 0, presentationParameters.BackBufferWidth, + presentationParameters.BackBufferHeight); + + raise_ResourceCreated(this, new ResourceCreatedEventArgs(nativeDevice)); + GraphicsResourceTracker.Instance.UpdateGraphicsDeviceReference(this); + + if (this.indexBuffer != null) + { + NativeDevice.SetIndexBuffer(this.indexBuffer); + } + + if (this.currentVertexBufferBindings != null) + { + NativeDevice.SetVertexBuffers(this.currentVertexBufferBindings); + } + + if (this.blendState != null) + { + this.blendState.NativeBlendState.Apply(this); + } + + if (this.rasterizerState != null) + { + this.rasterizerState.NativeRasterizerState.Apply(this); + } + + if (this.depthStencilState != null) + { + this.depthStencilState.NativeDepthStencilState.Apply(this); + } + + if (this.samplerStateCollection != null) + { + for (int i = 0; i < 8; i++) + { + if (this.samplerStateCollection[i] != null) + { + this.samplerStateCollection[i].NativeSamplerState.Apply(this, i); + } + } + } + } + } + + protected void raise_Disposing(object sender, EventArgs args) + { + if (Disposing != null) + Disposing(sender, args); + } + + protected void raise_DeviceResetting(object sender, EventArgs args) + { + if (DeviceResetting != null) + DeviceResetting(sender, args); + } + + protected void raise_DeviceReset(object sender, EventArgs args) + { + if (DeviceReset != null) + DeviceReset(sender, args); + } + + protected void raise_DeviceLost(object sender, EventArgs args) + { + if (DeviceLost != null) + DeviceLost(sender, args); + } + + protected void raise_ResourceCreated(object sender, ResourceCreatedEventArgs args) + { + if (ResourceCreated != null) + ResourceCreated(sender, args); + } + + protected void raise_ResourceDestroyed(object sender, ResourceDestroyedEventArgs args) + { + if (ResourceDestroyed != null) + ResourceDestroyed(sender, args); + } + } } diff --git a/ANX.Framework/Graphics/VertexDeclaration.cs b/ANX.Framework/Graphics/VertexDeclaration.cs index 05ea93f2..ea9194af 100644 --- a/ANX.Framework/Graphics/VertexDeclaration.cs +++ b/ANX.Framework/Graphics/VertexDeclaration.cs @@ -1,90 +1,77 @@ -#region Using Statements using System; using System.Runtime.InteropServices; -#endregion // Using Statements - // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. // For details see: http://anxframework.codeplex.com/license namespace ANX.Framework.Graphics { - public class VertexDeclaration : GraphicsResource - { - private int vertexStride; - private VertexElement[] elements; + public class VertexDeclaration : GraphicsResource + { + private VertexElement[] elements; - public VertexDeclaration(params VertexElement[] elements) - { - this.elements = elements; + public int VertexStride + { + get; + private set; + } + public VertexDeclaration(params VertexElement[] elements) + { + this.elements = elements; - for (int i = 0; i < this.elements.Length; i++) - { - this.vertexStride += GetElementStride(this.elements[i].VertexElementFormat); - } - } + for (int i = 0; i < this.elements.Length; i++) + VertexStride += GetElementStride(this.elements[i].VertexElementFormat); + } - public VertexDeclaration(int vertexStride, params VertexElement[] elements) - { - this.elements = elements; - this.vertexStride = vertexStride; - } + public VertexDeclaration(int vertexStride, params VertexElement[] elements) + { + this.elements = elements; + VertexStride = vertexStride; + } - public override void Dispose() - { - throw new NotImplementedException(); - } + public VertexElement[] GetVertexElements() + { + if (elements != null) + return elements.Clone() as VertexElement[]; + else + return null; + } - public int VertexStride - { - get - { - return this.vertexStride; - } - } + public override void Dispose() + { + Dispose(true); + } - public VertexElement[] GetVertexElements() - { - if (elements != null) - { - return elements.Clone() as VertexElement[]; - } - else - { - return null; - } - } + protected override void Dispose([MarshalAs(UnmanagedType.U1)] bool disposeManaged) + { + // Nothing to dispose + } - protected override void Dispose([MarshalAs(UnmanagedType.U1)] bool disposeManaged) - { - throw new NotImplementedException(); - } - - private int GetElementStride(VertexElementFormat format) - { - switch (format) - { - case VertexElementFormat.NormalizedShort2: - case VertexElementFormat.Byte4: - case VertexElementFormat.Color: - case VertexElementFormat.HalfVector2: - case VertexElementFormat.Short2: - case VertexElementFormat.Single: - return 4; - case VertexElementFormat.HalfVector4: - case VertexElementFormat.NormalizedShort4: - case VertexElementFormat.Short4: - case VertexElementFormat.Vector2: - return 8; - case VertexElementFormat.Vector3: - return 12; - case VertexElementFormat.Vector4: - return 16; - default: - throw new ArgumentException("unknown VertexElementFormat size '" + format.ToString() + "'"); - } - } - } + private int GetElementStride(VertexElementFormat format) + { + switch (format) + { + case VertexElementFormat.NormalizedShort2: + case VertexElementFormat.Byte4: + case VertexElementFormat.Color: + case VertexElementFormat.HalfVector2: + case VertexElementFormat.Short2: + case VertexElementFormat.Single: + return 4; + case VertexElementFormat.HalfVector4: + case VertexElementFormat.NormalizedShort4: + case VertexElementFormat.Short4: + case VertexElementFormat.Vector2: + return 8; + case VertexElementFormat.Vector3: + return 12; + case VertexElementFormat.Vector4: + return 16; + default: + throw new ArgumentException("unknown VertexElementFormat size '" + format + "'"); + } + } + } } diff --git a/ANX.Framework/GraphicsDeviceManager.cs b/ANX.Framework/GraphicsDeviceManager.cs index 334584b6..152185fd 100644 --- a/ANX.Framework/GraphicsDeviceManager.cs +++ b/ANX.Framework/GraphicsDeviceManager.cs @@ -172,12 +172,27 @@ namespace ANX.Framework public void Dispose() { - throw new NotImplementedException(); + Dispose(true); + GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { - throw new NotImplementedException(); + if (disposing) + { + if (game != null) + if (game.Services.GetService(typeof(IGraphicsDeviceService)) == this) + game.Services.RemoveService(typeof(IGraphicsDeviceService)); + + if (graphicsDevice != null) + { + graphicsDevice.Dispose(); + graphicsDevice = null; + } + + if (Disposed != null) + Disposed(this, EventArgs.Empty); + } } protected GraphicsDeviceInformation FindBestDevice(bool anySuitableDevice) diff --git a/ANX.Framework/NonXNA/AddIn.cs b/ANX.Framework/NonXNA/AddIn.cs index 86392d20..e7670ed2 100644 --- a/ANX.Framework/NonXNA/AddIn.cs +++ b/ANX.Framework/NonXNA/AddIn.cs @@ -104,7 +104,7 @@ namespace ANX.Framework.NonXNA this.assembly = TypeHelper.GetAssemblyFrom(creatorType); this.creatorType = creatorType; Type = AddInSystemFactory.GetAddInType(creatorType); - this.supportedPlatforms = (ISupportedPlatforms)Activator.CreateInstance(supportedPlatformsType); + this.supportedPlatforms = TypeHelper.Create(supportedPlatformsType); Version = assembly.GetName().Version; } #endregion @@ -122,8 +122,8 @@ namespace ANX.Framework.NonXNA if (instance == null && IsSupported) { try - { - instance = Activator.CreateInstance(creatorType) as ICreator; + { + instance = TypeHelper.Create(creatorType); ; } catch (Exception ex) { @@ -131,9 +131,7 @@ namespace ANX.Framework.NonXNA } if (instance != null) - { AddInSystemFactory.Instance.AddCreator(instance); - } } } #endregion diff --git a/ANX.Framework/NonXNA/Logger.cs b/ANX.Framework/NonXNA/Logger.cs index 73775793..40d60763 100644 --- a/ANX.Framework/NonXNA/Logger.cs +++ b/ANX.Framework/NonXNA/Logger.cs @@ -97,6 +97,17 @@ namespace ANX.Framework.NonXNA } #endregion + #region ErrorAndThrow + public static void ErrorAndThrow(string message) where T : Exception, new () + { + string text = CurrentTimeStamp + "| Error: " + message + BuildStackTrace(); + WriteToConsole(text); + WriteToFile(text); + + throw (T)Activator.CreateInstance(typeof(T), message); + } + #endregion + #region BuildStackTrace private static string BuildStackTrace() { diff --git a/ANX.Framework/NonXNA/Reflection/AssemblyLoader.cs b/ANX.Framework/NonXNA/Reflection/AssemblyLoader.cs index 8a593ce9..42de7c2d 100644 --- a/ANX.Framework/NonXNA/Reflection/AssemblyLoader.cs +++ b/ANX.Framework/NonXNA/Reflection/AssemblyLoader.cs @@ -65,13 +65,17 @@ namespace ANX.Framework.NonXNA.Reflection foreach (string file in assembliesInPath) { if (file.EndsWith("OpenTK.dll") || + file.EndsWith("OpenTK.GLControl.dll") || + file.EndsWith("OpenTK.Compatibility.dll") || file.EndsWith("SharpDX.dll") || file.EndsWith("SharpDX.Direct3D11.dll") || file.EndsWith("SharpDX.Direct3D10.dll") || file.EndsWith("SharpDX.D3DCompiler.dll") || file.EndsWith("SharpDX.DXGI.dll") || file.EndsWith("SharpDX.XInput.dll") || - file.EndsWith("SharpDX.DirectInput.dll")) + file.EndsWith("SharpDX.DirectInput.dll") || + file.EndsWith("WaveUtils.dll") || + file.EndsWith("SharpDX.XAudio2.dll")) { continue; } @@ -166,10 +170,11 @@ namespace ANX.Framework.NonXNA.Reflection continue; } - bool isTypeValidInputDevice = TypeHelper.IsAnyTypeAssignableFrom(InputDeviceFactory.ValidInputDeviceCreators, type); + bool isTypeValidInputDevice = TypeHelper.IsAnyTypeAssignableFrom(InputDeviceFactory.ValidInputDeviceCreators, + type); if (isTypeValidInputDevice && isTypeCreatable) { - var inputCreator = Activator.CreateInstance(type) as IInputDeviceCreator; + var inputCreator = TypeHelper.Create(type); InputDeviceFactory.Instance.AddCreator(type, inputCreator); } } diff --git a/ANX.Framework/NonXNA/Reflection/TypeHelper.cs b/ANX.Framework/NonXNA/Reflection/TypeHelper.cs index 9bb1b1fc..793f39e4 100644 --- a/ANX.Framework/NonXNA/Reflection/TypeHelper.cs +++ b/ANX.Framework/NonXNA/Reflection/TypeHelper.cs @@ -158,5 +158,12 @@ namespace ANX.Framework.NonXNA.Reflection #endif } #endregion + + #region Create + public static T Create(Type type) + { + return (T)Activator.CreateInstance(type); + } + #endregion } } diff --git a/ANX.Framework/NonXNA/RenderSystem/INativeGraphicsDevice.cs b/ANX.Framework/NonXNA/RenderSystem/INativeGraphicsDevice.cs index 0b9955fa..8172e604 100644 --- a/ANX.Framework/NonXNA/RenderSystem/INativeGraphicsDevice.cs +++ b/ANX.Framework/NonXNA/RenderSystem/INativeGraphicsDevice.cs @@ -1,50 +1,52 @@ -#region Using Statements using System; using ANX.Framework.Graphics; -#endregion // Using Statements - // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. // For details see: http://anxframework.codeplex.com/license namespace ANX.Framework.NonXNA { - public interface INativeGraphicsDevice : IDisposable - { - void Clear(ref Color color); - void Clear(ClearOptions options, Vector4 color, float depth, int stencil); + public interface INativeGraphicsDevice : IDisposable + { + void Clear(ref Color color); + void Clear(ClearOptions options, Vector4 color, float depth, int stencil); - void Present(); + void Present(); - void DrawIndexedPrimitives(PrimitiveType primitiveType, int baseVertex, int minVertexIndex, int numVertices, int startIndex, int primitiveCount); + void DrawIndexedPrimitives(PrimitiveType primitiveType, int baseVertex, int minVertexIndex, int numVertices, + int startIndex, int primitiveCount); - void DrawInstancedPrimitives(PrimitiveType primitiveType, int baseVertex, int minVertexIndex, int numVertices, int startIndex, int primitiveCount, int instanceCount); + void DrawInstancedPrimitives(PrimitiveType primitiveType, int baseVertex, int minVertexIndex, int numVertices, + int startIndex, int primitiveCount, int instanceCount); - void DrawUserIndexedPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, Array indexData, int indexOffset, int primitiveCount, VertexDeclaration vertexDeclaration, IndexElementSize indexFormat) where T : struct, IVertexType; + void DrawUserIndexedPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, + Array indexData, int indexOffset, int primitiveCount, VertexDeclaration vertexDeclaration, + IndexElementSize indexFormat) where T : struct, IVertexType; - void DrawUserPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int primitiveCount, VertexDeclaration vertexDeclaration) where T : struct, IVertexType; + void DrawUserPrimitives(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int primitiveCount, + VertexDeclaration vertexDeclaration) where T : struct, IVertexType; - void DrawPrimitives(PrimitiveType primitiveType, int vertexOffset, int primitiveCount); + void DrawPrimitives(PrimitiveType primitiveType, int vertexOffset, int primitiveCount); #if XNAEXT - void SetConstantBuffer(int slot, ConstantBuffer constantBuffer); + void SetConstantBuffer(int slot, ConstantBuffer constantBuffer); #endif - void SetVertexBuffers(VertexBufferBinding[] vertexBuffers); + void SetVertexBuffers(VertexBufferBinding[] vertexBuffers); - void SetIndexBuffer(IndexBuffer indexBuffer); + void SetIndexBuffer(IndexBuffer indexBuffer); - void SetViewport(Viewport viewport); + void SetViewport(Viewport viewport); - void SetRenderTargets(params RenderTargetBinding[] renderTargets); + void SetRenderTargets(params RenderTargetBinding[] renderTargets); - void GetBackBufferData(Nullable rect, T[] data, int startIndex, int elementCount) where T : struct; - void GetBackBufferData(T[] data) where T : struct; - void GetBackBufferData(T[] data, int startIndex, int elementCount) where T : struct; + void GetBackBufferData(Nullable rect, T[] data, int startIndex, int elementCount) where T : struct; + void GetBackBufferData(T[] data) where T : struct; + void GetBackBufferData(T[] data, int startIndex, int elementCount) where T : struct; - void ResizeBuffers(PresentationParameters presentationParameters); + void ResizeBuffers(PresentationParameters presentationParameters); - bool VSync { get; set; } - } + bool VSync { get; set; } + } } diff --git a/ANX.Framework/NonXNA/ThreadHelper.cs b/ANX.Framework/NonXNA/ThreadHelper.cs new file mode 100644 index 00000000..b2ae6fad --- /dev/null +++ b/ANX.Framework/NonXNA/ThreadHelper.cs @@ -0,0 +1,17 @@ +using System; +using System.Threading; + +namespace ANX.Framework.NonXNA +{ + internal static class ThreadHelper + { + public static void Sleep(int milliseconds) + { +#if WINDOWSMETRO + // TODO: search replacement +#else + Thread.Sleep(milliseconds); +#endif + } + } +} diff --git a/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK.csproj b/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK.csproj index fc4d2e3d..980a5e61 100644 --- a/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK.csproj +++ b/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK.csproj @@ -54,10 +54,6 @@ False ..\..\lib\OpenTK\OpenTK.dll - - False - ..\..\lib\OpenTK\OpenTK.Compatibility.dll - diff --git a/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK_Linux.csproj b/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK_Linux.csproj index 79b6218d..37e41e4a 100644 --- a/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK_Linux.csproj +++ b/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK_Linux.csproj @@ -54,10 +54,6 @@ False ..\..\lib\OpenTK\OpenTK.dll - - False - ..\..\lib\OpenTK\OpenTK.Compatibility.dll - diff --git a/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK_PSVita.csproj b/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK_PSVita.csproj index 9fcfe197..acf770b0 100644 --- a/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK_PSVita.csproj +++ b/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK_PSVita.csproj @@ -54,10 +54,6 @@ False ..\..\lib\OpenTK\OpenTK.dll - - False - ..\..\lib\OpenTK\OpenTK.Compatibility.dll - diff --git a/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK_WindowsMetro.csproj b/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK_WindowsMetro.csproj index e2bc9b75..4612cd2b 100644 --- a/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK_WindowsMetro.csproj +++ b/InputSystems/ANX.InputDevices.OpenTK/ANX.InputDevices.OpenTK_WindowsMetro.csproj @@ -56,10 +56,6 @@ False ..\..\lib\OpenTK\OpenTK.dll - - False - ..\..\lib\OpenTK\OpenTK.Compatibility.dll - diff --git a/RenderSystems/ANX.Framework.Windows.GL3/Creator.cs b/RenderSystems/ANX.Framework.Windows.GL3/Creator.cs index d387cdd2..1f1ff71d 100644 --- a/RenderSystems/ANX.Framework.Windows.GL3/Creator.cs +++ b/RenderSystems/ANX.Framework.Windows.GL3/Creator.cs @@ -18,6 +18,7 @@ namespace ANX.RenderSystem.Windows.GL3 { [PercentageComplete(90)] [TestState(TestStateAttribute.TestState.Untested)] + [Developer("AstrorEnales")] public class Creator : IRenderSystemCreator { #region Public @@ -79,8 +80,7 @@ namespace ANX.RenderSystem.Windows.GL3 #endregion #region CreateGraphicsDevice - INativeGraphicsDevice IRenderSystemCreator.CreateGraphicsDevice( - PresentationParameters presentationParameters) + INativeGraphicsDevice IRenderSystemCreator.CreateGraphicsDevice(PresentationParameters presentationParameters) { AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); return new GraphicsDeviceWindowsGL3(presentationParameters); @@ -145,8 +145,9 @@ namespace ANX.RenderSystem.Windows.GL3 #endregion #if XNAEXT - #region CreateConstantBuffer - public INativeConstantBuffer CreateConstantBuffer(GraphicsDevice graphics, ConstantBuffer managedBuffer, BufferUsage usage) + #region CreateConstantBuffer (TODO) + public INativeConstantBuffer CreateConstantBuffer(GraphicsDevice graphics, ConstantBuffer managedBuffer, + BufferUsage usage) { AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); @@ -250,7 +251,7 @@ namespace ANX.RenderSystem.Windows.GL3 public ReadOnlyCollection GetAdapterList() { AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); - + var result = new List(); foreach (DisplayDevice device in DisplayDevice.AvailableDisplays) { @@ -316,9 +317,11 @@ namespace ANX.RenderSystem.Windows.GL3 } #endregion - public bool IsLanguageSupported(EffectSourceLanguage sourceLanguage) + #region IsLanguageSupported + public bool IsLanguageSupported(EffectSourceLanguage sourceLanguage) { return sourceLanguage == EffectSourceLanguage.GLSL_FX || sourceLanguage == EffectSourceLanguage.GLSL; - } - } + } + #endregion + } } diff --git a/RenderSystems/ANX.Framework.Windows.GL3/GraphicsDeviceWindowsGL3.cs b/RenderSystems/ANX.Framework.Windows.GL3/GraphicsDeviceWindowsGL3.cs index 9d5460c6..dd6513da 100644 --- a/RenderSystems/ANX.Framework.Windows.GL3/GraphicsDeviceWindowsGL3.cs +++ b/RenderSystems/ANX.Framework.Windows.GL3/GraphicsDeviceWindowsGL3.cs @@ -51,6 +51,8 @@ namespace ANX.RenderSystem.Windows.GL3 { get { + if (Current == null || Current.nativeContext == null) + return false; return Current.nativeContext.IsCurrent; } } @@ -78,11 +80,6 @@ namespace ANX.RenderSystem.Windows.GL3 Current = this; ResetDevice(presentationParameters); } - - ~GraphicsDeviceWindowsGL3() - { - Dispose(); - } #endregion #region ResetDevice @@ -471,16 +468,16 @@ namespace ANX.RenderSystem.Windows.GL3 activeEffect = null; boundRenderTargets = null; - if (nativeContext != null) - { - nativeContext.Dispose(); - nativeContext = null; - } if (nativeWindowInfo != null) { nativeWindowInfo.Dispose(); nativeWindowInfo = null; } + if (nativeContext != null) + { + nativeContext.Dispose(); + nativeContext = null; + } } #endregion } diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL.csproj b/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL.csproj index 450f385e..9e1b0ca2 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL.csproj +++ b/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL.csproj @@ -57,10 +57,6 @@ False ..\..\lib\OpenTK\OpenTK.dll - - False - ..\..\lib\OpenTK\OpenTK.Compatibility.dll - diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL_Linux.csproj b/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL_Linux.csproj index b67aac47..e34a5c6c 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL_Linux.csproj +++ b/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL_Linux.csproj @@ -57,10 +57,6 @@ False ..\..\lib\OpenTK\OpenTK.dll - - False - ..\..\lib\OpenTK\OpenTK.Compatibility.dll - diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL_PSVita.csproj b/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL_PSVita.csproj index d238081c..5bcef598 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL_PSVita.csproj +++ b/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL_PSVita.csproj @@ -57,10 +57,6 @@ False ..\..\lib\OpenTK\OpenTK.dll - - False - ..\..\lib\OpenTK\OpenTK.Compatibility.dll - diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL_WindowsMetro.csproj b/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL_WindowsMetro.csproj index 1c7ebb80..12f000ab 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL_WindowsMetro.csproj +++ b/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL_WindowsMetro.csproj @@ -59,10 +59,6 @@ False ..\..\lib\OpenTK\OpenTK.dll - - False - ..\..\lib\OpenTK\OpenTK.Compatibility.dll - diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffect.cs b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffect.cs index a34784a5..1721cbd0 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffect.cs +++ b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffect.cs @@ -2,9 +2,9 @@ using System.IO; using ANX.Framework.Audio; using ANX.Framework.NonXNA.SoundSystem; -using OpenTK.Audio; +using OpenTK.Audio.OpenAL; using WaveUtils; -using ALFormat = OpenTK.Audio.ALFormat; +using ALFormat = OpenTK.Audio.OpenAL.ALFormat; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffectInstance.cs b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffectInstance.cs index f651a5e3..980f49a2 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffectInstance.cs +++ b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffectInstance.cs @@ -1,7 +1,7 @@ using System; using ANX.Framework.Audio; using ANX.Framework.NonXNA.SoundSystem; -using OpenTK.Audio; +using OpenTK.Audio.OpenAL; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license.