Skip to content

Commit

Permalink
use babel-plugin-inline-import to embed glsl shader code inline
Browse files Browse the repository at this point in the history
  • Loading branch information
transcranial committed Nov 20, 2017
1 parent e9aa265 commit 9239e2c
Show file tree
Hide file tree
Showing 48 changed files with 198 additions and 95 deletions.
6 changes: 3 additions & 3 deletions src/WebGL2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import vertexShaderSource from './webgl/vertexShader.glsl'

class WebGL2 {
constructor() {
this.isSupported = false
Expand Down Expand Up @@ -37,10 +39,8 @@ class WebGL2 {
createCommonVertexShader() {
const gl = this.context

const source = require('./webgl/vertexShader.glsl')

const vertexShader = gl.createShader(gl.VERTEX_SHADER)
gl.shaderSource(vertexShader, source)
gl.shaderSource(vertexShader, vertexShaderSource)
gl.compileShader(vertexShader)

const success = gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)
Expand Down
21 changes: 21 additions & 0 deletions src/activations/programSources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import softmaxProgramSource from './softmax.glsl'
import eluProgramSource from './elu.glsl'
import seluProgramSource from './selu.glsl'
import softplusProgramSource from './softplus.glsl'
import softsignProgramSource from './softsign.glsl'
import reluProgramSource from './relu.glsl'
import tanhProgramSource from './tanh.glsl'
import sigmoidProgramSource from './sigmoid.glsl'
import hardSigmoidProgramSource from './hard_sigmoid.glsl'
import linearProgramSource from './linear.glsl'

export { softmaxProgramSource as softmax }
export { eluProgramSource as elu }
export { seluProgramSource as selu }
export { softplusProgramSource as softplus }
export { softsignProgramSource as softsign }
export { reluProgramSource as relu }
export { tanhProgramSource as tanh }
export { sigmoidProgramSource as sigmoid }
export { hardSigmoidProgramSource as hard_sigmoid }
export { linearProgramSource as linear }
3 changes: 2 additions & 1 deletion src/layers/advanced_activations/ELU.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import cwise from 'cwise'
import programSource from './ELU.glsl'

/**
* ELU advanced activation layer class
Expand All @@ -23,7 +24,7 @@ export default class ELU extends Layer {

// GPU setup
if (this.gpu) {
this.program = webgl2.compileProgram(require('./ELU.glsl'))
this.program = webgl2.compileProgram(programSource)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/advanced_activations/LeakyReLU.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import { relu } from '../../activations'
import programSource from './LeakyReLU.glsl'

/**
* LeakyReLU advanced activation layer class
Expand All @@ -23,7 +24,7 @@ export default class LeakyReLU extends Layer {

// GPU setup
if (this.gpu) {
this.program = webgl2.compileProgram(require('./LeakyReLU.glsl'))
this.program = webgl2.compileProgram(programSource)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/advanced_activations/PReLU.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import cwise from 'cwise'
import programSource from './PReLU.glsl'

/**
* PReLU advanced activation layer class
Expand All @@ -28,7 +29,7 @@ export default class PReLU extends Layer {

// GPU setup
if (this.gpu) {
this.program = webgl2.compileProgram(require('./PReLU.glsl'))
this.program = webgl2.compileProgram(programSource)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/advanced_activations/ThresholdedReLU.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import cwise from 'cwise'
import programSource from './ThresholdedReLU.glsl'

/**
* ThresholdedReLU advanced activation layer class
Expand All @@ -23,7 +24,7 @@ export default class ThresholdedReLU extends Layer {

// GPU setup
if (this.gpu) {
this.program = webgl2.compileProgram(require('./ThresholdedReLU.glsl'))
this.program = webgl2.compileProgram(programSource)
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/layers/convolutional/Conv2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import * as tensorUtils from '../../utils/tensorUtils'
import _ from 'lodash'
import ops from 'ndarray-ops'
import gemm from 'ndarray-gemm'
import mapInputProgramSource from '../../webgl/mapInput.glsl'
import mapInputFragmentsProgramSource from '../../webgl/mapInput.fragments.glsl'
import matMulProgramSource from '../../webgl/matMul.glsl'
import * as activationProgramSources from '../../activations/programSources'

/**
* Conv2D layer class
Expand Down Expand Up @@ -81,10 +85,10 @@ export default class Conv2D extends Layer {

// GPU setup
if (this.gpu) {
this.mapInputProgram = webgl2.compileProgram(require('../../webgl/mapInput.glsl'))
this.mapInputFragmentsProgram = webgl2.compileProgram(require('../../webgl/mapInput.fragments.glsl'))
this.matMulProgram = webgl2.compileProgram(require('../../webgl/matMul.glsl'))
this.activationProgram = webgl2.compileProgram(require(`../../activations/${this.activation}.glsl`))
this.mapInputProgram = webgl2.compileProgram(mapInputProgramSource)
this.mapInputFragmentsProgram = webgl2.compileProgram(mapInputFragmentsProgramSource)
this.matMulProgram = webgl2.compileProgram(matMulProgramSource)
this.activationProgram = webgl2.compileProgram(activationProgramSources[this.activation])
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/layers/convolutional/Conv2DTranspose.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { webgl2 } from '../../WebGL2'
import * as tensorUtils from '../../utils/tensorUtils'
import ops from 'ndarray-ops'
import gemm from 'ndarray-gemm'
import matMulProgramSource from '../../webgl/matMul.glsl'
import convTransposeProgramSource from './Conv2DTranspose.glsl'
import * as activationProgramSources from '../../activations/programSources'

/**
* Conv2DTranspose layer class
Expand Down Expand Up @@ -65,9 +68,9 @@ export default class Conv2DTranspose extends Layer {

// GPU setup
if (this.gpu) {
this.matMulProgram = webgl2.compileProgram(require('../../webgl/matMul.glsl'))
this.convTransposeProgram = webgl2.compileProgram(require('./Conv2DTranspose.glsl'))
this.activationProgram = webgl2.compileProgram(require(`../../activations/${this.activation}.glsl`))
this.matMulProgram = webgl2.compileProgram(matMulProgramSource)
this.convTransposeProgram = webgl2.compileProgram(convTransposeProgramSource)
this.activationProgram = webgl2.compileProgram(activationProgramSources[this.activation])
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/layers/convolutional/Conv3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import * as tensorUtils from '../../utils/tensorUtils'
import _ from 'lodash'
import ops from 'ndarray-ops'
import gemm from 'ndarray-gemm'
import mapInputProgramSource from '../../webgl/mapInput.glsl'
import mapInputFragmentsProgramSource from '../../webgl/mapInput.fragments.glsl'
import matMulProgramSource from '../../webgl/matMul.glsl'
import * as activationProgramSources from '../../activations/programSources'

/**
* Conv3D layer class
Expand Down Expand Up @@ -81,10 +85,10 @@ export default class Conv3D extends Layer {

// GPU setup
if (this.gpu) {
this.mapInputProgram = webgl2.compileProgram(require('../../webgl/mapInput.glsl'))
this.mapInputFragmentsProgram = webgl2.compileProgram(require('../../webgl/mapInput.fragments.glsl'))
this.matMulProgram = webgl2.compileProgram(require('../../webgl/matMul.glsl'))
this.activationProgram = webgl2.compileProgram(require(`../../activations/${this.activation}.glsl`))
this.mapInputProgram = webgl2.compileProgram(mapInputProgramSource)
this.mapInputFragmentsProgram = webgl2.compileProgram(mapInputFragmentsProgramSource)
this.matMulProgram = webgl2.compileProgram(matMulProgramSource)
this.activationProgram = webgl2.compileProgram(activationProgramSources[this.activation])
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/convolutional/Cropping1D.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import ops from 'ndarray-ops'
import mapInputProgramSource from '../../webgl/mapInput.glsl'

/**
* Cropping1D layer class
Expand All @@ -27,7 +28,7 @@ export default class Cropping1D extends Layer {

// GPU setup
if (this.gpu) {
this.mapInputProgram = webgl2.compileProgram(require('../../webgl/mapInput.glsl'))
this.mapInputProgram = webgl2.compileProgram(mapInputProgramSource)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/convolutional/Cropping2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import ops from 'ndarray-ops'
import mapInputProgramSource from '../../webgl/mapInput.glsl'

/**
* Cropping2D layer class
Expand Down Expand Up @@ -37,7 +38,7 @@ export default class Cropping2D extends Layer {

// GPU setup
if (this.gpu) {
this.mapInputProgram = webgl2.compileProgram(require('../../webgl/mapInput.glsl'))
this.mapInputProgram = webgl2.compileProgram(mapInputProgramSource)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/convolutional/Cropping3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import ops from 'ndarray-ops'
import mapInputProgramSource from '../../webgl/mapInput.glsl'

/**
* Cropping3D layer class
Expand Down Expand Up @@ -37,7 +38,7 @@ export default class Cropping3D extends Layer {

// GPU setup
if (this.gpu) {
this.mapInputProgram = webgl2.compileProgram(require('../../webgl/mapInput.glsl'))
this.mapInputProgram = webgl2.compileProgram(mapInputProgramSource)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/convolutional/SeparableConv2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import _ from 'lodash'
import ops from 'ndarray-ops'
import gemm from 'ndarray-gemm'
import Conv2D from './Conv2D'
import * as activationProgramSources from '../../activations/programSources'

/**
* _DepthwiseConv2D layer class
Expand Down Expand Up @@ -301,7 +302,7 @@ export default class SeparableConv2D extends Layer {

// GPU setup
if (this.gpu) {
this.activationProgram = webgl2.compileProgram(require(`../../activations/${this.activation}.glsl`))
this.activationProgram = webgl2.compileProgram(activationProgramSources[this.activation])
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/convolutional/UpSampling1D.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import ops from 'ndarray-ops'
import mapInputProgramSource from '../../webgl/mapInput.glsl'

/**
* UpSampling1D layer class
Expand All @@ -22,7 +23,7 @@ export default class UpSampling1D extends Layer {

// GPU setup
if (this.gpu) {
this.mapInputProgram = webgl2.compileProgram(require('../../webgl/mapInput.glsl'))
this.mapInputProgram = webgl2.compileProgram(mapInputProgramSource)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/convolutional/UpSampling2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import ops from 'ndarray-ops'
import mapInputProgramSource from '../../webgl/mapInput.glsl'

/**
* UpSampling2D layer class
Expand Down Expand Up @@ -30,7 +31,7 @@ export default class UpSampling2D extends Layer {

// GPU setup
if (this.gpu) {
this.mapInputProgram = webgl2.compileProgram(require('../../webgl/mapInput.glsl'))
this.mapInputProgram = webgl2.compileProgram(mapInputProgramSource)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/convolutional/UpSampling3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import ops from 'ndarray-ops'
import mapInputProgramSource from '../../webgl/mapInput.glsl'

/**
* UpSampling3D layer class
Expand Down Expand Up @@ -30,7 +31,7 @@ export default class UpSampling3D extends Layer {

// GPU setup
if (this.gpu) {
this.mapInputProgram = webgl2.compileProgram(require('../../webgl/mapInput.glsl'))
this.mapInputProgram = webgl2.compileProgram(mapInputProgramSource)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/convolutional/ZeroPadding1D.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import ops from 'ndarray-ops'
import mapInputProgramSource from '../../webgl/mapInput.glsl'

/**
* ZeroPadding1D layer class
Expand All @@ -27,7 +28,7 @@ export default class ZeroPadding1D extends Layer {

// GPU setup
if (this.gpu) {
this.mapInputProgram = webgl2.compileProgram(require('../../webgl/mapInput.glsl'))
this.mapInputProgram = webgl2.compileProgram(mapInputProgramSource)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/convolutional/ZeroPadding2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import ops from 'ndarray-ops'
import mapInputProgramSource from '../../webgl/mapInput.glsl'

/**
* ZeroPadding2D layer class
Expand Down Expand Up @@ -37,7 +38,7 @@ export default class ZeroPadding2D extends Layer {

// GPU setup
if (this.gpu) {
this.mapInputProgram = webgl2.compileProgram(require('../../webgl/mapInput.glsl'))
this.mapInputProgram = webgl2.compileProgram(mapInputProgramSource)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/convolutional/ZeroPadding3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import ops from 'ndarray-ops'
import mapInputProgramSource from '../../webgl/mapInput.glsl'

/**
* ZeroPadding3D layer class
Expand Down Expand Up @@ -37,7 +38,7 @@ export default class ZeroPadding3D extends Layer {

// GPU setup
if (this.gpu) {
this.mapInputProgram = webgl2.compileProgram(require('../../webgl/mapInput.glsl'))
this.mapInputProgram = webgl2.compileProgram(mapInputProgramSource)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/layers/core/Activation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Layer from '../../Layer'
import Tensor from '../../Tensor'
import { webgl2 } from '../../WebGL2'
import * as activations from '../../activations'
import * as activationProgramSources from '../../activations/programSources'

/**
* Activation layer class
Expand All @@ -24,7 +25,7 @@ export default class Activation extends Layer {

// GPU setup
if (this.gpu) {
this.program = webgl2.compileProgram(require(`../../activations/${this.activation}.glsl`))
this.program = webgl2.compileProgram(activationProgramSources[this.activation])
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/layers/core/Dense.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as activations from '../../activations'
import { webgl2 } from '../../WebGL2'
import { gemv } from 'ndarray-blas-level2'
import ops from 'ndarray-ops'
import matMulProgramSource from '../../webgl/matMul.glsl'
import * as activationProgramSources from '../../activations/programSources'

/**
* Dense layer class
Expand Down Expand Up @@ -41,8 +43,8 @@ export default class Dense extends Layer {

// GPU setup
if (this.gpu) {
this.matMulProgram = webgl2.compileProgram(require('../../webgl/matMul.glsl'))
this.activationProgram = webgl2.compileProgram(require(`../../activations/${this.activation}.glsl`))
this.matMulProgram = webgl2.compileProgram(matMulProgramSource)
this.activationProgram = webgl2.compileProgram(activationProgramSources[this.activation])
this.outputPreactiv.createGLTexture({ type: '2d', format: 'float' })
this.output.createGLTexture({ type: '2d', format: 'float' })
}
Expand Down
Loading

0 comments on commit 9239e2c

Please sign in to comment.