From 4a3f2016571b098eeac1adf6db14540b72afaab0 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 27 Aug 2017 21:14:42 +1000 Subject: [PATCH] Remember to free the memory allocated for mipmaps --- ClassicalSharp/GraphicsAPI/Direct3D9Api.cs | 22 ++++++------------- .../GraphicsAPI/IGraphicsAPI.Core.cs | 19 ++++++++-------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs index 83075c4e3..b74881454 100644 --- a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs +++ b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs @@ -3,12 +3,12 @@ using System; using System.Drawing; using System.Drawing.Imaging; +using System.Runtime.InteropServices; using System.Threading; using OpenTK; using SharpDX; using SharpDX.Direct3D9; using D3D = SharpDX.Direct3D9; -using WinWindowInfo = OpenTK.Platform.Windows.WinWindowInfo; namespace ClassicalSharp.GraphicsAPI { @@ -193,21 +193,13 @@ namespace ClassicalSharp.GraphicsAPI { IntPtr prev = scan0; for (int lvl = 1; lvl <= 4; lvl++) { int size = (width / 2) * (height / 2) * 4; - IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocHGlobal(size); - GenMipmaps(width / 2, height / 2, ptr, width, height, prev); + IntPtr cur = Marshal.AllocHGlobal(size); + GenMipmaps(width / 2, height / 2, cur, prev); - texture.SetData(lvl, LockFlags.None, ptr, size); - prev = ptr; - width /= 2; height /= 2; - } - } - - unsafe void DoMipmaps(D3D.Texture texture, int lvl, int lvlWidth, int lvlHeight, - int width, int height, IntPtr scan0) { - int[] pixels = new int[lvlWidth * lvlHeight]; - fixed (int* ptr = pixels) { - GenMipmaps(lvlWidth, lvlHeight, (IntPtr)ptr, width, height, scan0); - texture.SetData(lvl, LockFlags.None, (IntPtr)ptr, lvlWidth * lvlHeight * 4); + texture.SetData(lvl, LockFlags.None, cur, size); + if (prev != scan0) Marshal.FreeHGlobal(prev); + + prev = cur; width /= 2; height /= 2; } } diff --git a/ClassicalSharp/GraphicsAPI/IGraphicsAPI.Core.cs b/ClassicalSharp/GraphicsAPI/IGraphicsAPI.Core.cs index 9b9a670c5..e0a1cfbac 100644 --- a/ClassicalSharp/GraphicsAPI/IGraphicsAPI.Core.cs +++ b/ClassicalSharp/GraphicsAPI/IGraphicsAPI.Core.cs @@ -175,18 +175,17 @@ namespace ClassicalSharp.GraphicsAPI { return (aAve << 24) | (rAve << 16) | (gAve << 8) | bAve; } - internal static unsafe void GenMipmaps(int lvlWidth, int lvlHeight, IntPtr lvlScan0, - int width, int height, IntPtr scan0) { - int* baseSrc = (int*)scan0, baseDst = (int*)lvlScan0; + internal static unsafe void GenMipmaps(int width, int height, IntPtr lvlScan0, IntPtr scan0) { + int* baseSrc = (int*)scan0, baseDst = (int*)lvlScan0; + int srcWidth = width << 1; - for (int y = 0; y < lvlHeight; y++) { - int srcY = y * height / lvlHeight; - int* src0 = baseSrc + srcY * width; - int* src1 = src0 + width; - int* dst = baseDst + y * lvlWidth; + for (int y = 0; y < height; y++) { + int srcY = (y << 1); + int* src0 = baseSrc + srcY * srcWidth, src1 = src0 + srcWidth; + int* dst = baseDst + y * width; - for (int x = 0; x < lvlWidth; x++) { - int srcX = x * width / lvlWidth; + for (int x = 0; x < width; x++) { + int srcX = (x << 1); int src00 = src0[srcX], src01 = src0[srcX + 1]; int src10 = src1[srcX], src11 = src1[srcX + 1];