diff --git a/VisualPinball.Plugins.Test/ResourceTests.cs b/VisualPinball.Plugins.Test/ResourceTests.cs index 897994cbd9..e2a021b4f0 100644 --- a/VisualPinball.Plugins.Test/ResourceTests.cs +++ b/VisualPinball.Plugins.Test/ResourceTests.cs @@ -168,6 +168,15 @@ public void IncompatibleConcreteFormatDoesNotReachConsumer() Assert.That(count, Is.EqualTo(1)); } [Test] + public void Rgb565FramesAreTwoBytesPerPixel() + { + Assert.That(PluginDisplayFrame.BytesPerPixel(DisplayEncoding.Rgb565), Is.EqualTo(2)); + Assert.That(PluginDisplayFrame.BytesPerPixel(DisplayEncoding.Rgb24 | DisplayEncoding.Rgb565), Is.EqualTo(0)); + var frame = new PluginDisplayFrame(new byte[3 * 2 * 2], 3, 2, 3 * 2, DisplayEncoding.Rgb565, 1, 0, 1); + Assert.That(frame.Stride, Is.EqualTo(6)); + Assert.Throws(() => new PluginDisplayFrame(new byte[3 * 2], 3, 2, 3, DisplayEncoding.Rgb565, 1, 0, 1)); + } + [Test] public void ThrowingObserverCannotLeakPublication() { var faults = new List(); var registry = new PluginResourceRegistry(faults.Add); diff --git a/VisualPinball.Plugins/Resources/ResourceContracts.cs b/VisualPinball.Plugins/Resources/ResourceContracts.cs index d5947b350d..446da0694a 100644 --- a/VisualPinball.Plugins/Resources/ResourceContracts.cs +++ b/VisualPinball.Plugins/Resources/ResourceContracts.cs @@ -169,7 +169,12 @@ public enum DisplayEncoding /// Four bytes per pixel, in red, green, blue, alpha order, each from 0 to 255. How alpha applies is given by /// . /// - Rgba32 = 16 + Rgba32 = 16, + /// + /// Two bytes per pixel, a little-endian 16-bit value with red in its top 5 bits, green in the middle 6 and blue in the + /// low 5, as color DMD hardware takes it. + /// + Rgb565 = 32 } /// The color space of a display frame's color values. @@ -522,13 +527,23 @@ public readonly struct PluginDisplayFrame public PluginDisplayFrame(ReadOnlyMemory pixels, int width, int height, int stride, DisplayEncoding encoding, long sequence, long timestampUs, long instance, FrameColorSpace colorSpace = FrameColorSpace.Srgb, FrameAlpha alpha = FrameAlpha.None) { - int bytes = encoding == DisplayEncoding.Rgb24 ? 3 : encoding == DisplayEncoding.Rgba32 ? 4 : 1; - if (encoding != DisplayEncoding.Gray2 && encoding != DisplayEncoding.Gray4 && encoding != DisplayEncoding.Luminance8 - && encoding != DisplayEncoding.Rgb24 && encoding != DisplayEncoding.Rgba32) throw new ArgumentException("A frame needs one concrete VPE encoding."); + int bytes = BytesPerPixel(encoding); + if (bytes == 0) throw new ArgumentException("A frame needs one concrete VPE encoding."); if (width <= 0 || height <= 0 || stride < checked(width * bytes) || pixels.Length < checked(stride * height)) throw new ArgumentException("Display frame shape does not match its buffer."); Pixels = pixels; Width = width; Height = height; Stride = stride; Encoding = encoding; Sequence = sequence; TimestampUs = timestampUs; Instance = instance; ColorSpace = colorSpace; Alpha = alpha; } + + /// Bytes per pixel of a single concrete encoding, or 0 for and combined flags. + /// The encoding. + /// 1 for the gray and luminance encodings, 2 for RGB565, 3 for RGB24, 4 for RGBA32, else 0. + public static int BytesPerPixel(DisplayEncoding encoding) => encoding switch { + DisplayEncoding.Gray2 or DisplayEncoding.Gray4 or DisplayEncoding.Luminance8 => 1, + DisplayEncoding.Rgb565 => 2, + DisplayEncoding.Rgb24 => 3, + DisplayEncoding.Rgba32 => 4, + _ => 0 + }; } /// A source of display frames, published as . diff --git a/VisualPinball.Unity/VisualPinball.Unity.Test/DotMatrixRgb565Tests.cs b/VisualPinball.Unity/VisualPinball.Unity.Test/DotMatrixRgb565Tests.cs new file mode 100644 index 0000000000..b3ed6dc32a --- /dev/null +++ b/VisualPinball.Unity/VisualPinball.Unity.Test/DotMatrixRgb565Tests.cs @@ -0,0 +1,55 @@ +// Visual Pinball Engine +// Copyright (C) 2026 freezy and VPE Team +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +using NUnit.Framework; +using Unity.Collections; + +namespace VisualPinball.Unity.Test +{ + public class DotMatrixRgb565Tests + { + [Test] + public void Rgb565FramesExpandToRgb24BottomRowFirst() + { + // top row red, green; bottom row blue, white; little-endian 5-6-5 with red in the top bits + var frame = new byte[] { + 0x00, 0xf8, 0xe0, 0x07, + 0x1f, 0x00, 0xff, 0xff, + }; + using var texture = new NativeArray(2 * 2 * 3, Allocator.Temp); + + DotMatrixDisplayComponent.CopyRgb565Frame(frame, 2, 2, texture); + + Assert.That(texture.ToArray(), Is.EqualTo(new byte[] { + 0, 0, 255, 255, 255, 255, // the texture starts with the bottom row + 255, 0, 0, 0, 255, 0, + })); + } + + [Test] + public void Rgb565ChannelsRepeatTheirTopBits() + { + // r = 0b10000, g = 0b100000, b = 0b00001 + var value = 0b10000_100000_00001; + var frame = new[] { (byte)value, (byte)(value >> 8) }; + using var texture = new NativeArray(3, Allocator.Temp); + + DotMatrixDisplayComponent.CopyRgb565Frame(frame, 1, 1, texture); + + Assert.That(texture.ToArray(), Is.EqualTo(new byte[] { 0b10000_100, 0b100000_10, 0b00001_000 })); + } + } +} diff --git a/VisualPinball.Unity/VisualPinball.Unity.Test/DotMatrixRgb565Tests.cs.meta b/VisualPinball.Unity/VisualPinball.Unity.Test/DotMatrixRgb565Tests.cs.meta new file mode 100644 index 0000000000..2ac107d847 --- /dev/null +++ b/VisualPinball.Unity/VisualPinball.Unity.Test/DotMatrixRgb565Tests.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: ff62cd094fa112b40801966777550154 \ No newline at end of file diff --git a/VisualPinball.Unity/VisualPinball.Unity/Display/DotMatrixDisplayComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/Display/DotMatrixDisplayComponent.cs index 27e51b2bfc..1a938c0f60 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Display/DotMatrixDisplayComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Display/DotMatrixDisplayComponent.cs @@ -230,6 +230,18 @@ private void UploadFrame(DisplayFrameFormat format, byte[] frame) } break; + case DisplayFrameFormat.Dmd565: + if (frame.Length == _width * _height * 2) { + CopyRgb565Frame(frame, _width, _height, _texture.GetRawTextureData()); + _texture.Apply(); + + } else { + if (_frameThrottle.ShouldLog(out var suppressed)) { + Logger.Warn("Cannot render {Bytes} bytes of RGB565 data to DMD {DisplayId} of {Width}x{Height} ({Suppressed} suppressed).", frame.Length, _id, _width, _height, suppressed); + } + } + break; + case DisplayFrameFormat.Segment: if (_frameThrottle.ShouldLog(out var segmentSuppressed)) { Logger.Warn("DMD {DisplayId} cannot render segment data, use a segment display component ({Suppressed} suppressed).", _id, segmentSuppressed); @@ -249,6 +261,25 @@ private void CopyRgb24FrameToTexture(byte[] frame, NativeArray textureData } } + /// + /// Expands 5-6-5 dots into the RGB24 texture, bottom row first, each channel's top bits repeated into its low ones so + /// that full intensity stays 255. + /// + internal static void CopyRgb565Frame(byte[] frame, int width, int height, NativeArray textureData) + { + for (var y = 0; y < height; y++) { + var src = (height - y - 1) * width * 2; + var dst = y * width * 3; + for (var x = 0; x < width; x++, src += 2, dst += 3) { + var v = frame[src] | frame[src + 1] << 8; + int r = v >> 11, g = (v >> 5) & 0x3f, b = v & 0x1f; + textureData[dst] = (byte)(r << 3 | r >> 2); + textureData[dst + 1] = (byte)(g << 2 | g >> 4); + textureData[dst + 2] = (byte)(b << 3 | b >> 2); + } + } + } + private static unsafe void CopyData(T[] array, int offset, int count, NativeArray dst, int dstOffset = 0) where T : unmanaged { fixed (T * srcPtr = array) { @@ -292,6 +323,7 @@ private void UpdatePalette(DisplayFrameFormat format) case DisplayFrameFormat.Dmd24: case DisplayFrameFormat.Video24: + case DisplayFrameFormat.Dmd565: // no palette to handle here break; diff --git a/VisualPinball.Unity/VisualPinball.Unity/Display/SegmentDisplayComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/Display/SegmentDisplayComponent.cs index 96c28e881f..edbd7d85ad 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Display/SegmentDisplayComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Display/SegmentDisplayComponent.cs @@ -285,6 +285,8 @@ public override void UpdateFrame(DisplayFrameFormat format, byte[] source) case DisplayFrameFormat.Dmd4: case DisplayFrameFormat.Dmd8: case DisplayFrameFormat.Dmd24: + case DisplayFrameFormat.Dmd565: + case DisplayFrameFormat.Video24: if (_frameThrottle.ShouldLog(out var suppressed)) { Logger.Warn("Segment display {DisplayId} cannot render pixel data, use a DMD component ({Suppressed} suppressed).", Id, suppressed); } diff --git a/VisualPinball.Unity/VisualPinball.Unity/Game/Engine/IGamelogicEngine.cs b/VisualPinball.Unity/VisualPinball.Unity/Game/Engine/IGamelogicEngine.cs index 6e77710037..020e96dc4a 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Game/Engine/IGamelogicEngine.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Game/Engine/IGamelogicEngine.cs @@ -357,7 +357,8 @@ public enum DisplayFrameFormat Segment, AlphaNumeric, // gets a byte-array converted string Numeric, // gets a byte-array converted float - Video24 // rgb video (3x 0-255) + Video24, // rgb video (3x 0-255) + Dmd565 // rgb565 (2 bytes per dot, little-endian, red in the top 5 bits) } public class DisplayFrameData diff --git a/VisualPinball.Unity/VisualPinball.Unity/Plugins/GamelogicPluginResources.cs b/VisualPinball.Unity/VisualPinball.Unity/Plugins/GamelogicPluginResources.cs index c4bca810a3..845890d9ba 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Plugins/GamelogicPluginResources.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Plugins/GamelogicPluginResources.cs @@ -248,7 +248,7 @@ private void DisplayFrame(object sender, DisplayFrameData args) if (!_displays.TryGetValue(args.Id, out var display)) return; var format = FromLegacy(args.Format); if (format == DisplayEncoding.None) return; - int stride = display.Width * (format == DisplayEncoding.Rgb24 ? 3 : 1); + int stride = display.Width * PluginDisplayFrame.BytesPerPixel(format); if (args.Data == null || args.Data.Length < stride * display.Height) { _fault(new ArgumentException("Invalid frame buffer for " + args.Id)); return; } display.Source.Publish(new PluginDisplayFrame(args.Data, display.Width, display.Height, stride, format, ++_sequence, Timestamp(), display.Instance)); } @@ -308,7 +308,8 @@ private void AddLampSemantics(LampSource source, bool isCoil, string id, (Contro /// Converts a gamelogic engine's to the plugin : /// to , to /// , to , - /// and and to . + /// and to , + /// and to . /// /// The engine's frame format. /// @@ -318,13 +319,15 @@ private void AddLampSemantics(LampSource source, bool isCoil, string id, (Contro public static DisplayEncoding FromLegacy(DisplayFrameFormat format) => format switch { DisplayFrameFormat.Dmd2 => DisplayEncoding.Gray2, DisplayFrameFormat.Dmd4 => DisplayEncoding.Gray4, DisplayFrameFormat.Dmd8 => DisplayEncoding.Luminance8, DisplayFrameFormat.Dmd24 => DisplayEncoding.Rgb24, - DisplayFrameFormat.Video24 => DisplayEncoding.Rgb24, _ => DisplayEncoding.None + DisplayFrameFormat.Video24 => DisplayEncoding.Rgb24, DisplayFrameFormat.Dmd565 => DisplayEncoding.Rgb565, + _ => DisplayEncoding.None }; /// /// Converts a plugin to the to request from a gamelogic engine /// through : to /// , to , - /// to , and anything else to + /// to , to + /// , and anything else to /// . The DMD plugin also uses it to hand frames to its native pipeline. /// /// @@ -334,7 +337,8 @@ private void AddLampSemantics(LampSource source, bool isCoil, string id, (Contro /// The matching engine frame format. public static DisplayFrameFormat ToLegacy(DisplayEncoding format) => format switch { DisplayEncoding.Gray2 => DisplayFrameFormat.Dmd2, DisplayEncoding.Gray4 => DisplayFrameFormat.Dmd4, - DisplayEncoding.Rgb24 => DisplayFrameFormat.Dmd24, _ => DisplayFrameFormat.Dmd8 + DisplayEncoding.Rgb24 => DisplayFrameFormat.Dmd24, DisplayEncoding.Rgb565 => DisplayFrameFormat.Dmd565, + _ => DisplayFrameFormat.Dmd8 }; /// /// Unsubscribes from the engine, unpublishes every resource, removes the native output listener and, if raw identification