Files
Jiaqi Liu d1b2c05eb4 Optimize resource loading and fix PAL3A script execution (#92)
* Optimize DDS texture creation and static POL meshes

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.

* Add core protocol regression tests and copyright checks

Cover SCE command contracts, binary readers, resource layouts and full local CPK decompression for PAL3 and PAL3A. Fix CVD time rebasing, binary read bounds and short reads, and CPK/NAV validation.

Standardize first-party copyright headers, preserve correct creation years, and update console source generation. Validate both variants in Unity and verify all live CPK entries.

* Implement PAL3A command handlers and isolate script execution

Correct PAL3A parameter signatures and add handlers for fortune selection,
skybox sequences, actor facing and attacks, team and progression state,
camera constraints, and battle preparation flags.

Give script instances separate locals, advance grouped waits concurrently,
and pause PAL3A parents only for their directly decoded child calls. Cancel
stale interactions and save replay on reset, preserve finite action semantics,
and restore camera orientation after releasing horizontal constraints.

Persist supported state in v2 saves and add protocol, runtime, and progression
regressions. Full combat gameplay remains outside this change.

Validation: PAL3 274/274 and PAL3A 291/291 EditMode tests; 18 PAL3 and 22 PAL3A
runtime checks. All 77,889 PAL3A instruction boundaries match EXE signatures.

* Run CI only for pull requests and upgrade build actions
2026-11-13 09:28:31 +08:00

132 lines
3.4 KiB
C#

// ------------------------------------------------------------------------
// Copyright (c) 2021-2026, Jiaqi (0x7c13) Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ------------------------------------------------------------------------
namespace Pal3.Core.DataReader.Dxt
{
using System;
using Utilities;
/// <summary>
/// Decode DXT1 image data and convert it to RGBA32(RGB888).
/// </summary>
public static class Dxt1Decoder
{
// 4 chars header label
// 30 integers + 4 chars format info
// Total of: 4 + 30 x 4 + 4 = 128
private const int DDS_FILE_HEADER_SIZE = 128;
public static byte[] ToRgba32(byte[] data, int width, int height)
{
byte[] buffer = new byte[width * height * 4];
ToRgba32(data, width, height, buffer);
return buffer;
}
public static unsafe void ToRgba32(byte[] data, int width, int height, byte[] buffer, int sourceOffset = DDS_FILE_HEADER_SIZE)
{
if (width <= 0 || height <= 0 || buffer == null || (long)buffer.Length < (long)width * height * 4)
{
throw new ArgumentException("buffer is null or too small");
}
if (sourceOffset < 0 || data == null ||
(long)sourceOffset + ((width + 3L) / 4) * ((height + 3L) / 4) * 8 > data.Length)
throw new ArgumentException("DXT source data is too small");
fixed (byte* srcStart = &data[sourceOffset], dstStart = buffer)
{
byte* src = srcStart;
byte* dst = dstStart;
for (int j = 0; j < (height + 3) / 4; j++)
{
for (int i = 0; i < (width + 3) / 4; i++)
{
DecodeDxt1Block(i, j, width, height, src, dst);
src += 8; // 2 + 2 + 4
}
}
}
}
private static unsafe void DecodeDxt1Block(int x, int y, int width, int height, byte* src, byte* dst)
{
ushort color0 = *(ushort*)src;
ushort color1 = *(ushort*)(src + 2);
CoreUtility.Rgb565ToRgb888(color0, out var r0, out var g0, out var b0);
CoreUtility.Rgb565ToRgb888(color1, out var r1, out var g1, out var b1);
uint lookupTable = *(uint*)(src + 4);
for (int blockY = 0; blockY < 4; blockY++)
{
for (int blockX = 0; blockX < 4; blockX++)
{
byte r = 0, g = 0, b = 0, a = 255;
uint index = (lookupTable >> 2 * (4 * blockY + blockX)) & 0x03;
if (color0 > color1)
{
switch (index)
{
case 0:
r = r0; g = g0; b = b0;
break;
case 1:
r = r1; g = g1; b = b1;
break;
case 2:
r = (byte) ((2 * r0 + r1) / 3);
g = (byte) ((2 * g0 + g1) / 3);
b = (byte) ((2 * b0 + b1) / 3);
break;
case 3:
r = (byte) ((r0 + 2 * r1) / 3);
g = (byte) ((g0 + 2 * g1) / 3);
b = (byte) ((b0 + 2 * b1) / 3);
break;
}
}
else
{
switch (index)
{
case 0:
r = r0; g = g0; b = b0;
break;
case 1:
r = r1; g = g1; b = b1;
break;
case 2:
r = (byte) ((r0 + r1) / 2);
g = (byte) ((g0 + g1) / 2);
b = (byte) ((b0 + b1) / 2);
break;
case 3:
r = 0;
g = 0;
b = 0;
a = 0;
break;
}
}
int px = (x << 2) + blockX;
int py = (y << 2) + blockY;
if ((px < width) && (py < height))
{
int offset = ((py * width) + px) << 2;
*(dst + offset) = r;
*(dst + offset + 1) = g;
*(dst + offset + 2) = b;
*(dst + offset + 3) = a;
}
}
}
}
}
}