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

Update per mdn webgl recommendations #9474

Merged
merged 2 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/gl/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Context {
extTextureFilterAnisotropic: any;
extTextureFilterAnisotropicMax: any;
extTextureHalfFloat: any;
extRenderToTextureHalfFloat: any;
extTimerQuery: any;

constructor(gl: WebGLRenderingContext) {
Expand Down Expand Up @@ -113,6 +114,7 @@ class Context {
this.extTextureHalfFloat = gl.getExtension('OES_texture_half_float');
if (this.extTextureHalfFloat) {
gl.getExtension('OES_texture_half_float_linear');
this.extRenderToTextureHalfFloat = gl.getExtension('EXT_color_buffer_half_float');
}

this.extTimerQuery = gl.getExtension('EXT_disjoint_timer_query');
Expand Down
13 changes: 3 additions & 10 deletions src/render/draw_heatmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,10 @@ function bindFramebuffer(context, painter, layer) {
function bindTextureToFramebuffer(context, painter, texture, fbo) {
const gl = context.gl;
// Use the higher precision half-float texture where available (producing much smoother looking heatmaps);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, painter.width / 4, painter.height / 4, 0, gl.RGBA,
context.extTextureHalfFloat ? context.extTextureHalfFloat.HALF_FLOAT_OES : gl.UNSIGNED_BYTE, null);

// Otherwise, fall back to a low precision texture
const internalFormat = context.extRenderToTextureHalfFloat ? context.extTextureHalfFloat.HALF_FLOAT_OES : gl.UNSIGNED_BYTE;
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, painter.width / 4, painter.height / 4, 0, gl.RGBA, internalFormat, null);
fbo.colorAttachment.set(texture);

// If using half-float texture as a render target is not supported, fall back to a low precision texture
if (context.extTextureHalfFloat && gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE) {
context.extTextureHalfFloat = null;
fbo.colorAttachment.setDirty();
bindTextureToFramebuffer(context, painter, texture, fbo);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why it was implemented this way is because of this excerpt from the EXT_color_buffer_half_float spec:

Although it is expected that all implementations supporting this extension support rendering to at least one HALF_FLOAT_OES format, applications must check framebuffer completeness to determine which formats are supported.

So basically it says that the extension being enabled doesn't guarantee that writing to a half-float framebuffer will succeed. I don't know how often this situation happens in practice (maybe never?), but we should be aware of the risk of this PR breaking heatmaps for some users (where it won't fall back to low precision properly).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a recommendation for the desktop GL extension GL_EXT_color_buffer_half_float which gets exposed a bit differently for the webgl one.

For example, here are the thorough tests that chromium goes through before exposing it to the WebGL API, and does the completeness check. I have not seen similar suggestion in mdn or the webgl spec reference.

I tested this last friday on both a iphone 6 and mali-t760 (galaxy s3) which don't report supporting this extension, and fallback appropriately. I would be ok in leaving it as is, but with these concerns we could leave an assert probe and see whether it gets triggered until the beta completes?

}

function renderTextureToMap(painter, layer) {
Expand Down
3 changes: 3 additions & 0 deletions src/render/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class Program<Us: UniformBindings> {
gl.linkProgram(this.program);
assert(gl.getProgramParameter(this.program, gl.LINK_STATUS), (gl.getProgramInfoLog(this.program): any));

gl.deleteShader(vertexShader);
gl.deleteShader(fragmentShader);

this.numAttributes = gl.getProgramParameter(this.program, gl.ACTIVE_ATTRIBUTES);

this.attributes = {};
Expand Down
3 changes: 3 additions & 0 deletions test/release/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const pages = {
},
"mapbox-gl-rtl-text": {
"title": "Add support for right-to-left scripts"
},
"heatmap-layer": {
"title": "Add a heatmap layer"
}
};

Expand Down