Webgl worker / add tests, some typing and documentation

The worker currently works by receiving GENERATE_BUFFERS messages and
will send back the same kind of message, with the generated buffers
attached. All properties of the original message are kept, so that
when a GENERATE_BUFFERS message comes back to the main thread it
is possible to know what and how the buffers where generated.

This is typically used for the `projectionTransform` matrix, and
will also be necessary when working with tiles.
This commit is contained in:
Olivier Guyot
2019-05-18 15:40:27 +02:00
parent a366803cdd
commit e0983cb1c6
4 changed files with 99 additions and 21 deletions
+18 -11
View File
@@ -1,15 +1,21 @@
/**
* A worker that does cpu-heavy tasks related to webgl rendering.
* @module ol/worker/webgl
* A worker that does cpu-heavy tasks related to webgl rendering
*/
import {POINT_INSTRUCTIONS_COUNT, POINT_VERTEX_STRIDE, writePointFeatureToBuffers} from '../renderer/webgl/Layer.js';
import {
POINT_INSTRUCTIONS_COUNT,
POINT_VERTEX_STRIDE,
WebGLWorkerMessageType,
writePointFeatureToBuffers
} from '../renderer/webgl/Layer.js';
import {assign} from '../obj.js';
onmessage = event => {
if (event.data.type === 'generate-buffer') {
const renderInstructions = new Float32Array(event.data.renderInstructions);
const customAttributesCount = event.data.customAttributesCount || 0;
const received = event.data;
if (received.type === WebGLWorkerMessageType.GENERATE_BUFFERS) {
const renderInstructions = new Float32Array(received.renderInstructions);
const customAttributesCount = received.customAttributesCount || 0;
const instructionsCount = POINT_INSTRUCTIONS_COUNT + customAttributesCount;
const projectionTransform = event.data.projectionTransform;
const elementsCount = renderInstructions.length / instructionsCount;
const indexBuffer = new Uint32Array(elementsCount * 6);
@@ -26,13 +32,14 @@ onmessage = event => {
instructionsCount);
}
postMessage({
type: 'buffers-generated',
/** @type {import('../renderer/webgl/Layer').WebGLWorkerGenerateBuffersMessage} */
const message = assign({
vertexBuffer: vertexBuffer.buffer,
indexBuffer: indexBuffer.buffer,
renderInstructions: renderInstructions.buffer,
projectionTransform
}, [vertexBuffer.buffer, indexBuffer.buffer, renderInstructions.buffer]);
renderInstructions: renderInstructions.buffer
}, received);
postMessage(message, [vertexBuffer.buffer, indexBuffer.buffer, renderInstructions.buffer]);
}
};