From 28d07be7f4605ab260248d92d169e8b00c9f8bf2 Mon Sep 17 00:00:00 2001 From: zhujie Date: Tue, 13 Jun 2023 16:10:16 +0800 Subject: [PATCH] version upgrade. --- OpenTkWPFHost/Control/BitmapOpenTkControl.cs | 72 +++++++++++--------- OpenTkWPFHost/OpenTkWPFHost.csproj | 6 +- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/OpenTkWPFHost/Control/BitmapOpenTkControl.cs b/OpenTkWPFHost/Control/BitmapOpenTkControl.cs index c5434c4..7263437 100644 --- a/OpenTkWPFHost/Control/BitmapOpenTkControl.cs +++ b/OpenTkWPFHost/Control/BitmapOpenTkControl.cs @@ -60,6 +60,7 @@ public Brush FpsBrush public BitmapOpenTkControl() : base() { + this.SizeChanged += ThreadOpenTkControl_SizeChanged; } protected override void OnLoaded(object sender, RoutedEventArgs args) @@ -99,6 +100,7 @@ private void CompositionTarget_Rendering(object sender, EventArgs e) this.InvalidateVisual(); } + private void ThreadOpenTkControl_SizeChanged(object sender, SizeChangedEventArgs e) { _recentTarget = this.RenderSetting.CreateRenderTarget(this); @@ -240,7 +242,6 @@ private Pipeline BuildPipeline(TaskScheduler glContextTaskSche return new Pipeline(renderBlock, canvasBlock); } - private RenderSetting _workingRenderSetting; private MultiBitmapCanvas _renderCanvas; @@ -251,9 +252,43 @@ protected override void StartRender() _renderTokenSource = new CancellationTokenSource(); var renderer = this.Renderer; var glSettings = this.GlSettings; - _workingRenderSetting = this.RenderSetting; + var workingRenderSetting = this.RenderSetting; + Initialize(workingRenderSetting); + _recentTarget = workingRenderSetting.CreateRenderTarget(this); + _renderCanvas = new MultiBitmapCanvas((int)(_maxParallelism * 3)); + _multiStoragePixelBuffer = new MultiPixelBuffer(_maxParallelism * 3); + var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); + _mainContextWrapper = glSettings.NewContext(); + _mainContextWrapper.MakeCurrent(); + var samples = glSettings.MSAASamples; + if (samples > 1) + { + this._frameBuffer = new OffScreenMSAAFrameBuffer(samples); + GL.Enable(EnableCap.Multisample); + } + else + { + this._frameBuffer = new SimpleFrameBuffer(); + } + + GL.Enable(EnableCap.DebugOutput); + GL.DebugMessageCallback(DebugProc, IntPtr.Zero); + _taskScheduler = new GLTaskScheduler(_mainContextWrapper, DebugProc); + var pipeline = BuildPipeline(_taskScheduler, scheduler); + _renderTask = Task.Run(async () => + { + using (_renderTokenSource) + { + await RenderThread(_renderTokenSource.Token, _mainContextWrapper, pipeline, renderer, + _renderSemaphoreEnable); + } + }); + } + + private void Initialize(RenderSetting workingRenderSetting) + { var parallelismMax = (uint)Environment.ProcessorCount / 2; //hyper threading or P-E cores? - switch (_workingRenderSetting.RenderTactic) + switch (workingRenderSetting.RenderTactic) { case RenderTactic.Default: _isInternalTrigger = false; @@ -294,37 +329,6 @@ protected override void StartRender() default: throw new ArgumentOutOfRangeException(); } - - _recentTarget = _workingRenderSetting.CreateRenderTarget(this); - _renderCanvas = new MultiBitmapCanvas((int)(_maxParallelism * 3)); - _multiStoragePixelBuffer = new MultiPixelBuffer(_maxParallelism * 3); - this.SizeChanged += ThreadOpenTkControl_SizeChanged; - var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); - _mainContextWrapper = glSettings.NewContext(); - _mainContextWrapper.MakeCurrent(); - var samples = glSettings.MSAASamples; - if (samples > 1) - { - this._frameBuffer = new OffScreenMSAAFrameBuffer(samples); - GL.Enable(EnableCap.Multisample); - } - else - { - this._frameBuffer = new SimpleFrameBuffer(); - } - - GL.Enable(EnableCap.DebugOutput); - GL.DebugMessageCallback(DebugProc, IntPtr.Zero); - _taskScheduler = new GLTaskScheduler(_mainContextWrapper, DebugProc); - var pipeline = BuildPipeline(_taskScheduler, scheduler); - _renderTask = Task.Run(async () => - { - using (_renderTokenSource) - { - await RenderThread(_renderTokenSource.Token, _mainContextWrapper, pipeline, renderer, - _renderSemaphoreEnable); - } - }); } private GLTaskScheduler _taskScheduler; diff --git a/OpenTkWPFHost/OpenTkWPFHost.csproj b/OpenTkWPFHost/OpenTkWPFHost.csproj index f11772f..66deddd 100644 --- a/OpenTkWPFHost/OpenTkWPFHost.csproj +++ b/OpenTkWPFHost/OpenTkWPFHost.csproj @@ -7,9 +7,9 @@ OpenTK WPF host control Copyright © 2021 true - 4.0.0.1 - 4.0.0.1 - 4.0.0.1 + 4.1.0.0 + 4.1.0.0 + 4.1.0.0 true bin\$(Configuration)\ Karl Zhu