1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-02-28 10:39:40 +00:00

Move common shadow code to shared shadow shader headers

This commit is contained in:
AnyOldName3 2018-06-24 23:40:52 +01:00
parent 933ed77ef6
commit a7e53df278
9 changed files with 70 additions and 148 deletions

View file

@ -18,6 +18,8 @@ set(SHADER_FILES
parallax.glsl parallax.glsl
s360_fragment.glsl s360_fragment.glsl
s360_vertex.glsl s360_vertex.glsl
shadows_vertex.glsl
shadows_fragment.glsl
) )
copy_all_resource_files(${CMAKE_CURRENT_SOURCE_DIR} ${OPENMW_SHADERS_ROOT} ${DDIRRELATIVE} "${SHADER_FILES}") copy_all_resource_files(${CMAKE_CURRENT_SOURCE_DIR} ${OPENMW_SHADERS_ROOT} ${DDIRRELATIVE} "${SHADER_FILES}")

View file

@ -1,7 +1,5 @@
#version 120 #version 120
#define SHADOWS @shadows_enabled
#if @diffuseMap #if @diffuseMap
uniform sampler2D diffuseMap; uniform sampler2D diffuseMap;
varying vec2 diffuseMapUV; varying vec2 diffuseMapUV;
@ -57,13 +55,7 @@ centroid varying vec4 passColor;
varying vec3 passViewPos; varying vec3 passViewPos;
varying vec3 passNormal; varying vec3 passNormal;
#if SHADOWS #include "shadows_fragment.glsl"
@foreach shadow_texture_unit_index @shadow_texture_unit_list
uniform sampler2DShadow shadowTexture@shadow_texture_unit_index;
varying vec4 shadowSpaceCoords@shadow_texture_unit_index;
@endforeach
#endif // SHADOWS
#include "lighting.glsl" #include "lighting.glsl"
#include "parallax.glsl" #include "parallax.glsl"
@ -122,29 +114,7 @@ void main()
gl_FragData[0].xyz = mix(gl_FragData[0].xyz, decalTex.xyz, decalTex.a); gl_FragData[0].xyz = mix(gl_FragData[0].xyz, decalTex.xyz, decalTex.a);
#endif #endif
float shadowing = 1.0; float shadowing = unshadowedLightRatio();
#if SHADOWS
#if @shadowMapsOverlap
bool doneShadows = false;
@foreach shadow_texture_unit_index @shadow_texture_unit_list
if (!doneShadows)
{
vec2 shadowXY = shadowSpaceCoords@shadow_texture_unit_index.xy / shadowSpaceCoords@shadow_texture_unit_index.w;
if (all(lessThan(shadowXY, vec2(1.0, 1.0))) && all(greaterThan(shadowXY, vec2(0.0, 0.0))))
{
shadowing *= shadow2DProj(shadowTexture@shadow_texture_unit_index, shadowSpaceCoords@shadow_texture_unit_index).r;
if (all(lessThan(shadowXY, vec2(0.95, 0.95))) && all(greaterThan(shadowXY, vec2(0.05, 0.05))))
doneShadows = true;
}
}
@endforeach
#else
@foreach shadow_texture_unit_index @shadow_texture_unit_list
shadowing *= shadow2DProj(shadowTexture@shadow_texture_unit_index, shadowSpaceCoords@shadow_texture_unit_index).r;
@endforeach
#endif
#endif // SHADOWS
#if !PER_PIXEL_LIGHTING #if !PER_PIXEL_LIGHTING
gl_FragData[0] *= lighting + vec4(shadowDiffuseLighting * shadowing, 0); gl_FragData[0] *= lighting + vec4(shadowDiffuseLighting * shadowing, 0);

View file

@ -1,7 +1,5 @@
#version 120 #version 120
#define SHADOWS @shadows_enabled
#if @diffuseMap #if @diffuseMap
varying vec2 diffuseMapUV; varying vec2 diffuseMapUV;
#endif #endif
@ -48,12 +46,7 @@ centroid varying vec4 passColor;
varying vec3 passViewPos; varying vec3 passViewPos;
varying vec3 passNormal; varying vec3 passNormal;
#if SHADOWS #include "shadows_vertex.glsl"
@foreach shadow_texture_unit_index @shadow_texture_unit_list
uniform int shadowTextureUnit@shadow_texture_unit_index;
varying vec4 shadowSpaceCoords@shadow_texture_unit_index;
@endforeach
#endif // SHADOWS
#include "lighting.glsl" #include "lighting.glsl"
@ -110,12 +103,5 @@ void main(void)
passViewPos = viewPos.xyz; passViewPos = viewPos.xyz;
passNormal = gl_Normal.xyz; passNormal = gl_Normal.xyz;
#if SHADOWS setupShadowCoords(viewPos);
// This matrix has the opposite handedness to the others used here, so multiplication must have the vector to the left. Alternatively it could be transposed after construction, but that's extra work for the GPU just to make the code look a tiny bit cleaner.
mat4 eyePlaneMat;
@foreach shadow_texture_unit_index @shadow_texture_unit_list
eyePlaneMat = mat4(gl_EyePlaneS[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneT[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneR[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneQ[shadowTextureUnit@shadow_texture_unit_index]);
shadowSpaceCoords@shadow_texture_unit_index = viewPos * eyePlaneMat;
@endforeach
#endif // SHADOWS
} }

View file

@ -0,0 +1,36 @@
#define SHADOWS @shadows_enabled
#if SHADOWS
@foreach shadow_texture_unit_index @shadow_texture_unit_list
uniform sampler2DShadow shadowTexture@shadow_texture_unit_index;
varying vec4 shadowSpaceCoords@shadow_texture_unit_index;
@endforeach
#endif // SHADOWS
float unshadowedLightRatio()
{
float shadowing = 1.0;
#if SHADOWS
#if @shadowMapsOverlap
bool doneShadows = false;
@foreach shadow_texture_unit_index @shadow_texture_unit_list
if (!doneShadows)
{
vec2 shadowXY = shadowSpaceCoords@shadow_texture_unit_index.xy / shadowSpaceCoords@shadow_texture_unit_index.w;
if (all(lessThan(shadowXY, vec2(1.0, 1.0))) && all(greaterThan(shadowXY, vec2(0.0, 0.0))))
{
shadowing = min(shadow2DProj(shadowTexture@shadow_texture_unit_index, shadowSpaceCoords@shadow_texture_unit_index).r, shadowing);
if (all(lessThan(shadowXY, vec2(0.95, 0.95))) && all(greaterThan(shadowXY, vec2(0.05, 0.05))))
doneShadows = true;
}
}
@endforeach
#else
@foreach shadow_texture_unit_index @shadow_texture_unit_list
shadowing = min(shadow2DProj(shadowTexture@shadow_texture_unit_index, shadowSpaceCoords@shadow_texture_unit_index).r, shadowing);
@endforeach
#endif
#endif // SHADOWS
return shadowing;
}

View file

@ -0,0 +1,20 @@
#define SHADOWS @shadows_enabled
#if SHADOWS
@foreach shadow_texture_unit_index @shadow_texture_unit_list
uniform int shadowTextureUnit@shadow_texture_unit_index;
varying vec4 shadowSpaceCoords@shadow_texture_unit_index;
@endforeach
#endif // SHADOWS
void setupShadowCoords(vec4 viewPos)
{
#if SHADOWS
// This matrix has the opposite handedness to the others used here, so multiplication must have the vector to the left. Alternatively it could be transposed after construction, but that's extra work for the GPU just to make the code look a tiny bit cleaner.
mat4 eyePlaneMat;
@foreach shadow_texture_unit_index @shadow_texture_unit_list
eyePlaneMat = mat4(gl_EyePlaneS[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneT[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneR[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneQ[shadowTextureUnit@shadow_texture_unit_index]);
shadowSpaceCoords@shadow_texture_unit_index = viewPos * eyePlaneMat;
@endforeach
#endif // SHADOWS
}

View file

@ -1,7 +1,5 @@
#version 120 #version 120
#define SHADOWS @shadows_enabled
varying vec2 uv; varying vec2 uv;
uniform sampler2D diffuseMap; uniform sampler2D diffuseMap;
@ -27,13 +25,7 @@ centroid varying vec4 passColor;
varying vec3 passViewPos; varying vec3 passViewPos;
varying vec3 passNormal; varying vec3 passNormal;
#if SHADOWS #include "shadows_fragment.glsl"
@foreach shadow_texture_unit_index @shadow_texture_unit_list
uniform sampler2DShadow shadowTexture@shadow_texture_unit_index;
varying vec4 shadowSpaceCoords@shadow_texture_unit_index;
@endforeach
#endif // SHADOWS
#include "lighting.glsl" #include "lighting.glsl"
#include "parallax.glsl" #include "parallax.glsl"
@ -74,29 +66,7 @@ void main()
gl_FragData[0].a *= texture2D(blendMap, blendMapUV).a; gl_FragData[0].a *= texture2D(blendMap, blendMapUV).a;
#endif #endif
float shadowing = 1.0; float shadowing = unshadowedLightRatio();
#if SHADOWS
#if @shadowMapsOverlap
bool doneShadows = false;
@foreach shadow_texture_unit_index @shadow_texture_unit_list
if (!doneShadows)
{
vec2 shadowXY = shadowSpaceCoords@shadow_texture_unit_index.xy / shadowSpaceCoords@shadow_texture_unit_index.w;
if (all(lessThan(shadowXY, vec2(1.0, 1.0))) && all(greaterThan(shadowXY, vec2(0.0, 0.0))))
{
shadowing *= shadow2DProj(shadowTexture@shadow_texture_unit_index, shadowSpaceCoords@shadow_texture_unit_index).r;
if (all(lessThan(shadowXY, vec2(0.95, 0.95))) && all(greaterThan(shadowXY, vec2(0.05, 0.05))))
doneShadows = true;
}
}
@endforeach
#else
@foreach shadow_texture_unit_index @shadow_texture_unit_list
shadowing *= shadow2DProj(shadowTexture@shadow_texture_unit_index, shadowSpaceCoords@shadow_texture_unit_index).r;
@endforeach
#endif
#endif // SHADOWS
#if !PER_PIXEL_LIGHTING #if !PER_PIXEL_LIGHTING
gl_FragData[0] *= lighting + vec4(shadowDiffuseLighting * shadowing, 0); gl_FragData[0] *= lighting + vec4(shadowDiffuseLighting * shadowing, 0);

View file

@ -1,7 +1,5 @@
#version 120 #version 120
#define SHADOWS @shadows_enabled
varying vec2 uv; varying vec2 uv;
varying float depth; varying float depth;
@ -16,12 +14,7 @@ centroid varying vec4 passColor;
varying vec3 passViewPos; varying vec3 passViewPos;
varying vec3 passNormal; varying vec3 passNormal;
#if SHADOWS #include "shadows_vertex.glsl"
@foreach shadow_texture_unit_index @shadow_texture_unit_list
uniform int shadowTextureUnit@shadow_texture_unit_index;
varying vec4 shadowSpaceCoords@shadow_texture_unit_index;
@endforeach
#endif // SHADOWS
#include "lighting.glsl" #include "lighting.glsl"
@ -44,12 +37,5 @@ void main(void)
uv = gl_MultiTexCoord0.xy; uv = gl_MultiTexCoord0.xy;
#if SHADOWS setupShadowCoords(viewPos);
// This matrix has the opposite handedness to the others used here, so multiplication must have the vector to the left. Alternatively it could be transposed after construction, but that's extra work for the GPU just to make the code look a tiny bit cleaner.
mat4 eyePlaneMat;
@foreach shadow_texture_unit_index @shadow_texture_unit_list
eyePlaneMat = mat4(gl_EyePlaneS[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneT[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneR[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneQ[shadowTextureUnit@shadow_texture_unit_index]);
shadowSpaceCoords@shadow_texture_unit_index = viewPos * eyePlaneMat;
@endforeach
#endif // SHADOWS
} }

View file

@ -1,7 +1,6 @@
#version 120 #version 120
#define REFRACTION @refraction_enabled #define REFRACTION @refraction_enabled
#define SHADOWS @shadows_enabled
// Inspired by Blender GLSL Water by martinsh ( https://devlog-martinsh.blogspot.de/2012/07/waterundewater-shader-wip.html ) // Inspired by Blender GLSL Water by martinsh ( https://devlog-martinsh.blogspot.de/2012/07/waterundewater-shader-wip.html )
@ -143,12 +142,7 @@ uniform vec3 nodePosition;
uniform float rainIntensity; uniform float rainIntensity;
#if SHADOWS #include "shadows_fragment.glsl"
@foreach shadow_texture_unit_index @shadow_texture_unit_list
uniform sampler2DShadow shadowTexture@shadow_texture_unit_index;
varying vec4 shadowSpaceCoords@shadow_texture_unit_index;
@endforeach
#endif // SHADOWS
float frustumDepth; float frustumDepth;
@ -166,33 +160,7 @@ void main(void)
vec2 UV = worldPos.xy / (8192.0*5.0) * 3.0; vec2 UV = worldPos.xy / (8192.0*5.0) * 3.0;
UV.y *= -1.0; UV.y *= -1.0;
#if SHADOWS float shadow = unshadowedLightRatio();
float shadowing = 1.0;
#if @shadowMapsOverlap
bool doneShadows = false;
@foreach shadow_texture_unit_index @shadow_texture_unit_list
if (!doneShadows)
{
vec2 shadowXY = shadowSpaceCoords@shadow_texture_unit_index.xy / shadowSpaceCoords@shadow_texture_unit_index.w;
if (all(lessThan(shadowXY, vec2(1.0, 1.0))) && all(greaterThan(shadowXY, vec2(0.0, 0.0))))
{
shadowing *= shadow2DProj(shadowTexture@shadow_texture_unit_index, shadowSpaceCoords@shadow_texture_unit_index).r;
if (all(lessThan(shadowXY, vec2(0.95, 0.95))) && all(greaterThan(shadowXY, vec2(0.05, 0.05))))
doneShadows = true;
}
}
@endforeach
#else
@foreach shadow_texture_unit_index @shadow_texture_unit_list
shadowing *= shadow2DProj(shadowTexture@shadow_texture_unit_index, shadowSpaceCoords@shadow_texture_unit_index).r;
@endforeach
#endif
float shadow = shadowing;
#else // NOT SHADOWS
float shadow = 1.0;
#endif // SHADOWS
vec2 screenCoords = screenCoordsPassthrough.xy / screenCoordsPassthrough.z; vec2 screenCoords = screenCoordsPassthrough.xy / screenCoordsPassthrough.z;
screenCoords.y = (1.0-screenCoords.y); screenCoords.y = (1.0-screenCoords.y);

View file

@ -4,14 +4,7 @@ varying vec3 screenCoordsPassthrough;
varying vec4 position; varying vec4 position;
varying float depthPassthrough; varying float depthPassthrough;
#define SHADOWS @shadows_enabled #include "shadows_vertex.glsl"
#if SHADOWS
@foreach shadow_texture_unit_index @shadow_texture_unit_list
uniform int shadowTextureUnit@shadow_texture_unit_index;
varying vec4 shadowSpaceCoords@shadow_texture_unit_index;
@endforeach
#endif // SHADOWS
void main(void) void main(void)
{ {
@ -29,14 +22,5 @@ void main(void)
depthPassthrough = gl_Position.z; depthPassthrough = gl_Position.z;
#if SHADOWS setupShadowCoords(gl_ModelViewMatrix * gl_Vertex);
vec4 viewPos = gl_ModelViewMatrix * gl_Vertex;
// This matrix has the opposite handedness to the others used here, so multiplication must have the vector to the left. Alternatively it could be transposed after construction, but that's extra work for the GPU just to make the code look a tiny bit cleaner.
mat4 eyePlaneMat;
@foreach shadow_texture_unit_index @shadow_texture_unit_list
eyePlaneMat = mat4(gl_EyePlaneS[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneT[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneR[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneQ[shadowTextureUnit@shadow_texture_unit_index]);
shadowSpaceCoords@shadow_texture_unit_index = viewPos * eyePlaneMat;
@endforeach
#endif // SHADOWS
} }