Skip to content

Commit

Permalink
[rend2] Fix alphaGen portal
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SomaZ committed Feb 18, 2024
1 parent 494abea commit a257512
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 64 deletions.
12 changes: 5 additions & 7 deletions shared/rd-rend2/glsl/fogpass.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 11 additions & 14 deletions shared/rd-rend2/glsl/generic.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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;
Expand Down
21 changes: 9 additions & 12 deletions shared/rd-rend2/glsl/lightall.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
28 changes: 12 additions & 16 deletions shared/rd-rend2/glsl/refraction.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
16 changes: 7 additions & 9 deletions shared/rd-rend2/glsl/shadowvolume.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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[];
Expand All @@ -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();

Expand Down
4 changes: 1 addition & 3 deletions shared/rd-rend2/tr_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions shared/rd-rend2/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a257512

Please sign in to comment.