mirror of
https://github.com/0x7c13/Pal3.Unity.git
synced 2026-11-26 17:38:10 +08:00
Separate file decoding from Unity upload through format-neutral texture data and explicit world, sprite and skybox policies. Validate DDS layouts, preserve alpha coverage and use native BC uploads with software RGBA fallback. Compact referenced POL vertices and batch nearby static opaque groups sharing materials. Keep water and dynamic geometry separate, with source ranges retained only in the Editor. Add DDS layout and POL index regression tests. Validation: PAL3 and PAL3A each passed 24 Editor tests, eight combined Game and ResourceViewer style regressions, 936 paired skybox comparisons, and both macOS Player builds on Metal. Editor provenance and Core tests are excluded from Players.
94 lines
3.6 KiB
C#
94 lines
3.6 KiB
C#
// See LICENSE in the project root.
|
|
|
|
namespace Engine.Core.Implementation
|
|
{
|
|
using System;
|
|
using System.Buffers;
|
|
using Abstraction;
|
|
|
|
/// <summary>A base-level RGBA32 upload buffer, optionally owned by the shared array pool.</summary>
|
|
public sealed class Rgba32TextureDataSource : ITextureDataSource
|
|
{
|
|
public int Width { get; }
|
|
public int Height { get; }
|
|
public int MipCount => 1;
|
|
public TextureDataFormat Format => TextureDataFormat.Rgba32;
|
|
public bool HasTransparency { get; }
|
|
|
|
private byte[] _data;
|
|
private readonly int _length;
|
|
private readonly bool _returnToPool;
|
|
|
|
/// <summary>Borrow a buffer; the caller must keep it unchanged until upload completes.</summary>
|
|
public Rgba32TextureDataSource(int width, int height, byte[] data, bool hasTransparency = true)
|
|
: this(width, height, data, hasTransparency, returnToPool: false) { }
|
|
|
|
private Rgba32TextureDataSource(int width, int height, byte[] data,
|
|
bool hasTransparency, bool returnToPool)
|
|
{
|
|
if (width <= 0 || height <= 0) throw new ArgumentOutOfRangeException(nameof(width));
|
|
_length = checked(width * height * 4);
|
|
if (data == null || data.Length < _length) throw new ArgumentException("Incomplete RGBA32 data", nameof(data));
|
|
Width = width;
|
|
Height = height;
|
|
HasTransparency = hasTransparency;
|
|
_data = data;
|
|
_returnToPool = returnToPool;
|
|
}
|
|
|
|
/// <summary>Transfer a rented buffer to this source, which returns it on Dispose.</summary>
|
|
internal static Rgba32TextureDataSource TakePooledBuffer(int width, int height, byte[] data,
|
|
bool hasTransparency) => new(width, height, data, hasTransparency, returnToPool: true);
|
|
|
|
public ArraySegment<byte> GetMipData(int mip)
|
|
{
|
|
if (mip != 0) throw new ArgumentOutOfRangeException(nameof(mip));
|
|
if (_data == null) throw new ObjectDisposedException(nameof(Rgba32TextureDataSource));
|
|
return new ArraySegment<byte>(_data, 0, _length);
|
|
}
|
|
|
|
public void CopyMipToRgba32(int mip, byte[] destination)
|
|
{
|
|
var data = GetMipData(mip);
|
|
Buffer.BlockCopy(data.Array, data.Offset, destination, 0, data.Count);
|
|
}
|
|
|
|
public ITexturePixelReader CreatePixelReader()
|
|
{
|
|
var data = GetMipData(0);
|
|
var ownedPixels = new byte[data.Count];
|
|
Buffer.BlockCopy(data.Array, data.Offset, ownedPixels, 0, data.Count);
|
|
return new PixelReader(Width, Height, ownedPixels);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_data == null) return;
|
|
if (_returnToPool) ArrayPool<byte>.Shared.Return(_data);
|
|
_data = null;
|
|
}
|
|
|
|
private sealed class PixelReader : ITexturePixelReader
|
|
{
|
|
private readonly int _width;
|
|
private readonly int _height;
|
|
private readonly byte[] _pixels;
|
|
|
|
public PixelReader(int width, int height, byte[] pixels)
|
|
{
|
|
_width = width;
|
|
_height = height;
|
|
_pixels = pixels;
|
|
}
|
|
|
|
public (byte r, byte g, byte b, byte a) GetPixel(int x, int y)
|
|
{
|
|
if ((uint)x >= (uint)_width || (uint)y >= (uint)_height)
|
|
throw new ArgumentOutOfRangeException(nameof(x));
|
|
int offset = (y * _width + x) * 4;
|
|
return (_pixels[offset], _pixels[offset + 1], _pixels[offset + 2], _pixels[offset + 3]);
|
|
}
|
|
}
|
|
}
|
|
}
|