Sync shaders from vkdoom

This commit is contained in:
Magnus Norddahl 2024-03-04 23:43:30 +01:00
parent 85f1687769
commit 6ccc1fbc82
3 changed files with 36 additions and 29 deletions

View file

@ -1,6 +1,6 @@
static const char* trace_levelmesh_glsl = R"glsl( 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) SurfaceInfo GetSurface(int primitiveIndex)
{ {
@ -16,7 +16,7 @@ vec2 GetSurfaceUV(int primitiveIndex, vec3 primitiveWeights)
vertices[elements[index + 0]].uv * primitiveWeights.z; 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) if (surface.TextureIndex == 0)
{ {
@ -25,7 +25,16 @@ vec4 BlendTexture(SurfaceInfo surface, vec2 uv, vec4 rayColor)
else else
{ {
vec4 color = texture(textures[surface.TextureIndex], uv); 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; 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; return ray_color * exp(-medium * depth);
ray_color.rgb *= exp(-medium.rgb * vec3(z));
return ray_color;
} }
)glsl"; )glsl";

View file

@ -2,7 +2,7 @@ static const char* trace_light_glsl = R"glsl(
#include <shaders/lightmap/montecarlo.glsl> #include <shaders/lightmap/montecarlo.glsl>
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) 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; float attenuation = distAttenuation * angleAttenuation * spotAttenuation;
if (attenuation > 0.0) 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) #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; 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; 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 #else
incoming.rgb += TracePointLightRay(origin, light.Origin, minDistance, rayColor).rgb; incoming += TracePointLightRay(origin, light.Origin, minDistance, rayColor);
#endif #endif
} }
} }
@ -53,7 +53,7 @@ vec3 TraceLight(vec3 origin, vec3 normal, LightInfo light, float extraDistance)
return incoming; 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); vec3 dir = normalize(lightpos - origin);
float tmax = distance(origin, lightpos); 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); 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) if (result.primitiveIndex == -1)
return rayColor; return rayColor;
SurfaceInfo surface = GetSurface(result.primitiveIndex); SurfaceInfo surface = GetSurface(result.primitiveIndex);
// Blend with surface texture // Pass through surface texture
rayColor = BlendTexture(surface, GetSurfaceUV(result.primitiveIndex, result.primitiveWeights), rayColor); rayColor = PassRayThroughSurface(surface, GetSurfaceUV(result.primitiveIndex, result.primitiveWeights), rayColor);
// Stop if there is no light left // Stop if there is no light left
if (rayColor.r + rayColor.g + rayColor.b <= 0.0) if (rayColor.r + rayColor.g + rayColor.b <= 0.0)
return vec4(0.0); return vec3(0.0);
// Move to surface hit point // Move to surface hit point
origin += dir * result.t; origin += dir * result.t;
@ -82,7 +82,7 @@ vec4 TracePointLightRay(vec3 origin, vec3 lightpos, float tmin, vec4 rayColor)
// Move through the portal, if any // Move through the portal, if any
TransformRay(surface.PortalIndex, origin, dir); TransformRay(surface.PortalIndex, origin, dir);
} }
return vec4(0.0); return vec3(0.0);
} }
)glsl"; )glsl";

View file

@ -2,7 +2,7 @@ static const char* trace_sunlight_glsl = R"glsl(
#include <shaders/lightmap/montecarlo.glsl> #include <shaders/lightmap/montecarlo.glsl>
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) vec3 TraceSunLight(vec3 origin, vec3 normal)
{ {
@ -14,7 +14,7 @@ vec3 TraceSunLight(vec3 origin, vec3 normal)
vec3 incoming = vec3(0.0); vec3 incoming = vec3(0.0);
const float dist = 65536.0; const float dist = 65536.0;
vec4 rayColor = vec4(SunColor.rgb * SunIntensity, 1.0); vec3 rayColor = SunColor.rgb * SunIntensity;
#if defined(USE_SOFTSHADOWS) #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; vec2 gridoffset = getVogelDiskSample(i, step_count, gl_FragCoord.x + gl_FragCoord.y * 13.37) * lightsize;
vec3 pos = target + xdir * gridoffset.x + ydir * gridoffset.y; 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 #else
incoming.rgb = TraceSunRay(origin, minDistance, SunDir, dist, rayColor).rgb; incoming = TraceSunRay(origin, minDistance, SunDir, dist, rayColor);
#endif #endif
return incoming * angleAttenuation; 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++) for (int i = 0; i < 3; i++)
{ {
TraceResult result = TraceFirstHit(origin, tmin, dir, tmax); 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) if (result.primitiveIndex == -1)
return vec4(0.0); return vec3(0.0);
SurfaceInfo surface = GetSurface(result.primitiveIndex); 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) if (surface.Sky > 0.0)
return rayColor; return rayColor;
// Blend with surface texture // Pass through surface texture
rayColor = BlendTexture(surface, GetSurfaceUV(result.primitiveIndex, result.primitiveWeights), rayColor); rayColor = PassRayThroughSurface(surface, GetSurfaceUV(result.primitiveIndex, result.primitiveWeights), rayColor);
// Stop if there is no light left // Stop if there is no light left
if (rayColor.r + rayColor.g + rayColor.b <= 0.0) if (rayColor.r + rayColor.g + rayColor.b <= 0.0)
return vec4(0.0); return vec3(0.0);
// Move to surface hit point // Move to surface hit point
origin += dir * result.t; origin += dir * result.t;
tmax -= result.t; tmax -= result.t;
if (tmax <= tmin) if (tmax <= tmin)
return vec4(0.0); return vec3(0.0);
// Move through the portal, if any // Move through the portal, if any
TransformRay(surface.PortalIndex, origin, dir); TransformRay(surface.PortalIndex, origin, dir);
} }
return vec4(0.0); return vec3(0.0);
} }
)glsl"; )glsl";