Merge pull request #9896 from jahow/fix-webgl-warnings

Misc. WebGL fixes
This commit is contained in:
Olivier Guyot
2019-08-31 14:57:22 +02:00
committed by GitHub
5 changed files with 47 additions and 6 deletions
+5 -1
View File
@@ -17,7 +17,7 @@ const vectorSource = new Vector({
attributions: 'National UFO Reporting Center' attributions: 'National UFO Reporting Center'
}); });
const texture = document.createElement('img'); const texture = new Image();
texture.src = 'data/ufo_shapes.png'; texture.src = 'data/ufo_shapes.png';
// This describes the content of the associated sprite sheet // This describes the content of the associated sprite sheet
@@ -136,3 +136,7 @@ map.on('pointermove', function(evt) {
info.innerText = 'On ' + datetime + ', lasted ' + duration + ' seconds and had a "' + shape + '" shape.'; info.innerText = 'On ' + datetime + ', lasted ' + duration + ' seconds and had a "' + shape + '" shape.';
}); });
}); });
texture.addEventListener('load', function() {
map.render();
});
+11 -3
View File
@@ -245,7 +245,7 @@ class WebGLPointsLayerRenderer extends WebGLLayerRenderer {
); );
this.hitProgram_ = this.helper.getProgram( this.hitProgram_ = this.helper.getProgram(
HIT_FRAGMENT_SHADER, HIT_FRAGMENT_SHADER,
options.vertexShader || VERTEX_SHADER VERTEX_SHADER
); );
this.sizeCallback_ = options.sizeCallback || function() { this.sizeCallback_ = options.sizeCallback || function() {
@@ -336,6 +336,8 @@ class WebGLPointsLayerRenderer extends WebGLLayerRenderer {
} else { } else {
this.renderInstructions_ = new Float32Array(event.data.renderInstructions); this.renderInstructions_ = new Float32Array(event.data.renderInstructions);
} }
this.getLayer().changed();
} }
}.bind(this)); }.bind(this));
} }
@@ -344,7 +346,7 @@ class WebGLPointsLayerRenderer extends WebGLLayerRenderer {
* @inheritDoc * @inheritDoc
*/ */
renderFrame(frameState) { renderFrame(frameState) {
const renderCount = this.indicesBuffer_.getArray() ? this.indicesBuffer_.getArray().length : 0; const renderCount = this.indicesBuffer_.getSize();
this.helper.drawElements(0, renderCount); this.helper.drawElements(0, renderCount);
this.helper.finalizeDraw(frameState); this.helper.finalizeDraw(frameState);
const canvas = this.helper.getCanvas(); const canvas = this.helper.getCanvas();
@@ -544,6 +546,12 @@ class WebGLPointsLayerRenderer extends WebGLLayerRenderer {
* @param {import("../../PluggableMap.js").FrameState} frameState current frame state * @param {import("../../PluggableMap.js").FrameState} frameState current frame state
*/ */
renderHitDetection(frameState) { renderHitDetection(frameState) {
// skip render entirely if vertices buffers for display & hit detection have different sizes
// this typically means both buffers are temporarily out of sync
if (this.hitVerticesBuffer_.getSize() !== this.verticesBuffer_.getSize()) {
return;
}
this.hitRenderTarget_.setSize(frameState.size); this.hitRenderTarget_.setSize(frameState.size);
this.helper.useProgram(this.hitProgram_); this.helper.useProgram(this.hitProgram_);
@@ -561,7 +569,7 @@ class WebGLPointsLayerRenderer extends WebGLLayerRenderer {
this.helper.enableAttributeArray(DefaultAttrib.ROTATE_WITH_VIEW, 1, FLOAT, bytesPerFloat * stride, bytesPerFloat * 7); this.helper.enableAttributeArray(DefaultAttrib.ROTATE_WITH_VIEW, 1, FLOAT, bytesPerFloat * stride, bytesPerFloat * 7);
this.helper.enableAttributeArray(DefaultAttrib.COLOR, 4, FLOAT, bytesPerFloat * stride, bytesPerFloat * 8); this.helper.enableAttributeArray(DefaultAttrib.COLOR, 4, FLOAT, bytesPerFloat * stride, bytesPerFloat * 8);
const renderCount = this.indicesBuffer_.getArray() ? this.indicesBuffer_.getArray().length : 0; const renderCount = this.indicesBuffer_.getSize();
this.helper.drawElements(0, renderCount); this.helper.drawElements(0, renderCount);
} }
} }
+9
View File
@@ -95,6 +95,7 @@ class WebGLArrayBuffer {
} }
/** /**
* Will return null if the buffer was not initialized
* @return {Float32Array|Uint32Array} Array. * @return {Float32Array|Uint32Array} Array.
*/ */
getArray() { getArray() {
@@ -107,6 +108,14 @@ class WebGLArrayBuffer {
getUsage() { getUsage() {
return this.usage; return this.usage;
} }
/**
* Will return 0 if the buffer is not initialized
* @return {number} Array size
*/
getSize() {
return this.array ? this.array.length : 0;
}
} }
/** /**
+5 -2
View File
@@ -539,7 +539,6 @@ class WebGLHelper extends Disposable {
let textureSlot = 0; let textureSlot = 0;
this.uniforms_.forEach(function(uniform) { this.uniforms_.forEach(function(uniform) {
value = typeof uniform.value === 'function' ? uniform.value(frameState) : uniform.value; value = typeof uniform.value === 'function' ? uniform.value(frameState) : uniform.value;
// apply value based on type // apply value based on type
if (value instanceof HTMLCanvasElement || value instanceof HTMLImageElement || value instanceof ImageData) { if (value instanceof HTMLCanvasElement || value instanceof HTMLImageElement || value instanceof ImageData) {
// create a texture & put data // create a texture & put data
@@ -551,7 +550,11 @@ class WebGLHelper extends Disposable {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, value);
const imageReady = !(value instanceof HTMLImageElement) || /** @type {HTMLImageElement} */(value).complete;
if (imageReady) {
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, value);
}
// fill texture slots by increasing index // fill texture slots by increasing index
gl.uniform1i(this.getUniformLocation(uniform.name), textureSlot++); gl.uniform1i(this.getUniformLocation(uniform.name), textureSlot++);
+17
View File
@@ -75,4 +75,21 @@ describe('ol.webgl.Buffer', function() {
}); });
describe('#getSize', function() {
let b;
beforeEach(function() {
b = new WebGLArrayBuffer(ARRAY_BUFFER);
});
it('returns 0 when the buffer array is not initialized', function() {
expect(b.getSize()).to.be(0);
});
it('returns the size of the array otherwise', function() {
b.ofSize(12);
expect(b.getSize()).to.be(12);
});
});
}); });