Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Game.Client/GarrysMod/LuaCam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static int Start(ILuaInterface lua) {

if (data.GetMemberStr("type", "3D")!.StartsWith('2')) {
using MatRenderContextPtr renderContext = new(materials);
ClientModeShared.SetupVGuiMatrices(true, renderContext);
ClientModeShared.SetupVGuiMatrices(true, renderContext.Context);
((ClientModeShared)clientMode).SetupGModSurface(true);
renderContext.OverrideAlphaWriteEnable(true, true);
Start2DCount++;
Expand Down Expand Up @@ -140,7 +140,7 @@ static int End2D(ILuaInterface lua) {
using MatRenderContextPtr renderContext = new(materials);
renderContext.OverrideAlphaWriteEnable(false, false);
((ClientModeShared)clientMode).SetupGModSurface(false);
ClientModeShared.SetupVGuiMatrices(false, renderContext);
ClientModeShared.SetupVGuiMatrices(false, renderContext.Context);
return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions Game.Client/GarrysMod/SpawnIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ void Render() {

renderContext.ClearColor4ub(255, 0, 0, 0);
renderContext.ClearBuffers(true, true);
RenderPass(red, ent, renderContext, viewSetup.Width, viewSetup.Height);
RenderPass(red, ent, renderContext.Context, viewSetup.Width, viewSetup.Height);

renderContext.ClearColor4ub(0, 255, 0, 0);
renderContext.ClearBuffers(true, true);
RenderPass(green, ent, renderContext, viewSetup.Width, viewSetup.Height);
RenderPass(green, ent, renderContext.Context, viewSetup.Width, viewSetup.Height);

renderContext.ClearColor4ub(0, 0, 255, 0);
renderContext.ClearBuffers(true, true);
RenderPass(blue, ent, renderContext, viewSetup.Width, viewSetup.Height);
RenderPass(blue, ent, renderContext.Context, viewSetup.Width, viewSetup.Height);

CombinePasses(red, green, blue, viewSetup.Width, viewSetup.Height);

Expand Down
2 changes: 1 addition & 1 deletion Game.Client/HLClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ public void HudUpdate(bool active) {

gHUD.UpdateHud(active);

BaseAnimating.AutoAllowBoneAccess boneAccess = new(true, true);
using BaseAnimating.AutoAllowBoneAccess boneAccess = new(true, true);
IGameSystem.UpdateAllSystems(frameTime);

vgui!.GetAnimationController().UpdateAnimations(gpGlobals.CurTime);
Expand Down
19 changes: 14 additions & 5 deletions Game.Client/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,11 @@ public void RenderView(in ViewSetup viewRender, ClearFlags clearFlags, RenderVie
SkyboxVisibility skyboxVisible = SkyboxVisibility.NotVisible;

{
SkyboxView skyView = new SkyboxView(this);
SkyboxView skyView = FreeSkyboxViews.Count > 0 ? FreeSkyboxViews.Pop() : new SkyboxView(this);
if ((drew3dSkybox = skyView.Setup(in viewRender, ref clearFlags, ref skyboxVisible)) != false)
AddViewToScene(skyView);
skyView.ReleaseLists();
FreeSkyboxViews.Push(skyView);
}

if ((clearFlags & ClearFlags.ClearColor) == 0) {
Expand Down Expand Up @@ -515,7 +516,7 @@ public void RenderView(in ViewSetup viewRender, ClearFlags clearFlags, RenderVie
// VGui_PostRender();
using (renderContext = new MatRenderContextPtr(materials)) {
#if GMOD_DLL
((ClientModeShared)clientMode).PostRenderVGui(renderContext);
((ClientModeShared)clientMode).PostRenderVGui(renderContext.Context);
#endif
if (pTexture != null) {
// renderContext.OverrideAlphaWriteEnable(false, true);
Expand Down Expand Up @@ -743,30 +744,38 @@ private void DrawWorldAndEntities(bool drawSkybox, in ViewSetup viewIn, ClearFla
DetermineWaterRenderInfo(in fogVolumeInfo, out WaterRenderInfo info);

if (info.CheapWater) {
SimpleWorldView noWaterView = new SimpleWorldView(this);
SimpleWorldView noWaterView = FreeSimpleWorldViews.Count > 0 ? FreeSimpleWorldViews.Pop() : new SimpleWorldView(this);
noWaterView.Setup(in viewIn, clearFlags, drawSkybox, in fogVolumeInfo, in info);
AddViewToScene(noWaterView);
noWaterView.ReleaseLists();
FreeSimpleWorldViews.Push(noWaterView);
return;
}

if (!r_ForceWaterLeaf.GetBool())
fogVolumeInfo.VisibleFogVolumeLeaf = -1;

if (!fogVolumeInfo.EyeInFogVolume) {
AboveWaterView aboveWaterView = new AboveWaterView(this);
AboveWaterView aboveWaterView = FreeAboveWaterViews.Count > 0 ? FreeAboveWaterViews.Pop() : new AboveWaterView(this);
aboveWaterView.Setup(in viewIn, drawSkybox, in fogVolumeInfo, in info);
AddViewToScene(aboveWaterView);
aboveWaterView.ReleaseLists();
FreeAboveWaterViews.Push(aboveWaterView);
}
else {
UnderWaterView underWaterView = new UnderWaterView(this);
UnderWaterView underWaterView = FreeUnderWaterViews.Count > 0 ? FreeUnderWaterViews.Pop() : new UnderWaterView(this);
underWaterView.Setup(in viewIn, drawSkybox, in fogVolumeInfo, in info);
AddViewToScene(underWaterView);
underWaterView.ReleaseLists();
FreeUnderWaterViews.Push(underWaterView);
}
}

readonly Stack<SkyboxView> FreeSkyboxViews = [];
readonly Stack<SimpleWorldView> FreeSimpleWorldViews = [];
readonly Stack<AboveWaterView> FreeAboveWaterViews = [];
readonly Stack<UnderWaterView> FreeUnderWaterViews = [];

public static bool DoesViewPlaneIntersectWater(float waterZ, int leafWaterDataID) {
if (leafWaterDataID == -1)
return false;
Expand Down
2 changes: 1 addition & 1 deletion Game.Client/ViewBeams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ private void DrawSegs(int noise_divisions, ReadOnlySpan<float> noise, Model spri
// Specify all the segments.
using MatRenderContextPtr renderContext = new(materials);
BeamSegDraw segDraw = new();
segDraw.Start(renderContext, segments, null);
segDraw.Start(renderContext.Context, segments, null);

for (i = 0; i < segments; i++) {
Assert(noiseIndex < (noise_divisions << 16));
Expand Down
1 change: 1 addition & 0 deletions Game.Client/ViewRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ public virtual void ReleaseLists() {
WorldRenderList?.Release();
WorldRenderList = null;
ClientRenderablesList.Shared.Free(RenderablesList);
RenderablesList = null!;
}
public override DrawFlags GetDrawFlags() {
return DrawFlags;
Expand Down
26 changes: 14 additions & 12 deletions Game.Server/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Game.Server;

Expand Down Expand Up @@ -426,6 +427,7 @@ public void SetPhysicsFlag(PlayerPhysFlag flag, bool set) {
public TimeUnit_t LerpTime;
public bool LagCompensation;
public bool PredictWeapons;
readonly List<UserCmd> AvailableCommandsBuf = [];

public static void SendProxy_CropFlagsToPlayerFlagBitsLength(SendProp prop, object instance, IFieldAccessor field, ref DVariant outData, int element, int objectID) {
int mask = (1 << Constants.PLAYER_FLAG_BITS) - 1;
Expand Down Expand Up @@ -943,7 +945,7 @@ public virtual void ImpulseCommands() {
}

if (tr.Fraction != 1.0) { // line hit something, so paint a decal
// todo
// todo
}

break;
Expand Down Expand Up @@ -1219,7 +1221,7 @@ CommandContext RemoveAllCommandContextsExceptNewest() {

void RemoveAllCommandContexts() => CommandContext.Clear();

void ReplaceContextCommands(CommandContext ctx, UserCmd[] cmds, int commands) {
void ReplaceContextCommands(CommandContext ctx, ReadOnlySpan<UserCmd> cmds, int commands) {
ctx.Cmds.Clear();

ctx.NumCmds = commands;
Expand Down Expand Up @@ -1386,7 +1388,7 @@ public override void PhysicsSimulate() {

int command_context_count = GetCommandContextCount();

List<UserCmd> vecAvailCommands = [];
AvailableCommandsBuf.Clear();

for (int context_number = 0; context_number < command_context_count; context_number++) {
CommandContext ctx = GetCommandContext(context_number);
Expand All @@ -1404,32 +1406,32 @@ public override void PhysicsSimulate() {
while (droppedcmds > numbackup) {
UserCmd lastCmd = GetLastUserCommand();
lastCmd.CommandNumber++;
vecAvailCommands.Add(lastCmd);
AvailableCommandsBuf.Add(lastCmd);
droppedcmds--;
}

while (droppedcmds > 0) {
int cmdnum = ctx.NumCmds + droppedcmds - 1;
vecAvailCommands.Add(ctx.Cmds[cmdnum]);
AvailableCommandsBuf.Add(ctx.Cmds[cmdnum]);
droppedcmds--;
}
}

for (int i = ctx.NumCmds - 1; i >= 0; i--)
vecAvailCommands.Add(ctx.Cmds[i]);
AvailableCommandsBuf.Add(ctx.Cmds[i]);

LastCmd = ctx.Cmds[ctx.Cmds.Count - 1];
}

int commandLimit = IsSimulatingOnAlternateTicks() ? 2 : 1;
int commandsToRun = vecAvailCommands.Count;
if (gpGlobals.SimTicksThisFrame >= commandLimit && vecAvailCommands.Count > commandLimit) {
int commandsToRollOver = Math.Min(vecAvailCommands.Count, (gpGlobals.SimTicksThisFrame - 1));
commandsToRun = vecAvailCommands.Count - commandsToRollOver;
int commandsToRun = AvailableCommandsBuf.Count;
if (gpGlobals.SimTicksThisFrame >= commandLimit && AvailableCommandsBuf.Count > commandLimit) {
int commandsToRollOver = Math.Min(AvailableCommandsBuf.Count, (gpGlobals.SimTicksThisFrame - 1));
commandsToRun = AvailableCommandsBuf.Count - commandsToRollOver;
Assert(commandsToRun >= 0);
if (commandsToRollOver > 0) {
CommandContext ctx = RemoveAllCommandContextsExceptNewest();
ReplaceContextCommands(ctx, [.. vecAvailCommands.GetRange(vecAvailCommands.Count - commandsToRollOver, commandsToRollOver)], commandsToRollOver);
ReplaceContextCommands(ctx, CollectionsMarshal.AsSpan(AvailableCommandsBuf).Slice(AvailableCommandsBuf.Count - commandsToRollOver, commandsToRollOver), commandsToRollOver);
}
else
RemoveAllCommandContexts();
Expand Down Expand Up @@ -1458,7 +1460,7 @@ public override void PhysicsSimulate() {
IPredictionSystem.SuppressHostEvents(this);

for (int i = 0; i < commandsToRun; ++i) {
PlayerRunCommand(vecAvailCommands[i], MoveHelperServer.s_MoveHelperServer);
PlayerRunCommand(AvailableCommandsBuf[i], MoveHelperServer.s_MoveHelperServer);

if (PhysicsController != null) {
UpdateVPhysicsPosition(NewVPhysicsPosition, NewVPhysicsVelocity, (float)vphysicsArrivalTime);
Expand Down
2 changes: 1 addition & 1 deletion Source.Common/MaterialSystem/IMaterialSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ void DrawScreenSpaceRectangle(
#endif
}

public readonly struct MatRenderContextPtr : IDisposable, IMatRenderContext
public readonly ref struct MatRenderContextPtr : IDisposable
{
public readonly IMatRenderContext Context;

Expand Down
27 changes: 19 additions & 8 deletions Source.Engine/DtLocalTransfer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static class LocalTransfer
"(SP only) - show entities that were copied using small optimized lists (FL_EDICT_PARTIAL_CHANGE)."
);

static ServerDatatableStack? s_ServerStack;
static ClientDatatableStack? s_ClientStack;

public static void TransferEntity(BaseEdict edict, SendTable sendTable, object srcEnt, RecvTable recvTable, object destEnt, bool newlyCreated, bool justEnteredPVS, int objectID) {
TotalEntChanges++;
EdictChangeInfo changeInfo = g_SharedChangeInfo!.ChangeInfos[edict.GetChangeInfo()];
Expand All @@ -49,14 +52,18 @@ public static void TransferEntity(BaseEdict edict, SendTable sendTable, object s
FastSortList(propIndices, changeOffsets);

// Setup the structure to traverse the source tree.
ErrorIfNot(precalc != null, ($"SendTable_Encode: Missing precalc for SendTable {sendTable.NetTableName}."));
ServerDatatableStack serverStack = new(precalc, srcEnt, objectID);
if (precalc == null)
Error($"SendTable_Encode: Missing precalc for SendTable {sendTable.NetTableName}.");
ServerDatatableStack serverStack = s_ServerStack ??= new(precalc, srcEnt, objectID);
serverStack.Reset(precalc, srcEnt, objectID);
serverStack.Init(true);

// Setup the structure to traverse the dest tree.
RecvDecoder? decoder = recvTable.Decoder;
ErrorIfNot(decoder != null, ($"RecvTable_Decode: table '{recvTable.GetName()}' missing a decoder."));
ClientDatatableStack clientStack = new(decoder, destEnt, objectID);
if (decoder == null)
Error($"RecvTable_Decode: table '{recvTable.GetName()}' missing a decoder.");
ClientDatatableStack clientStack = s_ClientStack ??= new(decoder, destEnt, objectID);
clientStack.Reset(decoder, destEnt, objectID);
clientStack.Init(true);

// Cool. We can get away with just transferring a few.
Expand Down Expand Up @@ -86,14 +93,18 @@ public static void TransferEntity(BaseEdict edict, SendTable sendTable, object s
else {
// Setup the structure to traverse the source tree.
SendTablePrecalc precalc = sendTable.Precalc!;
ErrorIfNot(precalc != null, ($"SendTable_Encode: Missing precalc for SendTable {sendTable.NetTableName}."));
ServerDatatableStack serverStack = new(precalc, srcEnt, objectID);
if (precalc == null)
Error($"SendTable_Encode: Missing precalc for SendTable {sendTable.NetTableName}.");
ServerDatatableStack serverStack = s_ServerStack ??= new(precalc, srcEnt, objectID);
serverStack.Reset(precalc, srcEnt, objectID);
serverStack.Init();

// Setup the structure to traverse the dest tree.
RecvDecoder? decoder = recvTable.Decoder;
ErrorIfNot(decoder != null, ($"RecvTable_Decode: table '{recvTable.GetName()}' missing a decoder."));
ClientDatatableStack clientStack = new(decoder, destEnt, objectID);
if (decoder == null)
Error($"RecvTable_Decode: table '{recvTable.GetName()}' missing a decoder.");
ClientDatatableStack clientStack = s_ClientStack ??= new(decoder, destEnt, objectID);
clientStack.Reset(decoder, destEnt, objectID);
clientStack.Init();

AddToPartialChangeEntsList(Array.IndexOf(sv.Edicts, edict), false);
Expand Down
16 changes: 15 additions & 1 deletion Source.Engine/EngineRecvTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ public DatatableStack(SendTablePrecalc precalc, object instance, int objectID) {
ObjectID = objectID;
}

public void Reset(SendTablePrecalc precalc, object instance, int objectID) {
Precalc = precalc;
Instance = instance;
ObjectID = objectID;
CurPropIndex = 0;
CurProp = null;
Initted = false;
}

protected static readonly object Uncomputed = new();

public void Init(bool explicitRoutes = false) {
Expand Down Expand Up @@ -227,12 +236,17 @@ public int GetObjectID() {

public class ClientDatatableStack : DatatableStack
{
readonly RecvDecoder Decoder;
RecvDecoder Decoder;

public ClientDatatableStack(RecvDecoder decoder, object instance, int objectID) : base(decoder.Precalc, instance, objectID) {
Decoder = decoder;
}

public void Reset(RecvDecoder decoder, object instance, int objectID) {
Decoder = decoder;
Reset(decoder.Precalc, instance, objectID);
}

public override void RecurseAndCallProxies(SendNode node, object? instance) {
Proxies[node.GetRecursiveProxyIndex()] = instance;

Expand Down
30 changes: 16 additions & 14 deletions Source.Engine/FrameSnapshot.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

using CommunityToolkit.HighPerformance;

using System.Buffers;

using Source.Common;
using Source.Common.Commands;
using Source.Common.Engine;

namespace Source.Engine;

public class FrameSnapshotEntry
public struct FrameSnapshotEntry
{
public ServerClass? Class;
public int SerialNumber;
Expand Down Expand Up @@ -45,6 +47,10 @@ public void ReleaseReference() {
volatile int References;

public void Dispose() {
if (ValidEntities != null)
ArrayPool<ushort>.Shared.Return(ValidEntities);
if (Entities != null)
ArrayPool<FrameSnapshotEntry>.Shared.Return(Entities);
ValidEntities = null;
Entities = null;
TempEntities = null;
Expand Down Expand Up @@ -95,32 +101,28 @@ public FrameSnapshot CreateEmptySnapshot(int tickcount, int maxEntities) {
snap.NumEntities = maxEntities;
snap.NumValidEntities = 0;
snap.ValidEntities = null;
snap.Entities = new FrameSnapshotEntry[maxEntities];

for (int i = 0; i < maxEntities; i++) {
snap.Entities[i] = new FrameSnapshotEntry {
Class = null,
SerialNumber = -1,
PackedData = INVALID_PACKED_ENTITY_HANDLE
};
}
snap.Entities = ArrayPool<FrameSnapshotEntry>.Shared.Rent(maxEntities);
snap.Entities.AsSpan(0, maxEntities).Fill(new FrameSnapshotEntry {
Class = null,
SerialNumber = -1,
PackedData = INVALID_PACKED_ENTITY_HANDLE
});

FrameSnapshots.AddLast(snap);
snap.ListIndex = FrameSnapshots.Count - 1;
return snap;
}

public FrameSnapshot TakeTickSnapshot(int tickcount) {
Span<ushort> validEntities = stackalloc ushort[Constants.MAX_EDICTS];

FrameSnapshot snap = CreateEmptySnapshot(tickcount, sv.NumEdicts);
ushort[] validEntities = ArrayPool<ushort>.Shared.Rent(sv.NumEdicts);

int maxclients = sv.GetClientCount();
int numValid = 0;

for (int i = 0; i < sv.NumEdicts; i++) {
Edict edict = sv.Edicts![i];
FrameSnapshotEntry entry = snap.Entities![i];
ref FrameSnapshotEntry entry = ref snap.Entities![i];

IServerUnknown? unk = edict.GetUnknown();

Expand All @@ -145,7 +147,7 @@ public FrameSnapshot TakeTickSnapshot(int tickcount) {
}

snap.NumValidEntities = numValid;
snap.ValidEntities = validEntities[..numValid].ToArray();
snap.ValidEntities = validEntities;

snap.ExplicitDeleteSlots.AddRange(ExplicitDeleteSlots);
ExplicitDeleteSlots.Clear();
Expand Down
Loading
Loading