From 2100ddb7124fad5d528e2c3e459b4f03691fd797 Mon Sep 17 00:00:00 2001 From: Sour Date: Sat, 30 Mar 2019 11:52:15 -0400 Subject: [PATCH] Debugger: Improved tilemap viewer performance --- Core/PpuTools.cpp | 15 ++++++--------- Core/PpuTools.h | 4 ++-- InteropDLL/DebugApiWrapper.cpp | 2 +- UI/Debugger/PpuViewer/frmTilemapViewer.cs | 23 ++++++++++++----------- UI/Interop/DebugApi.cs | 2 +- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Core/PpuTools.cpp b/Core/PpuTools.cpp index 8607237..e31a64c 100644 --- a/Core/PpuTools.cpp +++ b/Core/PpuTools.cpp @@ -58,7 +58,7 @@ void PpuTools::BlendColors(uint8_t output[4], uint8_t input[4]) output[3] = 0xFF; } -uint32_t PpuTools::GetRgbPixelColor(uint8_t colorIndex, uint8_t palette, uint8_t bpp, bool directColorMode, uint16_t basePaletteOffset) +uint32_t PpuTools::GetRgbPixelColor(uint8_t* cgram, uint8_t colorIndex, uint8_t palette, uint8_t bpp, bool directColorMode, uint16_t basePaletteOffset) { uint16_t paletteColor; if(bpp == 8 && directColorMode) { @@ -68,7 +68,6 @@ uint32_t PpuTools::GetRgbPixelColor(uint8_t colorIndex, uint8_t palette, uint8_t (((colorIndex & 0xC0) | ((palette & 0x04) << 3)) << 7) ); } else { - uint8_t* cgram = _ppu->GetCgRam(); uint16_t paletteRamOffset = basePaletteOffset + (palette * (1 << bpp) + colorIndex) * 2; paletteColor = cgram[paletteRamOffset] | (cgram[paletteRamOffset + 1] << 8); } @@ -150,7 +149,7 @@ void PpuTools::GetTileView(GetTileViewOptions options, uint32_t *outBuffer) if(directColor) { rgbColor = ToArgb(((color & 0x07) << 2) | ((color & 0x38) << 4) | ((color & 0xC0) << 7)); } else { - rgbColor = GetRgbPixelColor(color, 0, 8, false, 0); + rgbColor = GetRgbPixelColor(cgram, color, 0, 8, false, 0); } outBuffer[((row * 8) + y) * (options.Width * 8) + column * 8 + x] = rgbColor; } @@ -170,7 +169,7 @@ void PpuTools::GetTileView(GetTileViewOptions options, uint32_t *outBuffer) for(int x = 0; x < 8; x++) { uint8_t color = GetTilePixelColor(ram, ramMask, bpp, pixelStart, 7 - x); if(color != 0) { - outBuffer[((row * 8) + y) * (options.Width * 8) + column * 8 + x] = GetRgbPixelColor(color, options.Palette, bpp, directColor, 0); + outBuffer[((row * 8) + y) * (options.Width * 8) + column * 8 + x] = GetRgbPixelColor(cgram, color, options.Palette, bpp, directColor, 0); } } } @@ -190,7 +189,7 @@ void PpuTools::GetTileView(GetTileViewOptions options, uint32_t *outBuffer) } } -void PpuTools::GetTilemap(GetTilemapOptions options, uint32_t* outBuffer) +void PpuTools::GetTilemap(GetTilemapOptions options, uint8_t* vram, uint8_t* cgram, uint32_t* outBuffer) { static constexpr uint8_t layerBpp[8][4] = { { 2,2,2,2 }, { 4,4,2,0 }, { 4,4,0,0 }, { 8,4,0,0 }, { 8,2,0,0 }, { 4,2,0,0 }, { 4,0,0,0 }, { 8,0,0,0 } @@ -206,8 +205,6 @@ void PpuTools::GetTilemap(GetTilemapOptions options, uint32_t* outBuffer) basePaletteOffset = options.Layer * 64; } - uint8_t *vram = _ppu->GetVideoRam(); - uint8_t *cgram = _ppu->GetCgRam(); LayerConfig layer = state.Layers[options.Layer]; uint32_t bgColor = ToArgb((cgram[1] << 8) | cgram[0]); @@ -238,7 +235,7 @@ void PpuTools::GetTilemap(GetTilemapOptions options, uint32_t* outBuffer) if(directColor) { rgbColor = ToArgb(((color & 0x07) << 2) | ((color & 0x38) << 4) | ((color & 0xC0) << 7)); } else { - rgbColor = GetRgbPixelColor(color, 0, 8, false, 0); + rgbColor = GetRgbPixelColor(cgram, color, 0, 8, false, 0); } outBuffer[((row * 8) + y) * 1024 + column * 8 + x] = rgbColor; } @@ -277,7 +274,7 @@ void PpuTools::GetTilemap(GetTilemapOptions options, uint32_t* outBuffer) uint8_t color = GetTilePixelColor(vram, Ppu::VideoRamSize - 1, bpp, pixelStart, shift); if(color != 0) { uint8_t palette = bpp == 8 ? 0 : (vram[addr + 1] >> 2) & 0x07; - outBuffer[((row * 8) + y) * 1024 + column * 8 + x] = GetRgbPixelColor(color, palette, bpp, directColor, basePaletteOffset); + outBuffer[((row * 8) + y) * 1024 + column * 8 + x] = GetRgbPixelColor(cgram, color, palette, bpp, directColor, basePaletteOffset); } } } diff --git a/Core/PpuTools.h b/Core/PpuTools.h index 01f39c0..e124ca4 100644 --- a/Core/PpuTools.h +++ b/Core/PpuTools.h @@ -17,13 +17,13 @@ private: uint32_t ToArgb(uint16_t color); void BlendColors(uint8_t output[4], uint8_t input[4]); - uint32_t GetRgbPixelColor(uint8_t colorIndex, uint8_t palette, uint8_t bpp, bool directColorMode, uint16_t basePaletteOffset); + uint32_t GetRgbPixelColor(uint8_t* cgram, uint8_t colorIndex, uint8_t palette, uint8_t bpp, bool directColorMode, uint16_t basePaletteOffset); public: PpuTools(Console *console, Ppu *ppu); void GetTileView(GetTileViewOptions options, uint32_t *outBuffer); - void GetTilemap(GetTilemapOptions options, uint32_t *outBuffer); + void GetTilemap(GetTilemapOptions options, uint8_t* vram, uint8_t* cgram, uint32_t *outBuffer); void SetViewerUpdateTiming(uint32_t viewerId, uint16_t scanline, uint16_t cycle); void RemoveViewer(uint32_t viewerId); diff --git a/InteropDLL/DebugApiWrapper.cpp b/InteropDLL/DebugApiWrapper.cpp index 5c06579..e0a44ed 100644 --- a/InteropDLL/DebugApiWrapper.cpp +++ b/InteropDLL/DebugApiWrapper.cpp @@ -69,7 +69,7 @@ extern "C" DllExport void __stdcall GetMemoryAccessCounts(uint32_t offset, uint32_t length, SnesMemoryType memoryType, MemoryOperationType operationType, uint32_t* counts) { GetDebugger()->GetMemoryAccessCounter()->GetAccessCounts(offset, length, memoryType, operationType, counts); } DllExport void __stdcall GetCdlData(uint32_t offset, uint32_t length, SnesMemoryType memoryType, uint8_t* cdlData) { GetDebugger()->GetCodeDataLogger()->GetCdlData(offset, length, memoryType, cdlData); } - DllExport void __stdcall GetTilemap(GetTilemapOptions options, uint32_t *buffer) { GetDebugger()->GetPpuTools()->GetTilemap(options, buffer); } + DllExport void __stdcall GetTilemap(GetTilemapOptions options, uint8_t *vram, uint8_t *cgram, uint32_t *buffer) { GetDebugger()->GetPpuTools()->GetTilemap(options, vram, cgram, buffer); } DllExport void __stdcall GetTileView(GetTileViewOptions options, uint32_t *buffer) { GetDebugger()->GetPpuTools()->GetTileView(options, buffer); } DllExport void __stdcall SetViewerUpdateTiming(uint32_t viewerId, uint16_t scanline, uint16_t cycle) { GetDebugger()->GetPpuTools()->SetViewerUpdateTiming(viewerId, scanline, cycle); } diff --git a/UI/Debugger/PpuViewer/frmTilemapViewer.cs b/UI/Debugger/PpuViewer/frmTilemapViewer.cs index dfda272..ad47d40 100644 --- a/UI/Debugger/PpuViewer/frmTilemapViewer.cs +++ b/UI/Debugger/PpuViewer/frmTilemapViewer.cs @@ -18,6 +18,8 @@ namespace Mesen.GUI.Debugger private NotificationListener _notifListener; private GetTilemapOptions _options; private DebugState _state; + private byte[] _cgram; + private byte[] _vram; private byte[] _tilemapData; private Bitmap _tilemapImage; private bool _zoomed; @@ -72,13 +74,14 @@ namespace Mesen.GUI.Debugger private void RefreshData() { _state = DebugApi.GetState(); - lock(_tilemapData) { - DebugApi.GetTilemap(_options, _tilemapData); - } + _vram = DebugApi.GetMemoryState(SnesMemoryType.VideoRam); + _cgram = DebugApi.GetMemoryState(SnesMemoryType.CGRam); } private void RefreshViewer() { + DebugApi.GetTilemap(_options, _vram, _cgram, _tilemapData); + int mapWidth = _state.Ppu.BgMode == 7 ? 1024 : _state.Ppu.Layers[_options.Layer].DoubleWidth ? 512 : 256; int mapHeight = _state.Ppu.BgMode == 7 ? 1024 : _state.Ppu.Layers[_options.Layer].DoubleHeight ? 512 : 256; if(_tilemapImage.Width != mapWidth || _tilemapImage.Height != mapHeight) { @@ -86,14 +89,12 @@ namespace Mesen.GUI.Debugger picTilemap.Image = _tilemapImage; } using(Graphics g = Graphics.FromImage(_tilemapImage)) { - lock(_tilemapData) { - GCHandle handle = GCHandle.Alloc(_tilemapData, GCHandleType.Pinned); - Bitmap source = new Bitmap(mapWidth, mapHeight, 4 * 1024, PixelFormat.Format32bppArgb, handle.AddrOfPinnedObject()); - try { - g.DrawImage(source, 0, 0); - } finally { - handle.Free(); - } + GCHandle handle = GCHandle.Alloc(_tilemapData, GCHandleType.Pinned); + Bitmap source = new Bitmap(mapWidth, mapHeight, 4 * 1024, PixelFormat.Format32bppArgb, handle.AddrOfPinnedObject()); + try { + g.DrawImage(source, 0, 0); + } finally { + handle.Free(); } } diff --git a/UI/Interop/DebugApi.cs b/UI/Interop/DebugApi.cs index 570c971..4a90176 100644 --- a/UI/Interop/DebugApi.cs +++ b/UI/Interop/DebugApi.cs @@ -71,7 +71,7 @@ namespace Mesen.GUI return buffer; } - [DllImport(DllPath)] public static extern void GetTilemap(GetTilemapOptions options, [In, Out] byte[] buffer); + [DllImport(DllPath)] public static extern void GetTilemap(GetTilemapOptions options, byte[] vram, byte[] cgram, [In, Out] byte[] buffer); [DllImport(DllPath)] public static extern void GetTileView(GetTileViewOptions options, [In, Out] byte[] buffer); [DllImport(DllPath)] public static extern void SetViewerUpdateTiming(Int32 viewerId, Int32 scanline, Int32 cycle);