From 0e88359f1ef4e18186e07b6abc094bda72368e6e Mon Sep 17 00:00:00 2001 From: zeromus Date: Thu, 30 Jan 2014 04:54:02 +0000 Subject: [PATCH] bizwaregl-some simplification and cleanup and reduction of pointless api calls --- .../GraphicsControl_TK.cs | 3 +- .../IGL_TK.cs | 56 +++++++++---------- Bizware/BizHawk.Bizware.BizwareGL/IGL.cs | 2 + 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/Bizware/BizHawk.Bizware.BizwareGL.OpenTK/GraphicsControl_TK.cs b/Bizware/BizHawk.Bizware.BizwareGL.OpenTK/GraphicsControl_TK.cs index 335b5f9cf0..32b74697cc 100644 --- a/Bizware/BizHawk.Bizware.BizwareGL.OpenTK/GraphicsControl_TK.cs +++ b/Bizware/BizHawk.Bizware.BizwareGL.OpenTK/GraphicsControl_TK.cs @@ -46,8 +46,7 @@ namespace BizHawk.Bizware.BizwareGL.Drivers.OpenTK public void End() { - //this slows things down too much - //Owner.MakeDefaultCurrent(); + Owner.MakeDefaultCurrent(); } public new void SwapBuffers() diff --git a/Bizware/BizHawk.Bizware.BizwareGL.OpenTK/IGL_TK.cs b/Bizware/BizHawk.Bizware.BizwareGL.OpenTK/IGL_TK.cs index cf8d341218..b87321cbd8 100644 --- a/Bizware/BizHawk.Bizware.BizwareGL.OpenTK/IGL_TK.cs +++ b/Bizware/BizHawk.Bizware.BizwareGL.OpenTK/IGL_TK.cs @@ -56,6 +56,7 @@ namespace BizHawk.Bizware.BizwareGL.Drivers.OpenTK //misc initialization CreateRenderStates(); GL.Enable(EnableCap.Texture2D); + PurgeStateCache(); } void IDisposable.Dispose() @@ -241,12 +242,12 @@ namespace BizHawk.Bizware.BizwareGL.Drivers.OpenTK unsafe void IGL.BindVertexLayout(VertexLayout layout) { - StatePendingVertexLayouts[CurrentContext] = layout; + sStatePendingVertexLayout = layout; } unsafe void IGL.BindArrayData(void* pData) { - MyBindArrayData(StatePendingVertexLayouts[CurrentContext], pData); + MyBindArrayData(sStatePendingVertexLayout, pData); } void IGL.DrawArrays(PrimitiveType mode, int first, int count) @@ -278,9 +279,13 @@ namespace BizHawk.Bizware.BizwareGL.Drivers.OpenTK void IGL.SetPipelineUniformSampler(PipelineUniform uniform, IntPtr texHandle) { //set the sampler index into the uniform first - //GL.Uniform1(uniform.Id.ToInt32(), uniform.SamplerIndex); //now bind the texture - GL.ActiveTexture((TextureUnit)((int)TextureUnit.Texture0 + uniform.SamplerIndex)); + if(sActiveTexture != uniform.SamplerIndex) + { + sActiveTexture = uniform.SamplerIndex; + var selectedUnit = (TextureUnit)((int)TextureUnit.Texture0 + uniform.SamplerIndex); + GL.ActiveTexture(selectedUnit); + } GL.BindTexture(TextureTarget.Texture2D, texHandle.ToInt32()); } @@ -437,8 +442,6 @@ namespace BizHawk.Bizware.BizwareGL.Drivers.OpenTK //--------------- //my utility methods - global::OpenTK.Graphics.IGraphicsContext CurrentContext { get { return global::OpenTK.Graphics.GraphicsContext.CurrentContext; } } - GLControl CastControl(swf.Control swfControl) { GLControl glc = swfControl as GLControl; @@ -471,16 +474,13 @@ namespace BizHawk.Bizware.BizwareGL.Drivers.OpenTK if(n==0) throw new InvalidOperationException("Error compiling shader (CompileShader)" + "\r\n\r\n" + resultLog); } - - Dictionary StateCurrentVertexLayouts = new Dictionary(); - Dictionary StatePendingVertexLayouts = new Dictionary(); - WorkingDictionary> VertexAttribEnables = new WorkingDictionary>(); + void UnbindVertexAttributes() { //HAMNUTS: //its not clear how many bindings we'll have to disable before we can enable the ones we need.. //so lets just disable the ones we remember we have bound - var currBindings = VertexAttribEnables[CurrentContext]; + var currBindings = sVertexAttribEnables; foreach (var index in currBindings) GL.DisableVertexAttribArray(index); currBindings.Clear(); @@ -491,8 +491,8 @@ namespace BizHawk.Bizware.BizwareGL.Drivers.OpenTK UnbindVertexAttributes(); //HAMNUTS (continued) - var currBindings = VertexAttribEnables[CurrentContext]; - StateCurrentVertexLayouts[CurrentContext] = StatePendingVertexLayouts[CurrentContext]; + var currBindings = sVertexAttribEnables; + sStateCurrentVertexLayout = sStatePendingVertexLayout; foreach (var kvp in layout.Items) { @@ -504,28 +504,15 @@ namespace BizHawk.Bizware.BizwareGL.Drivers.OpenTK internal void MakeDefaultCurrent() { - MakeContextCurrent(this.GraphicsContext, OffscreenNativeWindow.WindowInfo); + MakeContextCurrent(this.GraphicsContext,OffscreenNativeWindow.WindowInfo); } internal void MakeContextCurrent(IGraphicsContext context, global::OpenTK.Platform.IWindowInfo windowInfo) { - //TODO - if we're churning through contexts quickly, this will sort of be a memory leak, since they'll be memoized forever in here - //maybe make it a weakptr or something - - //dont do anything if we're already current - IGraphicsContext currentForThread = null; - if (ThreadsForContexts.TryGetValue(Thread.CurrentThread, out currentForThread)) - if (currentForThread == context) - return; - - NativeWindowsForContexts[context] = windowInfo; context.MakeCurrent(windowInfo); - ThreadsForContexts[Thread.CurrentThread] = context; + PurgeStateCache(); } - Dictionary ThreadsForContexts = new Dictionary(); - Dictionary NativeWindowsForContexts = new Dictionary(); - void CreateRenderStates() { _rsBlendNone = new MyBlendState(false, BlendingFactorSrc.One, BlendEquationMode.FuncAdd, BlendingFactorDest.Zero, BlendingFactorSrc.One, BlendEquationMode.FuncAdd, BlendingFactorDest.Zero); @@ -535,6 +522,19 @@ namespace BizHawk.Bizware.BizwareGL.Drivers.OpenTK } MyBlendState _rsBlendNone, _rsBlendNormal; + + //state caches + int sActiveTexture; + VertexLayout sStateCurrentVertexLayout; + VertexLayout sStatePendingVertexLayout; + HashSet sVertexAttribEnables = new HashSet(); + void PurgeStateCache() + { + sStateCurrentVertexLayout = null; + sStatePendingVertexLayout = null; + sVertexAttribEnables.Clear(); + sActiveTexture = -1; + } } //class IGL_TK diff --git a/Bizware/BizHawk.Bizware.BizwareGL/IGL.cs b/Bizware/BizHawk.Bizware.BizwareGL/IGL.cs index 5b9d096cfb..dd277db5a8 100644 --- a/Bizware/BizHawk.Bizware.BizwareGL/IGL.cs +++ b/Bizware/BizHawk.Bizware.BizwareGL/IGL.cs @@ -14,6 +14,8 @@ namespace BizHawk.Bizware.BizwareGL /// And possibly, quite possibly, Direct3d.. even though none of your shaders would work. (could use nvidia CG, native dlls in necessary since this would only be for windows) /// TODO - This really needs to be split up into an internal and a user interface. so many of the functions are made to support the smart wrappers /// Maybe make a method that returns an interface used for advanced methods (and IGL_TK could implement that as well and just "return this:") + /// + /// NOTE: THIS SHOULD NOT BE ASSUMED TO BE THREAD SAFE! Make a new IGL if you want to use it in a new thread. I hope that will work... /// public interface IGL : IDisposable {