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
3 changes: 1 addition & 2 deletions Game.Shared/GarrysMod/LuaGameSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion Source.Common/MaterialSystem/IShaderSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static bool IsTranslucent(IShaderShadow renderState) {
void InitShaderInstance(IShader shader, IMaterialVar[]? shaderParams, ReadOnlySpan<char> materialName, ReadOnlySpan<char> textureGroupName);
bool InitRenderState(IShader shader, IMaterialVar[] shaderParams, ref IShaderShadow shaderRenderState, ReadOnlySpan<char> 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();
Expand Down
19 changes: 19 additions & 0 deletions Source.Filesystem/BaseFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,28 @@ readonly ref struct Open_Op(FileOpenOptions options) : IFirstToThePostOp<IFileHa
}

public IFileHandle? Open(ReadOnlySpan<char> fileName, FileOpenOptions options, ReadOnlySpan<char> pathID) {
if ((options & FileOpenOptions.Append) == FileOpenOptions.Read && (options & FileOpenOptions.Extended) == 0) {
Span<char> fileNameBuff = stackalloc char[MAX_PATH];
return FirstToThePost<IFileHandle?, Open_Op>(FixUpPath(fileName, fileNameBuff), pathID, new Open_Op(options), null, out _);
}
return FirstToThePost<IFileHandle?, Open_Op>(fileName, pathID, new Open_Op(options), null, out _);
}

private static ReadOnlySpan<char> FixUpPath(ReadOnlySpan<char> fileName, Span<char> fixedUpFileName) {
strcpy(fixedUpFileName, fileName);
StrTools.FixSlashes(fixedUpFileName, StrTools.CORRECT_PATH_SEPARATOR);
StrTools.FixDoubleSlashes(fixedUpFileName);

Span<char> 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();
Expand Down
11 changes: 8 additions & 3 deletions Source.MaterialSystem/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private void PrecacheMappingDimensions() {
readonly List<FileNameHandle_t> VMTIncludes = [];

private string GetTextureGroupName() {
return "";
return texGroupName;
}

public static bool LoadVMTFile(IFileSystem fileSystem, ref KeyValues keyValues, KeyValues patchKeyValues, ReadOnlySpan<char> materialName, bool absolutePath, List<FileNameHandle_t>? includes = null) {
Expand Down Expand Up @@ -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?

Expand Down Expand Up @@ -509,6 +511,7 @@ public bool PrecacheVars(KeyValues? inVmtKeyValues = null, KeyValues? inPatchKey
}
}

materials.Unlock(hMaterialLock);
return ok;
}

Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions Source.MaterialSystem/MaterialSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions Source.ShaderAPI.Gl46/MeshMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class MeshMgr : IMeshMgr
internal IMaterialSystem MaterialSystem;

internal void Flush() {
BufferedMesh.Flush();
BufferedMesh?.Flush();
}

internal void DiscardVertexBuffers() {
Expand Down Expand Up @@ -111,7 +111,7 @@ internal void RenderPassWithVertexAndIndexBuffers() {
List<VertexBufferGl46> DynamicVertexBuffers = [];
IndexBufferGl46? DynamicIndexBuffer;

BufferedMeshGl46 BufferedMesh;
BufferedMeshGl46? BufferedMesh;
DynamicMeshGl46 DynamicMesh;
DynamicMeshGl46 DynamicFlexMesh;
uint ZeroVertexBuffer;
Expand Down
4 changes: 4 additions & 0 deletions Source.ShaderAPI.Gl46/ShaderShadow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion Source.ShaderAPI.Gl46/ShaderSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -266,7 +267,6 @@ private void PrepForShaderDraw(IShader shader, Span<IMaterialVar> 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<char> materialName, ReadOnlySpan<char> textureGroupName) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Source.StudioRender/StudioRenderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading