Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions VisualPinball.Plugins.Test/ResourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentException>(() => new PluginDisplayFrame(new byte[3 * 2], 3, 2, 3, DisplayEncoding.Rgb565, 1, 0, 1));
}
[Test]
public void ThrowingObserverCannotLeakPublication()
{
var faults = new List<Exception>(); var registry = new PluginResourceRegistry(faults.Add);
Expand Down
23 changes: 19 additions & 4 deletions VisualPinball.Plugins/Resources/ResourceContracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// <see cref="PluginDisplayFrame.Alpha"/>.
/// </summary>
Rgba32 = 16
Rgba32 = 16,
/// <summary>
/// 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.
/// </summary>
Rgb565 = 32
}

/// <summary>The color space of a display frame's color values.</summary>
Expand Down Expand Up @@ -522,13 +527,23 @@ public readonly struct PluginDisplayFrame
public PluginDisplayFrame(ReadOnlyMemory<byte> 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;
}

/// <summary>Bytes per pixel of a single concrete encoding, or 0 for <see cref="DisplayEncoding.None"/> and combined flags.</summary>
/// <param name="encoding">The encoding.</param>
/// <returns>1 for the gray and luminance encodings, 2 for RGB565, 3 for RGB24, 4 for RGBA32, else 0.</returns>
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
};
}

/// <summary>A source of display frames, published as <see cref="ResourceKind.Display"/>.</summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.

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<byte>(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<byte>(3, Allocator.Temp);

DotMatrixDisplayComponent.CopyRgb565Frame(frame, 1, 1, texture);

Assert.That(texture.ToArray(), Is.EqualTo(new byte[] { 0b10000_100, 0b100000_10, 0b00001_000 }));
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>());
_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);
Expand All @@ -249,6 +261,25 @@ private void CopyRgb24FrameToTexture(byte[] frame, NativeArray<byte> textureData
}
}

/// <summary>
/// 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.
/// </summary>
internal static void CopyRgb565Frame(byte[] frame, int width, int height, NativeArray<byte> 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>(T[] array, int offset, int count, NativeArray<T> dst, int dstOffset = 0) where T : unmanaged
{
fixed (T * srcPtr = array) {
Expand Down Expand Up @@ -292,6 +323,7 @@ private void UpdatePalette(DisplayFrameFormat format)

case DisplayFrameFormat.Dmd24:
case DisplayFrameFormat.Video24:
case DisplayFrameFormat.Dmd565:
// no palette to handle here
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -308,7 +308,8 @@ private void AddLampSemantics(LampSource source, bool isCoil, string id, (Contro
/// Converts a gamelogic engine's <see cref="DisplayFrameFormat"/> to the plugin <see cref="DisplayEncoding"/>:
/// <see cref="DisplayFrameFormat.Dmd2"/> to <see cref="DisplayEncoding.Gray2"/>, <see cref="DisplayFrameFormat.Dmd4"/> to
/// <see cref="DisplayEncoding.Gray4"/>, <see cref="DisplayFrameFormat.Dmd8"/> to <see cref="DisplayEncoding.Luminance8"/>,
/// and <see cref="DisplayFrameFormat.Dmd24"/> and <see cref="DisplayFrameFormat.Video24"/> to <see cref="DisplayEncoding.Rgb24"/>.
/// <see cref="DisplayFrameFormat.Dmd24"/> and <see cref="DisplayFrameFormat.Video24"/> to <see cref="DisplayEncoding.Rgb24"/>,
/// and <see cref="DisplayFrameFormat.Dmd565"/> to <see cref="DisplayEncoding.Rgb565"/>.
/// </summary>
/// <param name="format">The engine's frame format.</param>
/// <returns>
Expand All @@ -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
};
/// <summary>
/// Converts a plugin <see cref="DisplayEncoding"/> to the <see cref="DisplayFrameFormat"/> to request from a gamelogic engine
/// through <see cref="IDisplayFrameFormatPreference.RequestDisplayFrameFormat"/>: <see cref="DisplayEncoding.Gray2"/> to
/// <see cref="DisplayFrameFormat.Dmd2"/>, <see cref="DisplayEncoding.Gray4"/> to <see cref="DisplayFrameFormat.Dmd4"/>,
/// <see cref="DisplayEncoding.Rgb24"/> to <see cref="DisplayFrameFormat.Dmd24"/>, and anything else to
/// <see cref="DisplayEncoding.Rgb24"/> to <see cref="DisplayFrameFormat.Dmd24"/>, <see cref="DisplayEncoding.Rgb565"/> to
/// <see cref="DisplayFrameFormat.Dmd565"/>, and anything else to
/// <see cref="DisplayFrameFormat.Dmd8"/>. The DMD plugin also uses it to hand frames to its native pipeline.
/// </summary>
/// <param name="format">
Expand All @@ -334,7 +337,8 @@ private void AddLampSemantics(LampSource source, bool isCoil, string id, (Contro
/// <returns>The matching engine frame format.</returns>
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
};
/// <summary>
/// Unsubscribes from the engine, unpublishes every resource, removes the native output listener and, if raw identification
Expand Down
Loading