Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing Shader Code for FMA #506

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/shaders/main.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,20 @@ vec3 get_normal(vec2 uv, out vec3 tangent, out vec3 binormal) {
}

vec3 unpack_normal(vec4 rgba) {
vec3 n = rgba.xzy * 2.0 - vec3(1.0);
n.z *= -1.0;
vec3 n = fma(rgba.xzy, vec3(2.0, 2.0, -2.0), vec3(-1.0, -1.0, 1.0));
return n;
}

vec4 pack_normal(vec3 n, float a) {
n.z *= -1.0;
return vec4((n.xzy + vec3(1.0)) * 0.5, a);
return vec4(fma(n.xzy, vec3(0.5, -0.5, 0.5), vec3(0.5)), a);
}

float random(in vec2 xy) {
return fract(sin(dot(xy, vec2(12.9898, 78.233))) * 43758.5453);
}

vec2 rotate(vec2 v, float cosa, float sina) {
return vec2(cosa * v.x - sina * v.y, sina * v.x + cosa * v.y);
return vec2(fma(cosa, v.x, - sina * v.y), fma(sina, v.x, cosa * v.y));
}

// Moves a point around a pivot point.
Expand All @@ -224,7 +222,7 @@ vec4 height_blend(vec4 a_value, float a_height, vec4 b_value, float b_height, fl
float ma = max(a_height + (1.0 - blend), b_height + blend) - (1.001 - blend_sharpness);
float b1 = max(a_height + (1.0 - blend) - ma, 0.0);
float b2 = max(b_height + blend - ma, 0.0);
return (a_value * b1 + b_value * b2) / (b1 + b2);
return fma(a_value, vec4(b1), b_value * b2) / (b1 + b2);
} else {
float contrast = 1.0 - blend_sharpness;
float factor = (blend - contrast) / contrast;
Expand All @@ -235,7 +233,7 @@ vec4 height_blend(vec4 a_value, float a_height, vec4 b_value, float b_height, fl
vec2 detiling(vec2 uv, vec2 uv_center, int mat_id, inout float normal_rotation){
if (_texture_detile_array[mat_id] >= 0.001){
uv_center = floor(uv_center) + 0.5;
float detile = (random(uv_center) - 0.5) * 2.0 * TAU * _texture_detile_array[mat_id]; // -180deg to 180deg
float detile = fma(random(uv_center), 2.0, -1.0) * TAU * _texture_detile_array[mat_id]; // -180deg to 180deg
uv = rotate_around(uv, uv_center, detile);
// Accumulate total rotation for normal rotation
normal_rotation += detile;
Expand All @@ -244,9 +242,9 @@ vec2 detiling(vec2 uv, vec2 uv_center, int mat_id, inout float normal_rotation){
}

vec2 rotate_normal(vec2 normal, float angle) {
angle += PI * 0.5;
angle = fma(PI, 0.5, angle);
float new_y = dot(vec2(cos(angle), sin(angle)), normal);
angle -= PI * 0.5;
angle = fma(PI, -0.5, angle);
float new_x = dot(vec2(cos(angle) ,sin(angle)) ,normal);
return vec2(new_x, new_y);
}
Expand Down Expand Up @@ -326,7 +324,7 @@ void get_material(vec2 base_uv, uint control, ivec3 iuv_center, vec3 normal, out
float blend_weights(float weight, float detail) {
weight = smoothstep(0.0, 1.0, weight);
weight = sqrt(weight * 0.5);
float result = max(0.1 * weight, 10.0 * (weight + detail) + 1.0f - (detail + 10.0));
float result = max(0.1 * weight, fma(10.0, (weight + detail), 1.0f - (detail + 10.0)));
return result;
}

Expand Down
10 changes: 5 additions & 5 deletions src/shaders/world_noise.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ float hashf(float f) {
}

float hashv2(vec2 v) {
return fract(1e4 * sin(17.0 * v.x + v.y * 0.1) * (0.1 + abs(sin(v.y * 13.0 + v.x))));
return fract(1e4 * sin(fma(17.0, v.x, v.y * 0.1)) * (0.1 + abs(sin(fma(v.y, 13.0, v.x)))));
}

// https://iquilezles.org/articles/morenoise/
vec3 noise2D(vec2 x) {
vec2 f = fract(x);
// Quintic Hermine Curve. Similar to SmoothStep()
vec2 u = f*f*f*(f*(f*6.0-15.0)+10.0);
vec2 du = 30.0*f*f*(f*(f-2.0)+1.0);
vec2 u = f * f * f * fma(f, fma(f, vec2(6.0), vec2(-15.0)), vec2(10.0));
vec2 du = 30.0 * f * f * fma(f, (f - 2.0), vec2(1.0));

vec2 p = floor(x);

Expand All @@ -63,8 +63,8 @@ vec3 noise2D(vec2 x) {
float k1 = b - a;
float k2 = c - a;
float k3 = a - b - c + d;
return vec3( k0 + k1 * u.x + k2 * u.y + k3 * u.x * u.y,
du * ( vec2(k1, k2) + k3 * u.yx) );
return vec3( fma(k3, u.x * u.y, fma(u.y, k2, fma(k1, u.x, k0))),
du * fma(u.yx, vec2(k3), vec2(k1, k2)) );
}

float world_noise(vec2 p) {
Expand Down