Skip to content

Commit 4fe59fc

Browse files
authored
Merge pull request #53 from lootic/shaders-once-lazy-init
Improved performance addLine, suggestion #2
2 parents eb8c8bb + 5195b74 commit 4fe59fc

File tree

11 files changed

+153
-122
lines changed

11 files changed

+153
-122
lines changed

dist/WebglBaseLine.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { ColorRGBA } from "./ColorRGBA";
33
* Baseline class
44
*/
55
export declare class WebglBaseLine {
6+
private static readonly vertCode;
7+
private static readonly fragCode;
8+
private static program;
69
intensity: number;
710
visible: boolean;
811
/**
@@ -63,6 +66,8 @@ export declare class WebglBaseLine {
6366
* @internal
6467
*/
6568
_coord: number;
69+
initProgram(webgl: WebGLRenderingContext): void;
70+
private createProgram;
6671
/**
6772
* @internal
6873
*/

dist/WebglBaseLine.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/WebglBaseLine.js

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/WebglBaseLine.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/webglplot.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/webglplot.esm.js

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,40 @@ class WebglBaseLine {
2626
this.visible = true;
2727
this.intensity = 1;
2828
}
29+
initProgram(webgl) {
30+
if (!WebglBaseLine.program) {
31+
this.createProgram(webgl);
32+
}
33+
this._prog = WebglBaseLine.program;
34+
}
35+
createProgram(webgl) {
36+
const vertShader = webgl.createShader(webgl.VERTEX_SHADER);
37+
webgl.shaderSource(vertShader, WebglBaseLine.vertCode);
38+
webgl.compileShader(vertShader);
39+
const fragShader = webgl.createShader(webgl.FRAGMENT_SHADER);
40+
webgl.shaderSource(fragShader, WebglBaseLine.fragCode);
41+
webgl.compileShader(fragShader);
42+
WebglBaseLine.program = webgl.createProgram();
43+
webgl.attachShader(WebglBaseLine.program, vertShader);
44+
webgl.attachShader(WebglBaseLine.program, fragShader);
45+
webgl.linkProgram(WebglBaseLine.program);
46+
}
2947
}
48+
WebglBaseLine.vertCode = `
49+
attribute vec2 coordinates;
50+
uniform mat2 uscale;
51+
uniform vec2 uoffset;
52+
53+
void main(void) {
54+
gl_Position = vec4(uscale*coordinates + uoffset, 0.0, 1.0);
55+
}`;
56+
WebglBaseLine.fragCode = `
57+
precision mediump float;
58+
uniform highp vec4 uColor;
59+
60+
void main(void) {
61+
gl_FragColor = uColor;
62+
}`;
3063

3164
/**
3265
* The standard Line class
@@ -385,37 +418,10 @@ class WebGLPlot {
385418
* ```
386419
*/
387420
addLine(line) {
421+
line.initProgram(this.webgl);
388422
line._vbuffer = this.webgl.createBuffer();
389423
this.webgl.bindBuffer(this.webgl.ARRAY_BUFFER, line._vbuffer);
390424
this.webgl.bufferData(this.webgl.ARRAY_BUFFER, line.xy, this.webgl.STREAM_DRAW);
391-
const vertCode = `
392-
attribute vec2 coordinates;
393-
uniform mat2 uscale;
394-
uniform vec2 uoffset;
395-
396-
void main(void) {
397-
gl_Position = vec4(uscale*coordinates + uoffset, 0.0, 1.0);
398-
}`;
399-
// Create a vertex shader object
400-
const vertShader = this.webgl.createShader(this.webgl.VERTEX_SHADER);
401-
// Attach vertex shader source code
402-
this.webgl.shaderSource(vertShader, vertCode);
403-
// Compile the vertex shader
404-
this.webgl.compileShader(vertShader);
405-
// Fragment shader source code
406-
const fragCode = `
407-
precision mediump float;
408-
uniform highp vec4 uColor;
409-
void main(void) {
410-
gl_FragColor = uColor;
411-
}`;
412-
const fragShader = this.webgl.createShader(this.webgl.FRAGMENT_SHADER);
413-
this.webgl.shaderSource(fragShader, fragCode);
414-
this.webgl.compileShader(fragShader);
415-
line._prog = this.webgl.createProgram();
416-
this.webgl.attachShader(line._prog, vertShader);
417-
this.webgl.attachShader(line._prog, fragShader);
418-
this.webgl.linkProgram(line._prog);
419425
this.webgl.bindBuffer(this.webgl.ARRAY_BUFFER, line._vbuffer);
420426
line._coord = this.webgl.getAttribLocation(line._prog, "coordinates");
421427
this.webgl.vertexAttribPointer(line._coord, 2, this.webgl.FLOAT, false, 0, 0);

dist/webglplot.js

Lines changed: 1 addition & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)