From c9271a7772c668551e7d9a9469e0d5fd0a9de37a Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Fri, 16 Jul 2021 15:44:55 +0200 Subject: [PATCH] Cache brush surface texture numbers instead of re-marshaling them on each access. Fixes textures getting mixed up on brush model entities after map switch. --- Assets/Scripts/Data/QTexture.cs | 2 +- Assets/Scripts/Game/GameState.cs | 4 ++-- Assets/Scripts/Support/BrushModel.cs | 9 +++++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/Data/QTexture.cs b/Assets/Scripts/Data/QTexture.cs index 104eca0..5da4158 100644 --- a/Assets/Scripts/Data/QTexture.cs +++ b/Assets/Scripts/Data/QTexture.cs @@ -45,7 +45,7 @@ public class QGLTexture // Conveniently make use of the fact that texNum is the first field in QGLTexture, // so we don't have to marshal the entire struct to get the texture number out of it. - public static uint TexNumFromPtr(IntPtr ptr) => ptr == IntPtr.Zero ? 0 : unchecked((uint)Marshal.ReadInt32(ptr)); + public static uint TexNumFromPtr(IntPtr ptr) => ptr != IntPtr.Zero ? (uint)Marshal.ReadInt32(ptr) : 0; } /// diff --git a/Assets/Scripts/Game/GameState.cs b/Assets/Scripts/Game/GameState.cs index d9677f4..5b44a85 100644 --- a/Assets/Scripts/Game/GameState.cs +++ b/Assets/Scripts/Game/GameState.cs @@ -51,10 +51,10 @@ public class GameState // TODO FIXME This is wrong for brush model entities uq.CurrentStyle.SetupWorldModelRenderer(mr); // TODO FIXME this currently leaks Materials - uint texNum = surfaceMesh.Texture.TextureNum; + uint texNum = surfaceMesh.TextureNum; if (uq.GameAssets.TryGetTexture(texNum, out var texture)) { - uint fbNum = surfaceMesh.Texture.FullBrightNum; + uint fbNum = surfaceMesh.FullBrightNum; uq.GameAssets.TryGetTexture(fbNum, out var fullBright); uq.CurrentStyle.SetWorldModelTextures(mr.material, texture, fullBright, null); diff --git a/Assets/Scripts/Support/BrushModel.cs b/Assets/Scripts/Support/BrushModel.cs index 41d4c75..cc508f0 100644 --- a/Assets/Scripts/Support/BrushModel.cs +++ b/Assets/Scripts/Support/BrushModel.cs @@ -159,13 +159,18 @@ public class BrushModel public class SurfaceMesh { public Mesh Mesh { get; } - public QTexture Texture { get; } + public uint TextureNum { get; } + public uint FullBrightNum { get; } + public uint WarpImageNum { get; } public int Lightmap { get; } public SurfaceMesh(Mesh mesh, IntPtr texturePtr, int lightmap) { Mesh = mesh; - Texture = Marshal.PtrToStructure(texturePtr); + var texture = Marshal.PtrToStructure(texturePtr); + TextureNum = texture.TextureNum; + FullBrightNum = texture.FullBrightNum; + WarpImageNum = texture.WarpImageNum; Lightmap = lightmap; }