Skip to content

Commit

Permalink
\#4: port updateVelocity to shader preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
loganzartman committed Feb 21, 2023
1 parent ce6a006 commit 95140b5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 84 deletions.
14 changes: 14 additions & 0 deletions src/shader/uniforms.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import {GLSLUniform} from '../glslpp';

export const fPressureSampler = new GLSLUniform(
'fPressureSampler',
'sampler2D'
);
export const neighborsTableSampler = new GLSLUniform(
'neighborsTableSampler',
'sampler2D'
);
export const massSampler = new GLSLUniform('massSampler', 'sampler2D');
export const positionSampler = new GLSLUniform('positionSampler', 'sampler2D');
export const velocitySampler = new GLSLUniform('velocitySampler', 'sampler2D');
export const velocityGuessSampler = new GLSLUniform(
'velocityGuessSampler',
'sampler2D'
);

export const keyParticleSampler = new GLSLUniform(
'keyParticleSampler',
Expand All @@ -22,6 +30,12 @@ export const keyParticleResolution = new GLSLUniform(
);
export const resolution = new GLSLUniform('resolution', 'ivec2');

export const collisionDistance = new GLSLUniform('collisionDistance', 'float');
export const dt = new GLSLUniform('dt', 'float');
export const eta = new GLSLUniform('eta', 'float');
export const hSmoothing = new GLSLUniform('hSmoothing', 'float');
export const particleRestitution = new GLSLUniform(
'particleRestitution',
'float'
);
export const sigma = new GLSLUniform('sigma', 'float');
50 changes: 50 additions & 0 deletions src/shader/updateVelocity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {compile, glsl} from '../glslpp';
import {foreachNeighbor} from './foreachNeighbor';
import {
collisionDistance,
dt,
eta,
fPressureSampler,
massSampler,
particleRestitution,
positionSampler,
velocityGuessSampler,
velocitySampler,
} from './uniforms';

export const updateVelocityFs = compile(glsl`
out vec4 outVelocity;
void main() {
ivec2 texCoord = ivec2(gl_FragCoord.xy);
vec2 velocityGuess = texelFetch(${velocityGuessSampler}, texCoord, 0).rg;
vec2 ownPos = texelFetch(${positionSampler}, texCoord, 0).rg;
vec2 ownVel = texelFetch(${velocitySampler}, texCoord, 0).rg;
float ownMass = texelFetch(${massSampler}, texCoord, 0).r;
vec2 fPressure = texelFetch(${fPressureSampler}, texCoord, 0).rg;
vec2 velocity = velocityGuess + (${dt} / ownMass) * fPressure;
float collidedMass = 0.;
vec2 dvCollsion = vec2(0.);
${foreachNeighbor}(neighborTexCoord, {
float neighborMass = texelFetch(${massSampler}, neighborTexCoord, 0).x;
vec2 neighborPos = texelFetch(${positionSampler}, neighborTexCoord, 0).xy;
vec2 neighborVel = texelFetch(${velocitySampler}, neighborTexCoord, 0).xy;
vec2 dx = ownPos - neighborPos;
vec2 dv = ownVel - neighborVel;
float d = length(dx) + ${eta};
float dotDxDv = dot(dx, dv);
if (d < ${collisionDistance} && dotDxDv < 0.) {
collidedMass += neighborMass;
dvCollsion += neighborMass * (1. + ${particleRestitution}) * (dotDxDv / d) * (dx / d);
}
})
float collisionTerm = 1. / (ownMass + collidedMass);
velocity -= collisionTerm * dvCollsion;
outVelocity = vec4(velocity, 0.0, 0.0);
}
`);
4 changes: 2 additions & 2 deletions src/simulationGPU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {createProgram, createShader} from './gl';
import {GPUState, State} from './state';
import {memoize} from './util';
import {advectParticlesFs} from './shader/advectParticles';
import updateVelocityFrag from './updateVelocity.frag.glsl';
import {updateVelocityFs} from './shader/updateVelocity';
import {Params} from './params';
import {getCopyVertexVert, getQuadVAO} from './gpuUtil';
import {updateDensityFs} from './shader/updateDensity';
Expand Down Expand Up @@ -457,7 +457,7 @@ export const advectParticlesGPU = (
};

const getUpdateVelocityFrag = memoize((gl: WebGL2RenderingContext) =>
createShader(gl, {source: updateVelocityFrag, type: gl.FRAGMENT_SHADER})
createShader(gl, {source: updateVelocityFs, type: gl.FRAGMENT_SHADER})
);
const getUpdateVelocityProgram = memoize((gl: WebGL2RenderingContext) =>
createProgram(gl, {
Expand Down
82 changes: 0 additions & 82 deletions src/updateVelocity.frag.glsl

This file was deleted.

0 comments on commit 95140b5

Please sign in to comment.