Skip to content

Commit

Permalink
Refactor raster particle update shader (internal-1586)
Browse files Browse the repository at this point in the history
* Refactor raster particle update shader

* Update src/shaders/raster_particle_update.fragment.glsl

* Update src/shaders/raster_particle_update.fragment.glsl

* Update test expectations

* Increase tolerance

* Don't mix using boolean

* Simplify GLSL
  • Loading branch information
rreusser authored and stepankuzmin committed Jul 18, 2024
1 parent 0719926 commit 590c8a4
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/render/program/raster_particle_program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import type Context from '../../gl/context';
import type {UniformValues} from '../uniform_binding';

export const RASTER_PARTICLE_POS_OFFSET: number = 0.15;
export const RASTER_PARTICLE_POS_OFFSET: number = 0.05;
export const RASTER_PARTICLE_POS_SCALE: number = 1.0 + 2.0 * RASTER_PARTICLE_POS_OFFSET;

export type RasterParticleUniformsType = {
Expand Down
12 changes: 6 additions & 6 deletions src/shaders/_prelude.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ in float v_cutoff_opacity;
#endif

// This function should be used in cases where mipmap usage is expected and
// the sampling coordinates are not continous. The lod_parameter should be
// a continous function derived from the sampling coordinates.
// the sampling coordinates are not continous. The lod_parameter should be
// a continous function derived from the sampling coordinates.
vec4 textureLodCustom(sampler2D image, vec2 pos, vec2 lod_coord) {
vec2 size = vec2(textureSize(image, 0));
vec2 dx = dFdx(lod_coord.xy * size);
vec2 dy = dFdy(lod_coord.xy * size);
float delta_max_sqr = max(dot(dx, dx), dot(dy, dy));
float lod = 0.5 * log2(delta_max_sqr);
float lod = 0.5 * log2(delta_max_sqr);
// Note: textureLod doesn't support anisotropic filtering
// We could use textureGrad instead which supports it, but it's discouraged
// We could use textureGrad instead which supports it, but it's discouraged
// in the ARM Developer docs:
// "Do not use textureGrad() unless absolutely necessary.
// "Do not use textureGrad() unless absolutely necessary.
// It is much slower that texture() and textureLod()..."
// https://developer.arm.com/documentation/101897/0301/Buffers-and-textures/Texture-sampling-performance
return textureLod(image, pos, lod);
Expand All @@ -80,4 +80,4 @@ vec4 applyLUT(highp sampler3D lut, vec4 col) {

vec3 applyLUT(highp sampler3D lut, vec3 col) {
return applyLUT(lut, vec4(col, 1.0)).rgb;
}
}
2 changes: 1 addition & 1 deletion src/shaders/line.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void main() {
float alpha2 = clamp(min(dist - (v_width2.t - edgeBlur), v_width2.s - dist) / edgeBlur, 0.0, 1.0);
if (alpha2 < 1.) {
float smoothAlpha = smoothstep(0.6, 1.0, alpha2);
if (border_color.a == 0.0) {
if (border_color.a == 0.0) {
float Y = (out_color.a > 0.01) ? luminance(out_color.rgb / out_color.a) : 1.; // out_color is premultiplied
float adjustment = (Y > 0.) ? 0.5 / Y : 0.45;
if (out_color.a > 0.25 && Y < 0.25) {
Expand Down
2 changes: 1 addition & 1 deletion src/shaders/raster_particle_draw.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {
gl_Position = AWAY;
v_particle_speed = 0.0;
} else {
gl_Position = vec4(2.0 * pos - vec2(1.0), 0.0, 1.0);
gl_Position = vec4(2.0 * pos - 1.0, 0, 1);
v_particle_speed = length(velocity);
}
gl_PointSize = 1.0;
Expand Down
35 changes: 27 additions & 8 deletions src/shaders/raster_particle_update.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ uniform highp float u_rand_seed;

in highp vec2 v_tex_coord;

vec2 linearstep(vec2 edge0, vec2 edge1, vec2 x) {
return clamp((x - edge0) / (edge1 - edge0), vec2(0), vec2(1));
}

// pseudo-random generator
const highp vec3 rand_constants = vec3(12.9898, 78.233, 4375.85453);
highp float rand(const highp vec2 co) {
Expand All @@ -25,14 +29,29 @@ void main() {

highp vec2 seed = (pos + v_tex_coord) * u_rand_seed;
highp vec2 random_pos = vec2(rand(seed + 1.3), rand(seed + 2.1));
highp float speed = velocity == INVALID_VELOCITY ? 0.0 : length(velocity);
highp float reset_rate_bump = speed * u_reset_rate;
highp vec2 particle_pos_min = -u_particle_pos_offset;
highp vec2 particle_pos_max = vec2(1.0) + u_particle_pos_offset;
// drop rate 0: (min pos) < x < (max pos), else drop rate 1
highp vec2 pos_drop_rate = vec2(1.0) - step(particle_pos_min, pos) + step(particle_pos_max, pos);
highp float drop_rate = max(u_reset_rate + reset_rate_bump, length(pos_drop_rate));
highp float drop = step(1.0 - drop_rate, rand(seed));

// An ad hoc mask that's 1 inside the tile and ramps to zero outside the
// boundary. The constant power of 4 is tuned to cause particles to traverse
// roughly the width of the boundary before dropping.
highp vec2 persist_rate = pow(
linearstep(vec2(-u_particle_pos_offset), vec2(0), pos) *
linearstep(vec2(1.0 + u_particle_pos_offset), vec2(1), pos),
vec2(4)
);

// Raise the persist rate to the inverse power of the number of steps
// taken to traverse the boundary. This yields a per-frame persist
// rate which gives the overall chance of dropping by the time it
// traverses the entire boundary buffer.
highp vec2 per_frame_persist = pow(persist_rate, abs(dp) / u_particle_pos_offset);

// Combine drop probability wrt x-boundary and y-boundary into a single drop rate
highp float drop_rate = 1.0 - per_frame_persist.x * per_frame_persist.y;

// Apply a hard drop cutoff outside the boundary of what we encode
drop_rate = any(greaterThanEqual(abs(pos - 0.5), vec2(0.5 + u_particle_pos_offset))) ? 1.0 : drop_rate;

highp float drop = step(1.0 - drop_rate - u_reset_rate, rand(seed));
highp vec2 next_pos = mix(pos, random_pos, drop);

glFragColor = pack_pos_to_rgba(next_pos);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"test": {
"width": 256,
"height": 256,
"allowed": 0.005,
"allowed": 0.006,
"operations": [
["wait", 100],
["wait", 100],
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 590c8a4

Please sign in to comment.