From 6ccc1fbc823d39b99a7d6aa85aa4473f1e168d42 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Mon, 4 Mar 2024 23:43:30 +0100 Subject: [PATCH] Sync shaders from vkdoom --- src/lightmapper/glsl/trace_levelmesh.glsl.h | 21 ++++++++++++------ src/lightmapper/glsl/trace_light.glsl.h | 20 ++++++++--------- src/lightmapper/glsl/trace_sunlight.glsl.h | 24 ++++++++++----------- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/lightmapper/glsl/trace_levelmesh.glsl.h b/src/lightmapper/glsl/trace_levelmesh.glsl.h index 0a830cd..f1714dc 100644 --- a/src/lightmapper/glsl/trace_levelmesh.glsl.h +++ b/src/lightmapper/glsl/trace_levelmesh.glsl.h @@ -1,6 +1,6 @@ static const char* trace_levelmesh_glsl = R"glsl( -vec4 BeerLambertSimple(vec4 medium, vec4 ray_color); +vec3 BeerLambertSimple(vec3 medium, float depth, vec3 ray_color); SurfaceInfo GetSurface(int primitiveIndex) { @@ -16,7 +16,7 @@ vec2 GetSurfaceUV(int primitiveIndex, vec3 primitiveWeights) vertices[elements[index + 0]].uv * primitiveWeights.z; } -vec4 BlendTexture(SurfaceInfo surface, vec2 uv, vec4 rayColor) +vec3 PassRayThroughSurface(SurfaceInfo surface, vec2 uv, vec3 rayColor) { if (surface.TextureIndex == 0) { @@ -25,7 +25,16 @@ vec4 BlendTexture(SurfaceInfo surface, vec2 uv, vec4 rayColor) else { vec4 color = texture(textures[surface.TextureIndex], uv); - return BeerLambertSimple(vec4(1.0 - color.rgb, color.a * surface.Alpha), rayColor); + + // To do: currently we do not know the material/renderstyle of the surface. + // + // This means we can't apply translucency and we can't do something like BeerLambertSimple. + // In order to improve this SurfaceInfo needs additional info. + // + // return BeerLambertSimple(1.0 - color.rgb, color.a * surface.Alpha, rayColor); + + // Assume the renderstyle is basic alpha blend for now. + return rayColor * (1.0 - color.a * surface.Alpha); } } @@ -36,11 +45,9 @@ void TransformRay(uint portalIndex, inout vec3 origin, inout vec3 dir) dir = (transformationMatrix * vec4(dir, 0.0)).xyz; } -vec4 BeerLambertSimple(vec4 medium, vec4 ray_color) // based on Beer-Lambert law +vec3 BeerLambertSimple(vec3 medium, float depth, vec3 ray_color) // based on Beer-Lambert law { - float z = medium.w; - ray_color.rgb *= exp(-medium.rgb * vec3(z)); - return ray_color; + return ray_color * exp(-medium * depth); } )glsl"; diff --git a/src/lightmapper/glsl/trace_light.glsl.h b/src/lightmapper/glsl/trace_light.glsl.h index b36b94a..840b7f5 100644 --- a/src/lightmapper/glsl/trace_light.glsl.h +++ b/src/lightmapper/glsl/trace_light.glsl.h @@ -2,7 +2,7 @@ static const char* trace_light_glsl = R"glsl( #include -vec4 TracePointLightRay(vec3 origin, vec3 lightpos, float tmin, vec4 rayColor); +vec3 TracePointLightRay(vec3 origin, vec3 lightpos, float tmin, vec3 rayColor); vec3 TraceLight(vec3 origin, vec3 normal, LightInfo light, float extraDistance) { @@ -26,7 +26,7 @@ vec3 TraceLight(vec3 origin, vec3 normal, LightInfo light, float extraDistance) float attenuation = distAttenuation * angleAttenuation * spotAttenuation; if (attenuation > 0.0) { - vec4 rayColor = vec4(light.Color.rgb * (attenuation * light.Intensity), 1.0); + vec3 rayColor = light.Color.rgb * (attenuation * light.Intensity); #if defined(USE_SOFTSHADOWS) @@ -41,11 +41,11 @@ vec3 TraceLight(vec3 origin, vec3 normal, LightInfo light, float extraDistance) vec2 gridoffset = getVogelDiskSample(i, step_count, gl_FragCoord.x + gl_FragCoord.y * 13.37) * lightsize; vec3 pos = light.Origin + xdir * gridoffset.x + ydir * gridoffset.y; - incoming.rgb += TracePointLightRay(origin, pos, minDistance, rayColor).rgb / float(step_count); + incoming += TracePointLightRay(origin, pos, minDistance, rayColor) / float(step_count); } #else - incoming.rgb += TracePointLightRay(origin, light.Origin, minDistance, rayColor).rgb; + incoming += TracePointLightRay(origin, light.Origin, minDistance, rayColor); #endif } } @@ -53,7 +53,7 @@ vec3 TraceLight(vec3 origin, vec3 normal, LightInfo light, float extraDistance) return incoming; } -vec4 TracePointLightRay(vec3 origin, vec3 lightpos, float tmin, vec4 rayColor) +vec3 TracePointLightRay(vec3 origin, vec3 lightpos, float tmin, vec3 rayColor) { vec3 dir = normalize(lightpos - origin); float tmax = distance(origin, lightpos); @@ -62,18 +62,18 @@ vec4 TracePointLightRay(vec3 origin, vec3 lightpos, float tmin, vec4 rayColor) { TraceResult result = TraceFirstHit(origin, tmin, dir, tmax); - // We hit nothing. Point light is visible. + // Stop if we hit nothing - the point light is visible. if (result.primitiveIndex == -1) return rayColor; SurfaceInfo surface = GetSurface(result.primitiveIndex); - // Blend with surface texture - rayColor = BlendTexture(surface, GetSurfaceUV(result.primitiveIndex, result.primitiveWeights), rayColor); + // Pass through surface texture + rayColor = PassRayThroughSurface(surface, GetSurfaceUV(result.primitiveIndex, result.primitiveWeights), rayColor); // Stop if there is no light left if (rayColor.r + rayColor.g + rayColor.b <= 0.0) - return vec4(0.0); + return vec3(0.0); // Move to surface hit point origin += dir * result.t; @@ -82,7 +82,7 @@ vec4 TracePointLightRay(vec3 origin, vec3 lightpos, float tmin, vec4 rayColor) // Move through the portal, if any TransformRay(surface.PortalIndex, origin, dir); } - return vec4(0.0); + return vec3(0.0); } )glsl"; diff --git a/src/lightmapper/glsl/trace_sunlight.glsl.h b/src/lightmapper/glsl/trace_sunlight.glsl.h index e7cc26c..8d81f9e 100644 --- a/src/lightmapper/glsl/trace_sunlight.glsl.h +++ b/src/lightmapper/glsl/trace_sunlight.glsl.h @@ -2,7 +2,7 @@ static const char* trace_sunlight_glsl = R"glsl( #include -vec4 TraceSunRay(vec3 origin, float tmin, vec3 dir, float tmax, vec4 rayColor); +vec3 TraceSunRay(vec3 origin, float tmin, vec3 dir, float tmax, vec3 rayColor); vec3 TraceSunLight(vec3 origin, vec3 normal) { @@ -14,7 +14,7 @@ vec3 TraceSunLight(vec3 origin, vec3 normal) vec3 incoming = vec3(0.0); const float dist = 65536.0; - vec4 rayColor = vec4(SunColor.rgb * SunIntensity, 1.0); + vec3 rayColor = SunColor.rgb * SunIntensity; #if defined(USE_SOFTSHADOWS) @@ -30,27 +30,27 @@ vec3 TraceSunLight(vec3 origin, vec3 normal) { vec2 gridoffset = getVogelDiskSample(i, step_count, gl_FragCoord.x + gl_FragCoord.y * 13.37) * lightsize; vec3 pos = target + xdir * gridoffset.x + ydir * gridoffset.y; - incoming.rgb += TraceSunRay(origin, minDistance, normalize(pos - origin), dist, rayColor).rgb / float(step_count); + incoming += TraceSunRay(origin, minDistance, normalize(pos - origin), dist, rayColor) / float(step_count); } #else - incoming.rgb = TraceSunRay(origin, minDistance, SunDir, dist, rayColor).rgb; + incoming = TraceSunRay(origin, minDistance, SunDir, dist, rayColor); #endif return incoming * angleAttenuation; } -vec4 TraceSunRay(vec3 origin, float tmin, vec3 dir, float tmax, vec4 rayColor) +vec3 TraceSunRay(vec3 origin, float tmin, vec3 dir, float tmax, vec3 rayColor) { for (int i = 0; i < 3; i++) { TraceResult result = TraceFirstHit(origin, tmin, dir, tmax); - // We hit nothing. We have to hit a sky surface to hit the sky. + // Stop if we hit nothing. We have to hit a sky surface to hit the sky. if (result.primitiveIndex == -1) - return vec4(0.0); + return vec3(0.0); SurfaceInfo surface = GetSurface(result.primitiveIndex); @@ -58,23 +58,23 @@ vec4 TraceSunRay(vec3 origin, float tmin, vec3 dir, float tmax, vec4 rayColor) if (surface.Sky > 0.0) return rayColor; - // Blend with surface texture - rayColor = BlendTexture(surface, GetSurfaceUV(result.primitiveIndex, result.primitiveWeights), rayColor); + // Pass through surface texture + rayColor = PassRayThroughSurface(surface, GetSurfaceUV(result.primitiveIndex, result.primitiveWeights), rayColor); // Stop if there is no light left if (rayColor.r + rayColor.g + rayColor.b <= 0.0) - return vec4(0.0); + return vec3(0.0); // Move to surface hit point origin += dir * result.t; tmax -= result.t; if (tmax <= tmin) - return vec4(0.0); + return vec3(0.0); // Move through the portal, if any TransformRay(surface.PortalIndex, origin, dir); } - return vec4(0.0); + return vec3(0.0); } )glsl";