Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for WebGL2 #13367

Open
DonIsaac opened this issue Feb 11, 2022 · 14 comments · May be fixed by #15136
Open

Add support for WebGL2 #13367

DonIsaac opened this issue Feb 11, 2022 · 14 comments · May be fixed by #15136

Comments

@DonIsaac
Copy link

The Problem

The primary issue I'm encountering is not being able to use anti-aliasing render buffers. These would not be available without some other code changes, but the first step in this process is exposing access to WebGL2.

The secondary benefit is being able to pass uniforms all at once using a uniform block. While a large number of use cases would not see a significant performance boost, applications using a large number of uniforms and rendering at 60fps would.

Additionally WebGL2 adds a bunch of new features which would be nice to have.

Potential Solutions

A basic, backwards-compatible solution would be to add a feature flag, via a property on the existing options object, when creating a WebGL layer. By default, WebGL2 uses GLSL 3.0 (whereas WebGL uses GLSL2.1), which has a large number of breaking changes. This is fine when developing a new application where you are writing your own shaders, but problematic for both existing applications and the shader builder code. The shader builder could be adapted to handle WebGL2, but that would be a much larger amount of work.

@tschaub
Copy link
Member

tschaub commented Feb 12, 2022

Can you describe a bit more about what you are trying to do?

@DonIsaac
Copy link
Author

For my specific use case, I'm making a map displaying a high number of pins on a massive display, specifically 9 4K screens in a 3x3 grid. Due to the size and resolution of this map, aliasing effects are highly visible. Pin images are either a SVG or a PNG (the difference isn't relevant as aliasing is visible on both). Pins are rendered and animated with custom shaders using a WebGLPointsRenderer.

@ahocevar
Copy link
Member

As an alternative, you could try setting a higher pixelRatio on your map, e.g.

new Map({
  // ...
  pixelRatio: devicePixelRatio * 2
});

Maybe the result will be a canvas that is too big for the browser to handle, but at least it's worth a try.

@DonIsaac
Copy link
Author

This solution did work, at the expense of frame rate. However, the frame rate is still good enough for this to work for me, thank you!

All that being said, I still think it would be beneficial to add WebGL2 support. Starting to do so would be a simple change to the getContext function in src/ol/webgl.js. One possible implementation could be

// src/ol/webgl.js (CONTEXT_IDS declared on line 86)
/**
 * @const
 * @type {Array<string>}
 */
const CONTEXT_IDS = ['experimental-webgl', 'webgl', 'webkit-3d', 'moz-webgl'];

/**
 * @param {HTMLCanvasElement} canvas Canvas.
 * @param {Object} [opt_attributes] Attributes.
 * @param {boolean} [request_webgl2] Attempt to get a {@link WebGL2RenderingContext}, falling back to WebGL1. Defaults to `false`.
 * @return {WebGLRenderingContext | WebGL2RenderingContext} WebGL rendering context. if `request_webgl2` is `true` and WebGL2 is available, a {@link WebGL2RenderingContext} is returned.
 */
export function getContext(canvas, opt_attributes, request_webgl2 = false) {
  const attributes = assign({preserveDrawingBuffer: true}, opt_attributes);
  const contextsToTry = request_webgl2 ? ['webgl2', ...CONTEXT_IDS] : CONTEXT_IDS
  for (const id of contextsToTry) {
    try {
      const context = canvas.getContext(id, attributes);
      if (context) {
        return /** @type {!WebGLRenderingContext} */ (context);
      }
    } catch (e) {
      // pass
    }
  }
  return null;
}

@stale
Copy link

stale bot commented Apr 17, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 17, 2022
@DonIsaac
Copy link
Author

Any further updates or thoughts on WebGL2?

@stale stale bot removed the stale label Apr 21, 2022
@jahow
Copy link
Contributor

jahow commented Apr 22, 2022

I think implementing Webgl2 support in a way that allows us to really benefit from it would be a lot of work, and IMO not really worth the energy. I would personally hold my breath for WebGPU on this one 🙂

@birkskyum
Copy link

birkskyum commented Oct 25, 2023

In MapLibre support for using a webgl2 context was added not too long ago, with a fallback to webgl1. The shaders weren't changed, so they're as they were with webgl1, so it's alone the shift to webgl2 context that solved many issues - and that work was mainly a matter of conditionally setting the extensions. It's also a good stepping stone towards webgpu, because webgl2 allow for some refactoring of the rendering pipeline that can help prepare. Here are the main changes that were made.

At the time of implementation in maplibre webgl2 had 92% browser support, according to caniuse, but it's a ~94% now and might soon reach a level where the fallback won't be needed. It'll take years before webgpu has ubiquitous browser support like that (i.e. the shader language is still a draft), so it might be possible to get some benefits of webgl2 in the meantime and as a result ease the eventual move to webgpu.

@tschaub
Copy link
Member

tschaub commented Oct 25, 2023

See #15136 for a pull request migrating to WebGL 2.

@tschaub tschaub linked a pull request Oct 25, 2023 that will close this issue
@mike-000
Copy link
Contributor

webgl2 had 92% browser support, according to caniuse, but it's a ~94%

Don't confuse browser support with device support. Different browsers will block different older graphics drivers due to security issues. In Chrome and Edge on Windows 10 this may be fixed by changing ANGLE backend from D3D11 to D3D11on12 in about:flags but all graphics performance including 2d may be degraded as a result.

@birkskyum
Copy link

birkskyum commented Oct 26, 2023

@mike-000 , thanks for pointing that out. As I understand caniuse is browser support, and browser version usage. The impact on the OS or device settings - is there any way to track that, or is that just a hidden number? We can see sometimes that users get errors when they try to create a webgl2 context, for whatever reason, and that might be contributed to some issues with device support issues rather than browser support. Basically, if I understand you correctly, the numbers from caniuse should be seen as a best-case scenario, but the realistic support percentage, accounting for the device issues, is lower, and it's necessary to run tests on my sites to tell exactly how much.

@mike-000
Copy link
Contributor

I have two unrelated ex-Windows 7 systems with Intel graphics running Windows 10 which have this issue, which I suspect is not unusual. Intel HD Graphics driver dated 2016 which both Windows and Intel report as being up to date. The flags fix works but significantly degrades performance. Firefox is not affected. Also no problems with ex-Windows 8 system with AMD graphics. All will be retired in 2 years when Windows 10 support ends.

@tschaub
Copy link
Member

tschaub commented Oct 26, 2023

It is overstating the problem to say that all Windows 10 users are unable to use WebGL 2, right?

We also shouldn't confuse Microsoft's commitment to providing security patches for its operating systems with this library's requirements.

It looks like https://web3dsurvey.com/webgl2 is an effort to collect statistics on WebGL 2 support. Currently showing 96.9% for Windows. I haven't checked what goes into that number, but I think there is sense in looking into survey tools like this. Looks like their collector script is included on https://threejs.org/ and a number of other sites.

@mike-000
Copy link
Contributor

It is overstating the problem to say that all Windows 10 users are unable to use WebGL 2, right?

Yes, I think it is Chromium based browsers combined with Windows 7 era Intel graphics causing the issue, not the Windows platform, otherwise Firefox would also be affected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants