Remember to free the memory allocated for mipmaps

This commit is contained in:
UnknownShadow200 2017-08-27 21:14:42 +10:00
parent 9cced70f2d
commit 4a3f201657
2 changed files with 16 additions and 25 deletions

View File

@ -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;
}
}

View File

@ -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];