mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Merge branch 'vercidium-optimisations' into 'master'
Optimised Fragment and Vertex Shaders See merge request veloren/veloren!394
This commit is contained in:
commit
4042496d10
@ -3,9 +3,8 @@
|
|||||||
#include <globals.glsl>
|
#include <globals.glsl>
|
||||||
|
|
||||||
in vec3 f_pos;
|
in vec3 f_pos;
|
||||||
in vec3 f_norm;
|
|
||||||
in vec3 f_col;
|
in vec3 f_col;
|
||||||
flat in uint f_bone_idx;
|
flat in vec3 f_norm;
|
||||||
|
|
||||||
layout (std140)
|
layout (std140)
|
||||||
uniform u_locals {
|
uniform u_locals {
|
||||||
@ -28,13 +27,7 @@ uniform u_bones {
|
|||||||
out vec4 tgt_color;
|
out vec4 tgt_color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec3 world_norm = (
|
vec3 light = get_sun_diffuse(f_norm, time_of_day.x) + light_at(f_pos, f_norm);
|
||||||
model_mat *
|
|
||||||
bones[f_bone_idx].bone_mat *
|
|
||||||
vec4(f_norm, 0.0)
|
|
||||||
).xyz;
|
|
||||||
|
|
||||||
vec3 light = get_sun_diffuse(world_norm, time_of_day.x) + light_at(f_pos, world_norm);
|
|
||||||
vec3 surf_color = model_col.rgb * f_col * 2.0 * light;
|
vec3 surf_color = model_col.rgb * f_col * 2.0 * light;
|
||||||
|
|
||||||
float fog_level = fog(f_pos.xy, focus_pos.xy);
|
float fog_level = fog(f_pos.xy, focus_pos.xy);
|
||||||
|
@ -23,17 +23,24 @@ uniform u_bones {
|
|||||||
};
|
};
|
||||||
|
|
||||||
out vec3 f_pos;
|
out vec3 f_pos;
|
||||||
out vec3 f_norm;
|
|
||||||
out vec3 f_col;
|
out vec3 f_col;
|
||||||
flat out uint f_bone_idx;
|
flat out vec3 f_norm;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
f_pos = (model_mat *
|
// Pre-calculate bone matrix
|
||||||
bones[v_bone_idx].bone_mat *
|
mat4 combined_mat = model_mat * bones[v_bone_idx].bone_mat;
|
||||||
|
|
||||||
|
f_pos = (
|
||||||
|
combined_mat *
|
||||||
vec4(v_pos, 1)).xyz;
|
vec4(v_pos, 1)).xyz;
|
||||||
f_norm = v_norm;
|
|
||||||
f_col = v_col;
|
f_col = v_col;
|
||||||
f_bone_idx = v_bone_idx;
|
|
||||||
|
// Calculate normal here rather than for each pixel in the fragment shader
|
||||||
|
f_norm = (
|
||||||
|
combined_mat *
|
||||||
|
vec4(v_norm, 0.0)
|
||||||
|
).xyz;
|
||||||
|
|
||||||
gl_Position = proj_mat * view_mat * vec4(f_pos, 1);
|
gl_Position = proj_mat * view_mat * vec4(f_pos, 1);
|
||||||
}
|
}
|
||||||
|
@ -16,19 +16,32 @@ vec3 light_at(vec3 wpos, vec3 wnorm) {
|
|||||||
const float LIGHT_AMBIENCE = 0.025;
|
const float LIGHT_AMBIENCE = 0.025;
|
||||||
|
|
||||||
vec3 light = vec3(0);
|
vec3 light = vec3(0);
|
||||||
for (uint i = 0u; i < light_count.x; i ++) {
|
|
||||||
vec3 light_pos = lights[i].light_pos.xyz;
|
|
||||||
float strength = attenuation_strength(wpos - light_pos);
|
|
||||||
|
|
||||||
vec3 color = strength
|
for (uint i = 0u; i < light_count.x; i++) {
|
||||||
* lights[i].light_col.rgb
|
|
||||||
* lights[i].light_col.a;
|
|
||||||
|
|
||||||
if (max(max(color.r, color.g), color.b) < 0.002) {
|
// Only access the array once
|
||||||
continue;
|
Light L = lights[i];
|
||||||
}
|
|
||||||
|
|
||||||
light += color * clamp(dot(normalize(light_pos - wpos), wnorm), LIGHT_AMBIENCE, 1.0);
|
vec3 light_pos = L.light_pos.xyz;
|
||||||
|
|
||||||
|
// Pre-calculate difference between light and fragment
|
||||||
|
vec3 difference = light_pos - wpos;
|
||||||
|
|
||||||
|
float strength = attenuation_strength(difference);
|
||||||
|
|
||||||
|
// Multiply the vec3 only once
|
||||||
|
vec3 color = L.light_col.rgb * (strength * L.light_col.a);
|
||||||
|
|
||||||
|
// This is commented out to avoid conditional branching. See here: https://community.khronos.org/t/glsl-float-multiply-by-zero/104391
|
||||||
|
// if (max(max(color.r, color.g), color.b) < 0.002) {
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Old: light += color * clamp(dot(normalize(difference), wnorm), LIGHT_AMBIENCE, 1.0);
|
||||||
|
|
||||||
|
// The dot product cannot be greater than one, so no need to clamp max value
|
||||||
|
// Also, rather than checking if it is smaller than LIGHT_AMBIENCE, add LIGHT_AMBIENCE instead
|
||||||
|
light += color * (max(0, dot(normalize(difference), wnorm)) + LIGHT_AMBIENCE);
|
||||||
}
|
}
|
||||||
return light;
|
return light;
|
||||||
}
|
}
|
@ -37,14 +37,16 @@ vec3 get_sun_diffuse(vec3 norm, float time_of_day) {
|
|||||||
|
|
||||||
float sun_light = get_sun_brightness(sun_dir);
|
float sun_light = get_sun_brightness(sun_dir);
|
||||||
|
|
||||||
|
// clamp() changed to max() as sun_dir.z is produced from a cos() function and therefore never greater than 1
|
||||||
|
|
||||||
vec3 sun_color = normalize(mix(
|
vec3 sun_color = normalize(mix(
|
||||||
mix(
|
mix(
|
||||||
DUSK_LIGHT,
|
DUSK_LIGHT,
|
||||||
NIGHT_LIGHT,
|
NIGHT_LIGHT,
|
||||||
clamp(sun_dir.z, 0, 1)
|
max(sun_dir.z, 0)
|
||||||
),
|
),
|
||||||
DAY_LIGHT,
|
DAY_LIGHT,
|
||||||
clamp(-sun_dir.z, 0, 1)
|
max(-sun_dir.z, 0)
|
||||||
));
|
));
|
||||||
|
|
||||||
vec3 diffuse_light = (SUN_AMBIANCE + max(dot(-norm, sun_dir), 0.0) * sun_color) * sun_light + PERSISTENT_AMBIANCE;
|
vec3 diffuse_light = (SUN_AMBIANCE + max(dot(-norm, sun_dir), 0.0) * sun_color) * sun_light + PERSISTENT_AMBIANCE;
|
||||||
@ -56,11 +58,10 @@ vec3 rand_offs(vec3 pos) {
|
|||||||
return sin(pos * vec3(1473.7 * pos.z + 472.3, 8891.1 * pos.x + 723.1, 3813.3 * pos.y + 982.5));
|
return sin(pos * vec3(1473.7 * pos.z + 472.3, 8891.1 * pos.x + 723.1, 3813.3 * pos.y + 982.5));
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 get_sky_color(vec3 dir, float time_of_day) {
|
// This has been extracted into a function to allow quick exit when detecting a star.
|
||||||
|
float is_star_at(vec3 dir) {
|
||||||
// Stars
|
|
||||||
float star_scale = 30.0;
|
float star_scale = 30.0;
|
||||||
float star = 0.0;
|
|
||||||
for (int i = 0; i < 2; i ++) {
|
for (int i = 0; i < 2; i ++) {
|
||||||
for (int j = 0; j < 2; j ++) {
|
for (int j = 0; j < 2; j ++) {
|
||||||
for (int k = 0; k < 2; k ++) {
|
for (int k = 0; k < 2; k ++) {
|
||||||
@ -68,61 +69,69 @@ vec3 get_sky_color(vec3 dir, float time_of_day) {
|
|||||||
vec3 pos = (floor(dir * star_scale) + vec3(i, j, k) - vec3(0.5)) / star_scale;
|
vec3 pos = (floor(dir * star_scale) + vec3(i, j, k) - vec3(0.5)) / star_scale;
|
||||||
|
|
||||||
// Noisy offsets
|
// Noisy offsets
|
||||||
pos += 3.0 * rand_offs(pos) / star_scale;
|
pos += (3.0 / star_scale) * rand_offs(pos);
|
||||||
|
|
||||||
// Find distance to fragment
|
// Find distance to fragment
|
||||||
float dist = length(normalize(pos) - dir);
|
float dist = length(normalize(pos) - dir);
|
||||||
|
|
||||||
// Star threshold
|
// Star threshold
|
||||||
if (dist < 0.0015) {
|
if (dist < 0.0015) {
|
||||||
star = 1.0;
|
return 1.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 get_sky_color(vec3 dir, float time_of_day) {
|
||||||
// Sky color
|
// Sky color
|
||||||
|
|
||||||
vec3 sun_dir = get_sun_dir(time_of_day);
|
vec3 sun_dir = get_sun_dir(time_of_day);
|
||||||
|
|
||||||
|
// Add white dots for stars. Note these flicker and jump due to FXAA
|
||||||
|
float star = is_star_at(dir);
|
||||||
|
|
||||||
|
// Replaced all clamp(sun_dir, 0, 1) with max(sun_dir, 0) because sun_dir is calculated from sin and cos, which are never > 1
|
||||||
|
|
||||||
vec3 sky_top = mix(
|
vec3 sky_top = mix(
|
||||||
mix(
|
mix(
|
||||||
SKY_DUSK_TOP,
|
SKY_DUSK_TOP,
|
||||||
SKY_NIGHT_TOP,
|
SKY_NIGHT_TOP,
|
||||||
clamp(sun_dir.z, 0, 1)
|
max(sun_dir.z, 0)
|
||||||
) + star,
|
),
|
||||||
SKY_DAY_TOP,
|
SKY_DAY_TOP,
|
||||||
clamp(-sun_dir.z, 0, 1)
|
max(-sun_dir.z, 0)
|
||||||
);
|
) + star;
|
||||||
|
|
||||||
vec3 sky_mid = mix(
|
vec3 sky_mid = mix(
|
||||||
mix(
|
mix(
|
||||||
SKY_DUSK_MID,
|
SKY_DUSK_MID,
|
||||||
SKY_NIGHT_MID,
|
SKY_NIGHT_MID,
|
||||||
clamp(sun_dir.z, 0, 1)
|
max(sun_dir.z, 0)
|
||||||
),
|
),
|
||||||
SKY_DAY_MID,
|
SKY_DAY_MID,
|
||||||
clamp(-sun_dir.z, 0, 1)
|
max(-sun_dir.z, 0)
|
||||||
);
|
);
|
||||||
|
|
||||||
vec3 sky_bot = mix(
|
vec3 sky_bot = mix(
|
||||||
mix(
|
mix(
|
||||||
SKY_DUSK_BOT,
|
SKY_DUSK_BOT,
|
||||||
SKY_NIGHT_BOT,
|
SKY_NIGHT_BOT,
|
||||||
clamp(sun_dir.z, 0, 1)
|
max(sun_dir.z, 0)
|
||||||
),
|
),
|
||||||
SKY_DAY_BOT,
|
SKY_DAY_BOT,
|
||||||
clamp(-sun_dir.z, 0, 1)
|
max(-sun_dir.z, 0)
|
||||||
);
|
);
|
||||||
|
|
||||||
vec3 sky_color = mix(
|
vec3 sky_color = mix(
|
||||||
mix(
|
mix(
|
||||||
sky_mid,
|
sky_mid,
|
||||||
sky_bot,
|
sky_bot,
|
||||||
pow(clamp(-dir.z, 0, 1), 0.4)
|
pow(max(-dir.z, 0), 0.4)
|
||||||
),
|
),
|
||||||
sky_top,
|
sky_top,
|
||||||
pow(clamp(dir.z, 0, 1), 1.0)
|
max(dir.z, 0)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Sun
|
// Sun
|
||||||
@ -139,8 +148,9 @@ vec3 get_sky_color(vec3 dir, float time_of_day) {
|
|||||||
|
|
||||||
float fog(vec2 f_pos, vec2 focus_pos) {
|
float fog(vec2 f_pos, vec2 focus_pos) {
|
||||||
float dist = distance(f_pos, focus_pos) / view_distance.x;
|
float dist = distance(f_pos, focus_pos) / view_distance.x;
|
||||||
float min_fog = 0.5;
|
const float min_fog = 0.5;
|
||||||
float max_fog = 1.0;
|
const float max_fog = 1.0;
|
||||||
|
const float diff_fog = 0.5; // max - min
|
||||||
|
|
||||||
return pow(clamp((dist - min_fog) / (max_fog - min_fog), 0.0, 1.0), 1.7);
|
return pow(clamp((dist - min_fog) / (diff_fog), 0.0, 1.0), 1.7);
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#include <globals.glsl>
|
#include <globals.glsl>
|
||||||
|
|
||||||
in vec3 f_pos;
|
in vec3 f_pos;
|
||||||
flat in uint f_pos_norm;
|
flat in vec3 f_norm;
|
||||||
in vec3 f_col;
|
in vec3 f_col;
|
||||||
in float f_light;
|
in float f_light;
|
||||||
|
|
||||||
@ -18,18 +18,6 @@ out vec4 tgt_color;
|
|||||||
#include <light.glsl>
|
#include <light.glsl>
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
// Calculate normal from packed data
|
|
||||||
vec3 f_norm;
|
|
||||||
uint norm_axis = (f_pos_norm >> 30) & 0x3u;
|
|
||||||
float norm_dir = float((f_pos_norm >> 29) & 0x1u) * 2.0 - 1.0;
|
|
||||||
if (norm_axis == 0u) {
|
|
||||||
f_norm = vec3(1.0, 0.0, 0.0) * norm_dir;
|
|
||||||
} else if (norm_axis == 1u) {
|
|
||||||
f_norm = vec3(0.0, 1.0, 0.0) * norm_dir;
|
|
||||||
} else {
|
|
||||||
f_norm = vec3(0.0, 0.0, 1.0) * norm_dir;
|
|
||||||
}
|
|
||||||
|
|
||||||
vec3 light = get_sun_diffuse(f_norm, time_of_day.x) * f_light + light_at(f_pos, f_norm);
|
vec3 light = get_sun_diffuse(f_norm, time_of_day.x) * f_light + light_at(f_pos, f_norm);
|
||||||
vec3 surf_color = f_col * light;
|
vec3 surf_color = f_col * light;
|
||||||
|
|
||||||
|
@ -11,10 +11,13 @@ uniform u_locals {
|
|||||||
};
|
};
|
||||||
|
|
||||||
out vec3 f_pos;
|
out vec3 f_pos;
|
||||||
flat out uint f_pos_norm;
|
flat out vec3 f_norm;
|
||||||
out vec3 f_col;
|
out vec3 f_col;
|
||||||
out float f_light;
|
out float f_light;
|
||||||
|
|
||||||
|
// First 3 normals are negative, next 3 are positive
|
||||||
|
vec3 normals[6] = vec3[]( vec3(-1,0,0), vec3(0,-1,0), vec3(0,0,-1), vec3(1,0,0), vec3(0,1,0), vec3(0,0,1) );
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
f_pos = vec3(
|
f_pos = vec3(
|
||||||
float((v_pos_norm >> 0) & 0x00FFu),
|
float((v_pos_norm >> 0) & 0x00FFu),
|
||||||
@ -22,7 +25,14 @@ void main() {
|
|||||||
float((v_pos_norm >> 16) & 0x1FFFu)
|
float((v_pos_norm >> 16) & 0x1FFFu)
|
||||||
) + model_offs;
|
) + model_offs;
|
||||||
|
|
||||||
f_pos_norm = v_pos_norm;
|
// TODO: last 3 bits in v_pos_norm should be a number between 0 and 5, rather than 0-2 and a direction.
|
||||||
|
uint norm_axis = (v_pos_norm >> 30) & 0x3u;
|
||||||
|
|
||||||
|
// Increase array access by 3 to access positive values
|
||||||
|
uint norm_dir = ((v_pos_norm >> 29) & 0x1u) * 3u;
|
||||||
|
|
||||||
|
// Use an array to avoid conditional branching
|
||||||
|
f_norm = normals[norm_axis + norm_dir];
|
||||||
|
|
||||||
f_col = vec3(
|
f_col = vec3(
|
||||||
float((v_col_light >> 8) & 0xFFu),
|
float((v_col_light >> 8) & 0xFFu),
|
||||||
|
@ -18,12 +18,12 @@ use vek::*;
|
|||||||
/// Represents the format of the pre-processed color target.
|
/// Represents the format of the pre-processed color target.
|
||||||
pub type TgtColorFmt = gfx::format::Rgba16F;
|
pub type TgtColorFmt = gfx::format::Rgba16F;
|
||||||
/// Represents the format of the pre-processed depth target.
|
/// Represents the format of the pre-processed depth target.
|
||||||
pub type TgtDepthFmt = gfx::format::DepthStencil;
|
pub type TgtDepthFmt = gfx::format::Depth;
|
||||||
|
|
||||||
/// Represents the format of the window's color target.
|
/// Represents the format of the window's color target.
|
||||||
pub type WinColorFmt = gfx::format::Rgba8;
|
pub type WinColorFmt = gfx::format::Rgba8;
|
||||||
/// Represents the format of the window's depth target.
|
/// Represents the format of the window's depth target.
|
||||||
pub type WinDepthFmt = gfx::format::DepthStencil;
|
pub type WinDepthFmt = gfx::format::Depth;
|
||||||
|
|
||||||
/// A handle to a pre-processed color target.
|
/// A handle to a pre-processed color target.
|
||||||
pub type TgtColorView = gfx::handle::RenderTargetView<gfx_backend::Resources, TgtColorFmt>;
|
pub type TgtColorView = gfx::handle::RenderTargetView<gfx_backend::Resources, TgtColorFmt>;
|
||||||
|
Loading…
Reference in New Issue
Block a user