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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions src/shaders/auto_shader.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ uniform int auto_overlay_texture : hint_range(0, 31) = 1;
bool auto_shader = region < 0 || bool(control & 0x1u);
out_mat.base = int(auto_shader) * auto_base_texture + int(!auto_shader) * int(control >>27u & 0x1Fu);
out_mat.over = int(auto_shader) * auto_overlay_texture + int(!auto_shader) * int(control >> 22u & 0x1Fu);
out_mat.blend = float(auto_shader) * clamp(
dot(vec3(0., 1., 0.), normal * auto_slope * 2. - (auto_slope * 2. - 1.))
out_mat.blend = fma(float(auto_shader), clamp(
dot(vec3(0., 1., 0.), fma(normal, vec3(auto_slope * 2.), vec3(fma(auto_slope, 2., -1.))))
- auto_height_reduction * .01 * v_vertex.y // Reduce as vertices get higher
, 0., 1.) +
float(!auto_shader) * float(control >>14u & 0xFFu) * 0.003921568627450; // 1./255.0
, 0., 1.),
float(!auto_shader) * float(control >>14u & 0xFFu) * 0.003921568627450); // 1./255.0

//INSERT: TEXTURE_ID
out_mat.base = int(control >>27u & 0x1Fu);
Expand Down
36 changes: 19 additions & 17 deletions src/shaders/debug_views.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ R"(
vec2 __ddx = dFdx(__p);
vec2 __ddy = dFdy(__p);
vec2 __w = max(abs(__ddx), abs(__ddy)) + 0.01;
vec2 __i = 2.0 * (abs(fract((__p - 0.5 * __w) / 2.0) - 0.5) - abs(fract((__p + 0.5 * __w) / 2.0) - 0.5)) / __w;
vec2 __i = 2.0 * (abs( fract( fma(vec2(-0.5), __w, __p) * 0.5) - 0.5) - abs( fract( fma(vec2(0.5), __w, vec2(0.5)) / 2.0) - 0.5)) / __w;
ALBEDO = vec3((0.5 - 0.5 * __i.x * __i.y) * 0.2 + 0.2);
ROUGHNESS = 0.7;
SPECULAR = 0.;
Expand Down Expand Up @@ -46,16 +46,18 @@ R"(

//INSERT: DEBUG_CONTROL_TEXTURE
// Show control map texture selection
float __ctrl_base = weight_inv * (
float(mat[0].base) * weights.x +
float(mat[1].base) * weights.y +
float(mat[2].base) * weights.z +
float(mat[3].base) * weights.w )/96.;
float __ctrl_over = weight_inv * (
float(mat[0].over) * weights.x +
float(mat[1].over) * weights.y +
float(mat[2].over) * weights.z +
float(mat[3].over) * weights.w )/96.;
float __ctrl_base = weight_inv *
fma(float(mat[0].base), weights[0].x,
fma(float(mat[1].base), weights[1].x,
fma(float(mat[2].base), weights[2].x,
float(mat[3].base) * weights[3].x )))/96.;

float __ctrl_over = weight_inv *
fma(float(mat[0].over), weights[0].x,
fma(float(mat[1].over), weights[1].x,
fma(float(mat[2].over), weights[2].x,
float(mat[3].over) * weights[3].x )))/96.;

ALBEDO = vec3(__ctrl_base, __ctrl_over, 0.);
ROUGHNESS = 1.;
SPECULAR = 0.;
Expand Down Expand Up @@ -135,8 +137,8 @@ R"(
vec3 __pixel_pos1 = (INV_VIEW_MATRIX * vec4(VERTEX,1.0)).xyz;
float __region_line = 0.5; // Region line thickness
__region_line = .1*sqrt(length(v_camera_pos - __pixel_pos1));
if (mod(__pixel_pos1.x * _vertex_density + __region_line*.5, _region_size) <= __region_line ||
mod(__pixel_pos1.z * _vertex_density + __region_line*.5, _region_size) <= __region_line ) {
if (mod( fma(__pixel_pos1.x, _vertex_density, __region_line*.5), _region_size) <= __region_line ||
mod( fma(__pixel_pos1.z, _vertex_density, __region_line*.5), _region_size) <= __region_line ) {
ALBEDO = vec3(1.);
}

Expand All @@ -151,13 +153,13 @@ R"(
vec3 __vertex_add = vec3(0.);
float __distance_factor = clamp(1.-length(v_camera_pos - __pixel_pos2)/__view_distance, 0., 1.);
// Draw vertex grid
if ( mod(__pixel_pos2.x * _vertex_density + __grid_line*.5, __grid_step) < __grid_line ||
mod(__pixel_pos2.z * _vertex_density + __grid_line*.5, __grid_step) < __grid_line ) {
if ( mod( fma(__pixel_pos2.x, _vertex_density, __grid_line*.5), __grid_step) < __grid_line ||
mod( fma(__pixel_pos2.z, _vertex_density, __grid_line*.5), __grid_step) < __grid_line ) {
__vertex_mul = vec3(0.5) * __distance_factor;
}
// Draw Vertices
if ( mod(UV.x + __grid_line*__vertex_size*.5, __grid_step) < __grid_line*__vertex_size &&
mod(UV.y + __grid_line*__vertex_size*.5, __grid_step) < __grid_line*__vertex_size ) {
if ( mod( fma(__vertex_size*.5, __grid_line, UV.x), __grid_step) < __grid_line*__vertex_size &&
mod( fma(__vertex_size*.5, __grid_line, UV.y), __grid_step) < __grid_line*__vertex_size ) {
__vertex_add = vec3(0.15) * __distance_factor;
}
ALBEDO = fma(ALBEDO, 1.-__vertex_mul, __vertex_add);
Expand Down
2 changes: 1 addition & 1 deletion src/shaders/gpu_depth.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ vec3 encode_rg(float value) {

void fragment() {
float depth = textureLod(depth_texture, SCREEN_UV, 0.).x;
vec3 ndc = vec3(SCREEN_UV * 2.0 - 1.0, depth);
vec3 ndc = vec3(fma(SCREEN_UV, vec2(2.0), vec2(-1.0)), depth);
vec4 view = INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
view.xyz /= view.w;
float depth_linear = -view.z;
Expand Down
54 changes: 26 additions & 28 deletions src/shaders/main.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -194,28 +194,26 @@ 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.
vec2 rotate_around(vec2 point, vec2 pivot, float angle){
float x = pivot.x + (point.x - pivot.x) * cos(angle) - (point.y - pivot.y) * sin(angle);
float y = pivot.y + (point.x - pivot.x) * sin(angle) + (point.y - pivot.y) * cos(angle);
float x = pivot.x + fma(point.x - pivot.x, cos(angle), (point.y - pivot.y) * -sin(angle));
float y = pivot.y + fma(point.x - pivot.x, sin(angle), (point.y - pivot.y) * cos(angle));
return vec2(x, y);
}

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 Expand Up @@ -381,27 +379,27 @@ void fragment() {
vec2 weights0 = vec2(1.0) - weights1;
// Adjust final weights by texture's height/depth + noise. 1 lookup
float noise3 = texture(noise_texture, uv*noise3_scale).r;
vec4 weights;
weights.x = blend_weights(weights0.x * weights0.y, clamp(mat[0].alb_ht.a + noise3, 0., 1.));
weights.y = blend_weights(weights0.x * weights1.y, clamp(mat[1].alb_ht.a + noise3, 0., 1.));
weights.z = blend_weights(weights1.x * weights0.y, clamp(mat[2].alb_ht.a + noise3, 0., 1.));
weights.w = blend_weights(weights1.x * weights1.y, clamp(mat[3].alb_ht.a + noise3, 0., 1.));
float weight_sum = weights.x + weights.y + weights.z + weights.w;
mat4 weights;
weights[0] = vec4(blend_weights(weights0.x * weights0.y, clamp(mat[0].alb_ht.a + noise3, 0., 1.)));
weights[1] = vec4(blend_weights(weights0.x * weights1.y, clamp(mat[1].alb_ht.a + noise3, 0., 1.)));
weights[2] = vec4(blend_weights(weights1.x * weights0.y, clamp(mat[2].alb_ht.a + noise3, 0., 1.)));
weights[3] = vec4(blend_weights(weights1.x * weights1.y, clamp(mat[3].alb_ht.a + noise3, 0., 1.)));
float weight_sum = weights[0].x + weights[1].x + weights[2].x + weights[3].x;
float weight_inv = 1.0 / weight_sum;

// Weighted average of albedo & height
vec4 albedo_height = weight_inv * (
mat[0].alb_ht * weights.x +
mat[1].alb_ht * weights.y +
mat[2].alb_ht * weights.z +
mat[3].alb_ht * weights.w );
vec4 albedo_height = weight_inv *
fma(mat[0].alb_ht, weights[0],
fma(mat[1].alb_ht, weights[1],
fma(mat[2].alb_ht, weights[2],
mat[3].alb_ht * weights[3] )));

// Weighted average of normal & rough
vec4 normal_rough = weight_inv * (
mat[0].nrm_rg * weights.x +
mat[1].nrm_rg * weights.y +
mat[2].nrm_rg * weights.z +
mat[3].nrm_rg * weights.w );
vec4 normal_rough = weight_inv *
fma(mat[0].nrm_rg, weights[0],
fma(mat[1].nrm_rg, weights[1],
fma(mat[2].nrm_rg, weights[2],
mat[3].nrm_rg * weights[3] )));

// Determine if we're in a region or not (region_uv.z>0)
vec3 region_uv = get_region_uv2(uv2);
Expand Down
12 changes: 6 additions & 6 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,7 +63,7 @@ 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,
return vec3( k0 + fma(k1, u.x, fma(k2, u.y, k3 * u.x * u.y)),
du * ( vec2(k1, k2) + k3 * u.yx) );
}

Expand Down Expand Up @@ -93,8 +93,8 @@ float get_noise_height(const vec2 uv) {
return 0.0;
}
//TODO: Offset/scale UVs are semi-dependent upon region size 1024. Base on v_vertex.xz instead
float noise = world_noise((uv + world_noise_offset.xz * 1024. / _region_size) * world_noise_scale * _region_size / 1024. * .1) *
world_noise_height * 10. + world_noise_offset.y * 100.;
float noise = fma( world_noise(fma(world_noise_offset.xz, vec2(1024. / _region_size), uv) * world_noise_scale * _region_size / 1024. * .1),
world_noise_height * 10., world_noise_offset.y * 100.);
weight = smoothstep(1.0 - world_noise_region_blend, 1.0, weight);
return mix(0.0, noise, weight);
}
Expand Down