Skip to content

Commit

Permalink
RT-lighting in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Mar 7, 2024
1 parent 757ee90 commit 45a9d2d
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 47 deletions.
5 changes: 5 additions & 0 deletions game/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,18 @@ void Renderer::drawTonemapping(Encoder<CommandBuffer>& cmd) {
float brightness = 0;
float contrast = 1;
float gamma = 1.f/2.2f;
float mul = 1;
};

Push p;
p.brightness = (settings.zVidBrightness - 0.5f)*0.1f;
p.contrast = std::max(1.5f - settings.zVidContrast, 0.01f);
p.gamma = p.gamma/std::max(2.0f*settings.zVidGamma, 0.01f);

static float mul = 1.f;
if(mul>0)
p.mul = mul;

cmd.setUniforms(*tonemapping.pso, tonemapping.uboTone, &p, sizeof(p));
cmd.draw(Resources::fsqVbo());
}
Expand Down
2 changes: 1 addition & 1 deletion game/graphics/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Shaders::Shaders() {

state.setBlendSource (RenderState::BlendMode::One);
state.setBlendDest (RenderState::BlendMode::SrcAlpha); // for debugging
state.setZTestMode (RenderState::ZTestMode::Always);
state.setZTestMode (RenderState::ZTestMode::NoEqual);
state.setZWriteEnabled(false);
sh = GothicShader::get("probe_ambient.vert.sprv");
vs = device.shader(sh.data,sh.len);
Expand Down
34 changes: 25 additions & 9 deletions game/graphics/sky/sky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ using namespace Tempest;

// https://www.slideshare.net/LukasLang/physically-based-lighting-in-unreal-engine-4
// https://www.slideshare.net/DICEStudio/moving-frostbite-to-physically-based-rendering
// static const float DirectSunLux = 64'000.f; // 85'000.f;
static const float DirectSunLux = 143'000.f;
static const float DirectMoonLux = 0.27f;

/* https://physicallybased.info
* The changing color of the Sun over the course of the day is mainly a result of the scattering of sunlight
* and is not due to changes in black-body radiation. The solar illuminance constant is equal to 128 000 lux,
* but atmospheric extinction brings the number of lux down to around 100 000 lux.
*
* clear sky = 6000 lx
* sky overcast = 2000 lx
*
* opaque = (100'000/pi)*NdotL = 31'800*NdotL
*
*/
static const float DirectSunLux = 128'000.f;
static const float DirectMoonLux = 1.f;

// static const float NightLight = 0.36f;
// static const float ShadowSunLux = 10'000.f;
// static const float ShadowSunLux = 10'000.f;
// static const float StreetLight = 10.f;

static float smoothstep(float edge0, float edge1, float x) {
Expand Down Expand Up @@ -236,17 +247,22 @@ void Sky::updateLight(const int64_t now) {
static float ambMul = 1;
// static auto groundAlbedo = Vec3(0.34f, 0.42f, 0.26f); // Foliage(MacBeth)
// static auto groundAlbedo = Vec3(0.39f, 0.40f, 0.33f);
static auto groundAlbedo = Vec3(0.47f); // Neutral5 (MacBeth)
static auto groundAlbedo = Vec3(0.3f); // aligned to sky-shading

const float dirY = sun.dir().y;
// const float dirY = sun.dir().y;
// float dayTint = std::max(dirY+0.01f, 0.f);

const float aDirect = linearstep(-0.0f, 0.8f, dirY);
// const float aDirect = linearstep(-0.0f, 0.8f, dirY);
const float sunOcclude = smoothstep(0.0f, 0.01f, sun.dir().y);

Vec3 direct;
direct = Vec3(1.0f) * DirectSunLux;
ambient = groundAlbedo * DirectSunLux * aDirect * sunOcclude * 0.02f;
direct = DirectSunLux * Vec3(1.0f);
ambient = DirectSunLux * float(1.0/M_PI) * groundAlbedo * sunOcclude;
// ambient *= 0.78f; // atmosphere transmission is in shader
ambient *= 0.68f; // NdoL prediction
ambient *= 0.5; // maybe in shadow or maybe not
ambient *= 2.0; // 2*pi, pi accounted in shader
ambient += Vec3(0.01f); // should avoid zeros

sun.setColor(direct*sunMul);
ambient = ambient*ambMul;
Expand Down
8 changes: 1 addition & 7 deletions shader/common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ const float IorWater = 1.0 / 1.52; // air / water
const float IorAir = 1.52; // water /air
const vec3 WaterAlbedo = vec3(0.8,0.9,1.0);

const vec3 GGroundAlbedo = vec3(0.1);
const vec3 GGroundAlbedo = vec3(0.3);
const float Fd_Lambert = (1.0/M_PI);
const float Fd_LambertInv = (M_PI);

/* Color value is multiplied to achive adequate albedo and correctness in GI.
* Need to scale it back, when material receives direct light.
*/
const float GColorScaleInv = 8;//(6);
const float GColorScale = (1.0/GColorScaleInv);

float linearDepth(float d, vec3 clipInfo) {
// z_n * z_f, z_n - z_f, z_f
return (clipInfo[0] / (clipInfo[1] * d + clipInfo[2]));
Expand Down
7 changes: 4 additions & 3 deletions shader/lighting/ambient_light.frag
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,18 @@ void main() {
const vec3 norm = normalFetch(gbufNormal, fragCoord);

// const vec3 linear = vec3(1);
const vec3 linear = textureLinear(diff); // * Fd_Lambert is accounted in integration
const vec3 linear = textureLinear(diff);
const float ao = textureSsao();

vec3 ambient = scene.ambient;
vec3 sky = skyIrradiance();

vec3 lcolor = mix(ambient, sky, max(0, 0.25 + norm.y*0.25));
//vec3 lcolor = mix(ambient, sky, max(0, norm.y*0.8));
vec3 lcolor = (ambient + sky)*0.5;
// vec3 lcolor = ambient + sky*clamp(norm.y*0.5, 0,1);

vec3 color = lcolor.rgb;
color *= linear;
color *= linear; // * Fd_Lambert is accounted in integration
color *= (1-ao);

// outColor = vec4(vec3(grayscale(color)), 0);
Expand Down
2 changes: 1 addition & 1 deletion shader/lighting/direct_light.frag
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void main(void) {
*/
#endif

const vec3 lcolor = scene.sunColor * Fd_Lambert * light * shadow * GColorScale;
const vec3 lcolor = scene.sunColor * Fd_Lambert * light * shadow;
const vec3 linear = textureLinear(diff.rgb);

vec3 color = linear*lcolor*scene.exposure;
Expand Down
2 changes: 1 addition & 1 deletion shader/lighting/irradiance.comp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void storeSample(vec3 dir, vec3 val) {

vec3 resolveMiss(const vec3 rayOrigin, const vec3 rayDirection) {
// PERF: need to preload sky lut early in shader
return textureSkyLUT(skyLUT, vec3(0,RPlanet,0), rayDirection, scene.sunDir) * scene.GSunIntensity * GColorScale;
return textureSkyLUT(skyLUT, vec3(0,RPlanet,0), rayDirection, scene.sunDir) * scene.GSunIntensity;
}

void gather() {
Expand Down
2 changes: 1 addition & 1 deletion shader/lighting/light.frag
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void main(void) {
const vec3 d = texelFetch(gbufDiffuse, ivec2(gl_FragCoord.xy), 0).xyz;
const vec3 linear = textureLinear(d.rgb);

vec3 color = linear*color*light*Fd_Lambert*GColorScale*0.5;
vec3 color = linear*color*light*Fd_Lambert*0.5;
//color *= scene.exposure;

outColor = vec4(color,0.0);
Expand Down
22 changes: 13 additions & 9 deletions shader/lighting/rt/probe_ambient.frag
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_control_flow_attributes : enable

//#extension GL_KHR_shader_subgroup_vote : enable

#include "lighting/rt/probe_common.glsl"
#include "lighting/tonemapping.glsl"
#include "lighting/purkinje_shift.glsl"
Expand Down Expand Up @@ -134,6 +136,10 @@ void gather(vec3 basePos, vec3 pos, vec3 norm, int lod) {

probeQuery pq;
probeQueryInitialize(pq, pos, lod);

// if(subgroupAllEqual(pq.pLow) && subgroupAllEqual(pq.pHigh))
// return;

while(probeQueryProceed(pq)) {
vec3 wpos = probeQueryWorldPos(pq);
ivec3 gPos = probeQueryGridPos(pq);
Expand All @@ -151,17 +157,15 @@ float textureSsao() { return 0; }
#endif

void main() {
const float minW = uintBitsToFloat(0x00000008);
const float z = texelFetch(depth,ivec2(gl_FragCoord.xy),0).x;
if(z>=1.0)
discard; // sky
const float minW = uintBitsToFloat(0x00000008);
const float z = texelFetch(depth,ivec2(gl_FragCoord.xy),0).x;

const vec3 diff = texelFetch(gbufDiffuse, ivec2(gl_FragCoord.xy), 0).rgb;
const vec3 norm = normalFetch(gbufNormal, ivec2(gl_FragCoord.xy));
const vec3 diff = texelFetch(gbufDiffuse, ivec2(gl_FragCoord.xy), 0).rgb;
const vec3 norm = normalFetch(gbufNormal, ivec2(gl_FragCoord.xy));

const int lod = probeGridComputeLod();
const vec3 basePos = unprojectDepth(z);
const vec3 pos = basePos + norm*probeCageBias;
const int lod = probeGridComputeLod();
const vec3 basePos = unprojectDepth(z);
const vec3 pos = basePos + norm*probeCageBias;

gather(basePos, pos, norm, lod);

Expand Down
39 changes: 29 additions & 10 deletions shader/lighting/rt/probe_lighting.comp
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,33 @@ vec3 resolveHit(const vec3 rayOrigin, const vec3 rayDirection, float rayT, vec3

const float lambert = max(0, dt); //max(opaque ? 0 : 0.2, dt);
const float shadow = max(0, shMap);
const vec3 lcolor = scene.sunColor * Fd_Lambert * lambert * shadow * GColorScale;
const vec3 lcolor = scene.sunColor * Fd_Lambert * lambert * shadow;

return lcolor;
//return scene.sunColor * scene.ambient;
}

vec3 resolveMiss(const vec3 rayOrigin, const vec3 rayDirection) {
// PERF: need to preload sky lut early in shader
return textureSkyLUT(skyLUT, vec3(0,RPlanet,0), rayDirection, scene.sunDir) * scene.GSunIntensity * GColorScale;
return textureSkyLUT(skyLUT, vec3(0,RPlanet,0), rayDirection, scene.sunDir) * scene.GSunIntensity;
}

vec3 resolveSecondBounce(vec3 pos, vec3 norm, uint selfProbeId) {
return scene.ambient;// * 2.0;

#if 0
return scene.ambient;
#elif 1
vec3 ambient = scene.ambient;
vec3 sky = probeReadAmbient(probesLightingPrev, 0, norm, vec3(0,1,0));
return (ambient + sky)*0.5;
#elif 0
vec3 ambient = scene.ambient;
// vec3 sky = skyIrradiance();
// vec3 sky = resolveMiss(pos, -norm);
vec3 sky = probeReadAmbient(probesLightingPrev, 0, norm, vec3(0,1,0));
return mix(ambient, sky, max(0, norm.y*0.8));
#elif 0
return resolveMiss(pos, -norm);
#else
// doesn't look coherent yet - need to keep offscreen data for long term
const ivec3 gridPos = ivec3(round(pos/(probeGridStep*1)));
const uint h = probeGridPosHash(gridPos) % hashTable.length();
Expand All @@ -110,19 +123,19 @@ vec3 resolveSecondBounce(vec3 pos, vec3 norm, uint selfProbeId) {
continue;
}

//return vec3(1000, 0,0);
return probeReadAmbient(probesLightingPrev, probeId, norm, p.normal);
}

// probeId = selfProbeId;
// return vec3(0.01,0,0)*scene.GSunIntensity;
return scene.ambient;
// return resolveMiss(pos, -norm);
// return scene.ambient;
return resolveMiss(pos, -norm);
// return vec3(0);
#endif
}

vec4 processSample(const vec3 rayOrigin, const vec3 rayDirection, uint probeId, uint sampleId) {
//return vec4(max(vec3(0),rayDirection*scene.GSunIntensity), 1);//*0.5 + vec3(0.5);

const ivec2 uv = gbufferCoord(probeId, sampleId);
const vec4 diff = texelFetch(gbufferDiff, uv, 0);
const vec4 nr = texelFetch(gbufferNorm, uv, 0);
Expand All @@ -137,11 +150,17 @@ vec4 processSample(const vec3 rayOrigin, const vec3 rayDirection, uint probeId,
// hit *= 0.001;
return vec4(hit, badbit ? 0 : 1);
}
#if 0
{
vec3 hit = resolveMiss(rayOrigin, rayDirection);
return vec4(hit, badbit ? 0 : 1);
}
#endif

const bool opaque = (bits & 0x1)!=0;
const bool semiSky = (bits & 0x8)!=0;

const vec3 albedo = textureLinear(diff.rgb);
const vec3 albedo = textureAlbedo(diff.rgb);
const vec3 norm = normalize(nr.xyz*2.0-vec3(1));

const float rayT = rt * probeRayDistance;
Expand All @@ -153,7 +172,7 @@ vec4 processSample(const vec3 rayOrigin, const vec3 rayDirection, uint probeId,

vec3 hit = vec3(0);
hit += albedo * resolveHit(rayOrigin, rayDirection, rayT, norm, opaque, shadow);
hit += albedo * resolveSecondBounce(pos, -rayDirection, probeId);
hit += albedo * resolveSecondBounce(pos, norm, probeId);
// hit += albedo * resolveHit(rayOrigin, rayDirection, rayT, norm, opaque, shadow);
// hit += albedo * resolveSecondBounce(pos, -rayDirection, probeId) / max(1.0 - albedo, 0.1);

Expand Down
3 changes: 2 additions & 1 deletion shader/lighting/sky_exposure.comp
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ void main() {
vec3 light = sunColor*Fd_Lambert*1.25 + ambient + skyIrradiance();
float lum = (vmax+vmin) * 0.5 * scene.GSunIntensity;
//lum = dot(light, vec3(1.0/3.0));
lum = dot(light, vec3(0.2125, 0.7154, 0.0721))*0.6 + lum*0.4;
lum = dot(light, vec3(0.2125, 0.7154, 0.0721))*0.9 + lum*0.1;

// float lum = tr.b;
scene.sunColor = sunColor * clouds * push.sunOcclusion;
scene.ambient = ambient * clouds;
scene.exposure = 1.0 / max(lum+push.baseL, 0.0000005);
//scene.exposure = 1.0 / max(6400, 0.0000005);
scene.luminanceMed = lum;
}
}
10 changes: 9 additions & 1 deletion shader/lighting/tonemapping.frag
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ layout(push_constant, std140) uniform PushConstant {
float brightness;
float contrast;
float gamma;
float mulExposure;
} push;

layout(binding = 0, std140) uniform UboScene {
Expand Down Expand Up @@ -118,6 +119,11 @@ void main() {
// return;
}

{
// outColor = vec4(vec3(luminance(color/exposure)/100000.0), 1);
// return;
}

{
// outColor = vec4(colorTemperatureToRGB(luminance(color) / push.exposure), 1);
// return;
Expand All @@ -130,12 +136,14 @@ void main() {
// color += vec3(0,0, shift.b);
}

color *= push.mulExposure;

// Brightness & Contrast
color = max(vec3(0), color + vec3(brightness));
color = color * vec3(contrast);

// Tonemapping
color = acesTonemap(color)*1.28;
color = acesTonemap(color);

// Gamma
//color = srgbEncode(color);
Expand Down
12 changes: 10 additions & 2 deletions shader/lighting/tonemapping.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,22 @@ vec3 textureLinear(vec3 rgb) {
// emissive objects, spells
return acesTonemapInv(linear);
#else
// NOTE: in theory, need to be same as textureAlbedo, but game does't look like gothic then :(
return acesTonemapInv(linear*0.78+0.001)*2.0;
#endif
}

vec3 textureAlbedo(vec3 rgb) {
// return vec3(0.58, 0.49, 0.46); // brick
// return vec3(0.26, 0.11, 0.06); // brick2
// return vec3(0.52, 0.41, 0.36); // wood
// return vec3(0.48, 0.53, 0.30); // grass
// return vec3(0.44, 0.39, 0.23); // sand
// return vec3(0.9);
// return acesTonemapInv(linear*0.8);
// return acesTonemapInv(linear*0.78+0.001);
return acesTonemapInv(linear*0.78+0.001)*GColorScaleInv; // adjusted to have 'realistic' albedo values
#endif
const vec3 linear = srgbDecode(rgb);
return acesTonemapInv(linear*0.78+0.001)*5.0; // adjusted to have 'realistic' albedo values
}

#endif
2 changes: 1 addition & 1 deletion shader/sky/sky_view_lut.frag
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void main() {
const vec3 viewPos = vec3(0.0, RPlanet + push.plPosY, 0.0);

const float DirectSunLux = scene.GSunIntensity;
const float DirectMoonLux = 0.27f;
const float DirectMoonLux = 0.32f;
// const float NightLight = 0.36f;
const float moonInt = DirectMoonLux/DirectSunLux;

Expand Down

0 comments on commit 45a9d2d

Please sign in to comment.