From 5e28f316603907b66592730fcff93fc9adf057f5 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Mon, 3 Sep 2007 21:47:34 +0000 Subject: [PATCH] Improved GL loading speed. Added background worker thread to W03_Extensions. Added debug output to failed WinGLContext.MakeCurrent() calls. Removed object field from Windows.MSG struct. --- .../WinForms/W03_Extensions.Designer.cs | 9 +++ Source/Examples/WinForms/W03_Extensions.cs | 66 ++++++++++++------- Source/OpenTK/GLControl.cs | 1 + Source/OpenTK/OpenGL/GLHelper.cs | 10 ++- Source/OpenTK/Platform/Windows/API.cs | 2 +- .../OpenTK/Platform/Windows/WinGLContext.cs | 5 +- 6 files changed, 65 insertions(+), 28 deletions(-) diff --git a/Source/Examples/WinForms/W03_Extensions.Designer.cs b/Source/Examples/WinForms/W03_Extensions.Designer.cs index 939c09f5..d3bdb4de 100644 --- a/Source/Examples/WinForms/W03_Extensions.Designer.cs +++ b/Source/Examples/WinForms/W03_Extensions.Designer.cs @@ -29,6 +29,7 @@ private void InitializeComponent() { this.listBox1 = new System.Windows.Forms.ListBox(); + this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); this.SuspendLayout(); // // listBox1 @@ -40,6 +41,13 @@ this.listBox1.Size = new System.Drawing.Size(284, 264); this.listBox1.TabIndex = 0; // + // backgroundWorker1 + // + this.backgroundWorker1.WorkerReportsProgress = true; + this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork); + this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted); + this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged); + // // W03_Extensions // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -55,5 +63,6 @@ #endregion private System.Windows.Forms.ListBox listBox1; + private System.ComponentModel.BackgroundWorker backgroundWorker1; } } \ No newline at end of file diff --git a/Source/Examples/WinForms/W03_Extensions.cs b/Source/Examples/WinForms/W03_Extensions.cs index 543eb725..53c0ef9a 100644 --- a/Source/Examples/WinForms/W03_Extensions.cs +++ b/Source/Examples/WinForms/W03_Extensions.cs @@ -26,6 +26,8 @@ namespace Examples.WinForms Type glClass; Type delegatesClass; Type importsClass; + int supported, all; // Number of supported extensions. + string driver; public W03_Extensions() { @@ -43,56 +45,72 @@ namespace Examples.WinForms glControl.CreateContext(); - //listBox1.BeginInvoke(new LoadExtensionsDelegate(LoadExtensions)); - ThreadPool.QueueUserWorkItem(new WaitCallback(LoadExtensions)); + driver = + GL.GetString(GL.Enums.StringName.VENDOR) + " " + + GL.GetString(GL.Enums.StringName.RENDERER) + " " + + GL.GetString(GL.Enums.StringName.VERSION); + + all = delegatesClass.GetFields(BindingFlags.Static | BindingFlags.NonPublic).Length; + this.Text = String.Format("Loading {0} functions...", all); + + this.backgroundWorker1.RunWorkerAsync(); } - delegate void LoadExtensionsDelegate(object data); - - void LoadExtensions(object data) + private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { - glControl.MakeCurrent(); - FieldInfo[] v = delegatesClass.GetFields(BindingFlags.Static | BindingFlags.NonPublic); - - int i = 0, supported = 0; - + int i = 0; + try { foreach (FieldInfo f in v) { - Delegate d = GL.GetDelegate(f.Name, f.FieldType); - - f.SetValue(null, d); - listBox1.Items.Add(String.Format("{0}/{1} {2}: {3}", - (++i).ToString(), v.Length, d != null ? "ok" : "failed", f.Name)); - - listBox1.Update(); - - //Thread.Sleep(1); - + object d = f.GetValue(delegatesClass); if (d != null) { ++supported; } - } - //this.Text = String.Format("Supported extensions: {0}", supported); + backgroundWorker1.ReportProgress((int)(((float)i / all) * 100.0f), + String.Format("{0}/{1} {2}: {3}", (++i).ToString(), all, d != null ? "ok" : "failed", f.Name)); + } } catch (Exception expt) { - MessageBox.Show("An error occured while loading extensions", "Extension loading failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + MessageBox.Show(expt.ToString(), "An error occured while loading extensions", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); throw; } } + private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) + { + listBox1.Items.Add(e.UserState as string); + /* + if ((e.UserState as string).Contains("failed")) + { + Graphics c = listBox1.CreateGraphics(); + c. + c.DrawRectangle(new Pen(Color.Gray), listBox1.GetItemRectangle(listBox1.Items.Count - 1)); + c.Dispose(); + } + else + { + } + */ + } #region IExample Members public void Launch() { - + } #endregion + + private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) + { + this.Text = String.Format("{0}: {1}/{2} OpenGL functions supported.", + driver, supported, all); + } } } \ No newline at end of file diff --git a/Source/OpenTK/GLControl.cs b/Source/OpenTK/GLControl.cs index 8998729f..3869c4dc 100644 --- a/Source/OpenTK/GLControl.cs +++ b/Source/OpenTK/GLControl.cs @@ -100,6 +100,7 @@ namespace OpenTK } this.Visible = true; + this.CreateControl(); GL.LoadAll(); Glu.LoadAll(); diff --git a/Source/OpenTK/OpenGL/GLHelper.cs b/Source/OpenTK/OpenGL/GLHelper.cs index 31cf4942..b2742f3b 100644 --- a/Source/OpenTK/OpenGL/GLHelper.cs +++ b/Source/OpenTK/OpenGL/GLHelper.cs @@ -202,10 +202,12 @@ namespace OpenTK.OpenGL /// public static Delegate GetDelegate(string name, Type signature) { - MethodInfo m = importsClass.GetMethod(name.Substring(2), BindingFlags.Static | BindingFlags.NonPublic); + //MethodInfo m = importsClass.GetMethod(name.Substring(2), BindingFlags.Static | BindingFlags.NonPublic); + MethodInfo m; return Utilities.GetExtensionDelegate(name, signature) ?? - (m != null ? Delegate.CreateDelegate(signature, m) : null); + ((m = importsClass.GetMethod(name.Substring(2), BindingFlags.Static | BindingFlags.NonPublic)) != null ? + Delegate.CreateDelegate(signature, m) : null); } #endregion @@ -245,6 +247,10 @@ namespace OpenTK.OpenGL } f.SetValue(null, d); + + //Type type = f.ReflectedType; + //TypedReference t = __makeref(type); + //f.SetValueDirect(t, d); } time.Stop(); diff --git a/Source/OpenTK/Platform/Windows/API.cs b/Source/OpenTK/Platform/Windows/API.cs index dae4fb46..22b77b78 100644 --- a/Source/OpenTK/Platform/Windows/API.cs +++ b/Source/OpenTK/Platform/Windows/API.cs @@ -2977,7 +2977,7 @@ namespace OpenTK.Platform.Windows public IntPtr LParam; public uint Time; public POINT Point; - public object RefObject; + //public object RefObject; public override string ToString() { diff --git a/Source/OpenTK/Platform/Windows/WinGLContext.cs b/Source/OpenTK/Platform/Windows/WinGLContext.cs index 05dc3537..58af5fbd 100644 --- a/Source/OpenTK/Platform/Windows/WinGLContext.cs +++ b/Source/OpenTK/Platform/Windows/WinGLContext.cs @@ -194,7 +194,10 @@ namespace OpenTK.Platform.Windows public void MakeCurrent() { - Wgl.Imports.MakeCurrent(deviceContext, renderContext); + if (!Wgl.Imports.MakeCurrent(deviceContext, renderContext)) + { + Debug.Print("WinGLContext.MakeCurrent() call failed. Error: {0}", Marshal.GetLastWin32Error()); + } } #endregion