From a2575127de40909367f847e282fb6d5b471c6d25 Mon Sep 17 00:00:00 2001 From: SomaZ <17459161+SomaZ@users.noreply.github.com> Date: Mon, 19 Feb 2024 00:13:34 +0100 Subject: [PATCH] [rend2] Fix alphaGen portal and some additional cleanup. Makes the entity ubo struct smaller, which is good. I unintentionally broke portal alphaGen when I rewrote the scene handling. u_LocalViewOrigin is not correct in any case anymore, because we can have the same entity in multiple views but we only upload one entity representation now. So we compute the needed vectors in the shaders in worldspace insted of localspace now. --- shared/rd-rend2/glsl/fogpass.glsl | 12 +++++------ shared/rd-rend2/glsl/generic.glsl | 25 ++++++++++------------- shared/rd-rend2/glsl/lightall.glsl | 21 +++++++++---------- shared/rd-rend2/glsl/refraction.glsl | 28 +++++++++++--------------- shared/rd-rend2/glsl/shadowvolume.glsl | 16 +++++++-------- shared/rd-rend2/tr_backend.cpp | 4 +--- shared/rd-rend2/tr_local.h | 4 +--- 7 files changed, 46 insertions(+), 64 deletions(-) diff --git a/shared/rd-rend2/glsl/fogpass.glsl b/shared/rd-rend2/glsl/fogpass.glsl index 633feaae54..13d8fa00e4 100644 --- a/shared/rd-rend2/glsl/fogpass.glsl +++ b/shared/rd-rend2/glsl/fogpass.glsl @@ -27,12 +27,11 @@ layout(std140) uniform Entity mat4 u_ModelMatrix; vec4 u_LocalLightOrigin; vec3 u_AmbientLight; - float u_LocalLightRadius; + float u_entityTime; vec3 u_DirectedLight; float u_FXVolumetricBase; vec3 u_ModelLightDir; float u_VertexLerp; - vec3 u_LocalViewOrigin; }; layout(std140) uniform ShaderInstance @@ -226,10 +225,10 @@ void main() normal = DeformNormal( position, normal ); #endif - mat4 MVP = u_viewProjectionMatrix * u_ModelMatrix; - gl_Position = MVP * vec4(position, 1.0); + vec4 wsPosition = u_ModelMatrix * vec4(position, 1.0); + gl_Position = u_viewProjectionMatrix * wsPosition; - var_WSPosition = (u_ModelMatrix * vec4(position, 1.0)).xyz; + var_WSPosition = wsPosition.xyz; #if defined(USE_ALPHA_TEST) var_TexCoords = attr_TexCoord0; #endif @@ -281,12 +280,11 @@ layout(std140) uniform Entity mat4 u_ModelMatrix; vec4 u_LocalLightOrigin; vec3 u_AmbientLight; - float u_LocalLightRadius; + float u_entityTime; vec3 u_DirectedLight; float u_FXVolumetricBase; vec3 u_ModelLightDir; float u_VertexLerp; - vec3 u_LocalViewOrigin; }; uniform int u_FogIndex; diff --git a/shared/rd-rend2/glsl/generic.glsl b/shared/rd-rend2/glsl/generic.glsl index 4958bc9ffc..419eda295a 100644 --- a/shared/rd-rend2/glsl/generic.glsl +++ b/shared/rd-rend2/glsl/generic.glsl @@ -46,13 +46,11 @@ layout(std140) uniform Entity mat4 u_ModelMatrix; vec4 u_LocalLightOrigin; vec3 u_AmbientLight; - float u_LocalLightRadius; + float u_entityTime; vec3 u_DirectedLight; float u_FXVolumetricBase; vec3 u_ModelLightDir; float u_VertexLerp; - vec3 u_LocalViewOrigin; - float u_entityTime; }; #if defined(USE_DEFORM_VERTEXES) || defined(USE_RGBAGEN) @@ -285,7 +283,7 @@ vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3 case TCGEN_ENVIRONMENT_MAPPED: { - vec3 viewer = normalize(u_LocalViewOrigin - position); + vec3 viewer = normalize(u_ViewOrigin - position); vec2 ref = reflect(viewer, normal).yz; tex.s = ref.x * -0.5 + 0.5; tex.t = ref.y * 0.5 + 0.5; @@ -294,7 +292,7 @@ vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3 case TCGEN_ENVIRONMENT_MAPPED_SP: { - vec3 viewer = normalize(u_LocalViewOrigin - position); + vec3 viewer = normalize(u_ViewOrigin - position); vec2 ref = reflect(viewer, normal).xy; tex.s = ref.x * -0.5; tex.t = ref.y * -0.5; @@ -381,10 +379,11 @@ vec4 CalcColor(vec3 position, vec3 normal) return color; } - vec3 viewer = u_LocalViewOrigin - position; + vec3 viewer = u_ViewOrigin - position; if (u_AlphaGen == AGEN_LIGHTING_SPECULAR) { + // TODO: Handle specular on player models and misc_model_statics correctly vec3 lightDir = normalize(vec3(-960.0, 1980.0, 96.0) - position); vec3 reflected = -reflect(lightDir, normal); @@ -438,11 +437,11 @@ void main() normal = DeformNormal( position, normal ); #endif - mat4 MVP = u_viewProjectionMatrix * u_ModelMatrix; - gl_Position = MVP * vec4(position, 1.0); + vec4 wsPosition = u_ModelMatrix * vec4(position, 1.0); + gl_Position = u_viewProjectionMatrix * wsPosition; #if defined(USE_TCGEN) - vec2 tex = GenTexCoords(u_TCGen0, position, normal, u_TCGen0Vector0, u_TCGen0Vector1); + vec2 tex = GenTexCoords(u_TCGen0, wsPosition.xyz, normal, u_TCGen0Vector0, u_TCGen0Vector1); #else vec2 tex = attr_TexCoord0.st; #endif @@ -465,14 +464,14 @@ void main() else { #if defined(USE_RGBAGEN) - var_Color = CalcColor(position, normal); + var_Color = CalcColor(wsPosition.xyz, normal); #else var_Color = u_VertColor * attr_Color + u_BaseColor; #endif } #if defined(USE_FOG) - var_WSPosition = (u_ModelMatrix * vec4(position, 1.0)).xyz; + var_WSPosition = wsPosition.xyz; #endif } @@ -509,13 +508,11 @@ layout(std140) uniform Entity mat4 u_ModelMatrix; vec4 u_LocalLightOrigin; vec3 u_AmbientLight; - float u_LocalLightRadius; + float u_entityTime; vec3 u_DirectedLight; float u_FXVolumetricBase; vec3 u_ModelLightDir; float u_VertexLerp; - vec3 u_LocalViewOrigin; - float u_entityTime; }; uniform sampler2D u_DiffuseMap; diff --git a/shared/rd-rend2/glsl/lightall.glsl b/shared/rd-rend2/glsl/lightall.glsl index 685c40ef39..05f0e7dd64 100644 --- a/shared/rd-rend2/glsl/lightall.glsl +++ b/shared/rd-rend2/glsl/lightall.glsl @@ -45,13 +45,11 @@ layout(std140) uniform Entity mat4 u_ModelMatrix; vec4 u_LocalLightOrigin; vec3 u_AmbientLight; - float u_LocalLightRadius; + float u_entityTime; vec3 u_DirectedLight; float u_FXVolumetricBase; vec3 u_ModelLightDir; float u_VertexLerp; - vec3 u_LocalViewOrigin; - float u_entityTime; }; #if defined(USE_SKELETAL_ANIMATION) @@ -159,7 +157,7 @@ vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3 case TCGEN_ENVIRONMENT_MAPPED: { - vec3 viewer = normalize(u_LocalViewOrigin - position); + vec3 viewer = normalize(u_ViewOrigin - position); vec2 ref = reflect(viewer, normal).yz; tex.s = ref.x * -0.5 + 0.5; tex.t = ref.y * 0.5 + 0.5; @@ -168,7 +166,7 @@ vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3 case TCGEN_ENVIRONMENT_MAPPED_SP: { - vec3 viewer = normalize(u_LocalViewOrigin - position); + vec3 viewer = normalize(u_ViewOrigin - position); vec2 ref = reflect(viewer, normal).xy; tex.s = ref.x * -0.5; tex.t = ref.y * -0.5; @@ -258,8 +256,10 @@ void main() #endif #endif + vec4 wsPosition = u_ModelMatrix * vec4(position, 1.0); + #if defined(USE_TCGEN) - vec2 texCoords = GenTexCoords(u_TCGen0, position, normal, u_TCGen0Vector0, u_TCGen0Vector1); + vec2 texCoords = GenTexCoords(u_TCGen0, wsPosition.xyz, normal, u_TCGen0Vector0, u_TCGen0Vector1); #else vec2 texCoords = attr_TexCoord0.st; #endif @@ -272,10 +272,9 @@ void main() vec4 disintegration = CalcColor(position); - mat4 MVP = u_viewProjectionMatrix * u_ModelMatrix; - gl_Position = MVP * vec4(position, 1.0); + gl_Position = u_viewProjectionMatrix * wsPosition; - position = (u_ModelMatrix * vec4(position, 1.0)).xyz; + position = wsPosition.xyz; normal = normalize(mat3(u_ModelMatrix) * normal); #if defined(PER_PIXEL_LIGHTING) tangent = normalize(mat3(u_ModelMatrix) * tangent); @@ -367,13 +366,11 @@ layout(std140) uniform Entity mat4 u_ModelMatrix; vec4 u_LocalLightOrigin; vec3 u_AmbientLight; - float u_LocalLightRadius; + float u_entityTime; vec3 u_DirectedLight; float u_FXVolumetricBase; vec3 u_ModelLightDir; float u_VertexLerp; - vec3 u_LocalViewOrigin; - float u_entityTime; }; struct Light diff --git a/shared/rd-rend2/glsl/refraction.glsl b/shared/rd-rend2/glsl/refraction.glsl index 7c78cdf187..eedfef223f 100644 --- a/shared/rd-rend2/glsl/refraction.glsl +++ b/shared/rd-rend2/glsl/refraction.glsl @@ -43,13 +43,11 @@ layout(std140) uniform Entity mat4 u_ModelMatrix; vec4 u_LocalLightOrigin; vec3 u_AmbientLight; - float u_LocalLightRadius; + float u_entityTime; vec3 u_DirectedLight; float u_FXVolumetricBase; vec3 u_ModelLightDir; float u_VertexLerp; - vec3 u_LocalViewOrigin; - float u_entityTime; }; #if defined(USE_DEFORM_VERTEXES) || defined(USE_RGBAGEN) @@ -231,14 +229,14 @@ vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3 } else if (TCGen == TCGEN_ENVIRONMENT_MAPPED) { - vec3 viewer = normalize(u_LocalViewOrigin - position); + vec3 viewer = normalize(u_ViewOrigin - position); vec2 ref = reflect(viewer, normal).yz; tex.s = ref.x * -0.5 + 0.5; tex.t = ref.y * 0.5 + 0.5; } else if (TCGen == TCGEN_ENVIRONMENT_MAPPED_SP) { - vec3 viewer = normalize(u_LocalViewOrigin - position); + vec3 viewer = normalize(u_ViewOrigin - position); vec2 ref = reflect(viewer, normal).xy; tex.s = ref.x * -0.5; tex.t = ref.y * -0.5; @@ -288,7 +286,7 @@ vec4 CalcColor(vec3 position, vec3 normal) color.rgb = clamp(u_DirectedLight * incoming + u_AmbientLight, 0.0, 1.0); } - vec3 viewer = u_LocalViewOrigin - position; + vec3 viewer = u_ViewOrigin - position; if (u_AlphaGen == AGEN_LIGHTING_SPECULAR) { @@ -349,11 +347,11 @@ void main() normal = DeformNormal( position, normal ); #endif - mat4 MVP = u_viewProjectionMatrix * u_ModelMatrix; - gl_Position = MVP * vec4(position, 1.0); + vec4 wsPosition = u_ModelMatrix * vec4(position, 1.0); + gl_Position = u_viewProjectionMatrix * wsPosition; #if defined(USE_TCGEN) - vec2 tex = GenTexCoords(u_TCGen0, position, normal, u_TCGen0Vector0, u_TCGen0Vector1); + vec2 tex = GenTexCoords(u_TCGen0, wsPosition.xyz, normal, u_TCGen0Vector0, u_TCGen0Vector1); #else vec2 tex = attr_TexCoord0.st; #endif @@ -382,7 +380,6 @@ void main() #endif } - vec3 ws_Position = mat3(u_ModelMatrix) * position; vec3 ws_Normal = normalize(mat3(u_ModelMatrix) * normal); vec3 ws_ViewDir = (u_ViewForward + u_ViewLeft * -gl_Position.x) + u_ViewUp * gl_Position.y; @@ -393,19 +390,20 @@ void main() #endif mat3 inverseModel = inverse(mat3(u_ModelMatrix)); + mat4 MVP = u_viewProjectionMatrix * u_ModelMatrix; vec3 refraction_vec = normalize(refract(ws_ViewDir, ws_Normal, etaR)); - vec3 new_pos = (distance * refraction_vec) + ws_Position; + vec3 new_pos = (distance * refraction_vec) + wsPosition.xyz; var_RefractPosR = vec4(inverseModel * new_pos, 1.0); var_RefractPosR = MVP * var_RefractPosR; refraction_vec = normalize(refract(ws_ViewDir, ws_Normal, etaG)); - new_pos = (distance * refraction_vec) + ws_Position; + new_pos = (distance * refraction_vec) + wsPosition.xyz; var_RefractPosG = vec4(inverseModel * new_pos, 1.0); var_RefractPosG = MVP * var_RefractPosG; refraction_vec = normalize(refract(ws_ViewDir, ws_Normal, etaB)); - new_pos = (distance * refraction_vec) + ws_Position; + new_pos = (distance * refraction_vec) + wsPosition.xyz; var_RefractPosB = vec4(inverseModel * new_pos, 1.0); var_RefractPosB = MVP * var_RefractPosB; } @@ -428,13 +426,11 @@ layout(std140) uniform Entity mat4 u_ModelMatrix; vec4 u_LocalLightOrigin; vec3 u_AmbientLight; - float u_LocalLightRadius; + float u_entityTime; vec3 u_DirectedLight; float u_FXVolumetricBase; vec3 u_ModelLightDir; float u_VertexLerp; - vec3 u_LocalViewOrigin; - float u_entityTime; }; uniform sampler2D u_TextureMap; diff --git a/shared/rd-rend2/glsl/shadowvolume.glsl b/shared/rd-rend2/glsl/shadowvolume.glsl index e0a1dcbb03..53c8676333 100644 --- a/shared/rd-rend2/glsl/shadowvolume.glsl +++ b/shared/rd-rend2/glsl/shadowvolume.glsl @@ -12,12 +12,11 @@ layout(std140) uniform Entity mat4 u_ModelMatrix; vec4 u_LocalLightOrigin; vec3 u_AmbientLight; - float u_LocalLightRadius; + float u_entityTime; vec3 u_DirectedLight; float u_FXVolumetricBase; vec3 u_ModelLightDir; float u_VertexLerp; - vec3 u_LocalViewOrigin; }; #if defined(USE_SKELETAL_ANIMATION) @@ -76,12 +75,11 @@ layout(std140) uniform Entity mat4 u_ModelMatrix; vec4 u_LocalLightOrigin; vec3 u_AmbientLight; - float u_LocalLightRadius; + float u_entityTime; vec3 u_DirectedLight; float u_FXVolumetricBase; vec3 u_ModelLightDir; float u_VertexLerp; - vec3 u_LocalViewOrigin; }; in vec3 var_Position[]; @@ -107,14 +105,14 @@ void main() mat4 MVP = u_viewProjectionMatrix * u_ModelMatrix; if (dot(cross(BmA,CmA), -u_ModelLightDir.xyz) > 0.0) { - vec3 L = u_ModelLightDir.xyz*u_LocalLightRadius; + vec3 L = u_ModelLightDir.xyz*u_LocalLightOrigin.w; - // front cap - gl_Position = MVP * vec4(var_Position[0].xyz, 1.0); + // front cap, avoids z-fighting with other shaders by NOT using the MVP, the other surfaces won't create z-fighting + gl_Position = u_viewProjectionMatrix * u_ModelMatrix * vec4(var_Position[0].xyz, 1.0); EmitVertex(); - gl_Position = MVP * vec4(var_Position[1].xyz, 1.0); + gl_Position = u_viewProjectionMatrix * u_ModelMatrix * vec4(var_Position[1].xyz, 1.0); EmitVertex(); - gl_Position = MVP * vec4(var_Position[2].xyz, 1.0); + gl_Position = u_viewProjectionMatrix * u_ModelMatrix * vec4(var_Position[2].xyz, 1.0); EmitVertex(); EndPrimitive(); diff --git a/shared/rd-rend2/tr_backend.cpp b/shared/rd-rend2/tr_backend.cpp index b2f5dbf683..0009e72003 100644 --- a/shared/rd-rend2/tr_backend.cpp +++ b/shared/rd-rend2/tr_backend.cpp @@ -2361,8 +2361,7 @@ static void RB_UpdateEntityLightConstants( } VectorCopy(lightDir, entityBlock.modelLightDir); - entityBlock.lightOrigin[3] = 0.0f; - entityBlock.lightRadius = lightRadius; + entityBlock.lightOrigin[3] = lightRadius; } static void RB_UpdateEntityMatrixConstants( @@ -2372,7 +2371,6 @@ static void RB_UpdateEntityMatrixConstants( orientationr_t ori; R_RotateForEntity(refEntity, &backEnd.viewParms, &ori); Matrix16Copy(ori.modelMatrix, entityBlock.modelMatrix); - VectorCopy(ori.viewOrigin, entityBlock.localViewOrigin); } static void RB_UpdateEntityModelConstants( diff --git a/shared/rd-rend2/tr_local.h b/shared/rd-rend2/tr_local.h index 80745ea1a5..8497853afd 100644 --- a/shared/rd-rend2/tr_local.h +++ b/shared/rd-rend2/tr_local.h @@ -776,13 +776,11 @@ struct EntityBlock matrix_t modelMatrix; vec4_t lightOrigin; vec3_t ambientLight; - float lightRadius; + float entityTime; vec3_t directedLight; float fxVolumetricBase; vec3_t modelLightDir; float vertexLerp; - vec3_t localViewOrigin; - float entityTime; }; struct ShaderInstanceBlock