diff --git a/Game.Shared/GarrysMod/LuaGameSystem.cs b/Game.Shared/GarrysMod/LuaGameSystem.cs index e7046cdf..f9b2679b 100644 --- a/Game.Shared/GarrysMod/LuaGameSystem.cs +++ b/Game.Shared/GarrysMod/LuaGameSystem.cs @@ -42,8 +42,7 @@ public override void LevelInitPreEntity() { gGM.LoadGamemode(Gamemode, false); gGM.SetGamemode(Gamemode, true); } - // Currently breaking things, uncomment once the path is fully implemented. - // RecacheMaterials(); + RecacheMaterials(); } static void RecacheMaterials() { diff --git a/Source.Common/MaterialSystem/IShaderSystem.cs b/Source.Common/MaterialSystem/IShaderSystem.cs index 00fd8d83..dedd18ea 100644 --- a/Source.Common/MaterialSystem/IShaderSystem.cs +++ b/Source.Common/MaterialSystem/IShaderSystem.cs @@ -38,7 +38,7 @@ public static bool IsTranslucent(IShaderShadow renderState) { void InitShaderInstance(IShader shader, IMaterialVar[]? shaderParams, ReadOnlySpan materialName, ReadOnlySpan textureGroupName); bool InitRenderState(IShader shader, IMaterialVar[] shaderParams, ref IShaderShadow shaderRenderState, ReadOnlySpan materialName); void ResetShaderState(); - // void CleanupRenderState(ref ShaderRenderState renderState); + void CleanupRenderState(IShaderShadow? renderState); void DrawElements(IShader shader, IMaterialVar[] parms, IShaderShadow renderState, VertexCompressionType vertexCompression, uint materialVarTimeStamp); void BeginFrame(); void QueueAllShaderCombos(); diff --git a/Source.Filesystem/BaseFileSystem.cs b/Source.Filesystem/BaseFileSystem.cs index fa660400..4aab845f 100644 --- a/Source.Filesystem/BaseFileSystem.cs +++ b/Source.Filesystem/BaseFileSystem.cs @@ -444,9 +444,28 @@ readonly ref struct Open_Op(FileOpenOptions options) : IFirstToThePostOp fileName, FileOpenOptions options, ReadOnlySpan pathID) { + if ((options & FileOpenOptions.Append) == FileOpenOptions.Read && (options & FileOpenOptions.Extended) == 0) { + Span fileNameBuff = stackalloc char[MAX_PATH]; + return FirstToThePost(FixUpPath(fileName, fileNameBuff), pathID, new Open_Op(options), null, out _); + } return FirstToThePost(fileName, pathID, new Open_Op(options), null, out _); } + private static ReadOnlySpan FixUpPath(ReadOnlySpan fileName, Span fixedUpFileName) { + strcpy(fixedUpFileName, fileName); + StrTools.FixSlashes(fixedUpFileName, StrTools.CORRECT_PATH_SEPARATOR); + StrTools.FixDoubleSlashes(fixedUpFileName); + + Span fixedUp = fixedUpFileName[..(int)strlen(fixedUpFileName)]; + bool isAbsolute = (fixedUp.Length > 1 && fixedUp[1] == ':') || (fixedUp.Length > 0 && (fixedUp[0] == '/' || fixedUp[0] == '\\')); + if (!isAbsolute) { + for (int i = 0; i < fixedUp.Length; i++) + fixedUp[i] = char.ToLowerInvariant(fixedUp[i]); + } + + return fixedUp; + } + public void RemoveAllSearchPaths() { using Lock.Scope scope = SearchPathLock.EnterScope(); diff --git a/Source.MaterialSystem/Material.cs b/Source.MaterialSystem/Material.cs index d64f91be..eb486894 100644 --- a/Source.MaterialSystem/Material.cs +++ b/Source.MaterialSystem/Material.cs @@ -272,7 +272,7 @@ private void PrecacheMappingDimensions() { readonly List VMTIncludes = []; private string GetTextureGroupName() { - return ""; + return texGroupName; } public static bool LoadVMTFile(IFileSystem fileSystem, ref KeyValues keyValues, KeyValues patchKeyValues, ReadOnlySpan materialName, bool absolutePath, List? includes = null) { @@ -473,6 +473,8 @@ public bool PrecacheVars(KeyValues? inVmtKeyValues = null, KeyValues? inPatchKey if (IsPrecachedVars()) return true; + MaterialLock hMaterialLock = materials.Lock(); + // How should we load VMT includes? // How should we allow async? @@ -509,6 +511,7 @@ public bool PrecacheVars(KeyValues? inVmtKeyValues = null, KeyValues? inPatchKey } } + materials.Unlock(hMaterialLock); return ok; } @@ -1179,11 +1182,13 @@ private void CleanUpMaterialProxy() { } private void CleanUpShaderParams() { - + ShaderParams = null; + VarCount = 0; } private void CleanUpStateSnapshots() { - + if (IsValidRenderState()) + materials.ShaderSystem.CleanupRenderState(ShaderRenderState); } int EnumerationID; diff --git a/Source.MaterialSystem/MaterialSystem.cs b/Source.MaterialSystem/MaterialSystem.cs index d19191d8..d3f800cf 100644 --- a/Source.MaterialSystem/MaterialSystem.cs +++ b/Source.MaterialSystem/MaterialSystem.cs @@ -1487,9 +1487,14 @@ public void SetMaterialProxyFactory(IMaterialProxyFactory? factory) { public void UncacheAllMaterials() { // todo: finish me!! + MaterialLock hLock = Lock(); + Flush(true); + foreach (var material in MaterialDict) { material.Uncache(); } + + Unlock(hLock); } public void ReloadTextures() { diff --git a/Source.ShaderAPI.Gl46/MeshMgr.cs b/Source.ShaderAPI.Gl46/MeshMgr.cs index 955b46bc..67ba6290 100644 --- a/Source.ShaderAPI.Gl46/MeshMgr.cs +++ b/Source.ShaderAPI.Gl46/MeshMgr.cs @@ -16,7 +16,7 @@ public class MeshMgr : IMeshMgr internal IMaterialSystem MaterialSystem; internal void Flush() { - BufferedMesh.Flush(); + BufferedMesh?.Flush(); } internal void DiscardVertexBuffers() { @@ -111,7 +111,7 @@ internal void RenderPassWithVertexAndIndexBuffers() { List DynamicVertexBuffers = []; IndexBufferGl46? DynamicIndexBuffer; - BufferedMeshGl46 BufferedMesh; + BufferedMeshGl46? BufferedMesh; DynamicMeshGl46 DynamicMesh; DynamicMeshGl46 DynamicFlexMesh; uint ZeroVertexBuffer; diff --git a/Source.ShaderAPI.Gl46/ShaderShadow.cs b/Source.ShaderAPI.Gl46/ShaderShadow.cs index 09e71ab7..e9080d4f 100644 --- a/Source.ShaderAPI.Gl46/ShaderShadow.cs +++ b/Source.ShaderAPI.Gl46/ShaderShadow.cs @@ -67,6 +67,10 @@ public class ShadowStateGl46 : IShaderShadow public void SetShaderUniform(IMaterialVar textureVar) { shaderUniforms.Add(textureVar); } + internal void CleanupRenderState() { + ContextData = null; + shaderUniforms.Clear(); + } public void ActivateShaderUniforms() { foreach (var var in shaderUniforms) { ShaderAPI.SetShaderUniform(var); diff --git a/Source.ShaderAPI.Gl46/ShaderSystem.cs b/Source.ShaderAPI.Gl46/ShaderSystem.cs index 6e2239b3..ab1caa22 100644 --- a/Source.ShaderAPI.Gl46/ShaderSystem.cs +++ b/Source.ShaderAPI.Gl46/ShaderSystem.cs @@ -104,6 +104,7 @@ public void DrawElements(IShader shader, IMaterialVar[] parms, IShaderShadow ren ShaderAPI.ShadeMode(ShadeMode.Flat); PrepForShaderDraw(shader, parms, renderState); + renderState.Activate(); ref BasePerMaterialContextData? contextData = ref ((ShadowStateGl46)renderState).ContextData; @@ -266,7 +267,6 @@ private void PrepForShaderDraw(IShader shader, Span vars, IShaderS Assert(RenderState == null); // LATER; plug into spew? RenderState = renderState; - renderState?.Activate(); // Activate the render state, this flushes out UBO's etc } public void InitShaderInstance(IShader shader, IMaterialVar[]? shaderParams, ReadOnlySpan materialName, ReadOnlySpan textureGroupName) { @@ -350,7 +350,12 @@ private void InitState(IShader shader, IMaterialVar[] shaderParams, ref IShaderS } private void InitRenderStateFlags(ref IShaderShadow renderState, IMaterialVar[] shaderParams) { + renderState.SetFlags(0); + } + public void CleanupRenderState(IShaderShadow? renderState) { + if (renderState != null) + ((ShadowStateGl46)renderState).CleanupRenderState(); } public void Draw(bool makeActualDrawCall = true) { diff --git a/Source.StudioRender/StudioRenderContext.cs b/Source.StudioRender/StudioRenderContext.cs index d7acc701..0eeb865a 100644 --- a/Source.StudioRender/StudioRenderContext.cs +++ b/Source.StudioRender/StudioRenderContext.cs @@ -580,7 +580,7 @@ private void Threaded_LoadMaterials(in Threaded_LoadMaterials_Data threadData) { lodData.Materials![i] = material!; if (material != null) { // Increment the reference count for the material. - // material.IncrementReferenceCount(); + material.IncrementReferenceCount(); threadData.Context.ComputeMaterialFlags(hdr, lodData, material); lodData.MaterialFlags[i] = UsesMouthShader(material) ? 1 : 0; }