Skip to content

Commit

Permalink
Implement crosstalk for tonemapping, begin implementing simple bloom
Browse files Browse the repository at this point in the history
  • Loading branch information
eira-fransham committed Dec 27, 2020
1 parent efce2f6 commit 81d0f40
Show file tree
Hide file tree
Showing 13 changed files with 1,364 additions and 223 deletions.
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 15 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
arraydeque = "0.4"
anyhow = "1"
async-trait = "0.1"
bsp = { path = "./bsp" }
collision = "0.20"
wgpu = "0.6"
winit = "0.23"
winit-async = { path = "./winit-async/" }
futures = "0.3"
bytemuck = "1"
lazy_static = "1.4"
cgmath = "0.17"
itertools = "0.9"
collision = "0.20"
fnv = "1"
rect_packer = "0.2"
futures = "0.3"
goldsrc-mdl = { path = "./goldsrc-mdl/" }
image = "0.23"
imgui = "0.6"
imgui-wgpu = "0.12"
imgui-winit-support = "0.6"
itertools = "0.9"
lazy_static = "1.4"
memoffset = "0.5"
anyhow = "1"
rect_packer = "0.2"
wgpu = "0.6"
winit = "0.23"
winit-async = { path = "./winit-async/" }

jemallocator = { version = "0.3", optional = true }
goldsrc-mdl = { path = "./goldsrc-mdl/" }

[build-dependencies]
shaderc = "0.6"
Expand Down
26 changes: 26 additions & 0 deletions shaders/dual_kawase_down.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#version 450
#pragma optimize(on)
#pragma shader_stage(fragment)

layout(location = 0) out vec4 outColor;

layout(set = 0, binding = 0) uniform sampler s_Color;
layout(set = 0, binding = 1) uniform Locals {
vec2 a_Offset;
};
layout(set = 1, binding = 0) uniform texture2D t_Diffuse;
layout(set = 1, binding = 1) uniform TexLocals {
vec2 a_InvResolution;
};

void main() {
vec2 uv = gl_FragCoord.xy * a_InvResolution;

vec4 sum = texture(sampler2D(t_Diffuse, s_Color), uv) * 4.0;
sum += texture(sampler2D(t_Diffuse, s_Color), uv - vec2(1.0) * a_Offset);
sum += texture(sampler2D(t_Diffuse, s_Color), uv + vec2(1.0) * a_Offset);
sum += texture(sampler2D(t_Diffuse, s_Color), uv + vec2(1.0, -1.0) * a_Offset);
sum += texture(sampler2D(t_Diffuse, s_Color), uv - vec2(1.0, -1.0) * a_Offset);

outColor = sum / 8.0;
}
29 changes: 29 additions & 0 deletions shaders/dual_kawase_up.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#version 450
#pragma optimize(on)
#pragma shader_stage(fragment)

layout(location = 0) out vec4 outColor;

layout(set = 0, binding = 0) uniform sampler s_Color;
layout(set = 0, binding = 1) uniform Locals {
vec2 a_Offset;
};
layout(set = 1, binding = 0) uniform texture2D t_Diffuse;
layout(set = 1, binding = 1) uniform TexLocals {
vec2 a_InvResolution;
};

void main() {
vec2 uv = gl_FragCoord.xy * a_InvResolution;

vec4 sum = texture(sampler2D(t_Diffuse, s_Color), uv + vec2(-2.0, 0.0) * a_Offset);
sum += texture(sampler2D(t_Diffuse, s_Color), uv + vec2(-1.0, 1.0) * a_Offset) * 2.0;
sum += texture(sampler2D(t_Diffuse, s_Color), uv + vec2(0.0, 2.0) * a_Offset);
sum += texture(sampler2D(t_Diffuse, s_Color), uv + vec2(1.0, 1.0) * a_Offset) * 2.0;
sum += texture(sampler2D(t_Diffuse, s_Color), uv + vec2(2.0, 0.0) * a_Offset);
sum += texture(sampler2D(t_Diffuse, s_Color), uv + vec2(1.0, -1.0) * a_Offset) * 2.0;
sum += texture(sampler2D(t_Diffuse, s_Color), uv + vec2(0.0, -2.0) * a_Offset);
sum += texture(sampler2D(t_Diffuse, s_Color), uv + vec2(-1.0, -1.0) * a_Offset) * 2.0;

outColor = sum / 12.0;
}
34 changes: 34 additions & 0 deletions shaders/hipass.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#version 450
#pragma optimize(on)
#pragma shader_stage(fragment)

layout(location = 0) out vec4 outColor;

layout(set = 0, binding = 0) uniform texture2D t_Diffuse;
layout(set = 0, binding = 1) uniform sampler s_Color;
layout(set = 0, binding = 2) uniform Locals {
vec2 a_InvResolution;
float a_Cutoff;
};

float luminance(vec3 color) {
const float RAmount = 0.2126;
const float GAmount = 0.7152;
const float BAmount = 0.0722;

return dot(color, vec3(RAmount, GAmount, BAmount));
}

vec3 ifLt(float a, float b, vec3 ifTrue, vec3 ifFalse) {
float lt = step(b, a);

return ifFalse * lt + ifTrue * (1 - lt);
}

void main() {
vec2 uv = gl_FragCoord.xy * a_InvResolution;
vec3 color = texture(sampler2D(t_Diffuse, s_Color), uv).rgb;

outColor = vec4(ifLt(luminance(color), a_Cutoff, vec3(0), color), 1.0);
}

89 changes: 74 additions & 15 deletions shaders/post.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
#pragma optimize(on)
#pragma shader_stage(fragment)

layout(location = 0) in vec2 v_TexCoord;

layout(location = 0) out vec4 outColor;

layout(set = 0, binding = 0) uniform texture2D t_Diffuse;
layout(set = 0, binding = 1) uniform sampler s_Color;
layout(set = 0, binding = 2) uniform Locals {
layout(set = 0, binding = 1) uniform texture2D t_Bloom;
layout(set = 0, binding = 2) uniform sampler s_Color;
layout(set = 0, binding = 3) uniform Locals {
float invGamma;
float intensity;
};
layout(set = 0, binding = 3) uniform PostLocals {
layout(set = 0, binding = 4) uniform PostLocals {
vec2 a_InvResolution;
bool a_FxaaEnabled;
uint a_TonemappingBitmap;
float a_InvCrosstalkAmt;
float a_Saturation;
float a_CrosstalkSaturation;
};

float luminance(vec3 color) {
Expand All @@ -32,27 +34,36 @@ vec3 applyLuminance(vec3 color, float lum) {
return color * scale;
}

float aces(float lum) {
float acesLum(float lum) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;

lum = (lum * (a * lum + b)) / (lum * (c * lum + d) + e);
lum = pow(lum, invGamma);

return clamp(lum, 0, 1);
return lum;
}

vec3 aces(vec3 color) {
float lum = aces(luminance(color));
vec3 acesLum(vec3 color) {
float lum = acesLum(luminance(color));

return applyLuminance(color, lum);
}

vec4 aces(vec4 color) {
return vec4(aces(vec3(color)), color.a);
vec4 acesLum(vec4 color) {
return vec4(acesLum(vec3(color)), color.a);
}

vec3 aces(vec3 color) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;

return (color * (a * color + b)) / (color * (c * color + d) + e);
}

vec3 ifBetween(float val, float min, float max, vec3 ifTrue, vec3 ifFalse) {
Expand Down Expand Up @@ -93,7 +104,55 @@ vec4 reinhard(vec4 color) {
return vec4(reinhard(vec3(color)), color.a);
}

vec3 crosstalk(vec3 tonemapped) {
float tonemappedMax = max(tonemapped.r, max(tonemapped.g, tonemapped.b));
vec3 ratio = tonemapped / tonemappedMax;
tonemappedMax = min(tonemappedMax, 1.0);

ratio = pow(ratio, vec3(a_Saturation / a_CrosstalkSaturation));
ratio = mix(ratio, vec3(1.0), pow(tonemappedMax, a_InvCrosstalkAmt));
ratio = pow(ratio, vec3(a_CrosstalkSaturation));

return ratio * tonemappedMax;
}

vec3 crosstalkLum(vec3 tonemapped) {
float tonemappedMax = luminance(tonemapped);
vec3 ratio = tonemapped / tonemappedMax;
tonemappedMax = min(tonemappedMax, 1.0);

ratio = pow(ratio, vec3(a_Saturation / a_CrosstalkSaturation));
ratio = mix(ratio, vec3(1.0), pow(tonemappedMax, a_InvCrosstalkAmt));
ratio = pow(ratio, vec3(a_CrosstalkSaturation));

return ratio * tonemappedMax;
}

void main() {
vec3 diffuse = texture(sampler2D(t_Diffuse, s_Color), v_TexCoord).rgb;
outColor = vec4(aces(diffuse * intensity), 1);
vec3 diffuse = texture(sampler2D(t_Bloom, s_Color), gl_FragCoord.xy * a_InvResolution).rgb * intensity;

bool tonemapping = (a_TonemappingBitmap & 0x1) != 0;
bool xyySpaceAces = (a_TonemappingBitmap & 0x2) != 0;
bool crosstalkEnabled = (a_TonemappingBitmap & 0x4) != 0;
bool xyySpaceCrosstalk = (a_TonemappingBitmap & 0x8) != 0;

vec3 final;
if (tonemapping) {
// We want to allow RGB>1 but we also want to fix degenerate/non-finite values
vec3 tonemapped = clamp(
xyySpaceAces ? acesLum(diffuse) : aces(diffuse),
vec3(0.0),
vec3(100.0)
);

if (crosstalkEnabled) {
final = xyySpaceCrosstalk ? crosstalkLum(tonemapped) : crosstalk(tonemapped);
} else {
final = tonemapped;
}
} else {
final = diffuse;
}

outColor = vec4(pow(final, vec3(invGamma)), 1.0);
}
12 changes: 2 additions & 10 deletions shaders/post.vert
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@
#pragma shader_stage(vertex)
#pragma optimize(on)

layout(location = 0) in vec4 a_Pos;
layout(location = 1) in vec2 a_TexCoord;

layout(location = 0) out vec2 v_TexCoord;

layout(binding = 3) uniform Locals {
vec2 u_InvResolution;
};
layout(location = 0) in vec2 a_Pos;

void main() {
v_TexCoord = a_TexCoord;
gl_Position = a_Pos;
gl_Position = vec4(a_Pos, 0, 1);
}
2 changes: 1 addition & 1 deletion shaders/world.frag
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ void main() {
sampler2D(t_Diffuse, s_Color),
(offset + vec2(v_Tex.xy) + vec2(v_TexStride * (frame % texCount), 0)) /
textureSize(sampler2D(t_Diffuse, s_Color), 0)
) * light * 8;
) * light * 32;
}
Loading

0 comments on commit 81d0f40

Please sign in to comment.