question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use two GlControls in the same form

See original GitHub issue

Step to reproduce:

  • Open HelloTriangle Project
  • Open SampleForm
  • Select RenderControl, set Dock to None
  • Copy and paste GlControl somewhere else in Form
  • Build and Run

On my system the original control is solid white and the second one is black. Nothing else is shown. Property ContextSharing is set to OwnContext for both controls.

I was not able to find what is causing the issue in the code. Here is what I have found:

  • Setting the Animation to false for the second control fix the issue
  • Resizing the window to mask the second control and show only the first one fix the issue!!
  • Turning off animation for both controls and adding a timer that invalidates both controls works

A possible cause is the Invalidate you use inside OnPaint() to animate.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
luca-piccionicommented, Oct 1, 2017

Each GlControl creates its own GL context by default. The function pointers relative to the underlying context may depends on the device context which the GL context is bound to. I say “may” because this is a Windows specific issue: if the two GL contexts are bound to different device contexts, they can share GL function pointers only if all device contexts have the same pixel format set; in the other case, function pointers must be reloaded.

Being said this, OpenGL.Net ignore the pixel format set on the GlControl instances; this means that DeviceContext.MakeCurrent will reload all function pointers anyway, causing sensible delays, each time a different GlControl is updated. With a single GlControl instance, a single GL context is current on the thread. DeviceContext.MakeCurrent optimize this case, avoiding function pointers reloading if the GL context made current does not change. Remember that GL function pointer have are scoped to TLS.

Indeed, having more than one GlControl instance cause updating all those, and because they have different device contexts, function pointers are continuously reloaded. This is noticeable applying animation to two GlControl instances and apply the patch of the previous post: animations are no more fluid as the simple example HelloTriangle.

I’ve not profiled it, but I bet that Gl.BindAPI() is the bottleneck: it uses reflection to query and set function pointers. Maybe it could be optimized by emitting optimized methods.

0reactions
luca-piccionicommented, Oct 9, 2017

Indeed my experiments lead to some awkward conclusion: Invalidate() executed by OnPaint() causes a synchronous refresh of the UserControl, leading to other control to the previous contents. Calling Invalidate() on other contexts (i.e. in a timer callback) works correctly.

Invalidate() invokes UnsafeNativeMethods.InvalidateRect, which points to WIN32 InvalidateRect; the man says:

The system sends a WM_PAINT message to a window whenever its update region is not empty and there are no other messages in the application queue for that window.

I wonder why the message is processed “immediately”. If the Animation property is set on the last GlControl within the form, both controls can be redrawn, with a resize operation, while animating; but invalidating the first controls will lead to the same behavior (only the invalidated control will be redrawn).

I tried to invoke asynchronously Invalidate() using BeginInvoke without luck. At the moment I’m out of ideas.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Two submit buttons in one form
The best way to deal with multiple submit buttons is using a switch case in the server script <form action="demo_form.php" method="get"> ...
Read more >
Is it possible to have two buttons with the same ID? - Discussion
I am wondering is it possible to have the same button on a form but in two different locations? I want to have...
Read more >
Why can't I select form and ActiveX controls?
For a Form control, click the control and make sure that the control border is displayed. Control border. For an ActiveX control: If...
Read more >
Can not move my buttons in forms - Microsoft Q&A
i am learning C# and i got to microsoft forms. so in my book it tells me to create a "TabControl" and add...
Read more >
Power App Mutli Screen Form Controls - YouTube
PowerApps #PowerAppsForms In this video I show how you can break out a form control over multiple screens. This helps provide a better...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found