Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Dependencies/OpenGL/Gl46.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System;
using System.Text;
using System.Security;
using Source.Common.MaterialSystem;
using System.Numerics;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -139,6 +140,32 @@ public static int GLEnum(this ShaderDepthFunc factor) {
_ => throw new NotImplementedException()
};
}
public static int GLEnum(this StencilOperation op) {
return op switch {
StencilOperation.Keep => GL_KEEP,
StencilOperation.Zero => GL_ZERO,
StencilOperation.Replace => GL_REPLACE,
StencilOperation.IncrSat => GL_INCR,
StencilOperation.DecrSat => GL_DECR,
StencilOperation.Invert => GL_INVERT,
StencilOperation.Incr => GL_INCR_WRAP,
StencilOperation.Decr => GL_DECR_WRAP,
_ => throw new NotImplementedException()
};
}
public static int GLEnum(this StencilComparisonFunction cmpfn) {
return cmpfn switch {
StencilComparisonFunction.Never => GL_NEVER,
StencilComparisonFunction.Less => GL_LESS,
StencilComparisonFunction.Equal => GL_EQUAL,
StencilComparisonFunction.LessEqual => GL_LEQUAL,
StencilComparisonFunction.Greater => GL_GREATER,
StencilComparisonFunction.NotEqual => GL_NOTEQUAL,
StencilComparisonFunction.GreaterEqual => GL_GEQUAL,
StencilComparisonFunction.Always => GL_ALWAYS,
_ => throw new NotImplementedException()
};
}
public static int GLEnum(this ShaderPolyMode polyMode) {
return polyMode switch {
ShaderPolyMode.Point => GL_POINT,
Expand Down
28 changes: 10 additions & 18 deletions Game.Assets/hl2/shaders/common_gl460.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define TCOMBINE_MULTIPLY 8
#define TCOMBINE_MASK_BASE_BY_DETAIL_ALPHA 9 // use alpha channel of detail to mask base
#define TCOMBINE_SSBUMP_BUMP 10 // use detail to modulate lighting as an ssbump
#define TCOMBINE_SSBUMP_NOBUMP 11 // detail is an ssbump but use it as an albedo. shader does the magic here - no user needs to specify mode 11
#define TCOMBINE_SSBUMP_NOBUMP 11 // detail is an ssbump but use it as an albedo. shader does the magic here - no user needs to specify mode 11

vec4 TextureCombine(vec4 baseColor, vec4 detailColor, int combine_mode, float fBlendFactor)
{
Expand All @@ -24,37 +24,29 @@ vec4 TextureCombine(vec4 baseColor, vec4 detailColor, int combine_mode, float fB
vec3 dc = vec3(mix(detailColor.r, detailColor.a, baseColor.a));
baseColor.rgb *= mix(vec3(1, 1, 1), 2.0 * dc, fBlendFactor);
}
if (combine_mode == TCOMBINE_RGB_EQUALS_BASE_x_DETAILx2)
else if (combine_mode == TCOMBINE_RGB_EQUALS_BASE_x_DETAILx2)
baseColor.rgb *= mix(vec3(1, 1, 1), 2.0 * detailColor.rgb, fBlendFactor);
if (combine_mode == TCOMBINE_RGB_ADDITIVE)
else if (combine_mode == TCOMBINE_RGB_ADDITIVE)
baseColor.rgb += fBlendFactor * detailColor.rgb;
if (combine_mode == TCOMBINE_DETAIL_OVER_BASE)
else if (combine_mode == TCOMBINE_DETAIL_OVER_BASE)
{
float fblend = fBlendFactor * detailColor.a;
baseColor.rgb = mix(baseColor.rgb, detailColor.rgb, fblend);
}
if (combine_mode == TCOMBINE_FADE)
{
else if (combine_mode == TCOMBINE_FADE)
baseColor = mix(baseColor, detailColor, fBlendFactor);
}
if (combine_mode == TCOMBINE_BASE_OVER_DETAIL)
else if (combine_mode == TCOMBINE_BASE_OVER_DETAIL)
{
float fblend = fBlendFactor * (1.0 - baseColor.a);
baseColor.rgb = mix(baseColor.rgb, detailColor.rgb, fblend);
baseColor.a = detailColor.a;
}
if (combine_mode == TCOMBINE_MULTIPLY)
{
else if (combine_mode == TCOMBINE_MULTIPLY)
baseColor = mix(baseColor, baseColor * detailColor, fBlendFactor);
}
if (combine_mode == TCOMBINE_MASK_BASE_BY_DETAIL_ALPHA)
{
else if (combine_mode == TCOMBINE_MASK_BASE_BY_DETAIL_ALPHA)
baseColor.a = mix(baseColor.a, baseColor.a * detailColor.a, fBlendFactor);
}
if (combine_mode == TCOMBINE_SSBUMP_NOBUMP)
{
else if (combine_mode == TCOMBINE_SSBUMP_NOBUMP)
baseColor.rgb = baseColor.rgb * dot(detailColor.rgb, vec3(2.0 / 3.0));
}
return baseColor;
}

Expand All @@ -67,7 +59,7 @@ vec3 TextureCombinePostLighting(vec3 lit_baseColor, vec4 detailColor, int combin
{
if (combine_mode == TCOMBINE_RGB_ADDITIVE_SELFILLUM)
lit_baseColor += fBlendFactor * detailColor.rgb;
if (combine_mode == TCOMBINE_RGB_ADDITIVE_SELFILLUM_THRESHOLD_FADE)
else if (combine_mode == TCOMBINE_RGB_ADDITIVE_SELFILLUM_THRESHOLD_FADE)
{
// fade in an unusual way - instead of fading out color, remap an increasing band of it from
// 0..1
Expand Down
8 changes: 0 additions & 8 deletions Game.Assets/hl2/shaders/common_gl460.vs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,10 @@ float GetVertexAttenForLight(vec3 worldPos, int lightNum, bool useStaticControlF
if (useStaticControlFlow)
{
if (g_bLightEnabled[lightNum])
{
result = VertexAttenInternal(worldPos, lightNum);
}
}
else
{
result = VertexAttenInternal(worldPos, lightNum);
}

return result;
}
Expand All @@ -137,9 +133,7 @@ vec3 DoLightingUnrolled(vec3 worldPos, vec3 worldNormal,
vec3 linearColor = vec3(0.0, 0.0, 0.0);

if (bStaticLight) // Static light
{
linearColor += GammaToLinear(staticLightingColor * cOverbright);
}

if (bDynamicLight) // Ambient light
{
Expand All @@ -154,9 +148,7 @@ vec3 DoLightingUnrolled(vec3 worldPos, vec3 worldNormal,
}

if (bDynamicLight)
{
linearColor += AmbientLight(worldNormal); //ambient light is already remapped
}

return linearColor;
}
Expand Down
41 changes: 41 additions & 0 deletions Game.Assets/hl2/shaders/shadow_gl460.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#version 460

in vec2 vs_TexCoord0;
in vec2 vs_TexCoord1;
in vec2 vs_TexCoord2;
in vec2 vs_TexCoord3;
in vec2 vs_TexCoord4;
in vec4 vs_ShadowColor;

layout(std140, binding = 6) uniform source_ps_constants {
vec4 ps_const[256];
};

#define g_ShadowColor ps_const[1]

layout(binding = 0) uniform sampler2D basetexture;

out vec4 fragColor;

void main()
{
vec4 samples[5];
samples[0] = texture(basetexture, vs_TexCoord0);
samples[1] = texture(basetexture, vs_TexCoord1);
samples[2] = texture(basetexture, vs_TexCoord2);
samples[3] = texture(basetexture, vs_TexCoord3);
samples[4] = texture(basetexture, vs_TexCoord4);

float shadowCoverage = (samples[0].a + samples[1].a + samples[2].a + samples[3].a + samples[4].a) * 0.2;

shadowCoverage = clamp(shadowCoverage - vs_ShadowColor.a, 0.0, 1.0);

vec4 result = shadowCoverage * g_ShadowColor - shadowCoverage;
result = 1.0 + result;

float alpha = 1.0;

// fog todo

fragColor = vec4(result.rgb, alpha);
}
47 changes: 47 additions & 0 deletions Game.Assets/hl2/shaders/shadow_gl460.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#version 460

layout(location = 0) in vec3 v_Position;
layout(location = 2) in vec4 v_Color;
layout(location = 10) in vec2 v_TexCoord;

layout(std140, binding = 0) uniform source_matrices {
mat4 viewMatrix;
mat4 projectionMatrix;
mat4 modelMatrix;
};

layout(std140, binding = 5) uniform source_vs_constants {
vec4 vs_const[256];
};

#define cBaseTexCoordTransform0 vs_const[48]
#define cBaseTexCoordTransform1 vs_const[49]
#define cTextureJitter0 vs_const[50]
#define cTextureJitter1 vs_const[51]

out vec2 vs_TexCoord0;
out vec2 vs_TexCoord1;
out vec2 vs_TexCoord2;
out vec2 vs_TexCoord3;
out vec2 vs_TexCoord4;
out vec4 vs_ShadowColor;

void main()
{
mat4 mvp = projectionMatrix * viewMatrix * modelMatrix;

gl_Position = mvp * vec4(v_Position, 1.0);

vs_ShadowColor = v_Color;

vec4 texCoordIn = vec4(v_TexCoord, 0.0, 1.0);
vec2 texCoord;
texCoord.x = dot(texCoordIn, cBaseTexCoordTransform0);
texCoord.y = dot(texCoordIn, cBaseTexCoordTransform1);

vs_TexCoord0 = texCoord;
vs_TexCoord1 = texCoord + cTextureJitter0.xy;
vs_TexCoord2 = texCoord - cTextureJitter0.xy;
vs_TexCoord3 = texCoord + cTextureJitter1.xy;
vs_TexCoord4 = texCoord - cTextureJitter1.xy;
}
25 changes: 25 additions & 0 deletions Game.Assets/hl2/shaders/shadowmodel_gl460.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#version 460

in vec3 vs_T0;
in vec3 vs_T1;
in vec3 vs_T2;
in float vs_T3;
in vec4 vs_Color;

layout(binding = 0) uniform sampler2D basetexture;

out vec4 fragColor;

void main()
{
if (vs_T1.x < 0.0 || vs_T1.y < 0.0 || vs_T1.z < 0.0)
discard;
if (vs_T2.x < 0.0 || vs_T2.y < 0.0 || vs_T2.z < 0.0)
discard;
if (vs_T3 < 0.0)
discard;

float shadowAlpha = texture(basetexture, vs_T0.xy).a;

fragColor = vec4(mix(vec3(1.0, 1.0, 1.0), vs_Color.xyz, shadowAlpha), 1.0);
}
58 changes: 58 additions & 0 deletions Game.Assets/hl2/shaders/shadowmodel_gl460.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#version 460

layout(location = 0) in vec3 v_Position;
layout(location = 1) in vec3 v_Normal;

layout(std140, binding = 0) uniform source_matrices {
mat4 viewMatrix;
mat4 projectionMatrix;
mat4 modelMatrix;
};

layout(std140, binding = 5) uniform source_vs_constants {
vec4 vs_const[256];
};

#define cShadowTextureMatrix0 vs_const[48]
#define cShadowTextureMatrix1 vs_const[49]
#define cShadowTextureMatrix2 vs_const[50]
#define cTexOrigin vs_const[51]
#define cTexScale vs_const[52]
#define cShadowConstants vs_const[53]
#define cModulationColor vs_const[47]

#define flShadowFalloffOffset cShadowConstants.x
#define flOneOverShadowDist cShadowConstants.y
#define flShadowScale cShadowConstants.z

out vec3 vs_T0;
out vec3 vs_T1;
out vec3 vs_T2;
out float vs_T3;
out vec4 vs_Color;

void main()
{
vec3 worldPos = (modelMatrix * vec4(v_Position, 1.0)).xyz;
vec3 worldNormal = mat3(modelMatrix) * v_Normal;

gl_Position = projectionMatrix * viewMatrix * vec4(worldPos, 1.0);

vec3 vTexturePos;
vTexturePos.x = dot(vec4(worldPos, 1.0), cShadowTextureMatrix0);
vTexturePos.y = dot(vec4(worldPos, 1.0), cShadowTextureMatrix1);
vTexturePos.z = dot(vec4(worldPos, 1.0), cShadowTextureMatrix2);

float flShadowFade = (vTexturePos.z - flShadowFalloffOffset) * flOneOverShadowDist;

vs_T0 = vTexturePos * cTexScale.xyz + cTexOrigin.xyz;

vs_T1.xyz = vTexturePos.xyz;
vs_T2.xyz = vec3(1.0, 1.0, 1.0) - vTexturePos.xyz;
vs_T2.z = 1.0 - flShadowFade;

vs_T3 = dot(worldNormal, -cShadowTextureMatrix2.xyz);

vs_Color.xyz = cModulationColor.xyz;
vs_Color.w = flShadowFade * flShadowScale;
}
31 changes: 29 additions & 2 deletions Game.Client/C_BaseAnimating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,31 @@ public override void ResetLatched() {
base.ResetLatched();
}
public bool IsRagdoll() => Ragdoll != null && RenderFX == (byte)RenderFx.Ragdoll;

public override ShadowType ShadowCastType() {
StudioHdr? studioHdr = GetModelPtr();
if (studioHdr == null || !studioHdr.SequencesAvailable())
return ShadowType.None;

if (IsEffectActive(EntityEffects.NoDraw | EntityEffects.NoShadow))
return ShadowType.None;

if (studioHdr.GetNumSeq() == 0)
return ShadowType.RenderToTexture;

if (!IsRagdoll()) {
if (studioHdr.GetNumPoseParameters() > 0)
return ShadowType.RenderToTextureDynamic;

if (studioHdr.GetRenderHdr().NumBoneControllers > 0)
return ShadowType.RenderToTextureDynamic;

if (studioHdr.GetRenderHdr().NumIKChains > 0)
return ShadowType.RenderToTextureDynamic;
}

return ShadowType.RenderToTexture;
}
public bool IsAboutToRagdoll() => RenderFX == (byte)RenderFx.Ragdoll;
public override void ClientThink() {
base.ClientThink();
Expand Down Expand Up @@ -148,7 +173,7 @@ public void StudioFrameAdvance() {
}

public TimeUnit_t GetSequenceMoveDist(StudioHdr? studioHdr, int sequence) {
Animation.GetSequenceLinearMotion(studioHdr, Sequence, PoseParameter, out Vector3 vecReturn);
Animation.GetSequenceLinearMotion(studioHdr, sequence, PoseParameter, out Vector3 vecReturn);

return vecReturn.Length();
}
Expand Down Expand Up @@ -337,7 +362,7 @@ protected virtual void ApplyBoneMatrixTransform(ref Matrix3x4 transform) {
public void DelayedInitModelEffects() { /* todo */ }
public void ClearRagdoll() { /* todo */ }

public virtual void Simulate() {
public override void Simulate() {
if (DelayInitModelEffects)
DelayedInitModelEffects();

Expand Down Expand Up @@ -371,6 +396,8 @@ public void RemoveBaseAnimatingInterpolatedVars() {
RemoveVar(this, DA_Cycle, false);
}

public bool ComputeHitboxSurroundingBox(out Vector3 vecWorldMins, out Vector3 vecWorldMaxs) => throw new NotImplementedException();

public override bool SetupBones(Span<Matrix3x4> boneToWorldOut, int maxBones, int boneMask, double currentTime) {
if (!boneToWorldOut.IsEmpty && !IsBoneAccessAllowed()) {
if (gpGlobals.RealTime >= SetupBones__lastWarning + 1.0f) {
Expand Down
Loading