From 4f726ddac2cd7f96852a6283770c8684897d9f85 Mon Sep 17 00:00:00 2001 From: schmelczerandras Date: Sat, 19 Sep 2020 17:31:03 +0200 Subject: [PATCH] Make descriptor access faster --- src/drawables/drawable.ts | 5 ++-- src/drawables/lights/circle-light.ts | 14 +++++------ src/drawables/lights/flashlight.ts | 24 +++++++++---------- src/drawables/shapes/circle.ts | 22 ++++++++--------- src/drawables/shapes/inverted-tunnel.ts | 22 ++++++++--------- src/drawables/shapes/tunnel.ts | 32 ++++++++++++------------- 6 files changed, 54 insertions(+), 65 deletions(-) diff --git a/src/drawables/drawable.ts b/src/drawables/drawable.ts index f6c7e21..300fc27 100644 --- a/src/drawables/drawable.ts +++ b/src/drawables/drawable.ts @@ -2,9 +2,8 @@ import { mat2d, vec2 } from 'gl-matrix'; import { DrawableDescriptor } from './drawable-descriptor'; export abstract class Drawable { - static get descriptor(): DrawableDescriptor { - throw new Error('This getter should be overriden'); - } + // This should be overriden by inherited classes + public static readonly descriptor: DrawableDescriptor; public abstract distance(target: vec2): number; diff --git a/src/drawables/lights/circle-light.ts b/src/drawables/lights/circle-light.ts index b74cad9..d2edff7 100644 --- a/src/drawables/lights/circle-light.ts +++ b/src/drawables/lights/circle-light.ts @@ -3,14 +3,12 @@ import { Drawable } from '../drawable'; import { DrawableDescriptor } from '../drawable-descriptor'; export class CircleLight extends Drawable { - public static get descriptor(): DrawableDescriptor { - return { - uniformName: 'circleLights', - uniformCountMacroName: 'CIRCLE_LIGHT_COUNT', - shaderCombinationSteps: [0, 1, 2, 4], - empty: new CircleLight(vec2.fromValues(0, 0), vec3.fromValues(0, 0, 0), 0), - }; - } + public static readonly descriptor: DrawableDescriptor = { + uniformName: 'circleLights', + uniformCountMacroName: 'CIRCLE_LIGHT_COUNT', + shaderCombinationSteps: [0, 1, 2, 4], + empty: new CircleLight(vec2.fromValues(0, 0), vec3.fromValues(0, 0, 0), 0), + }; constructor(public center: vec2, public color: vec3, public intensity: number) { super(); diff --git a/src/drawables/lights/flashlight.ts b/src/drawables/lights/flashlight.ts index bc8123d..7ddb1ef 100644 --- a/src/drawables/lights/flashlight.ts +++ b/src/drawables/lights/flashlight.ts @@ -3,19 +3,17 @@ import { Drawable } from '../drawable'; import { DrawableDescriptor } from '../drawable-descriptor'; export class Flashlight extends Drawable { - public static get descriptor(): DrawableDescriptor { - return { - uniformName: 'flashlights', - uniformCountMacroName: 'FLASHLIGHT_COUNT', - shaderCombinationSteps: [0, 1, 2, 4], - empty: new Flashlight( - vec2.fromValues(0, 0), - vec2.fromValues(0, 0), - vec3.fromValues(0, 0, 0), - 0 - ), - }; - } + public static readonly descriptor: DrawableDescriptor = { + uniformName: 'flashlights', + uniformCountMacroName: 'FLASHLIGHT_COUNT', + shaderCombinationSteps: [0, 1, 2, 4], + empty: new Flashlight( + vec2.fromValues(0, 0), + vec2.fromValues(0, 0), + vec3.fromValues(0, 0, 0), + 0 + ), + }; public constructor( public center: vec2, diff --git a/src/drawables/shapes/circle.ts b/src/drawables/shapes/circle.ts index 1debece..76c9bd5 100644 --- a/src/drawables/shapes/circle.ts +++ b/src/drawables/shapes/circle.ts @@ -3,10 +3,9 @@ import { Drawable } from '../drawable'; import { DrawableDescriptor } from '../drawable-descriptor'; export class Circle extends Drawable { - public static get descriptor(): DrawableDescriptor { - return { - sdf: { - shader: ` + public static descriptor: DrawableDescriptor = { + sdf: { + shader: ` uniform struct { vec2 center; float radius; @@ -25,14 +24,13 @@ export class Circle extends Drawable { )); } `, - distanceFunctionName: 'circleMinDistance', - }, - uniformName: 'circles', - uniformCountMacroName: 'CIRCLE_COUNT', - shaderCombinationSteps: [0, 1, 2, 3, 16, 32], - empty: new Circle(vec2.fromValues(0, 0), 0), - }; - } + distanceFunctionName: 'circleMinDistance', + }, + uniformName: 'circles', + uniformCountMacroName: 'CIRCLE_COUNT', + shaderCombinationSteps: [0, 1, 2, 3, 16, 32], + empty: new Circle(vec2.fromValues(0, 0), 0), + }; constructor(public center: vec2, public radius: number) { super(); diff --git a/src/drawables/shapes/inverted-tunnel.ts b/src/drawables/shapes/inverted-tunnel.ts index 09599c8..0c278f1 100644 --- a/src/drawables/shapes/inverted-tunnel.ts +++ b/src/drawables/shapes/inverted-tunnel.ts @@ -5,10 +5,9 @@ import { Drawable } from '../drawable'; import { DrawableDescriptor } from '../drawable-descriptor'; export class InvertedTunnel extends Drawable { - public static get descriptor(): DrawableDescriptor { - return { - sdf: { - shader: ` + public static descriptor: DrawableDescriptor = { + sdf: { + shader: ` uniform struct InvertedTunnel { vec2 from; vec2 toFromDelta; @@ -44,14 +43,13 @@ export class InvertedTunnel extends Drawable { minDistance = -myMinDistance; } `, - distanceFunctionName: 'invertedTunnelMinDistance', - }, - uniformName: 'invertedTunnels', - uniformCountMacroName: 'INVERTED_TUNNEL_COUNT', - shaderCombinationSteps: [0, 1, 4, 16, 32], - empty: new InvertedTunnel(vec2.fromValues(0, 0), vec2.fromValues(0, 0), 0, 0), - }; - } + distanceFunctionName: 'invertedTunnelMinDistance', + }, + uniformName: 'invertedTunnels', + uniformCountMacroName: 'INVERTED_TUNNEL_COUNT', + shaderCombinationSteps: [0, 1, 4, 16, 32], + empty: new InvertedTunnel(vec2.fromValues(0, 0), vec2.fromValues(0, 0), 0, 0), + }; constructor( public readonly from: vec2, diff --git a/src/drawables/shapes/tunnel.ts b/src/drawables/shapes/tunnel.ts index fb185d2..f01b013 100644 --- a/src/drawables/shapes/tunnel.ts +++ b/src/drawables/shapes/tunnel.ts @@ -5,10 +5,9 @@ import { Drawable } from '../drawable'; import { DrawableDescriptor } from '../drawable-descriptor'; export class Tunnel extends Drawable { - public static get descriptor(): DrawableDescriptor { - return { - sdf: { - shader: ` + public static descriptor: DrawableDescriptor = { + sdf: { + shader: ` uniform struct Tunnel { vec2 from; vec2 toFromDelta; @@ -44,19 +43,18 @@ export class Tunnel extends Drawable { minDistance = min(minDistance, myMinDistance); } `, - distanceFunctionName: 'tunnelMinDistance', - }, - uniformName: 'tunnels', - uniformCountMacroName: 'TUNNEL_COUNT', - shaderCombinationSteps: [0, 1, 4, 16, 32], - empty: new Tunnel( - vec2.fromValues(-100000, -100000), - vec2.fromValues(-100000, -100000), - 0, - 0 - ), - }; - } + distanceFunctionName: 'tunnelMinDistance', + }, + uniformName: 'tunnels', + uniformCountMacroName: 'TUNNEL_COUNT', + shaderCombinationSteps: [0, 1, 4, 16, 32], + empty: new Tunnel( + vec2.fromValues(-100000, -100000), + vec2.fromValues(-100000, -100000), + 0, + 0 + ), + }; constructor( public readonly from: vec2,