From 8e74511babaf5bd20a227b37b1418a91b1a7a579 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Sat, 28 Nov 2026 04:46:27 +0000 Subject: [PATCH 01/12] Add multi-platform Unity CI build and E2E test pipeline on develop --- .github/workflows/unity-multiplatform-e2e.yml | 373 ++++++++++++++++++ Assets/ASTC Encoder/Editor/CIBuildScript.cs | 320 +++++++++++++++ .../ASTC Encoder/Editor/CIBuildScript.cs.meta | 11 + Assets/Scripts/AstcE2ESmokeRunner.cs | 261 ++++++++++++ Assets/Scripts/AstcE2ESmokeRunner.cs.meta | 11 + docs/CI_E2E_GUIDE.md | 32 ++ scripts/build-linux-astcenc.sh | 149 +++++++ scripts/verify-native-plugins.sh | 89 +++++ 8 files changed, 1246 insertions(+) create mode 100644 .github/workflows/unity-multiplatform-e2e.yml create mode 100644 Assets/ASTC Encoder/Editor/CIBuildScript.cs create mode 100644 Assets/ASTC Encoder/Editor/CIBuildScript.cs.meta create mode 100644 Assets/Scripts/AstcE2ESmokeRunner.cs create mode 100644 Assets/Scripts/AstcE2ESmokeRunner.cs.meta create mode 100644 docs/CI_E2E_GUIDE.md create mode 100755 scripts/build-linux-astcenc.sh create mode 100755 scripts/verify-native-plugins.sh diff --git a/.github/workflows/unity-multiplatform-e2e.yml b/.github/workflows/unity-multiplatform-e2e.yml new file mode 100644 index 0000000..ddb6d93 --- /dev/null +++ b/.github/workflows/unity-multiplatform-e2e.yml @@ -0,0 +1,373 @@ +name: Unity Multi-Platform Build & E2E Tests + +on: + push: + branches: + - develop + pull_request: + branches: + - develop + workflow_dispatch: + inputs: + unity_version: + description: "Unity Editor version on GameCI (international equivalent of 2021.3.6f1c1)" + required: false + default: "2021.3.6f1" + type: string + run_android_emulator: + description: "Run Android ARM64 emulator E2E test on macOS runner" + required: false + default: true + type: boolean + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +env: + UNITY_VERSION: ${{ inputs.unity_version || '2021.3.6f1' }} + +jobs: + native-abi-check: + name: Native Plugins & Linux .so Build + runs-on: ubuntu-latest + steps: + - name: Checkout repository (with Git LFS & submodules) + uses: actions/checkout@v4 + with: + lfs: true + submodules: true + + - name: Build Linux x86_64 libastcenc-sse4.1-shared.so + run: | + chmod +x scripts/build-linux-astcenc.sh scripts/verify-native-plugins.sh + ./scripts/build-linux-astcenc.sh + + - name: Verify all platform native plugins and exported symbols + run: ./scripts/verify-native-plugins.sh + + - name: Upload Linux native plugin artifact + uses: actions/upload-artifact@v4 + with: + name: linux-astcenc-plugin + path: | + Assets/ASTC Encoder/Plugins/Linux + Assets/ASTC Encoder/Plugins/Linux.meta + retention-days: 1 + + check-unity-license: + name: Check Unity License Secrets + runs-on: ubuntu-latest + outputs: + has_license: ${{ steps.check.outputs.has_license }} + steps: + - name: Check configured Unity license secrets + id: check + env: + UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }} + UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }} + run: | + if [[ -n "${UNITY_LICENSE}" || -n "${UNITY_SERIAL}" ]]; then + echo "has_license=true" >> "$GITHUB_OUTPUT" + echo "Unity license secret detected. Running Unity Test Runner and multi-platform builds." >> "$GITHUB_STEP_SUMMARY" + else + echo "has_license=false" >> "$GITHUB_OUTPUT" + echo "::warning::UNITY_LICENSE (or UNITY_SERIAL) secret is not configured. Skipping Unity build/test jobs. See docs/CI_E2E_GUIDE.md." + { + echo "### Unity License Secret Not Configured" + echo "Skipped Unity Editor tests and multi-platform player builds because neither \`UNITY_LICENSE\` nor \`UNITY_SERIAL\` is configured in Repository Secrets." + echo "See \`docs/CI_E2E_GUIDE.md\` for setup instructions." + } >> "$GITHUB_STEP_SUMMARY" + fi + + unity-tests: + name: Unity Test Runner (EditMode) + needs: [native-abi-check, check-unity-license] + if: needs.check-unity-license.outputs.has_license == 'true' + runs-on: ubuntu-latest + steps: + - name: Checkout repository (with Git LFS) + uses: actions/checkout@v4 + with: + lfs: true + + - name: Download Linux native plugin + uses: actions/download-artifact@v4 + with: + name: linux-astcenc-plugin + path: Assets/ASTC Encoder/Plugins + + - name: Cache Unity Library + uses: actions/cache@v4 + with: + path: Library + key: Library-tests-${{ env.UNITY_VERSION }}-${{ hashFiles('Assets/**', 'Packages/**', 'ProjectSettings/**') }} + restore-keys: | + Library-tests-${{ env.UNITY_VERSION }}- + Library-tests- + + - name: Run Unity EditMode Tests + uses: game-ci/unity-test-runner@v4 + env: + UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }} + UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }} + UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }} + UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }} + with: + unityVersion: ${{ env.UNITY_VERSION }} + testMode: EditMode + artifactsPath: test-results + githubToken: ${{ secrets.GITHUB_TOKEN }} + + - name: Upload Unity test results + uses: actions/upload-artifact@v4 + if: always() + with: + name: unity-test-results + path: test-results + retention-days: 7 + + build-players: + name: Build (${{ matrix.targetPlatform }}) + needs: [native-abi-check, check-unity-license] + if: needs.check-unity-license.outputs.has_license == 'true' + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - targetPlatform: StandaloneLinux64 + customParameters: "-scriptingBackend Mono" + - targetPlatform: StandaloneWindows64 + customParameters: "-scriptingBackend Mono" + - targetPlatform: StandaloneOSX + customParameters: "-scriptingBackend Mono" + - targetPlatform: Android + customParameters: "-scriptingBackend IL2CPP -enableE2ESmoke" + - targetPlatform: iOS + customParameters: "-scriptingBackend IL2CPP -enableE2ESmoke" + steps: + - name: Checkout repository (with Git LFS) + uses: actions/checkout@v4 + with: + lfs: true + + - name: Download Linux native plugin + uses: actions/download-artifact@v4 + with: + name: linux-astcenc-plugin + path: Assets/ASTC Encoder/Plugins + + - name: Cache Unity Library + uses: actions/cache@v4 + with: + path: Library + key: Library-${{ matrix.targetPlatform }}-${{ env.UNITY_VERSION }}-${{ hashFiles('Assets/**', 'Packages/**', 'ProjectSettings/**') }} + restore-keys: | + Library-${{ matrix.targetPlatform }}-${{ env.UNITY_VERSION }}- + Library-${{ matrix.targetPlatform }}- + + - name: Build Unity Player for ${{ matrix.targetPlatform }} + uses: game-ci/unity-builder@v4 + env: + UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }} + UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }} + UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }} + UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }} + with: + unityVersion: ${{ env.UNITY_VERSION }} + targetPlatform: ${{ matrix.targetPlatform }} + buildName: ASTCEncoder + versioning: None + allowDirtyBuild: true + buildMethod: Aperture.Astcenc.Editor.CIBuildScript.Build + customParameters: ${{ matrix.customParameters }} + + - name: Upload ${{ matrix.targetPlatform }} build artifact + uses: actions/upload-artifact@v4 + with: + name: build-${{ matrix.targetPlatform }} + path: build/${{ matrix.targetPlatform }} + retention-days: 7 + + e2e-linux: + name: E2E Player (Linux x64) + needs: build-players + runs-on: ubuntu-latest + steps: + - name: Download Linux64 build + uses: actions/download-artifact@v4 + with: + name: build-StandaloneLinux64 + path: build/StandaloneLinux64 + + - name: Run headless Linux Player E2E smoke test + run: | + BIN="$(find build/StandaloneLinux64 -maxdepth 2 -type f \( -name 'ASTCEncoder.x86_64' -o -name 'ASTCEncoder' \) | head -n 1)" + if [[ -z "${BIN}" ]]; then + echo "ERROR: Linux player binary not found." >&2 + find build/StandaloneLinux64 -maxdepth 3 + exit 1 + fi + chmod +x "${BIN}" + set +e + timeout 120 "${BIN}" -batchmode -nographics -logfile e2e-linux.log -astc-e2e + STATUS=$? + set -e + cat e2e-linux.log + if [[ "${STATUS}" -ne 0 ]]; then + echo "ERROR: Linux player exited with status ${STATUS}" >&2 + exit "${STATUS}" + fi + grep -q "ASTC_E2E_PASSED" e2e-linux.log + + e2e-windows: + name: E2E Player (Windows x64) + needs: build-players + runs-on: windows-latest + steps: + - name: Download Windows64 build + uses: actions/download-artifact@v4 + with: + name: build-StandaloneWindows64 + path: build/StandaloneWindows64 + + - name: Run headless Windows Player E2E smoke test + shell: pwsh + run: | + $exe = Get-ChildItem -Path "build/StandaloneWindows64" -Filter "ASTCEncoder.exe" -Recurse | Select-Object -First 1 + if (-not $exe) { + Write-Error "Windows player executable ASTCEncoder.exe not found." + Get-ChildItem -Path "build/StandaloneWindows64" -Recurse + exit 1 + } + $logFile = Join-Path (Get-Location) "e2e-windows.log" + $proc = Start-Process -FilePath $exe.FullName -ArgumentList "-batchmode", "-nographics", "-logfile", $logFile, "-astc-e2e" -NoNewWindow -Wait -PassThru + if (Test-Path $logFile) { + Get-Content $logFile + } + if ($proc.ExitCode -ne 0) { + Write-Error "Windows player exited with code $($proc.ExitCode)" + exit $proc.ExitCode + } + if (-not (Select-String -Path $logFile -Pattern "ASTC_E2E_PASSED" -Quiet)) { + Write-Error "ASTC_E2E_PASSED marker not found in log." + exit 1 + } + + e2e-macos: + name: E2E Player (macOS Universal) + needs: build-players + runs-on: macos-latest + steps: + - name: Download macOS build + uses: actions/download-artifact@v4 + with: + name: build-StandaloneOSX + path: build/StandaloneOSX + + - name: Run headless macOS Player E2E smoke test + run: | + APP="$(find build/StandaloneOSX -maxdepth 2 -type d -name '*.app' | head -n 1)" + if [[ -z "${APP}" ]]; then + echo "ERROR: macOS .app bundle not found." >&2 + find build/StandaloneOSX -maxdepth 3 + exit 1 + fi + BIN="$(find "${APP}/Contents/MacOS" -maxdepth 1 -type f | head -n 1)" + chmod +x "${BIN}" + xattr -dr com.apple.quarantine "${APP}" || true + set +e + "${BIN}" -batchmode -nographics -logfile e2e-macos.log -astc-e2e + STATUS=$? + set -e + cat e2e-macos.log + if [[ "${STATUS}" -ne 0 ]]; then + echo "ERROR: macOS player exited with status ${STATUS}" >&2 + exit "${STATUS}" + fi + grep -q "ASTC_E2E_PASSED" e2e-macos.log + + e2e-android: + name: E2E Player (Android ARM64) + needs: build-players + runs-on: macos-latest + steps: + - name: Download Android APK + uses: actions/download-artifact@v4 + with: + name: build-Android + path: build/Android + + - name: Verify APK native libraries (arm64-v8a) + run: | + APK="$(find build/Android -name '*.apk' | head -n 1)" + if [[ -z "${APK}" ]]; then + echo "ERROR: Android APK not found." >&2 + find build/Android -maxdepth 3 + exit 1 + fi + echo "APK_PATH=${APK}" >> "$GITHUB_ENV" + unzip -l "${APK}" | grep "lib/arm64-v8a/libastcenc-neon-shared.so" + unzip -l "${APK}" | grep "lib/arm64-v8a/libil2cpp.so" + + - name: Run APK on Android ARM64 Emulator + if: ${{ inputs.run_android_emulator != false }} + uses: reactivecircus/android-emulator-runner@v2 + with: + api-level: 33 + arch: arm64-v8a + target: default + emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim + script: | + adb logcat -c + adb install -r "${APK_PATH}" + adb shell am start -n com.DefaultCompany.ASTCEncoder/com.unity3d.player.UnityPlayerActivity + for i in $(seq 1 45); do + LOGS="$(adb logcat -d -s Unity:V)" + if grep -q "ASTC_E2E_PASSED" <<< "${LOGS}"; then + echo "${LOGS}" + echo "Android ARM64 E2E passed!" + exit 0 + fi + if grep -q "ASTC_E2E_FAILED" <<< "${LOGS}"; then + echo "${LOGS}" + echo "ERROR: Android ARM64 E2E failed!" >&2 + exit 1 + fi + sleep 2 + done + echo "ERROR: Timed out waiting for ASTC_E2E_PASSED in adb logcat." >&2 + adb logcat -d -s Unity:V + exit 1 + + e2e-ios-xcodebuild: + name: E2E Link & Build (iOS Xcode ARM64) + needs: build-players + runs-on: macos-latest + steps: + - name: Download iOS Xcode project + uses: actions/download-artifact@v4 + with: + name: build-iOS + path: build/iOS + + - name: Verify iOS static library and compile with xcodebuild + run: | + XCODEPROJ="$(find build/iOS -maxdepth 3 -name 'Unity-iPhone.xcodeproj' | head -n 1)" + if [[ -z "${XCODEPROJ}" ]]; then + echo "ERROR: Unity-iPhone.xcodeproj not found." >&2 + find build/iOS -maxdepth 3 + exit 1 + fi + PROJECT_DIR="$(dirname "${XCODEPROJ}")" + find "${PROJECT_DIR}" -name 'libastcenc-neon-static.a' | grep -q 'libastcenc-neon-static.a' + xcodebuild \ + -project "${XCODEPROJ}" \ + -scheme Unity-iPhone \ + -sdk iphoneos \ + -configuration Release \ + CODE_SIGNING_ALLOWED=NO \ + CODE_SIGNING_REQUIRED=NO \ + CODE_SIGN_IDENTITY="" \ + build diff --git a/Assets/ASTC Encoder/Editor/CIBuildScript.cs b/Assets/ASTC Encoder/Editor/CIBuildScript.cs new file mode 100644 index 0000000..45c971b --- /dev/null +++ b/Assets/ASTC Encoder/Editor/CIBuildScript.cs @@ -0,0 +1,320 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using UnityEditor; +using UnityEditor.Build.Reporting; +using UnityEngine; + +namespace Aperture.Astcenc.Editor +{ + /// + /// Command-line and Editor build entry points for multi-platform CI builds (GameCI / batchmode). + /// + public static class CIBuildScript + { + private const string E2EDefineSymbol = "ASTC_CI_E2E"; + + /// + /// Main entry point invoked by GameCI (buildMethod: Aperture.Astcenc.Editor.CIBuildScript.Build) + /// or via Unity -batchmode -quit -executeMethod Aperture.Astcenc.Editor.CIBuildScript.Build. + /// + public static void Build() + { + Dictionary args = ParseCommandLineArgs(); + + string targetName = GetArg(args, "customBuildTarget", GetArg(args, "buildTarget", EditorUserBuildSettings.activeBuildTarget.ToString())); + if (!TryParseBuildTarget(targetName, out BuildTarget buildTarget)) + { + Fail($"Unsupported buildTarget: '{targetName}'."); + return; + } + + string buildName = GetArg(args, "customBuildName", PlayerSettings.productName); + string outputPath = GetArg(args, "customBuildPath", Path.Combine("build", buildTarget.ToString(), buildName)); + string scriptingBackendArg = GetArg(args, "scriptingBackend", string.Empty); + bool enableE2ESmoke = HasFlag(args, "enableE2ESmoke"); + bool isDevelopment = HasFlag(args, "development"); + + ExecuteBuild(buildTarget, outputPath, buildName, scriptingBackendArg, enableE2ESmoke, isDevelopment); + } + + [MenuItem("Build/CI/Android (ARM64 IL2CPP)")] + public static void BuildAndroidLocal() + { + ExecuteBuild(BuildTarget.Android, "build/Android/ASTCEncoder.apk", "ASTCEncoder", "IL2CPP", enableE2ESmoke: false, isDevelopment: false); + } + + [MenuItem("Build/CI/iOS (Xcode Project)")] + public static void BuildiOSLocal() + { + ExecuteBuild(BuildTarget.iOS, "build/iOS", "ASTCEncoder", "IL2CPP", enableE2ESmoke: false, isDevelopment: false); + } + + [MenuItem("Build/CI/Windows x64 (Mono)")] + public static void BuildWindows64Local() + { + ExecuteBuild(BuildTarget.StandaloneWindows64, "build/StandaloneWindows64/ASTCEncoder.exe", "ASTCEncoder", "Mono", enableE2ESmoke: false, isDevelopment: false); + } + + [MenuItem("Build/CI/macOS Universal (Mono)")] + public static void BuildOSXLocal() + { + ExecuteBuild(BuildTarget.StandaloneOSX, "build/StandaloneOSX/ASTCEncoder.app", "ASTCEncoder", "Mono", enableE2ESmoke: false, isDevelopment: false); + } + + [MenuItem("Build/CI/Linux x64 (Mono)")] + public static void BuildLinux64Local() + { + ExecuteBuild(BuildTarget.StandaloneLinux64, "build/StandaloneLinux64/ASTCEncoder.x86_64", "ASTCEncoder", "Mono", enableE2ESmoke: false, isDevelopment: false); + } + + private static void ExecuteBuild( + BuildTarget buildTarget, + string outputPath, + string buildName, + string scriptingBackendArg, + bool enableE2ESmoke, + bool isDevelopment) + { + BuildTargetGroup targetGroup = BuildPipeline.GetBuildTargetGroup(buildTarget); + string[] scenes = EditorBuildSettings.scenes + .Where(s => s.enabled && !string.IsNullOrEmpty(s.path)) + .Select(s => s.path) + .ToArray(); + + if (scenes.Length == 0) + { + Fail("No enabled scenes found in EditorBuildSettings."); + return; + } + + ConfigurePlatformSettings(buildTarget, targetGroup, scriptingBackendArg); + + string originalDefines = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup); + if (enableE2ESmoke) + { + AddDefineSymbol(targetGroup, E2EDefineSymbol); + } + + string locationPath = NormalizeOutputPath(buildTarget, outputPath, buildName); + EnsureParentDirectory(buildTarget, locationPath); + + Debug.Log($"[CIBuildScript] Building target={buildTarget}, group={targetGroup}, backend={PlayerSettings.GetScriptingBackend(targetGroup)}, e2e={enableE2ESmoke}, output='{locationPath}'"); + + BuildPlayerOptions options = new BuildPlayerOptions + { + scenes = scenes, + locationPathName = locationPath, + target = buildTarget, + targetGroup = targetGroup, + options = isDevelopment ? BuildOptions.Development : BuildOptions.None, + }; + + try + { + BuildReport report = BuildPipeline.BuildPlayer(options); + BuildSummary summary = report.summary; + + Debug.Log($"[CIBuildScript] Build finished: result={summary.result}, errors={summary.totalErrors}, warnings={summary.totalWarnings}, size={summary.totalSize} bytes, time={summary.totalTime}"); + + if (summary.result != BuildResult.Succeeded) + { + Fail($"Build failed for {buildTarget} with result {summary.result} ({summary.totalErrors} errors)."); + } + } + finally + { + if (enableE2ESmoke) + { + PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, originalDefines); + } + } + } + + private static void ConfigurePlatformSettings(BuildTarget buildTarget, BuildTargetGroup targetGroup, string scriptingBackendArg) + { + if (string.Equals(scriptingBackendArg, "Mono", StringComparison.OrdinalIgnoreCase)) + { + PlayerSettings.SetScriptingBackend(targetGroup, ScriptingImplementation.Mono2x); + } + else if (string.Equals(scriptingBackendArg, "IL2CPP", StringComparison.OrdinalIgnoreCase)) + { + PlayerSettings.SetScriptingBackend(targetGroup, ScriptingImplementation.IL2CPP); + } + else if (buildTarget == BuildTarget.StandaloneWindows64 || buildTarget == BuildTarget.StandaloneOSX) + { +#if UNITY_EDITOR_LINUX + PlayerSettings.SetScriptingBackend(targetGroup, ScriptingImplementation.Mono2x); +#endif + } + + switch (buildTarget) + { + case BuildTarget.Android: + PlayerSettings.SetScriptingBackend(BuildTargetGroup.Android, ScriptingImplementation.IL2CPP); + PlayerSettings.Android.targetArchitectures = AndroidArchitecture.ARM64; + EditorUserBuildSettings.buildAppBundle = false; + break; + + case BuildTarget.iOS: + PlayerSettings.SetScriptingBackend(BuildTargetGroup.iOS, ScriptingImplementation.IL2CPP); + PlayerSettings.iOS.sdkVersion = iOSSdkVersion.DeviceSDK; + break; + } + } + + private static string NormalizeOutputPath(BuildTarget buildTarget, string outputPath, string buildName) + { + string normalized = outputPath.TrimEnd('/', '\\'); + switch (buildTarget) + { + case BuildTarget.Android: + if (!normalized.EndsWith(".apk", StringComparison.OrdinalIgnoreCase) && + !normalized.EndsWith(".aab", StringComparison.OrdinalIgnoreCase)) + { + normalized = Path.Combine(normalized, buildName + ".apk"); + } + break; + + case BuildTarget.StandaloneWindows64: + if (!normalized.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) + { + normalized = Path.Combine(normalized, buildName + ".exe"); + } + break; + + case BuildTarget.StandaloneOSX: + if (!normalized.EndsWith(".app", StringComparison.OrdinalIgnoreCase)) + { + normalized = Path.Combine(normalized, buildName + ".app"); + } + break; + + case BuildTarget.StandaloneLinux64: + if (Directory.Exists(normalized) || !Path.HasExtension(normalized)) + { + if (!normalized.EndsWith(buildName, StringComparison.OrdinalIgnoreCase)) + { + normalized = Path.Combine(normalized, buildName + ".x86_64"); + } + } + break; + } + + return normalized; + } + + private static void EnsureParentDirectory(BuildTarget buildTarget, string locationPath) + { + if (buildTarget == BuildTarget.iOS) + { + Directory.CreateDirectory(locationPath); + } + else + { + string dir = Path.GetDirectoryName(locationPath); + if (!string.IsNullOrEmpty(dir)) + { + Directory.CreateDirectory(dir); + } + } + } + + private static void AddDefineSymbol(BuildTargetGroup targetGroup, string symbol) + { + string existing = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup); + List symbols = existing + .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) + .Select(s => s.Trim()) + .ToList(); + + if (!symbols.Contains(symbol)) + { + symbols.Add(symbol); + PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, string.Join(";", symbols)); + } + } + + private static bool TryParseBuildTarget(string targetName, out BuildTarget buildTarget) + { + if (Enum.TryParse(targetName, ignoreCase: true, out buildTarget)) + { + return true; + } + + switch (targetName.ToLowerInvariant()) + { + case "win64": + case "windows": + case "windows64": + buildTarget = BuildTarget.StandaloneWindows64; + return true; + case "osx": + case "macos": + case "osxuniversal": + buildTarget = BuildTarget.StandaloneOSX; + return true; + case "linux": + case "linux64": + buildTarget = BuildTarget.StandaloneLinux64; + return true; + default: + return false; + } + } + + private static Dictionary ParseCommandLineArgs() + { + Dictionary result = new Dictionary(StringComparer.OrdinalIgnoreCase); + string[] args = Environment.GetCommandLineArgs(); + for (int i = 0; i < args.Length; i++) + { + string arg = args[i]; + if (!arg.StartsWith("-", StringComparison.Ordinal)) + { + continue; + } + + string key = arg.TrimStart('-'); + if (i + 1 < args.Length && !args[i + 1].StartsWith("-", StringComparison.Ordinal)) + { + result[key] = args[i + 1]; + i++; + } + else + { + result[key] = "true"; + } + } + return result; + } + + private static string GetArg(Dictionary args, string key, string defaultValue) + { + return args.TryGetValue(key, out string value) && !string.IsNullOrEmpty(value) + ? value + : defaultValue; + } + + private static bool HasFlag(Dictionary args, string key) + { + return args.TryGetValue(key, out string value) && + !string.Equals(value, "false", StringComparison.OrdinalIgnoreCase) && + !string.Equals(value, "0", StringComparison.OrdinalIgnoreCase); + } + + private static void Fail(string message) + { + Debug.LogError($"[CIBuildScript] {message}"); + if (Application.isBatchMode) + { + EditorApplication.Exit(1); + } + else + { + throw new Exception(message); + } + } + } +} diff --git a/Assets/ASTC Encoder/Editor/CIBuildScript.cs.meta b/Assets/ASTC Encoder/Editor/CIBuildScript.cs.meta new file mode 100644 index 0000000..7bbaa53 --- /dev/null +++ b/Assets/ASTC Encoder/Editor/CIBuildScript.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5e2a7c4b8f1940369d6e1b2c7a485f31 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/AstcE2ESmokeRunner.cs b/Assets/Scripts/AstcE2ESmokeRunner.cs new file mode 100644 index 0000000..08674cc --- /dev/null +++ b/Assets/Scripts/AstcE2ESmokeRunner.cs @@ -0,0 +1,261 @@ +using System; +using System.IO; +using Aperture.Astcenc.Runtime; +using UnityEngine; +using UnityEngine.Experimental.Rendering; + +/// +/// Built-player end-to-end smoke test runner. +/// Activates in player builds when launched with the -astc-e2e command-line flag +/// or when built with the ASTC_CI_E2E scripting define symbol. +/// +public static class AstcE2ESmokeRunner +{ + private const string LogPrefix = "[ASTC_E2E]"; + +#if !UNITY_EDITOR + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] + private static void RunIfRequested() + { + if (!ShouldRun()) + { + return; + } + + Debug.Log($"{LogPrefix} Starting runtime end-to-end verification on platform={Application.platform}, arch={System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture}, gfx={SystemInfo.graphicsDeviceType}"); + + try + { + VerifyIsSupport(); + VerifyAllBlockSizes(); + VerifyEncodeToAstcAndKtxRoundTrip(); + VerifyNonSquareMipmaps(); + VerifyNormalAndMultiFormats(); + VerifyInPlaceCompressIfSupported(); + + Debug.Log($"{LogPrefix} ASTC_E2E_PASSED"); + Application.Quit(0); + } + catch (Exception ex) + { + Debug.LogError($"{LogPrefix} ASTC_E2E_FAILED: {ex}"); + Application.Quit(1); + } + } + + private static bool ShouldRun() + { +#if ASTC_CI_E2E + return true; +#else + string[] args = Environment.GetCommandLineArgs(); + for (int i = 0; i < args.Length; i++) + { + if (string.Equals(args[i], "-astc-e2e", StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + return false; +#endif + } +#endif + + private static void AssertTrue(bool condition, string message) + { + if (!condition) + { + throw new InvalidOperationException($"Assertion failed: {message}"); + } + } + + private static Texture2D CreateProceduralTexture(int width, int height, TextureFormat format, bool mipChain, bool linear = false) + { + Texture2D tex = new Texture2D(width, height, format, mipChain, linear); + Color[] pixels = new Color[width * height]; + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + pixels[y * width + x] = new Color( + (float)x / Math.Max(1, width - 1), + (float)y / Math.Max(1, height - 1), + ((x ^ y) & 0xFF) / 255.0f, + 0.75f); + } + } + tex.SetPixels(pixels); + tex.Apply(mipChain, false); + return tex; + } + + private static int ExpectedCompressedBytes(int width, int height, int mipCount, int blockDim) + { + int total = 0; + for (int i = 0; i < mipCount; i++) + { + int mw = Math.Max(1, width >> i); + int mh = Math.Max(1, height >> i); + int bx = (mw + blockDim - 1) / blockDim; + int by = (mh + blockDim - 1) / blockDim; + total += bx * by * 16; + } + return total; + } + + private static void VerifyIsSupport() + { + AssertTrue(!Encoder.IsSupport(null), "Null texture must not be supported."); + + Texture2D readable = CreateProceduralTexture(16, 16, TextureFormat.RGBA32, false); + try + { + AssertTrue(Encoder.IsSupport(readable, EncodeBackend.Cpu), "Readable RGBA32 texture must be supported on CPU backend."); + readable.Apply(false, true); + AssertTrue(!Encoder.IsSupport(readable, EncodeBackend.Cpu), "Non-readable texture must not be supported on CPU backend."); + } + finally + { + UnityEngine.Object.DestroyImmediate(readable); + } + } + + private static void VerifyAllBlockSizes() + { + (BlockSize size, int dim)[] cases = + { + (BlockSize.ASTC_4x4, 4), + (BlockSize.ASTC_5x5, 5), + (BlockSize.ASTC_6x6, 6), + (BlockSize.ASTC_8x8, 8), + (BlockSize.ASTC_10x10, 10), + (BlockSize.ASTC_12x12, 12), + }; + + Texture2D tex = CreateProceduralTexture(32, 32, TextureFormat.RGBA32, false); + try + { + foreach (var (blockSize, dim) in cases) + { + int expected = ExpectedCompressedBytes(32, 32, 1, dim); + using (AstcRaw raw = tex.CompressTexture(TextureType.Default, blockSize, CompressQuality.Fastest, backend: EncodeBackend.Cpu)) + { + AssertTrue(raw != null, $"CompressTexture returned null for {blockSize}."); + AssertTrue(raw.Length == expected, $"{blockSize}: expected {expected} bytes, got {raw.Length}."); + } + } + } + finally + { + UnityEngine.Object.DestroyImmediate(tex); + } + } + + private static void VerifyEncodeToAstcAndKtxRoundTrip() + { + const int width = 37; + const int height = 23; + Texture2D tex = CreateProceduralTexture(width, height, TextureFormat.RGBA32, false); + try + { + byte[] encoded = tex.EncodeToASTC(TextureType.Default, BlockSize.ASTC_6x6, CompressQuality.Fast, backend: EncodeBackend.Cpu); + int expectedPayload = ExpectedCompressedBytes(width, height, 1, 6); + AssertTrue(encoded != null, "EncodeToASTC returned null."); + AssertTrue(encoded.Length == AstcHeader.HeaderSize + expectedPayload, $"Expected {AstcHeader.HeaderSize + expectedPayload} bytes, got {encoded.Length}."); + + AssertTrue(AstcHeader.TryParse(encoded, 0, out uint bx, out uint by, out uint bz, out int w, out int h, out int d), "AstcHeader.TryParse failed."); + AssertTrue(bx == 6 && by == 6 && bz == 1, $"Invalid header block dims: {bx}x{by}x{bz}."); + AssertTrue(w == width && h == height && d == 1, $"Invalid header image dims: {w}x{h}x{d}."); + + string outPath = Path.Combine(Application.persistentDataPath, "astc_e2e_test.astc"); + File.WriteAllBytes(outPath, encoded); + byte[] readBack = File.ReadAllBytes(outPath); + AssertTrue(readBack.Length == encoded.Length, "Written .astc file length mismatch."); + File.Delete(outPath); + + using (AstcRaw raw = tex.CompressTexture(TextureType.Default, BlockSize.ASTC_6x6, CompressQuality.Fastest, backend: EncodeBackend.Cpu)) + { + byte[] ktx = AstcKtxContainer.Encode(raw, width, height, 1, GraphicsFormat.RGBA_ASTC6X6_UNorm); + AssertTrue(ktx != null && AstcKtxContainer.TryDecode(ktx, out KtxAstcData decoded), "KTX container round-trip failed."); + AssertTrue(decoded.Width == width && decoded.Height == height && decoded.MipCount == 1, "KTX decoded metadata mismatch."); + AssertTrue(decoded.RawAstcPayload.Length == raw.Length, "KTX decoded payload length mismatch."); + } + } + finally + { + UnityEngine.Object.DestroyImmediate(tex); + } + } + + private static void VerifyNonSquareMipmaps() + { + Texture2D tex = CreateProceduralTexture(64, 32, TextureFormat.RGBA32, true); + try + { + AssertTrue(tex.mipmapCount > 1, "Expected mipmapCount > 1 for mipmapped texture."); + int expected = ExpectedCompressedBytes(64, 32, tex.mipmapCount, 6); + using (AstcRaw raw = tex.CompressTexture(TextureType.Default, BlockSize.ASTC_6x6, CompressQuality.Fastest, backend: EncodeBackend.Cpu)) + { + AssertTrue(raw != null, "Mipmapped non-square CompressTexture returned null."); + AssertTrue(raw.Length == expected, $"Mipmapped non-square expected {expected} bytes, got {raw.Length}."); + } + } + finally + { + UnityEngine.Object.DestroyImmediate(tex); + } + } + + private static void VerifyNormalAndMultiFormats() + { + (TextureFormat fmt, TextureType type, bool linear)[] cases = + { + (TextureFormat.RGBA32, TextureType.Normal, true), + (TextureFormat.RGB24, TextureType.Default, false), + (TextureFormat.R8, TextureType.Default, true), + (TextureFormat.RGBAHalf, TextureType.Default, true), + (TextureFormat.RGBAFloat, TextureType.Default, true), + }; + + foreach (var (fmt, type, linear) in cases) + { + Texture2D tex = CreateProceduralTexture(16, 16, fmt, false, linear); + try + { + AssertTrue(Encoder.IsSupport(tex, EncodeBackend.Cpu), $"Expected format {fmt} ({tex.graphicsFormat}) to be supported."); + using (AstcRaw raw = tex.CompressTexture(type, BlockSize.ASTC_4x4, CompressQuality.Fastest, backend: EncodeBackend.Cpu)) + { + int expected = ExpectedCompressedBytes(16, 16, 1, 4); + AssertTrue(raw != null && raw.Length == expected, $"Format {fmt} ({type}) failed compression."); + } + } + finally + { + UnityEngine.Object.DestroyImmediate(tex); + } + } + } + + private static void VerifyInPlaceCompressIfSupported() + { + if (!SystemInfo.SupportsTextureFormat(TextureFormat.ASTC_6x6) || + !SystemInfo.IsFormatSupported(GraphicsFormat.RGBA_ASTC6X6_UNorm, FormatUsage.Sample)) + { + Debug.Log($"{LogPrefix} Skipping GPU in-place Compress check (ASTC GPU sampling unsupported in current graphics mode: {SystemInfo.graphicsDeviceType})."); + return; + } + + Texture2D tex = CreateProceduralTexture(32, 32, TextureFormat.RGBA32, false, linear: true); + try + { + bool ok = tex.Compress(TextureType.Default, BlockSize.ASTC_6x6, CompressQuality.Fastest, makeNoLongerReadable: true, backend: EncodeBackend.Auto); + AssertTrue(ok, "In-place Texture2D.Compress returned false."); + AssertTrue(tex.graphicsFormat == GraphicsFormat.RGBA_ASTC6X6_UNorm, $"Expected RGBA_ASTC6X6_UNorm, got {tex.graphicsFormat}."); + AssertTrue(!tex.isReadable, "Expected texture to be non-readable after makeNoLongerReadable=true."); + } + finally + { + UnityEngine.Object.DestroyImmediate(tex); + } + } +} diff --git a/Assets/Scripts/AstcE2ESmokeRunner.cs.meta b/Assets/Scripts/AstcE2ESmokeRunner.cs.meta new file mode 100644 index 0000000..024b570 --- /dev/null +++ b/Assets/Scripts/AstcE2ESmokeRunner.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7b4e9c1d2a5f483091c6e8a4f2d0b317 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/docs/CI_E2E_GUIDE.md b/docs/CI_E2E_GUIDE.md new file mode 100644 index 0000000..ede4768 --- /dev/null +++ b/docs/CI_E2E_GUIDE.md @@ -0,0 +1,32 @@ +# 多平台 Unity 构建与端到端测试指南(GitHub Actions + GameCI) + +本项目在 `develop` 分支配置了基于 **GitHub Actions + GameCI** 的自动化测试与多平台端到端构建流水线([`.github/workflows/unity-multiplatform-e2e.yml`](../.github/workflows/unity-multiplatform-e2e.yml)),覆盖: + +| 阶段 / 平台 | 运行环境 | 验证内容 | +| :--- | :--- | :--- | +| **Native Plugins & Linux `.so` Build** | `ubuntu-latest` | 校验 Git LFS 全部 6 个预编译原生库(含 Windows AVX2/SSE4.1)完整性、架构与导出符号;从 `ThirdParty/astc-encoder` 子模块编译 Linux x86_64 `libastcenc-sse4.1-shared.so` | +| **Unity Test Runner (EditMode)** | `ubuntu-latest` (GameCI) | 运行 `Assets/ASTC Encoder/Tests/Editor/AstcEncoderTests.cs` 黄金验证套件(ASTC/KTX 容器、磁盘缓存、队列调度、CPU 压缩/解压缩精度) | +| **Linux x64 Standalone** | `ubuntu-latest` | 构建 Linux Player 并以 `-batchmode -nographics -astc-e2e` 实际运行端到端压缩测试 | +| **Windows x64 Standalone** | `ubuntu-latest` 构建 → `windows-latest` 运行 | 跨平台构建 `.exe`,在真实 Windows Runner 上加载 AVX2/SSE4.1 DLL 执行端到端压缩测试 | +| **macOS Universal Standalone** | `ubuntu-latest` 构建 → `macos-latest` 运行 | 跨平台构建 `.app`,在真实 Apple Silicon macOS Runner 上加载通用二进制 `libastcenc-shared.dylib` 执行端到端压缩测试 | +| **Android ARM64 (IL2CPP)** | `ubuntu-latest` 构建 → `macos-latest` 模拟器运行 | 构建 ARM64 `.apk`,校验 `lib/arm64-v8a/libastcenc-neon-shared.so` 打包,并在 ARM64 Android Emulator 上安装启动校验 `adb logcat` 输出 `ASTC_E2E_PASSED` | +| **iOS ARM64 (IL2CPP)** | `ubuntu-latest` 导出 → `macos-latest` Xcode 编译 | 导出 Xcode 工程,并在 macOS Runner 上执行无签名 `xcodebuild -sdk iphoneos build`,验证 IL2CPP 编译与静态库 `libastcenc-neon-static.a` 链接 | + +--- + +## 一、配置 Unity License Secrets + +在 GitHub 仓库 **Settings → Secrets and variables → Actions** 中配置: + +- **Unity Personal(个人免费版)**: + - `UNITY_LICENSE`:`Unity_lic.ulf` 完整 XML 内容(注意新版 Unity Hub 需在 *Preferences → Licenses* 手动点击 *Add → Get a free personal license* 才会写入 `Unity_lic.ulf`,不要填入 `UnityEntitlementLicense.xml`) + - `UNITY_EMAIL`:Unity 账号邮箱 + - `UNITY_PASSWORD`:Unity 账号密码 +- **Unity Pro / Plus(序列号版)**:`UNITY_SERIAL` + `UNITY_EMAIL` + `UNITY_PASSWORD` + +--- + +## 二、触发方式 + +- **自动触发**:向 `develop` 分支推送代码或向 `develop` 提交 Pull Request 时自动运行。 +- **手动触发**:在 **Actions → Unity Multi-Platform Build & E2E Tests → Run workflow** 中触发,可自定义 `unity_version`(默认 `2021.3.6f1`)和 `run_android_emulator`。 diff --git a/scripts/build-linux-astcenc.sh b/scripts/build-linux-astcenc.sh new file mode 100755 index 0000000..25a2fb6 --- /dev/null +++ b/scripts/build-linux-astcenc.sh @@ -0,0 +1,149 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" +SUBMODULE_DIR="${REPO_ROOT}/ThirdParty/astc-encoder" +PLUGIN_ROOT="${1:-${REPO_ROOT}/Assets/ASTC Encoder/Plugins/Linux}" +WORK_DIR="$(mktemp -d)" + +cleanup() { + rm -rf "${WORK_DIR}" +} +trap cleanup EXIT + +if [[ ! -f "${SUBMODULE_DIR}/CMakeLists.txt" ]]; then + echo "[build-linux-astcenc] Initializing ThirdParty/astc-encoder submodule..." + git -C "${REPO_ROOT}" submodule update --init --depth 1 ThirdParty/astc-encoder +fi + +echo "[build-linux-astcenc] Building Linux x86_64 libastcenc-sse4.1-shared.so from ThirdParty/astc-encoder..." +cmake -S "${SUBMODULE_DIR}" -B "${WORK_DIR}/build" \ + -DCMAKE_BUILD_TYPE=Release \ + -DASTCENC_CLI=OFF \ + -DASTCENC_SHAREDLIB=ON \ + -DASTCENC_ISA_SSE41=ON \ + -DASTCENC_WERROR=OFF + +cmake --build "${WORK_DIR}/build" --target astcenc-sse4.1-shared --parallel "$(nproc)" + +BUILT_SO="$(find "${WORK_DIR}/build" -name 'libastcenc-sse4.1-shared.so' | head -n 1)" +if [[ -z "${BUILT_SO}" || ! -f "${BUILT_SO}" ]]; then + echo "[build-linux-astcenc] ERROR: libastcenc-sse4.1-shared.so was not produced." >&2 + exit 1 +fi + +strip --strip-unneeded "${BUILT_SO}" + +TARGET_DIR="${PLUGIN_ROOT}/x86_64" +mkdir -p "${TARGET_DIR}" +cp "${BUILT_SO}" "${TARGET_DIR}/libastcenc-sse4.1-shared.so" + +cat > "${PLUGIN_ROOT}.meta" <<'EOF' +fileFormatVersion: 2 +guid: 8e4a1c9b6d5f4028fb3a95647d1c0e86 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: +EOF + +cat > "${TARGET_DIR}.meta" <<'EOF' +fileFormatVersion: 2 +guid: 9f5b2d0c7e6a4139ac4b06758e2d1f97 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: +EOF + +cat > "${TARGET_DIR}/libastcenc-sse4.1-shared.so.meta" <<'EOF' +fileFormatVersion: 2 +guid: a06c3e1d8f7b4240bd5c17869f3e2a08 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + : Any + second: + enabled: 0 + settings: + Exclude Android: 1 + Exclude Editor: 0 + Exclude Linux64: 0 + Exclude OSXUniversal: 1 + Exclude Win: 1 + Exclude Win64: 1 + Exclude iOS: 1 + - first: + Android: Android + second: + enabled: 0 + settings: + CPU: ARMv7 + - first: + Any: + second: + enabled: 0 + settings: {} + - first: + Editor: Editor + second: + enabled: 1 + settings: + CPU: x86_64 + DefaultValueInitialized: true + OS: Linux + - first: + Standalone: Linux64 + second: + enabled: 1 + settings: + CPU: x86_64 + - first: + Standalone: OSXUniversal + second: + enabled: 0 + settings: + CPU: None + - first: + Standalone: Win + second: + enabled: 0 + settings: + CPU: None + - first: + Standalone: Win64 + second: + enabled: 0 + settings: + CPU: None + - first: + iPhone: iOS + second: + enabled: 0 + settings: + AddToEmbeddedBinaries: false + CPU: AnyCPU + CompileFlags: + FrameworkDependencies: + userData: + assetBundleName: + assetBundleVariant: +EOF + +echo "[build-linux-astcenc] Installed ${TARGET_DIR}/libastcenc-sse4.1-shared.so" +file "${TARGET_DIR}/libastcenc-sse4.1-shared.so" +nm -D --defined-only "${TARGET_DIR}/libastcenc-sse4.1-shared.so" | grep -E " T astcenc_" diff --git a/scripts/verify-native-plugins.sh b/scripts/verify-native-plugins.sh new file mode 100755 index 0000000..cbf40c7 --- /dev/null +++ b/scripts/verify-native-plugins.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" +PLUGINS_DIR="${REPO_ROOT}/Assets/ASTC Encoder/Plugins" + +REQUIRED_SYMBOLS=( + "astcenc_config_init" + "astcenc_context_alloc" + "astcenc_compress_image" + "astcenc_compress_reset" + "astcenc_compress_cancel" + "astcenc_decompress_image" + "astcenc_decompress_reset" + "astcenc_context_free" +) + +check_not_lfs_pointer() { + local path="$1" + if [[ ! -f "${path}" ]]; then + echo "ERROR: Missing native plugin: ${path}" >&2 + exit 1 + fi + if head -n 1 "${path}" | grep -q "^version https://git-lfs.github.com/spec/v1"; then + echo "ERROR: Native plugin is still a Git LFS pointer (run 'git lfs pull'): ${path}" >&2 + exit 1 + fi +} + +check_elf_symbols() { + local path="$1" + local exports + exports="$(nm -D --defined-only "${path}")" + for sym in "${REQUIRED_SYMBOLS[@]}"; do + if ! grep -q " T ${sym}$" <<< "${exports}"; then + echo "ERROR: Missing exported symbol '${sym}' in ${path}" >&2 + exit 1 + fi + done +} + +check_binary_contains_symbols() { + local path="$1" + local dump + dump="$(strings "${path}")" + for sym in "${REQUIRED_SYMBOLS[@]}"; do + if ! grep -q "${sym}" <<< "${dump}"; then + echo "ERROR: Missing expected symbol reference '${sym}' in ${path}" >&2 + exit 1 + fi + done +} + +echo "[verify-native-plugins] Checking prebuilt native plugins..." + +WIN_SSE41="${PLUGINS_DIR}/x64/astcenc-sse4.1-shared.dll" +WIN_AVX2="${PLUGINS_DIR}/x64/astcenc-avx2-shared.dll" +MAC_DYLIB="${PLUGINS_DIR}/Mac/libastcenc-shared.dylib" +ANDROID_ARM64="${PLUGINS_DIR}/Android/arm64-v8a/libastcenc-neon-shared.so" +ANDROID_ARMV7="${PLUGINS_DIR}/Android/armeabi-v7a/libastcenc-none-shared.so" +IOS_STATIC="${PLUGINS_DIR}/iOS/libastcenc-neon-static.a" + +for f in "${WIN_SSE41}" "${WIN_AVX2}" "${MAC_DYLIB}" "${ANDROID_ARM64}" "${ANDROID_ARMV7}" "${IOS_STATIC}"; do + check_not_lfs_pointer "${f}" + file "${f}" +done + +file "${WIN_SSE41}" | grep -q "PE32+.*x86-64" +file "${WIN_AVX2}" | grep -q "PE32+.*x86-64" +file "${MAC_DYLIB}" | grep -q "Mach-O universal binary" +file "${ANDROID_ARM64}" | grep -q "ELF 64-bit.*ARM aarch64" +file "${ANDROID_ARMV7}" | grep -q "ELF 32-bit.*ARM" +file "${IOS_STATIC}" | grep -q "ar archive" + +check_elf_symbols "${ANDROID_ARM64}" +check_elf_symbols "${ANDROID_ARMV7}" +check_binary_contains_symbols "${WIN_SSE41}" +check_binary_contains_symbols "${WIN_AVX2}" +check_binary_contains_symbols "${MAC_DYLIB}" +check_binary_contains_symbols "${IOS_STATIC}" + +if [[ -f "${PLUGINS_DIR}/Linux/x86_64/libastcenc-sse4.1-shared.so" ]]; then + LINUX_SO="${PLUGINS_DIR}/Linux/x86_64/libastcenc-sse4.1-shared.so" + file "${LINUX_SO}" | grep -q "ELF 64-bit.*x86-64" + check_elf_symbols "${LINUX_SO}" +fi + +echo "[verify-native-plugins] All native plugin binaries and required P/Invoke symbols verified." From 040e17a221a14b6115cfc25c95ff0477520bff0a Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Sat, 28 Nov 2026 04:52:12 +0000 Subject: [PATCH 02/12] Fix Unity 2021.3 LTS compilation by guarding Unity 2022.1+ TextureCreationFlags --- Assets/ASTC Encoder/Runtime/AstcCompressionQueue.cs | 4 ++++ Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Assets/ASTC Encoder/Runtime/AstcCompressionQueue.cs b/Assets/ASTC Encoder/Runtime/AstcCompressionQueue.cs index 43accf8..d9527b8 100644 --- a/Assets/ASTC Encoder/Runtime/AstcCompressionQueue.cs +++ b/Assets/ASTC Encoder/Runtime/AstcCompressionQueue.cs @@ -253,7 +253,11 @@ public void ProcessFrame() out int mipCount, out bool willGenMips)) { +#if UNITY_2022_1_OR_NEWER TextureCreationFlags flags = TextureCreationFlags.DontInitializePixels | TextureCreationFlags.DontUploadUponCreate; +#else + TextureCreationFlags flags = TextureCreationFlags.None; +#endif if (mipCount > 1) flags |= TextureCreationFlags.MipChain; diff --git a/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs b/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs index e29730f..1ce2de5 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs +++ b/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs @@ -1046,7 +1046,11 @@ internal static Cubemap CompressCubemap( return null; } +#if UNITY_2022_1_OR_NEWER TextureCreationFlags flags = TextureCreationFlags.DontInitializePixels | TextureCreationFlags.DontUploadUponCreate; +#else + TextureCreationFlags flags = TextureCreationFlags.None; +#endif if (mipCount > 1) flags |= TextureCreationFlags.MipChain; @@ -1099,7 +1103,11 @@ internal static Texture2DArray CompressTexture2DArray( return null; } +#if UNITY_2022_1_OR_NEWER TextureCreationFlags flags = TextureCreationFlags.DontInitializePixels | TextureCreationFlags.DontUploadUponCreate; +#else + TextureCreationFlags flags = TextureCreationFlags.None; +#endif if (mipCount > 1) flags |= TextureCreationFlags.MipChain; @@ -1267,7 +1275,11 @@ internal static Texture2D CompressToTexture( // Fast zero-readback path (including GPU-generated mipmaps) when all mip dimensions align cleanly and GPU natively samples ASTC if (CanUseDirectCopy(source.width, source.height, mipCount, blockSize, astcFormat)) { +#if UNITY_2022_1_OR_NEWER TextureCreationFlags flags = TextureCreationFlags.DontInitializePixels | TextureCreationFlags.DontUploadUponCreate; +#else + TextureCreationFlags flags = TextureCreationFlags.None; +#endif if (mipCount > 1) flags |= TextureCreationFlags.MipChain; From 6dee8c5672331d524a2df50fa83ce478c2b388aa Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Sat, 28 Nov 2026 04:58:19 +0000 Subject: [PATCH 03/12] Fix Editor compilation errors in AstcPerformanceBenchmarkRunner and pin unity-test-runner@v4.3.2 --- .github/workflows/unity-multiplatform-e2e.yml | 2 +- .../ASTC Encoder/Editor/AstcPerformanceBenchmarkRunner.cs | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/unity-multiplatform-e2e.yml b/.github/workflows/unity-multiplatform-e2e.yml index ddb6d93..ee6ef15 100644 --- a/.github/workflows/unity-multiplatform-e2e.yml +++ b/.github/workflows/unity-multiplatform-e2e.yml @@ -107,7 +107,7 @@ jobs: Library-tests- - name: Run Unity EditMode Tests - uses: game-ci/unity-test-runner@v4 + uses: game-ci/unity-test-runner@v4.3.2 env: UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }} UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }} diff --git a/Assets/ASTC Encoder/Editor/AstcPerformanceBenchmarkRunner.cs b/Assets/ASTC Encoder/Editor/AstcPerformanceBenchmarkRunner.cs index 334d93f..339b521 100644 --- a/Assets/ASTC Encoder/Editor/AstcPerformanceBenchmarkRunner.cs +++ b/Assets/ASTC Encoder/Editor/AstcPerformanceBenchmarkRunner.cs @@ -11,6 +11,7 @@ using UnityEngine; using UnityEngine.Experimental.Rendering; using Debug = UnityEngine.Debug; +using Encoder = Aperture.Astcenc.Runtime.Encoder; namespace Aperture.Astcenc.Editor { @@ -603,8 +604,9 @@ public static List RunCpuRawPixelsSuite(int warmup = 1, in using var srcRgba8 = new NativeArray(w * h * 4, Allocator.Persistent); using var dstAstc = new NativeArray(outCapacity, Allocator.Persistent); - for (int i = 0; i < srcRgba8.Length; i++) - srcRgba8[i] = (byte)((i * 37) & 0xFF); + var srcWritable = srcRgba8; + for (int i = 0; i < srcWritable.Length; i++) + srcWritable[i] = (byte)((i * 37) & 0xFF); var opt = AstcCompressOptions.Default; opt.Backend = EncodeBackend.Cpu; From 36889097752ab45f5a8d8c2dd56453f54db0a776 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Sat, 28 Nov 2026 05:03:22 +0000 Subject: [PATCH 04/12] Fix CS0170 definite assignment in AstcE2ESmokeRunner KTX round-trip check --- Assets/Scripts/AstcE2ESmokeRunner.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/AstcE2ESmokeRunner.cs b/Assets/Scripts/AstcE2ESmokeRunner.cs index 08674cc..7396ddf 100644 --- a/Assets/Scripts/AstcE2ESmokeRunner.cs +++ b/Assets/Scripts/AstcE2ESmokeRunner.cs @@ -176,7 +176,8 @@ private static void VerifyEncodeToAstcAndKtxRoundTrip() using (AstcRaw raw = tex.CompressTexture(TextureType.Default, BlockSize.ASTC_6x6, CompressQuality.Fastest, backend: EncodeBackend.Cpu)) { byte[] ktx = AstcKtxContainer.Encode(raw, width, height, 1, GraphicsFormat.RGBA_ASTC6X6_UNorm); - AssertTrue(ktx != null && AstcKtxContainer.TryDecode(ktx, out KtxAstcData decoded), "KTX container round-trip failed."); + AssertTrue(ktx != null, "KTX container encode returned null."); + AssertTrue(AstcKtxContainer.TryDecode(ktx, out KtxAstcData decoded), "KTX container decode failed."); AssertTrue(decoded.Width == width && decoded.Height == height && decoded.MipCount == 1, "KTX decoded metadata mismatch."); AssertTrue(decoded.RawAstcPayload.Length == raw.Length, "KTX decoded payload length mismatch."); } From 017220683598f193150ca5c2ee62e53dc27fb6de Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Sat, 28 Nov 2026 05:37:22 +0000 Subject: [PATCH 05/12] Fix astcenc 5.x parent_context NULL config requirement, pool parent key, and CI test runner flags --- .github/workflows/unity-multiplatform-e2e.yml | 5 +++ Assets/ASTC Encoder/Runtime/AstcContext.cs | 4 +- .../Runtime/Cpu/AstcContextPool.cs | 9 ++-- .../Runtime/NativeAPI/astcenc_api.cs | 45 +++++++++++++++++++ 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/.github/workflows/unity-multiplatform-e2e.yml b/.github/workflows/unity-multiplatform-e2e.yml index ee6ef15..ffb374f 100644 --- a/.github/workflows/unity-multiplatform-e2e.yml +++ b/.github/workflows/unity-multiplatform-e2e.yml @@ -20,6 +20,10 @@ on: default: true type: boolean +permissions: + contents: read + checks: write + concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true @@ -116,6 +120,7 @@ jobs: with: unityVersion: ${{ env.UNITY_VERSION }} testMode: EditMode + customParameters: -nographics artifactsPath: test-results githubToken: ${{ secrets.GITHUB_TOKEN }} diff --git a/Assets/ASTC Encoder/Runtime/AstcContext.cs b/Assets/ASTC Encoder/Runtime/AstcContext.cs index da190c0..e013358 100644 --- a/Assets/ASTC Encoder/Runtime/AstcContext.cs +++ b/Assets/ASTC Encoder/Runtime/AstcContext.cs @@ -161,7 +161,9 @@ public static AstcContext Create( } IntPtr context = IntPtr.Zero; - status = astcenc_api.astcenc_context_alloc(ref config, (uint)resolvedThreads, ref context, parentPtr); + status = parentPtr != IntPtr.Zero + ? astcenc_api.astcenc_context_alloc(IntPtr.Zero, (uint)resolvedThreads, ref context, parentPtr) + : astcenc_api.astcenc_context_alloc(ref config, (uint)resolvedThreads, ref context, IntPtr.Zero); if (status != astcenc_error.ASTCENC_SUCCESS) { Debug.LogError("Codec context alloc failed:" + status); diff --git a/Assets/ASTC Encoder/Runtime/Cpu/AstcContextPool.cs b/Assets/ASTC Encoder/Runtime/Cpu/AstcContextPool.cs index 9e181af..1d6bd6e 100644 --- a/Assets/ASTC Encoder/Runtime/Cpu/AstcContextPool.cs +++ b/Assets/ASTC Encoder/Runtime/Cpu/AstcContextPool.cs @@ -165,7 +165,7 @@ public void Dispose() private static readonly object s_Lock = new object(); private static readonly Dictionary> s_Compressors = new Dictionary>(); private static readonly Dictionary> s_Decompressors = new Dictionary>(); - private static readonly Dictionary s_Parents = new Dictionary(); + private static readonly Dictionary s_Parents = new Dictionary(); private static readonly List s_RetiredParents = new List(); private static int s_Generation; private static int s_InFlightCompressors; @@ -190,6 +190,7 @@ internal static CompressorLease RentCompressor( bool useUnityJobSystem = false) { var key = new CompressorKey(format, textureType, blockSize, quality, threadCount, useAlphaWeight, usePerceptual, alphaScaleRadius); + var parentKey = new CompressorKey(format, textureType, blockSize, quality, 1, useAlphaWeight, usePerceptual, alphaScaleRadius); lock (s_Lock) { int gen = s_Generation; @@ -201,11 +202,11 @@ internal static CompressorLease RentCompressor( return new CompressorLease(key, pooled, gen); } - if (!s_Parents.TryGetValue(blockSize, out AstcContext parent)) + if (!s_Parents.TryGetValue(parentKey, out AstcContext parent)) { - parent = AstcContext.Create(format, textureType, blockSize, quality, threadCount, useAlphaWeight, usePerceptual, alphaScaleRadius); + parent = AstcContext.Create(format, textureType, blockSize, quality, 1, useAlphaWeight, usePerceptual, alphaScaleRadius); if (parent != null) - s_Parents[blockSize] = parent; + s_Parents[parentKey] = parent; } AstcContext created = AstcContext.Create( diff --git a/Assets/ASTC Encoder/Runtime/NativeAPI/astcenc_api.cs b/Assets/ASTC Encoder/Runtime/NativeAPI/astcenc_api.cs index fc2269d..71e05ad 100644 --- a/Assets/ASTC Encoder/Runtime/NativeAPI/astcenc_api.cs +++ b/Assets/ASTC Encoder/Runtime/NativeAPI/astcenc_api.cs @@ -37,6 +37,13 @@ private static extern astcenc_error astcenc_context_alloc_neon( ref IntPtr context, IntPtr parent_context); + [DllImport(NeonLibraryName, EntryPoint = "astcenc_context_alloc", CallingConvention = CallingConvention.Cdecl)] + private static extern astcenc_error astcenc_context_alloc_parent_neon( + IntPtr config, + uint thread_count, + ref IntPtr context, + IntPtr parent_context); + [DllImport(NoneLibraryName, EntryPoint = "astcenc_context_alloc", CallingConvention = CallingConvention.Cdecl)] private static extern astcenc_error astcenc_context_alloc_none( ref astcenc_config config, @@ -44,6 +51,13 @@ private static extern astcenc_error astcenc_context_alloc_none( ref IntPtr context, IntPtr parent_context); + [DllImport(NoneLibraryName, EntryPoint = "astcenc_context_alloc", CallingConvention = CallingConvention.Cdecl)] + private static extern astcenc_error astcenc_context_alloc_parent_none( + IntPtr config, + uint thread_count, + ref IntPtr context, + IntPtr parent_context); + [DllImport(NeonLibraryName, EntryPoint = "astcenc_compress_image", CallingConvention = CallingConvention.Cdecl)] private static extern astcenc_error astcenc_compress_image_neon( IntPtr context, @@ -175,6 +189,17 @@ public static astcenc_error astcenc_context_alloc( : astcenc_context_alloc_none(ref config, thread_count, ref context, parent_context); } + public static astcenc_error astcenc_context_alloc( + IntPtr config, + uint thread_count, + ref IntPtr context, + IntPtr parent_context) + { + return IntPtr.Size == 8 + ? astcenc_context_alloc_parent_neon(config, thread_count, ref context, parent_context) + : astcenc_context_alloc_parent_none(config, thread_count, ref context, parent_context); + } + /** * @brief Compress an image. * @@ -329,8 +354,12 @@ private static bool ProbeAvx2Support() [DllImport(Avx2LibraryName, EntryPoint = "astcenc_context_alloc", CallingConvention = CallingConvention.Cdecl)] private static extern astcenc_error astcenc_context_alloc_avx2(ref astcenc_config config, uint thread_count, ref IntPtr context, IntPtr parent_context); + [DllImport(Avx2LibraryName, EntryPoint = "astcenc_context_alloc", CallingConvention = CallingConvention.Cdecl)] + private static extern astcenc_error astcenc_context_alloc_parent_avx2(IntPtr config, uint thread_count, ref IntPtr context, IntPtr parent_context); [DllImport(Sse41LibraryName, EntryPoint = "astcenc_context_alloc", CallingConvention = CallingConvention.Cdecl)] private static extern astcenc_error astcenc_context_alloc_sse41(ref astcenc_config config, uint thread_count, ref IntPtr context, IntPtr parent_context); + [DllImport(Sse41LibraryName, EntryPoint = "astcenc_context_alloc", CallingConvention = CallingConvention.Cdecl)] + private static extern astcenc_error astcenc_context_alloc_parent_sse41(IntPtr config, uint thread_count, ref IntPtr context, IntPtr parent_context); [DllImport(Avx2LibraryName, EntryPoint = "astcenc_compress_image", CallingConvention = CallingConvention.Cdecl)] private static extern astcenc_error astcenc_compress_image_avx2(IntPtr context, ref astcenc_image image, ref astcenc_swizzle swizzle, IntPtr data_out, UIntPtr data_len, uint thread_index); @@ -368,6 +397,9 @@ public static astcenc_error astcenc_config_init(astcenc_profile profile, uint bl public static astcenc_error astcenc_context_alloc(ref astcenc_config config, uint thread_count, ref IntPtr context, IntPtr parent_context) => s_UseAvx2 ? astcenc_context_alloc_avx2(ref config, thread_count, ref context, parent_context) : astcenc_context_alloc_sse41(ref config, thread_count, ref context, parent_context); + public static astcenc_error astcenc_context_alloc(IntPtr config, uint thread_count, ref IntPtr context, IntPtr parent_context) + => s_UseAvx2 ? astcenc_context_alloc_parent_avx2(config, thread_count, ref context, parent_context) : astcenc_context_alloc_parent_sse41(config, thread_count, ref context, parent_context); + public static astcenc_error astcenc_compress_image(IntPtr context, ref astcenc_image image, ref astcenc_swizzle swizzle, IntPtr data_out, UIntPtr data_len, uint thread_index) => s_UseAvx2 ? astcenc_compress_image_avx2(context, ref image, ref swizzle, data_out, data_len, thread_index) : astcenc_compress_image_sse41(context, ref image, ref swizzle, data_out, data_len, thread_index); @@ -457,6 +489,13 @@ public static extern astcenc_error astcenc_context_alloc( ref IntPtr context, IntPtr parent_context); + [DllImport(LibraryName, EntryPoint = "astcenc_context_alloc", CallingConvention = CallingConvention.Cdecl)] + public static extern astcenc_error astcenc_context_alloc( + IntPtr config, + uint thread_count, + ref IntPtr context, + IntPtr parent_context); + /** * @brief Compress an image. * @@ -569,6 +608,12 @@ public static astcenc_error astcenc_context_alloc( ref IntPtr context, IntPtr parent_context) => astcenc_error.ASTCENC_ERR_NOT_IMPLEMENTED; + public static astcenc_error astcenc_context_alloc( + IntPtr config, + uint thread_count, + ref IntPtr context, + IntPtr parent_context) => astcenc_error.ASTCENC_ERR_NOT_IMPLEMENTED; + public static astcenc_error astcenc_compress_image( IntPtr context, ref astcenc_image image, From 84adfe9959d8a2140ea17c9626ecb0573f48d492 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Sat, 28 Nov 2026 05:39:47 +0000 Subject: [PATCH 06/12] Add [loop] attributes in AstcGpuCore/AstcGpuBise HLSL to prevent UnityShaderCompiler unroll timeouts --- .../Runtime/Gpu/Shaders/AstcGpuBise.hlsl | 12 +- .../Runtime/Gpu/Shaders/AstcGpuCore.hlsl | 150 +++++++++--------- 2 files changed, 81 insertions(+), 81 deletions(-) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl index fc60c57..d8f4a92 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl @@ -406,7 +406,7 @@ void AstcEncodeBise64(inout uint4 dst, uint startBit, uint q, uint count, uint v if (t == 0u && qu == 0u) { - for (uint i = 0u; i < count; ++i) + [loop] for (uint i = 0u; i < count; ++i) { AstcWriteBits128(dst, bitPos, b, vals[i]); bitPos += b; @@ -415,7 +415,7 @@ void AstcEncodeBise64(inout uint4 dst, uint startBit, uint q, uint count, uint v else if (t != 0u) { uint lowMask = (b > 0u) ? ((1u << b) - 1u) : 0u; - for (uint i = 0u; i < count; i += 5u) + [loop] for (uint i = 0u; i < count; i += 5u) { uint v0 = vals[i]; uint v1 = (i + 1u < count) ? vals[i + 1u] : 0u; @@ -453,7 +453,7 @@ void AstcEncodeBise64(inout uint4 dst, uint startBit, uint q, uint count, uint v else { uint lowMask = (b > 0u) ? ((1u << b) - 1u) : 0u; - for (uint i = 0u; i < count; i += 3u) + [loop] for (uint i = 0u; i < count; i += 3u) { uint v0 = vals[i]; uint v1 = (i + 1u < count) ? vals[i + 1u] : 0u; @@ -487,7 +487,7 @@ void AstcEncodeBise16(inout uint4 dst, uint startBit, uint q, uint count, uint v if (t == 0u && qu == 0u) { - for (uint i = 0u; i < count; ++i) + [loop] for (uint i = 0u; i < count; ++i) { AstcWriteBits128(dst, bitPos, b, vals[i]); bitPos += b; @@ -496,7 +496,7 @@ void AstcEncodeBise16(inout uint4 dst, uint startBit, uint q, uint count, uint v else if (t != 0u) { uint lowMask = (b > 0u) ? ((1u << b) - 1u) : 0u; - for (uint i = 0u; i < count; i += 5u) + [loop] for (uint i = 0u; i < count; i += 5u) { uint v0 = vals[i]; uint v1 = (i + 1u < count) ? vals[i + 1u] : 0u; @@ -534,7 +534,7 @@ void AstcEncodeBise16(inout uint4 dst, uint startBit, uint q, uint count, uint v else { uint lowMask = (b > 0u) ? ((1u << b) - 1u) : 0u; - for (uint i = 0u; i < count; i += 3u) + [loop] for (uint i = 0u; i < count; i += 3u) { uint v0 = vals[i]; uint v1 = (i + 1u < count) ? vals[i + 1u] : 0u; diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl index a6f281f..df583ee 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl @@ -365,9 +365,9 @@ void AstcFitSubsetEndpoints( float4 minC = float4(1e9, 1e9, 1e9, 1e9); float4 maxC = float4(-1e9, -1e9, -1e9, -1e9); - for (uint y = 0u; y < BLOCK_Y; ++y) + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { - for (uint x = 0u; x < BLOCK_X; ++x) + [loop] for (uint x = 0u; x < BLOCK_X; ++x) { if (partFilter < 2u && AstcSelectPartition2(partSeed, x, y, smallBlock) != partFilter) continue; @@ -401,9 +401,9 @@ void AstcFitSubsetEndpoints( // Pass 2: MaxAccum principal axis (ARM astcenc style) float4 acc0 = 0.0, acc1 = 0.0, acc2 = 0.0, acc3 = 0.0; - for (uint y2 = 0u; y2 < BLOCK_Y; ++y2) + [loop] for (uint y2 = 0u; y2 < BLOCK_Y; ++y2) { - for (uint x2 = 0u; x2 < BLOCK_X; ++x2) + [loop] for (uint x2 = 0u; x2 < BLOCK_X; ++x2) { if (partFilter < 2u && AstcSelectPartition2(partSeed, x2, y2, smallBlock) != partFilter) continue; @@ -442,9 +442,9 @@ void AstcFitSubsetEndpoints( // Pass 3: Project onto principal axis for initial [minT, maxT] endpoints float minT = 1e9; float maxT = -1e9; - for (uint y3 = 0u; y3 < BLOCK_Y; ++y3) + [loop] for (uint y3 = 0u; y3 < BLOCK_Y; ++y3) { - for (uint x3 = 0u; x3 < BLOCK_X; ++x3) + [loop] for (uint x3 = 0u; x3 < BLOCK_X; ++x3) { if (partFilter < 2u && AstcSelectPartition2(partSeed, x3, y3, smallBlock) != partFilter) continue; @@ -462,7 +462,7 @@ void AstcFitSubsetEndpoints( #if !defined(ASTC_FAST_ONLY) // Optional least-squares endpoint refinement with separate RGB (imp) and Alpha (1.0) normal equations - for (uint iter = 0u; iter < refineIters; ++iter) + [loop] for (uint iter = 0u; iter < refineIters; ++iter) { float4 dir = (e1 - e0) * mask; if (dot(dir, dir) <= 1e-6) break; @@ -471,9 +471,9 @@ void AstcFitSubsetEndpoints( float a00_a = 0.0, a01_a = 0.0, a11_a = 0.0; float4 b0 = 0.0, b1 = 0.0; - for (uint yr = 0u; yr < BLOCK_Y; ++yr) + [loop] for (uint yr = 0u; yr < BLOCK_Y; ++yr) { - for (uint xr = 0u; xr < BLOCK_X; ++xr) + [loop] for (uint xr = 0u; xr < BLOCK_X; ++xr) { if (partFilter < 2u && AstcSelectPartition2(partSeed, xr, yr, smallBlock) != partFilter) continue; @@ -544,7 +544,7 @@ void AstcComputeWeightGrid1P( uint gridCount = gridW * gridH; float num[ASTC_MAX_GRID]; float den[ASTC_MAX_GRID]; - for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) + [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) { gridW01[i] = 0.0; num[i] = 0.0; @@ -556,9 +556,9 @@ void AstcComputeWeightGrid1P( bool exactGrid = (gridW == BLOCK_X && gridH == BLOCK_Y); bool alphaWeightsRgbOnly = (mask.a == 0.0 && effMode == ASTC_EFF_LDR_RGBA && _AstcUseAlphaWeight != 0u); - for (uint y = 0u; y < BLOCK_Y; ++y) + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { - for (uint x = 0u; x < BLOCK_X; ++x) + [loop] for (uint x = 0u; x < BLOCK_X; ++x) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); @@ -593,7 +593,7 @@ void AstcComputeWeightGrid1P( if (!exactGrid) { - for (uint k = 0u; k < gridCount; ++k) + [loop] for (uint k = 0u; k < gridCount; ++k) { gridW01[k] = (den[k] > 1e-6) ? saturate(num[k] / den[k]) : 0.0; } @@ -612,7 +612,7 @@ void AstcComputePlane1WeightGrid( uint gridCount = gridW * gridH; float num[ASTC_MAX_GRID]; float den[ASTC_MAX_GRID]; - for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) + [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) { gridW01[i] = 0.0; num[i] = 0.0; @@ -624,9 +624,9 @@ void AstcComputePlane1WeightGrid( bool exactGrid = (gridW == BLOCK_X && gridH == BLOCK_Y); bool weightByAlpha = (plane1Channel < 3u && effMode == ASTC_EFF_LDR_RGBA && _AstcUseAlphaWeight != 0u); - for (uint y = 0u; y < BLOCK_Y; ++y) + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { - for (uint x = 0u; x < BLOCK_X; ++x) + [loop] for (uint x = 0u; x < BLOCK_X; ++x) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); @@ -659,7 +659,7 @@ void AstcComputePlane1WeightGrid( if (!exactGrid) { - for (uint k = 0u; k < gridCount; ++k) + [loop] for (uint k = 0u; k < gridCount; ++k) { gridW01[k] = (den[k] > 1e-6) ? saturate(num[k] / den[k]) : 0.0; } @@ -681,7 +681,7 @@ void AstcComputeWeightGrid2P( uint gridCount = gridW * gridH; float num[ASTC_MAX_GRID]; float den[ASTC_MAX_GRID]; - for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) + [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) { gridW01[i] = 0.0; num[i] = 0.0; @@ -696,9 +696,9 @@ void AstcComputeWeightGrid2P( bool smallBlock = (BLOCK_X * BLOCK_Y) < 32u; AstcPart2Params p2 = AstcPreparePartition2(partSeed, smallBlock); - for (uint y = 0u; y < BLOCK_Y; ++y) + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { - for (uint x = 0u; x < BLOCK_X; ++x) + [loop] for (uint x = 0u; x < BLOCK_X; ++x) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); @@ -743,7 +743,7 @@ void AstcComputeWeightGrid2P( if (!exactGrid) { - for (uint k = 0u; k < gridCount; ++k) + [loop] for (uint k = 0u; k < gridCount; ++k) { gridW01[k] = (den[k] > 1e-6) ? saturate(num[k] / den[k]) : 0.0; } @@ -762,14 +762,14 @@ float AstcEvalError1PWithGrid( out float4 outChanErr) { uint epCodes[16]; - for (uint e = 0u; e < 16u; ++e) epCodes[e] = 0u; + [loop] for (uint e = 0u; e < 16u; ++e) epCodes[e] = 0u; float4 uqE0, uqE1; AstcQuantizeEndpoints(e0, e1, effMode, epQuant, epCodes, 0u, uqE0, uqE1); uint gridUq[ASTC_MAX_GRID]; uint gridCount = gridW * gridH; - for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) gridUq[i] = 0u; - for (uint k = 0u; k < gridCount; ++k) + [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) gridUq[i] = 0u; + [loop] for (uint k = 0u; k < gridCount; ++k) { uint uqW; AstcQuantizeWeight(gridW01[k], wQuant, uqW); @@ -779,9 +779,9 @@ float AstcEvalError1PWithGrid( float4 mask = AstcChannelMaskEx(effMode, 4u); float totalErr = 0.0; outChanErr = 0.0; - for (uint y = 0u; y < BLOCK_Y; ++y) + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { - for (uint x = 0u; x < BLOCK_X; ++x) + [loop] for (uint x = 0u; x < BLOCK_X; ++x) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); @@ -813,15 +813,15 @@ float AstcEvalErrorDualPlaneWithGrid( float bestError) { uint epCodes[16]; - for (uint e = 0u; e < 16u; ++e) epCodes[e] = 0u; + [loop] for (uint e = 0u; e < 16u; ++e) epCodes[e] = 0u; float4 uqE0, uqE1; AstcQuantizeEndpoints(e0, e1, effMode, epQuant, epCodes, 0u, uqE0, uqE1); uint grid0Uq[ASTC_MAX_GRID]; uint grid1Uq[ASTC_MAX_GRID]; uint gridCount = gridW * gridH; - for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) { grid0Uq[i] = 0u; grid1Uq[i] = 0u; } - for (uint k = 0u; k < gridCount; ++k) + [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) { grid0Uq[i] = 0u; grid1Uq[i] = 0u; } + [loop] for (uint k = 0u; k < gridCount; ++k) { uint uq0, uq1; AstcQuantizeWeight(grid0W01[k], wQuant, uq0); @@ -832,9 +832,9 @@ float AstcEvalErrorDualPlaneWithGrid( float4 mask = AstcChannelMaskEx(effMode, 4u); float totalErr = 0.0; - for (uint y = 0u; y < BLOCK_Y; ++y) + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { - for (uint x = 0u; x < BLOCK_X; ++x) + [loop] for (uint x = 0u; x < BLOCK_X; ++x) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); @@ -886,7 +886,7 @@ float AstcEvalError2PWithGrid( float bestError) { uint epCodes[16]; - for (uint e = 0u; e < 16u; ++e) epCodes[e] = 0u; + [loop] for (uint e = 0u; e < 16u; ++e) epCodes[e] = 0u; uint epPerPart = AstcGetEpCountForEffMode(effMode); float4 uq0_p0, uq1_p0, uq0_p1, uq1_p1; AstcQuantizeEndpoints(e0_p0, e1_p0, effMode, epQuant, epCodes, 0u, uq0_p0, uq1_p0); @@ -894,8 +894,8 @@ float AstcEvalError2PWithGrid( uint gridUq[ASTC_MAX_GRID]; uint gridCount = gridW * gridH; - for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) gridUq[i] = 0u; - for (uint k = 0u; k < gridCount; ++k) + [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) gridUq[i] = 0u; + [loop] for (uint k = 0u; k < gridCount; ++k) { uint uqW; AstcQuantizeWeight(gridW01[k], wQuant, uqW); @@ -904,9 +904,9 @@ float AstcEvalError2PWithGrid( float4 mask = AstcChannelMaskEx(effMode, 4u); float totalErr = 0.0; - for (uint y = 0u; y < BLOCK_Y; ++y) + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { - for (uint x = 0u; x < BLOCK_X; ++x) + [loop] for (uint x = 0u; x < BLOCK_X; ++x) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); @@ -945,9 +945,9 @@ void AstcFitBothPartitions2P( float4 wSum0 = 0.0, wSum1 = 0.0; // Pass 1: means for both partitions - for (uint y = 0u; y < BLOCK_Y; ++y) + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { - for (uint x = 0u; x < BLOCK_X; ++x) + [loop] for (uint x = 0u; x < BLOCK_X; ++x) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); @@ -971,9 +971,9 @@ void AstcFitBothPartitions2P( // Pass 2: covariance + principal axis via power iteration seed for both partitions float4 cov0R = 0.0, cov0G = 0.0, cov0B = 0.0, cov0A = 0.0; float4 cov1R = 0.0, cov1G = 0.0, cov1B = 0.0, cov1A = 0.0; - for (uint y2 = 0u; y2 < BLOCK_Y; ++y2) + [loop] for (uint y2 = 0u; y2 < BLOCK_Y; ++y2) { - for (uint x2 = 0u; x2 < BLOCK_X; ++x2) + [loop] for (uint x2 = 0u; x2 < BLOCK_X; ++x2) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x2, y2, effMode, imp); @@ -995,7 +995,7 @@ void AstcFitBothPartitions2P( float4 axis0 = float4(cov0R.r, cov0G.g, cov0B.b, cov0A.a); float4 axis1 = float4(cov1R.r, cov1G.g, cov1B.b, cov1A.a); - for (uint it = 0u; it < 3u; ++it) + [loop] for (uint it = 0u; it < 3u; ++it) { float4 n0 = float4(dot(cov0R, axis0), dot(cov0G, axis0), dot(cov0B, axis0), dot(cov0A, axis0)) * mask; float l0 = length(n0); @@ -1013,9 +1013,9 @@ void AstcFitBothPartitions2P( // Pass 3: min/max projection along each partition's principal axis float minT0 = 1e30, maxT0 = -1e30; float minT1 = 1e30, maxT1 = -1e30; - for (uint y3 = 0u; y3 < BLOCK_Y; ++y3) + [loop] for (uint y3 = 0u; y3 < BLOCK_Y; ++y3) { - for (uint x3 = 0u; x3 < BLOCK_X; ++x3) + [loop] for (uint x3 = 0u; x3 < BLOCK_X; ++x3) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x3, y3, effMode, imp); @@ -1061,9 +1061,9 @@ void AstcRefitEndpoints1P( float a00_a = 0.0, a01_a = 0.0, a11_a = 0.0; float4 b0 = 0.0, b1 = 0.0; - for (uint y = 0u; y < BLOCK_Y; ++y) + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { - for (uint x = 0u; x < BLOCK_X; ++x) + [loop] for (uint x = 0u; x < BLOCK_X; ++x) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); @@ -1116,14 +1116,14 @@ void AstcRefitEndpoints1P( if (!changed) return; uint candCodes[16]; - for (uint e = 0u; e < 16u; ++e) candCodes[e] = 0u; + [loop] for (uint e = 0u; e < 16u; ++e) candCodes[e] = 0u; float4 uq0, uq1; AstcQuantizeEndpoints(refitE0, refitE1, effMode, epQuant, candCodes, 0u, uq0, uq1); float refitErr = 0.0; - for (uint y2 = 0u; y2 < BLOCK_Y; ++y2) + [loop] for (uint y2 = 0u; y2 < BLOCK_Y; ++y2) { - for (uint x2 = 0u; x2 < BLOCK_X; ++x2) + [loop] for (uint x2 = 0u; x2 < BLOCK_X; ++x2) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x2, y2, effMode, imp); @@ -1138,7 +1138,7 @@ void AstcRefitEndpoints1P( if (refitErr < origErr) { - for (uint ei = 0u; ei < 8u; ++ei) epCodes[ei] = candCodes[ei]; + [loop] for (uint ei = 0u; ei < 8u; ++ei) epCodes[ei] = candCodes[ei]; } } @@ -1163,9 +1163,9 @@ void AstcRefitEndpointsDualPlane( float4 b0_p0 = 0.0, b1_p0 = 0.0; float b0_p1 = 0.0, b1_p1 = 0.0; - for (uint y = 0u; y < BLOCK_Y; ++y) + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { - for (uint x = 0u; x < BLOCK_X; ++x) + [loop] for (uint x = 0u; x < BLOCK_X; ++x) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); @@ -1254,14 +1254,14 @@ void AstcRefitEndpointsDualPlane( if (!changed) return; uint candCodes[16]; - for (uint e = 0u; e < 16u; ++e) candCodes[e] = 0u; + [loop] for (uint e = 0u; e < 16u; ++e) candCodes[e] = 0u; float4 uq0, uq1; AstcQuantizeEndpoints(refitE0, refitE1, effMode, epQuant, candCodes, 0u, uq0, uq1); float refitErr = 0.0; - for (uint y2 = 0u; y2 < BLOCK_Y; ++y2) + [loop] for (uint y2 = 0u; y2 < BLOCK_Y; ++y2) { - for (uint x2 = 0u; x2 < BLOCK_X; ++x2) + [loop] for (uint x2 = 0u; x2 < BLOCK_X; ++x2) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x2, y2, effMode, imp); @@ -1290,7 +1290,7 @@ void AstcRefitEndpointsDualPlane( if (refitErr < origErr) { - for (uint ei = 0u; ei < 8u; ++ei) epCodes[ei] = candCodes[ei]; + [loop] for (uint ei = 0u; ei < 8u; ++ei) epCodes[ei] = candCodes[ei]; } } @@ -1300,12 +1300,12 @@ void AstcRefitEndpointsDualPlane( uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) { uint epCodes[16]; - for (uint e = 0u; e < 16u; ++e) epCodes[e] = 0u; + [loop] for (uint e = 0u; e < 16u; ++e) epCodes[e] = 0u; uint epPerPart = AstcGetEpCountForEffMode(effMode); uint cem = AstcGetCemForEffMode(effMode); uint wCodes[64]; - for (uint wi = 0u; wi < 64u; ++wi) wCodes[wi] = 0u; + [loop] for (uint wi = 0u; wi < 64u; ++wi) wCodes[wi] = 0u; uint gridCount = c.gridW * c.gridH; uint totalWeights = gridCount; @@ -1319,7 +1319,7 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) float gridW01[ASTC_MAX_GRID]; AstcComputeWeightGrid2P(baseX, baseY, effMode, c.partSeed, uq0_p0, uq1_p0, uq0_p1, uq1_p1, c.gridW, c.gridH, gridW01); - for (uint k = 0u; k < gridCount; ++k) + [loop] for (uint k = 0u; k < gridCount; ++k) { uint dummyUq; wCodes[k] = AstcQuantizeWeight(gridW01[k], c.wQuant, dummyUq); @@ -1340,9 +1340,9 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) #if !defined(ASTC_FAST_ONLY) uint grid0Uq[ASTC_MAX_GRID]; uint grid1Uq[ASTC_MAX_GRID]; - for (uint zi = 0u; zi < ASTC_MAX_GRID_U; ++zi) { grid0Uq[zi] = 0u; grid1Uq[zi] = 0u; } + [loop] for (uint zi = 0u; zi < ASTC_MAX_GRID_U; ++zi) { grid0Uq[zi] = 0u; grid1Uq[zi] = 0u; } #endif - for (uint k = 0u; k < gridCount; ++k) + [loop] for (uint k = 0u; k < gridCount; ++k) { uint uq0, uq1; wCodes[2u * k] = AstcQuantizeWeight(grid0W01[k], c.wQuant, uq0); @@ -1371,9 +1371,9 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) #if !defined(ASTC_FAST_ONLY) uint gridUq[ASTC_MAX_GRID]; - for (uint zi = 0u; zi < ASTC_MAX_GRID_U; ++zi) gridUq[zi] = 0u; + [loop] for (uint zi = 0u; zi < ASTC_MAX_GRID_U; ++zi) gridUq[zi] = 0u; #endif - for (uint k = 0u; k < gridCount; ++k) + [loop] for (uint k = 0u; k < gridCount; ++k) { uint uqW; wCodes[k] = AstcQuantizeWeight(gridW01[k], c.wQuant, uqW); @@ -1543,7 +1543,7 @@ uint AstcSelectBestSeed2P(uint baseX, uint baseY, uint effMode) uint bestSeed = 17u; float bestScore = -1.0; - for (uint batch = 0u; batch < 16u; batch += 4u) + [loop] for (uint batch = 0u; batch < 16u; batch += 4u) { uint seed0 = AstcGetCuratedSeed(batch + 0u); uint seed1 = AstcGetCuratedSeed(batch + 1u); @@ -1561,9 +1561,9 @@ uint AstcSelectBestSeed2P(uint baseX, uint baseY, uint effMode) float4 s1_0 = 0.0, s1_1 = 0.0, s1_2 = 0.0, s1_3 = 0.0; float4 c1_0 = 0.0, c1_1 = 0.0, c1_2 = 0.0, c1_3 = 0.0; - for (uint y = 0u; y < BLOCK_Y; ++y) + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { - for (uint x = 0u; x < BLOCK_X; ++x) + [loop] for (uint x = 0u; x < BLOCK_X; ++x) { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); @@ -1636,9 +1636,9 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) float4 rawMax = float4(-1e30, -1e30, -1e30, -1e30); float4 rawFirst = 0.0; - for (uint y = 0u; y < BLOCK_Y; ++y) + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { - for (uint x = 0u; x < BLOCK_X; ++x) + [loop] for (uint x = 0u; x < BLOCK_X; ++x) { uint px = min(baseX + x, _AstcSrcWidth - 1u); uint py = min(baseY + y, _AstcSrcHeight - 1u); @@ -1831,8 +1831,8 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) uint normCachedG = 0u; float normGrid0[ASTC_MAX_GRID]; float normGrid1[ASTC_MAX_GRID]; - for (uint zn = 0u; zn < ASTC_MAX_GRID_U; ++zn) { normGrid0[zn] = 0.0; normGrid1[zn] = 0.0; } - for (uint ni = 0u; ni < normCandCount; ++ni) + [loop] for (uint zn = 0u; zn < ASTC_MAX_GRID_U; ++zn) { normGrid0[zn] = 0.0; normGrid1[zn] = 0.0; } + [loop] for (uint ni = 0u; ni < normCandCount; ++ni) { uint4 cfg = (ni == 0u) ? uint4(0x452u, 4u, ASTC_QUANT_5, ASTC_QUANT_256) : (ni == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_256) @@ -1939,10 +1939,10 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) uint cachedGW = 0u, cachedGH = 0u; float cachedGrid1P[ASTC_MAX_GRID]; - for (uint z1 = 0u; z1 < ASTC_MAX_GRID_U; ++z1) cachedGrid1P[z1] = 0.0; + [loop] for (uint z1 = 0u; z1 < ASTC_MAX_GRID_U; ++z1) cachedGrid1P[z1] = 0.0; float4 bestChanErr = 0.0; - for (uint c1 = 0u; c1 < cand1PCount; ++c1) + [loop] for (uint c1 = 0u; c1 < cand1PCount; ++c1) { uint bm, gw, gh, wQ, epQ; if (!hasAlpha) @@ -1998,8 +1998,8 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) uint dpCachedG = 0u; float dpGrid0[ASTC_MAX_GRID]; float dpGrid1[ASTC_MAX_GRID]; - for (uint zd = 0u; zd < ASTC_MAX_GRID_U; ++zd) { dpGrid0[zd] = 0.0; dpGrid1[zd] = 0.0; } - for (uint di = 0u; di < dpCount; ++di) + [loop] for (uint zd = 0u; zd < ASTC_MAX_GRID_U; ++zd) { dpGrid0[zd] = 0.0; dpGrid1[zd] = 0.0; } + [loop] for (uint di = 0u; di < dpCount; ++di) { uint4 cfg; if (!hasAlpha) @@ -2042,8 +2042,8 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) #endif uint p2CachedG = 0u; float p2Grid[ASTC_MAX_GRID]; - for (uint z2 = 0u; z2 < ASTC_MAX_GRID_U; ++z2) p2Grid[z2] = 0.0; - for (uint p2 = 0u; p2 < p2Count; ++p2) + [loop] for (uint z2 = 0u; z2 < ASTC_MAX_GRID_U; ++z2) p2Grid[z2] = 0.0; + [loop] for (uint p2 = 0u; p2 < p2Count; ++p2) { uint4 cfg; if (!hasAlpha) From 1fe355eca27b18ff9cb6ac6eb59264d1020a107b Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Sat, 28 Nov 2026 06:00:10 +0000 Subject: [PATCH 07/12] Add #pragma use_dxc to AstcGpuEncoder.compute to avoid FXC nested-loop compiler timeout --- Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute | 1 + 1 file changed, 1 insertion(+) diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute index 832c7b4..c2ebff4 100644 --- a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute @@ -1,4 +1,5 @@ #pragma editor_sync_compilation +#pragma use_dxc #pragma kernel EncodeTex4x4 ASTC_BLOCK_4X4 #pragma kernel EncodeTex5x5 ASTC_BLOCK_5X5 From ba072b5b1b36ed9ce0ec6b14f46f205cfc1dfd0a Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Sat, 28 Nov 2026 06:19:15 +0000 Subject: [PATCH 08/12] fix(gpu-shader): eliminate HLSL inlining/unrolling bloat causing FXC timeout & SPIR-V ID overflow --- .../Runtime/Gpu/Shaders/AstcGpuBise.hlsl | 157 +++--- .../Runtime/Gpu/Shaders/AstcGpuColor.hlsl | 39 +- .../Runtime/Gpu/Shaders/AstcGpuCore.hlsl | 506 +++++++++--------- .../Runtime/Resources/AstcGpuEncoder.compute | 1 - 4 files changed, 326 insertions(+), 377 deletions(-) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl index d8f4a92..8c4b3d5 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl @@ -35,32 +35,40 @@ #define ASTC_CEM_HDR_RGB_LDR_ALPHA 14u // 8 endpoint values (HDR RGB + LDR A) #define ASTC_CEM_HDR_RGBA_DIRECT 15u // 8 endpoint values (HDR RGB + HDR A) +// Compact 21-entry packed LUT: rangeVal (bits 0..8) | bits (9..12) | trits (13) | quints (14) +static const uint kAstcQuantTable[21] = +{ + 2u | (1u << 9u), + 3u | (0u << 9u) | (1u << 13u), + 4u | (2u << 9u), + 5u | (0u << 9u) | (1u << 14u), + 6u | (1u << 9u) | (1u << 13u), + 8u | (3u << 9u), + 10u | (1u << 9u) | (1u << 14u), + 12u | (2u << 9u) | (1u << 13u), + 16u | (4u << 9u), + 20u | (2u << 9u) | (1u << 14u), + 24u | (3u << 9u) | (1u << 13u), + 32u | (5u << 9u), + 40u | (3u << 9u) | (1u << 14u), + 48u | (4u << 9u) | (1u << 13u), + 64u | (6u << 9u), + 80u | (4u << 9u) | (1u << 14u), + 96u | (5u << 9u) | (1u << 13u), + 128u | (7u << 9u), + 160u | (5u << 9u) | (1u << 14u), + 192u | (6u << 9u) | (1u << 13u), + 256u | (8u << 9u) +}; + // Returns (range, bits, trits, quints) for a quantization mode 0..20. void AstcGetQuantParams(uint q, out uint rangeVal, out uint bits, out uint trits, out uint quints) { - trits = 0u; - quints = 0u; - if (q == 0u) { rangeVal = 2u; bits = 1u; } - else if (q == 1u) { rangeVal = 3u; bits = 0u; trits = 1u; } - else if (q == 2u) { rangeVal = 4u; bits = 2u; } - else if (q == 3u) { rangeVal = 5u; bits = 0u; quints = 1u; } - else if (q == 4u) { rangeVal = 6u; bits = 1u; trits = 1u; } - else if (q == 5u) { rangeVal = 8u; bits = 3u; } - else if (q == 6u) { rangeVal = 10u; bits = 1u; quints = 1u; } - else if (q == 7u) { rangeVal = 12u; bits = 2u; trits = 1u; } - else if (q == 8u) { rangeVal = 16u; bits = 4u; } - else if (q == 9u) { rangeVal = 20u; bits = 2u; quints = 1u; } - else if (q == 10u) { rangeVal = 24u; bits = 3u; trits = 1u; } - else if (q == 11u) { rangeVal = 32u; bits = 5u; } - else if (q == 12u) { rangeVal = 40u; bits = 3u; quints = 1u; } - else if (q == 13u) { rangeVal = 48u; bits = 4u; trits = 1u; } - else if (q == 14u) { rangeVal = 64u; bits = 6u; } - else if (q == 15u) { rangeVal = 80u; bits = 4u; quints = 1u; } - else if (q == 16u) { rangeVal = 96u; bits = 5u; trits = 1u; } - else if (q == 17u) { rangeVal = 128u; bits = 7u; } - else if (q == 18u) { rangeVal = 160u; bits = 5u; quints = 1u; } - else if (q == 19u) { rangeVal = 192u; bits = 6u; trits = 1u; } - else { rangeVal = 256u; bits = 8u; } + uint packed = kAstcQuantTable[min(q, 20u)]; + rangeVal = packed & 0x1FFu; + bits = (packed >> 9u) & 0xFu; + trits = (packed >> 13u) & 1u; + quints = (packed >> 14u) & 1u; } uint AstcGetBiseBitCount(uint count, uint q) @@ -261,82 +269,49 @@ uint AstcUnquantizeWeight(uint v, uint q) } // Quantizes an 8-bit endpoint value c8 in [0, 255] to the exact nearest BISE code for quant mode q. -uint AstcQuantizeColor(uint c8, uint q, out uint unquantOut) +// When min128 is true, constrains the unquantized value to >= 128 (HDR modeval/majcomp == 3 direct endpoints). +uint AstcQuantizeColorRange(uint c8, uint q, bool min128, out uint unquantOut) { uint rangeVal, b, t, qu; AstcGetQuantParams(q, rangeVal, b, t, qu); + if (min128) c8 = max(c8, 128u); if (q == ASTC_QUANT_256) { unquantOut = c8; return c8; } + int minK = min128 ? (int)(rangeVal >> 1u) : 0; int maxK = (int)rangeVal - 1; - int kEst = (int)floor((float)c8 * (float)maxK / 255.0 + 0.5); - kEst = clamp(kEst, 0, maxK); + int kEst = clamp((int)floor((float)c8 * (float)maxK / 255.0 + 0.5), minK, maxK); - uint bestCode = AstcLinearToCode((uint)kEst, q); - uint bestUq = AstcUnquantizeColor(bestCode, q); - int bestErr = abs((int)bestUq - (int)c8); + uint bestCode = 0u; + uint bestUq = 0u; + int bestErr = 10000; - if (kEst > 0) - { - uint cPrev = AstcLinearToCode((uint)(kEst - 1), q); - uint uqPrev = AstcUnquantizeColor(cPrev, q); - int ePrev = abs((int)uqPrev - (int)c8); - if (ePrev < bestErr) { bestErr = ePrev; bestCode = cPrev; bestUq = uqPrev; } - } - if (kEst < maxK) + [loop] for (int step = 0; step < 3; ++step) { - uint cNext = AstcLinearToCode((uint)(kEst + 1), q); - uint uqNext = AstcUnquantizeColor(cNext, q); - int eNext = abs((int)uqNext - (int)c8); - if (eNext < bestErr) { bestCode = cNext; bestUq = uqNext; } + int dk = (step == 0) ? 0 : ((step == 1) ? -1 : 1); + int k = kEst + dk; + if (k < minK || k > maxK) continue; + uint code = AstcLinearToCode((uint)k, q); + uint uq = AstcUnquantizeColor(code, q); + int err = abs((int)uq - (int)c8); + if (err < bestErr) { bestErr = err; bestCode = code; bestUq = uq; } } unquantOut = bestUq; return bestCode; } -// Same as AstcQuantizeColor, but constrains the unquantized value to >= 128 -// (guarantees bit 7 == 1 for HDR modeval/majcomp == 3 direct endpoints). -uint AstcQuantizeColorMin128(uint c8, uint q, out uint unquantOut) +uint AstcQuantizeColor(uint c8, uint q, out uint unquantOut) { - uint rangeVal, b, t, qu; - AstcGetQuantParams(q, rangeVal, b, t, qu); - c8 = max(c8, 128u); - if (q == ASTC_QUANT_256) - { - unquantOut = c8; - return c8; - } - - int minK = (int)(rangeVal >> 1u); - int maxK = (int)rangeVal - 1; - int kEst = (int)floor((float)c8 * (float)maxK / 255.0 + 0.5); - kEst = clamp(kEst, minK, maxK); - - uint bestCode = AstcLinearToCode((uint)kEst, q); - uint bestUq = AstcUnquantizeColor(bestCode, q); - int bestErr = abs((int)bestUq - (int)c8); - - if (kEst > minK) - { - uint cPrev = AstcLinearToCode((uint)(kEst - 1), q); - uint uqPrev = AstcUnquantizeColor(cPrev, q); - int ePrev = abs((int)uqPrev - (int)c8); - if (ePrev < bestErr) { bestErr = ePrev; bestCode = cPrev; bestUq = uqPrev; } - } - if (kEst < maxK) - { - uint cNext = AstcLinearToCode((uint)(kEst + 1), q); - uint uqNext = AstcUnquantizeColor(cNext, q); - int eNext = abs((int)uqNext - (int)c8); - if (eNext < bestErr) { bestCode = cNext; bestUq = uqNext; } - } + return AstcQuantizeColorRange(c8, q, false, unquantOut); +} - unquantOut = bestUq; - return bestCode; +uint AstcQuantizeColorMin128(uint c8, uint q, out uint unquantOut) +{ + return AstcQuantizeColorRange(c8, q, true, unquantOut); } // Quantizes a normalized weight w01 in [0, 1] to the exact nearest BISE code and returns unquantized [0, 64]. @@ -349,23 +324,19 @@ uint AstcQuantizeWeight(float w01, uint q, out uint unquantOut) int maxK = (int)rangeVal - 1; int kEst = clamp((int)floor(saturate(w01) * (float)maxK + 0.5), 0, maxK); - uint bestCode = AstcLinearToCode((uint)kEst, q); - uint bestUq = AstcUnquantizeWeight(bestCode, q); - float bestErr = abs((float)bestUq - w64); + uint bestCode = 0u; + uint bestUq = 0u; + float bestErr = 1e30; - if (kEst > 0) - { - uint cPrev = AstcLinearToCode((uint)(kEst - 1), q); - uint uqPrev = AstcUnquantizeWeight(cPrev, q); - float ePrev = abs((float)uqPrev - w64); - if (ePrev < bestErr) { bestErr = ePrev; bestCode = cPrev; bestUq = uqPrev; } - } - if (kEst < maxK) + [loop] for (int step = 0; step < 3; ++step) { - uint cNext = AstcLinearToCode((uint)(kEst + 1), q); - uint uqNext = AstcUnquantizeWeight(cNext, q); - float eNext = abs((float)uqNext - w64); - if (eNext < bestErr) { bestCode = cNext; bestUq = uqNext; } + int dk = (step == 0) ? 0 : ((step == 1) ? -1 : 1); + int k = kEst + dk; + if (k < 0 || k > maxK) continue; + uint code = AstcLinearToCode((uint)k, q); + uint uq = AstcUnquantizeWeight(code, q); + float err = abs((float)uq - w64); + if (err < bestErr) { bestErr = err; bestCode = code; bestUq = uq; } } unquantOut = bestUq; diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl index 7a8d7ee..b4c14a0 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl @@ -265,23 +265,22 @@ void AstcQuantizeHdrRgbEndpoints( out float3 uq1Lns) { // Baseline: majcomp == 3 direct mode (always valid) - uint r0 = (uint)clamp(floor(e0Lns.r / 256.0 + 0.5), 0.0, 255.0); - uint r1 = (uint)clamp(floor(e1Lns.r / 256.0 + 0.5), 0.0, 255.0); - uint g0 = (uint)clamp(floor(e0Lns.g / 256.0 + 0.5), 0.0, 255.0); - uint g1 = (uint)clamp(floor(e1Lns.g / 256.0 + 0.5), 0.0, 255.0); - uint b0 = 128u + (uint)clamp(floor(e0Lns.b / 512.0 + 0.5), 0.0, 127.0); - uint b1 = 128u + (uint)clamp(floor(e1Lns.b / 512.0 + 0.5), 0.0, 127.0); - - uint uqR0, uqR1, uqG0, uqG1, uqB0, uqB1; - codes[0] = AstcQuantizeColor(r0, epQuant, uqR0); - codes[1] = AstcQuantizeColor(r1, epQuant, uqR1); - codes[2] = AstcQuantizeColor(g0, epQuant, uqG0); - codes[3] = AstcQuantizeColor(g1, epQuant, uqG1); - codes[4] = AstcQuantizeColorMin128(b0, epQuant, uqB0); - codes[5] = AstcQuantizeColorMin128(b1, epQuant, uqB1); - - uq0Lns = float3((float)(uqR0 << 8u), (float)(uqG0 << 8u), (float)((uqB0 & 0x7Fu) << 9u)); - uq1Lns = float3((float)(uqR1 << 8u), (float)(uqG1 << 8u), (float)((uqB1 & 0x7Fu) << 9u)); + uint baseVals[6]; + baseVals[0] = (uint)clamp(floor(e0Lns.r / 256.0 + 0.5), 0.0, 255.0); + baseVals[1] = (uint)clamp(floor(e1Lns.r / 256.0 + 0.5), 0.0, 255.0); + baseVals[2] = (uint)clamp(floor(e0Lns.g / 256.0 + 0.5), 0.0, 255.0); + baseVals[3] = (uint)clamp(floor(e1Lns.g / 256.0 + 0.5), 0.0, 255.0); + baseVals[4] = 128u + (uint)clamp(floor(e0Lns.b / 512.0 + 0.5), 0.0, 127.0); + baseVals[5] = 128u + (uint)clamp(floor(e1Lns.b / 512.0 + 0.5), 0.0, 127.0); + + uint baseUq[6]; + [loop] for (uint bi = 0u; bi < 6u; ++bi) + { + codes[bi] = AstcQuantizeColorRange(baseVals[bi], epQuant, bi >= 4u, baseUq[bi]); + } + + uq0Lns = float3((float)(baseUq[0] << 8u), (float)(baseUq[2] << 8u), (float)((baseUq[4] & 0x7Fu) << 9u)); + uq1Lns = float3((float)(baseUq[1] << 8u), (float)(baseUq[3] << 8u), (float)((baseUq[5] & 0x7Fu) << 9u)); float3 d0 = uq0Lns - e0Lns; float3 d1 = uq1Lns - e1Lns; @@ -328,7 +327,7 @@ void AstcQuantizeHdrRgbEndpoints( int D1_12 = (R0 - B0) - B1_12; // Try finest resolution modes first (modeval 7..0) and pick the first that fits. - for (uint mi = 0u; mi < 8u; ++mi) + [loop] for (uint mi = 0u; mi < 8u; ++mi) { uint mv = 7u - mi; uint sh = (mv >> 1u) ^ 3u; @@ -359,7 +358,7 @@ void AstcQuantizeHdrRgbEndpoints( uint candCodes[6]; uint uqV[6]; - for (uint ci = 0u; ci < 6u; ++ci) + [loop] for (uint ci = 0u; ci < 6u; ++ci) { candCodes[ci] = AstcQuantizeColor(rawV[ci], epQuant, uqV[ci]); } @@ -371,7 +370,7 @@ void AstcQuantizeHdrRgbEndpoints( float candErr = dot(cd0, cd0) + dot(cd1, cd1); if (candErr < bestErr) { - for (uint cj = 0u; cj < 6u; ++cj) codes[cj] = candCodes[cj]; + [loop] for (uint cj = 0u; cj < 6u; ++cj) codes[cj] = candCodes[cj]; uq0Lns = cand0; uq1Lns = cand1; } diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl index df583ee..bc87cb3 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl @@ -224,79 +224,63 @@ void AstcQuantizeEndpoints( if (effMode == ASTC_EFF_LDR_NORMAL) { - uint l0 = (uint)clamp(floor(e0.x + 0.5), 0.0, 255.0); - uint l1 = (uint)clamp(floor(e1.x + 0.5), 0.0, 255.0); - uint a0 = (uint)clamp(floor(e0.w + 0.5), 0.0, 255.0); - uint a1 = (uint)clamp(floor(e1.w + 0.5), 0.0, 255.0); - - uint uqL0, uqL1, uqA0, uqA1; - codesOut[codeOffset + 0u] = AstcQuantizeColor(l0, epQuant, uqL0); - codesOut[codeOffset + 1u] = AstcQuantizeColor(l1, epQuant, uqL1); - codesOut[codeOffset + 2u] = AstcQuantizeColor(a0, epQuant, uqA0); - codesOut[codeOffset + 3u] = AstcQuantizeColor(a1, epQuant, uqA1); - - uqE0 = float4((float)uqL0, 0.0, 0.0, (float)uqA0); - uqE1 = float4((float)uqL1, 0.0, 0.0, (float)uqA1); + uint vals[4]; + vals[0] = (uint)clamp(floor(e0.x + 0.5), 0.0, 255.0); + vals[1] = (uint)clamp(floor(e1.x + 0.5), 0.0, 255.0); + vals[2] = (uint)clamp(floor(e0.w + 0.5), 0.0, 255.0); + vals[3] = (uint)clamp(floor(e1.w + 0.5), 0.0, 255.0); + + uint uq[4]; + [loop] for (uint i = 0u; i < 4u; ++i) + { + codesOut[codeOffset + i] = AstcQuantizeColor(vals[i], epQuant, uq[i]); + } + + uqE0 = float4((float)uq[0], 0.0, 0.0, (float)uq[2]); + uqE1 = float4((float)uq[1], 0.0, 0.0, (float)uq[3]); return; } if (effMode == ASTC_EFF_LDR_RGB || effMode == ASTC_EFF_LDR_RGBA) { - uint r0 = (uint)clamp(floor(e0.r + 0.5), 0.0, 255.0); - uint r1 = (uint)clamp(floor(e1.r + 0.5), 0.0, 255.0); - uint g0 = (uint)clamp(floor(e0.g + 0.5), 0.0, 255.0); - uint g1 = (uint)clamp(floor(e1.g + 0.5), 0.0, 255.0); - uint b0 = (uint)clamp(floor(e0.b + 0.5), 0.0, 255.0); - uint b1 = (uint)clamp(floor(e1.b + 0.5), 0.0, 255.0); - - uint uqR0, uqR1, uqG0, uqG1, uqB0, uqB1; - uint cR0 = AstcQuantizeColor(r0, epQuant, uqR0); - uint cR1 = AstcQuantizeColor(r1, epQuant, uqR1); - uint cG0 = AstcQuantizeColor(g0, epQuant, uqG0); - uint cG1 = AstcQuantizeColor(g1, epQuant, uqG1); - uint cB0 = AstcQuantizeColor(b0, epQuant, uqB0); - uint cB1 = AstcQuantizeColor(b1, epQuant, uqB1); - - uint uqA0 = 255u, uqA1 = 255u; - uint cA0 = 0u, cA1 = 0u; - if (effMode == ASTC_EFF_LDR_RGBA) + uint vals[8]; + vals[0] = (uint)clamp(floor(e0.r + 0.5), 0.0, 255.0); + vals[1] = (uint)clamp(floor(e1.r + 0.5), 0.0, 255.0); + vals[2] = (uint)clamp(floor(e0.g + 0.5), 0.0, 255.0); + vals[3] = (uint)clamp(floor(e1.g + 0.5), 0.0, 255.0); + vals[4] = (uint)clamp(floor(e0.b + 0.5), 0.0, 255.0); + vals[5] = (uint)clamp(floor(e1.b + 0.5), 0.0, 255.0); + vals[6] = (uint)clamp(floor(e0.a + 0.5), 0.0, 255.0); + vals[7] = (uint)clamp(floor(e1.a + 0.5), 0.0, 255.0); + + uint cArr[8]; + uint uqArr[8]; + [loop] for (uint z = 0u; z < 8u; ++z) { cArr[z] = 0u; uqArr[z] = 255u; } + + uint cnt = (effMode == ASTC_EFF_LDR_RGBA) ? 8u : 6u; + [loop] for (uint i = 0u; i < cnt; ++i) { - uint a0 = (uint)clamp(floor(e0.a + 0.5), 0.0, 255.0); - uint a1 = (uint)clamp(floor(e1.a + 0.5), 0.0, 255.0); - cA0 = AstcQuantizeColor(a0, epQuant, uqA0); - cA1 = AstcQuantizeColor(a1, epQuant, uqA1); + cArr[i] = AstcQuantizeColor(vals[i], epQuant, uqArr[i]); } // Khronos ASTC spec: CEM 8/12 triggers blue-contraction & endpoint swap whenever (r1+g1+b1) < (r0+g0+b0). // Guarantee s1 >= s0 so hardware always decodes direct endpoints without unexpected contraction/inversion. - if ((uqR0 + uqG0 + uqB0) > (uqR1 + uqG1 + uqB1)) + if ((uqArr[0] + uqArr[2] + uqArr[4]) > (uqArr[1] + uqArr[3] + uqArr[5])) { - uint t; - t = cR0; cR0 = cR1; cR1 = t; - t = cG0; cG0 = cG1; cG1 = t; - t = cB0; cB0 = cB1; cB1 = t; - t = cA0; cA0 = cA1; cA1 = t; - t = uqR0; uqR0 = uqR1; uqR1 = t; - t = uqG0; uqG0 = uqG1; uqG1 = t; - t = uqB0; uqB0 = uqB1; uqB1 = t; - t = uqA0; uqA0 = uqA1; uqA1 = t; + [loop] for (uint s = 0u; s < 8u; s += 2u) + { + uint tc = cArr[s]; cArr[s] = cArr[s + 1u]; cArr[s + 1u] = tc; + uint tu = uqArr[s]; uqArr[s] = uqArr[s + 1u]; uqArr[s + 1u] = tu; + } } - codesOut[codeOffset + 0u] = cR0; - codesOut[codeOffset + 1u] = cR1; - codesOut[codeOffset + 2u] = cG0; - codesOut[codeOffset + 3u] = cG1; - codesOut[codeOffset + 4u] = cB0; - codesOut[codeOffset + 5u] = cB1; - - uqE0 = float4((float)uqR0, (float)uqG0, (float)uqB0, (float)uqA0); - uqE1 = float4((float)uqR1, (float)uqG1, (float)uqB1, (float)uqA1); - - if (effMode == ASTC_EFF_LDR_RGBA) + [loop] for (uint k = 0u; k < cnt; ++k) { - codesOut[codeOffset + 6u] = cA0; - codesOut[codeOffset + 7u] = cA1; + codesOut[codeOffset + k] = cArr[k]; } + + uqE0 = float4((float)uqArr[0], (float)uqArr[2], (float)uqArr[4], (float)uqArr[6]); + uqE1 = float4((float)uqArr[1], (float)uqArr[3], (float)uqArr[5], (float)uqArr[7]); return; } @@ -1310,15 +1294,17 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) uint gridCount = c.gridW * c.gridH; uint totalWeights = gridCount; + float4 uqE0, uqE1; + AstcQuantizeEndpoints(c.e0, c.e1, effMode, c.epQuant, epCodes, 0u, uqE0, uqE1); + #if !defined(ASTC_FAST_ONLY) if (c.isTwoPart != 0u) { - float4 uq0_p0, uq1_p0, uq0_p1, uq1_p1; - AstcQuantizeEndpoints(c.e0, c.e1, effMode, c.epQuant, epCodes, 0u, uq0_p0, uq1_p0); + float4 uq0_p1, uq1_p1; AstcQuantizeEndpoints(c.e0_p1, c.e1_p1, effMode, c.epQuant, epCodes, epPerPart, uq0_p1, uq1_p1); float gridW01[ASTC_MAX_GRID]; - AstcComputeWeightGrid2P(baseX, baseY, effMode, c.partSeed, uq0_p0, uq1_p0, uq0_p1, uq1_p1, c.gridW, c.gridH, gridW01); + AstcComputeWeightGrid2P(baseX, baseY, effMode, c.partSeed, uqE0, uqE1, uq0_p1, uq1_p1, c.gridW, c.gridH, gridW01); [loop] for (uint k = 0u; k < gridCount; ++k) { uint dummyUq; @@ -1329,9 +1315,6 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) #endif if (c.isDualPlane != 0u) { - float4 uqE0, uqE1; - AstcQuantizeEndpoints(c.e0, c.e1, effMode, c.epQuant, epCodes, 0u, uqE0, uqE1); - float grid0W01[ASTC_MAX_GRID]; float grid1W01[ASTC_MAX_GRID]; AstcComputeWeightGrid1P(baseX, baseY, effMode, c.dualPlaneChannel, uqE0, uqE1, c.gridW, c.gridH, grid0W01); @@ -1363,9 +1346,6 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) } else { - float4 uqE0, uqE1; - AstcQuantizeEndpoints(c.e0, c.e1, effMode, c.epQuant, epCodes, 0u, uqE0, uqE1); - float gridW01[ASTC_MAX_GRID]; AstcComputeWeightGrid1P(baseX, baseY, effMode, 4u, uqE0, uqE1, c.gridW, c.gridH, gridW01); @@ -1397,6 +1377,8 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) // Header: BlockMode (11 bits) + PartitionCount-1 (2 bits) AstcWriteBits128(block, 0u, 11u, c.blockMode); + uint epStartBit = 17u; + uint epTotalCount = epPerPart; #if !defined(ASTC_FAST_ONLY) if (c.isTwoPart != 0u) { @@ -1404,7 +1386,8 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) AstcWriteBits128(block, 13u, 10u, c.partSeed); // Matched CEM for both partitions: low 2 bits = 00, upper 4 bits = cem AstcWriteBits128(block, 23u, 6u, cem << 2u); - AstcEncodeBise16(block, 29u, c.epQuant, epPerPart * 2u, epCodes); + epStartBit = 29u; + epTotalCount = epPerPart * 2u; } else #endif @@ -1416,8 +1399,8 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) uint weightBits = AstcGetBiseBitCount(totalWeights, c.wQuant); AstcWriteBits128(block, 128u - weightBits - 2u, 2u, c.dualPlaneChannel); } - AstcEncodeBise16(block, 17u, c.epQuant, epPerPart, epCodes); } + AstcEncodeBise16(block, epStartBit, c.epQuant, epTotalCount, epCodes); return block; } @@ -1791,6 +1774,12 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) best.e0_p1 = 0.0; best.e1_p1 = 0.0; +#if !defined(ASTC_FAST_ONLY) + float workGrid0[ASTC_MAX_GRID]; + float workGrid1[ASTC_MAX_GRID]; + [loop] for (uint zw = 0u; zw < ASTC_MAX_GRID_U; ++zw) { workGrid0[zw] = 0.0; workGrid1[zw] = 0.0; } +#endif + if (effMode == ASTC_EFF_LDR_NORMAL) { float4 nE0, nE1; @@ -1806,7 +1795,6 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) best.dualPlaneChannel = 3u; best.e0 = nE0; best.e1 = nE1; - return AstcAssembleBlock(baseX, baseY, effMode, best); #else if (_AstcQuality == 0u) { @@ -1819,76 +1807,52 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) best.dualPlaneChannel = 3u; best.e0 = nE0; best.e1 = nE1; - return AstcAssembleBlock(baseX, baseY, effMode, best); } - - // Balanced / High: compare Dual-Plane 4x4 (and 5x5 on >=5x5 blocks) reusing projections per grid + else + { + // Balanced / High: compare Dual-Plane 4x4 (and 5x5 on >=5x5 blocks) reusing projections per grid #if BLOCK_X >= 5 - uint normCandCount = 4u; + uint normCandCount = 4u; #else - uint normCandCount = 2u; + uint normCandCount = 2u; #endif - uint normCachedG = 0u; - float normGrid0[ASTC_MAX_GRID]; - float normGrid1[ASTC_MAX_GRID]; - [loop] for (uint zn = 0u; zn < ASTC_MAX_GRID_U; ++zn) { normGrid0[zn] = 0.0; normGrid1[zn] = 0.0; } - [loop] for (uint ni = 0u; ni < normCandCount; ++ni) - { - uint4 cfg = (ni == 0u) ? uint4(0x452u, 4u, ASTC_QUANT_5, ASTC_QUANT_256) - : (ni == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_256) - : (ni == 2u) ? uint4(0x4F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_128) - : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_256); - if (cfg.y != normCachedG) + uint normCachedG = 0u; + [loop] for (uint ni = 0u; ni < normCandCount; ++ni) { - AstcComputeWeightGrid1P(baseX, baseY, effMode, 3u, nE0, nE1, cfg.y, cfg.y, normGrid0); - AstcComputePlane1WeightGrid(baseX, baseY, effMode, 3u, nE0.a, nE1.a, cfg.y, cfg.y, normGrid1); - normCachedG = cfg.y; + uint4 cfg = (ni == 0u) ? uint4(0x452u, 4u, ASTC_QUANT_5, ASTC_QUANT_256) + : (ni == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_256) + : (ni == 2u) ? uint4(0x4F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_128) + : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_256); + if (cfg.y != normCachedG) + { + AstcComputeWeightGrid1P(baseX, baseY, effMode, 3u, nE0, nE1, cfg.y, cfg.y, workGrid0); + AstcComputePlane1WeightGrid(baseX, baseY, effMode, 3u, nE0.a, nE1.a, cfg.y, cfg.y, workGrid1); + normCachedG = cfg.y; + } + AstcTryCandidateDualPlaneWithGrid(baseX, baseY, effMode, 3u, nE0, nE1, cfg.x, cfg.y, cfg.y, workGrid0, workGrid1, cfg.z, cfg.w, best); } - AstcTryCandidateDualPlaneWithGrid(baseX, baseY, effMode, 3u, nE0, nE1, cfg.x, cfg.y, cfg.y, normGrid0, normGrid1, cfg.z, cfg.w, best); } - return AstcAssembleBlock(baseX, baseY, effMode, best); #endif } - - // ------------------------------------------------------------------------ - // Step 4: Color / HDR 1-Partition Endpoint Fitting - // ------------------------------------------------------------------------ + else + { + // ------------------------------------------------------------------------ + // Step 4: Color / HDR 1-Partition Endpoint Fitting + // ------------------------------------------------------------------------ #if defined(ASTC_FAST_ONLY) - uint refineIters = 0u; + uint refineIters = 0u; #else - uint refineIters = (_AstcQuality >= 1u) ? 2u : 0u; + uint refineIters = (_AstcQuality >= 1u) ? 2u : 0u; #endif - float4 e0, e1; - AstcFitSubsetEndpoints(baseX, baseY, effMode, 4u, 0u, 2u, refineIters, e0, e1); + float4 e0, e1; + AstcFitSubsetEndpoints(baseX, baseY, effMode, 4u, 0u, 2u, refineIters, e0, e1); - bool hasAlpha = (effMode == ASTC_EFF_LDR_RGBA || effMode == ASTC_EFF_HDR_RGB_LDR_ALPHA || effMode == ASTC_EFF_HDR_RGBA); + bool hasAlpha = (effMode == ASTC_EFF_LDR_RGBA || effMode == ASTC_EFF_HDR_RGB_LDR_ALPHA || effMode == ASTC_EFF_HDR_RGBA); - // ------------------------------------------------------------------------ - // Step 5: Fast Path (ASTC_FAST_ONLY or Quality == 0) - Single optimal mode - // ------------------------------------------------------------------------ + // ------------------------------------------------------------------------ + // Step 5: Fast Path (ASTC_FAST_ONLY or Quality == 0) - Single optimal mode + // ------------------------------------------------------------------------ #if defined(ASTC_FAST_ONLY) - best.e0 = e0; - best.e1 = e1; - if (!hasAlpha) - { -#if BLOCK_X == 4 - best.blockMode = 0x251u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_12; best.epQuant = ASTC_QUANT_256; -#else - best.blockMode = 0x0E3u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_6; best.epQuant = ASTC_QUANT_192; -#endif - } - else - { -#if BLOCK_X == 4 - best.blockMode = 0x053u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_8; best.epQuant = ASTC_QUANT_192; -#else - best.blockMode = 0x0E2u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_4; best.epQuant = ASTC_QUANT_192; -#endif - } - return AstcAssembleBlock(baseX, baseY, effMode, best); -#else - if (_AstcQuality == 0u) - { best.e0 = e0; best.e1 = e1; if (!hasAlpha) @@ -1907,170 +1871,186 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) best.blockMode = 0x0E2u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_4; best.epQuant = ASTC_QUANT_192; #endif } - return AstcAssembleBlock(baseX, baseY, effMode, best); - } - - // ------------------------------------------------------------------------ - // Step 6: Balanced / High Path - Multi-BlockMode Candidate Search (reusing projections per grid) - // ------------------------------------------------------------------------ - uint cand1PCount; - if (!hasAlpha) - { -#if BLOCK_X >= 8 - cand1PCount = 12u; -#elif BLOCK_X >= 6 - cand1PCount = 10u; -#elif BLOCK_X >= 5 - cand1PCount = 6u; #else - cand1PCount = 3u; + if (_AstcQuality == 0u) + { + best.e0 = e0; + best.e1 = e1; + if (!hasAlpha) + { +#if BLOCK_X == 4 + best.blockMode = 0x251u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_12; best.epQuant = ASTC_QUANT_256; +#else + best.blockMode = 0x0E3u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_6; best.epQuant = ASTC_QUANT_192; #endif - } - else - { -#if BLOCK_X >= 6 - cand1PCount = 8u; -#elif BLOCK_X >= 5 - cand1PCount = 5u; + } + else + { +#if BLOCK_X == 4 + best.blockMode = 0x053u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_8; best.epQuant = ASTC_QUANT_192; #else - cand1PCount = 3u; + best.blockMode = 0x0E2u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_4; best.epQuant = ASTC_QUANT_192; #endif - } - - uint cachedGW = 0u, cachedGH = 0u; - float cachedGrid1P[ASTC_MAX_GRID]; - [loop] for (uint z1 = 0u; z1 < ASTC_MAX_GRID_U; ++z1) cachedGrid1P[z1] = 0.0; - float4 bestChanErr = 0.0; - - [loop] for (uint c1 = 0u; c1 < cand1PCount; ++c1) - { - uint bm, gw, gh, wQ, epQ; - if (!hasAlpha) - { - if (c1 == 0u) { bm = 0x251u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_12; epQ = ASTC_QUANT_256; } - else if (c1 == 1u) { bm = 0x242u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_16; epQ = ASTC_QUANT_192; } - else if (c1 == 2u) { bm = 0x252u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_20; epQ = ASTC_QUANT_96; } - else if (c1 == 3u) { bm = 0x0F2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_5; epQ = ASTC_QUANT_256; } - else if (c1 == 4u) { bm = 0x0E3u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_192; } - else if (c1 == 5u) { bm = 0x0F3u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_8; epQ = ASTC_QUANT_64; } - else if (c1 == 6u) { bm = 0x114u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_3; epQ = ASTC_QUANT_256; } - else if (c1 == 7u) { bm = 0x108u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_80; } - else if (c1 == 8u) { bm = 0x143u; gw = 6u; gh = 4u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } - else if (c1 == 9u) { bm = 0x04Fu; gw = 4u; gh = 6u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } - else if (c1 == 10u){ bm = 0x046u; gw = 8u; gh = 4u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } - else { bm = 0x04Au; gw = 4u; gh = 8u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } + } } else { - if (c1 == 0u) { bm = 0x043u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } - else if (c1 == 1u) { bm = 0x053u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_8; epQ = ASTC_QUANT_192; } - else if (c1 == 2u) { bm = 0x251u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_12; epQ = ASTC_QUANT_96; } - else if (c1 == 3u) { bm = 0x0E2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } - else if (c1 == 4u) { bm = 0x0F2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_5; epQ = ASTC_QUANT_80; } - else if (c1 == 5u) { bm = 0x114u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_3; epQ = ASTC_QUANT_96; } - else if (c1 == 6u) { bm = 0x142u; gw = 6u; gh = 4u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } - else { bm = 0x04Eu; gw = 4u; gh = 6u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } - } - if (gw != cachedGW || gh != cachedGH) - { - AstcComputeWeightGrid1P(baseX, baseY, effMode, 4u, e0, e1, gw, gh, cachedGrid1P); - cachedGW = gw; - cachedGH = gh; - } - AstcTryCandidate1PWithGrid(baseX, baseY, effMode, e0, e1, bm, gw, gh, cachedGrid1P, wQ, epQ, best, bestChanErr); - } - - // Dual-Plane with dynamic CCS selection directly from bestChanErr (zero extra texture scan!) - { - uint bestCcs = 0u; - float maxChErr = bestChanErr.r; - if (bestChanErr.g > maxChErr) { maxChErr = bestChanErr.g; bestCcs = 1u; } - if (bestChanErr.b > maxChErr) { maxChErr = bestChanErr.b; bestCcs = 2u; } - if (hasAlpha && bestChanErr.a > maxChErr) { bestCcs = 3u; } - - float4 dpE0, dpE1; - AstcFitSubsetEndpoints(baseX, baseY, effMode, bestCcs, 0u, 2u, 1u, dpE0, dpE1); -#if BLOCK_X >= 5 - uint dpCount = 3u; -#else - uint dpCount = 2u; -#endif - uint dpCachedG = 0u; - float dpGrid0[ASTC_MAX_GRID]; - float dpGrid1[ASTC_MAX_GRID]; - [loop] for (uint zd = 0u; zd < ASTC_MAX_GRID_U; ++zd) { dpGrid0[zd] = 0.0; dpGrid1[zd] = 0.0; } - [loop] for (uint di = 0u; di < dpCount; ++di) - { - uint4 cfg; + // ------------------------------------------------------------------------ + // Step 6: Balanced / High Path - Multi-BlockMode Candidate Search (reusing projections per grid) + // ------------------------------------------------------------------------ + uint cand1PCount; if (!hasAlpha) { - cfg = (di == 0u) ? uint4(0x451u, 4u, ASTC_QUANT_3, ASTC_QUANT_256) - : (di == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_160) - : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_256); +#if BLOCK_X >= 8 + cand1PCount = 12u; +#elif BLOCK_X >= 6 + cand1PCount = 10u; +#elif BLOCK_X >= 5 + cand1PCount = 6u; +#else + cand1PCount = 3u; +#endif } else { - cfg = (di == 0u) ? uint4(0x451u, 4u, ASTC_QUANT_3, ASTC_QUANT_128) - : (di == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_48) - : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_160); +#if BLOCK_X >= 6 + cand1PCount = 8u; +#elif BLOCK_X >= 5 + cand1PCount = 5u; +#else + cand1PCount = 3u; +#endif } - if (cfg.y != dpCachedG) + + uint cachedGW = 0u, cachedGH = 0u; + float4 bestChanErr = 0.0; + + [loop] for (uint c1 = 0u; c1 < cand1PCount; ++c1) { - AstcComputeWeightGrid1P(baseX, baseY, effMode, bestCcs, dpE0, dpE1, cfg.y, cfg.y, dpGrid0); - AstcComputePlane1WeightGrid(baseX, baseY, effMode, bestCcs, AstcGetChannel(dpE0, bestCcs), AstcGetChannel(dpE1, bestCcs), cfg.y, cfg.y, dpGrid1); - dpCachedG = cfg.y; + uint bm, gw, gh, wQ, epQ; + if (!hasAlpha) + { + if (c1 == 0u) { bm = 0x251u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_12; epQ = ASTC_QUANT_256; } + else if (c1 == 1u) { bm = 0x242u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_16; epQ = ASTC_QUANT_192; } + else if (c1 == 2u) { bm = 0x252u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_20; epQ = ASTC_QUANT_96; } + else if (c1 == 3u) { bm = 0x0F2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_5; epQ = ASTC_QUANT_256; } + else if (c1 == 4u) { bm = 0x0E3u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_192; } + else if (c1 == 5u) { bm = 0x0F3u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_8; epQ = ASTC_QUANT_64; } + else if (c1 == 6u) { bm = 0x114u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_3; epQ = ASTC_QUANT_256; } + else if (c1 == 7u) { bm = 0x108u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_80; } + else if (c1 == 8u) { bm = 0x143u; gw = 6u; gh = 4u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } + else if (c1 == 9u) { bm = 0x04Fu; gw = 4u; gh = 6u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } + else if (c1 == 10u){ bm = 0x046u; gw = 8u; gh = 4u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } + else { bm = 0x04Au; gw = 4u; gh = 8u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } + } + else + { + if (c1 == 0u) { bm = 0x043u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } + else if (c1 == 1u) { bm = 0x053u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_8; epQ = ASTC_QUANT_192; } + else if (c1 == 2u) { bm = 0x251u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_12; epQ = ASTC_QUANT_96; } + else if (c1 == 3u) { bm = 0x0E2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } + else if (c1 == 4u) { bm = 0x0F2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_5; epQ = ASTC_QUANT_80; } + else if (c1 == 5u) { bm = 0x114u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_3; epQ = ASTC_QUANT_96; } + else if (c1 == 6u) { bm = 0x142u; gw = 6u; gh = 4u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } + else { bm = 0x04Eu; gw = 4u; gh = 6u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } + } + if (gw != cachedGW || gh != cachedGH) + { + AstcComputeWeightGrid1P(baseX, baseY, effMode, 4u, e0, e1, gw, gh, workGrid0); + cachedGW = gw; + cachedGH = gh; + } + AstcTryCandidate1PWithGrid(baseX, baseY, effMode, e0, e1, bm, gw, gh, workGrid0, wQ, epQ, best, bestChanErr); } - AstcTryCandidateDualPlaneWithGrid(baseX, baseY, effMode, bestCcs, dpE0, dpE1, cfg.x, cfg.y, cfg.y, dpGrid0, dpGrid1, cfg.z, cfg.w, best); - } - } - - // ------------------------------------------------------------------------ - // Step 7: High Quality Path (Quality >= 2) - Merged 2-Partition Directional Search - // ------------------------------------------------------------------------ - if (_AstcQuality >= 2u) - { - bool smallBlock = (BLOCK_X * BLOCK_Y) < 32u; - uint bestSeed = AstcSelectBestSeed2P(baseX, baseY, effMode); - AstcPart2Params p2Params = AstcPreparePartition2(bestSeed, smallBlock); - float4 e0_p0, e1_p0, e0_p1, e1_p1; - AstcFitBothPartitions2P(baseX, baseY, effMode, p2Params, e0_p0, e1_p0, e0_p1, e1_p1); + // Dual-Plane with dynamic CCS selection directly from bestChanErr (zero extra texture scan!) + { + uint bestCcs = 0u; + float maxChErr = bestChanErr.r; + if (bestChanErr.g > maxChErr) { maxChErr = bestChanErr.g; bestCcs = 1u; } + if (bestChanErr.b > maxChErr) { maxChErr = bestChanErr.b; bestCcs = 2u; } + if (hasAlpha && bestChanErr.a > maxChErr) { bestCcs = 3u; } + + float4 dpE0, dpE1; + AstcFitSubsetEndpoints(baseX, baseY, effMode, bestCcs, 0u, 2u, 1u, dpE0, dpE1); #if BLOCK_X >= 5 - uint p2Count = 4u; + uint dpCount = 3u; #else - uint p2Count = 2u; + uint dpCount = 2u; #endif - uint p2CachedG = 0u; - float p2Grid[ASTC_MAX_GRID]; - [loop] for (uint z2 = 0u; z2 < ASTC_MAX_GRID_U; ++z2) p2Grid[z2] = 0.0; - [loop] for (uint p2 = 0u; p2 < p2Count; ++p2) - { - uint4 cfg; - if (!hasAlpha) - { - if (p2 == 0u) cfg = uint4(0x042u, 4u, ASTC_QUANT_4, ASTC_QUANT_40); - else if (p2 == 1u) cfg = uint4(0x043u, 4u, ASTC_QUANT_6, ASTC_QUANT_24); - else if (p2 == 2u) cfg = uint4(0x0F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_24); - else cfg = uint4(0x0E2u, 5u, ASTC_QUANT_4, ASTC_QUANT_16); - } - else - { - if (p2 == 0u) cfg = uint4(0x051u, 4u, ASTC_QUANT_3, ASTC_QUANT_20); - else if (p2 == 1u) cfg = uint4(0x042u, 4u, ASTC_QUANT_4, ASTC_QUANT_16); - else if (p2 == 2u) cfg = uint4(0x0E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_24); - else cfg = uint4(0x0F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_12); + uint dpCachedG = 0u; + [loop] for (uint di = 0u; di < dpCount; ++di) + { + uint4 cfg; + if (!hasAlpha) + { + cfg = (di == 0u) ? uint4(0x451u, 4u, ASTC_QUANT_3, ASTC_QUANT_256) + : (di == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_160) + : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_256); + } + else + { + cfg = (di == 0u) ? uint4(0x451u, 4u, ASTC_QUANT_3, ASTC_QUANT_128) + : (di == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_48) + : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_160); + } + if (cfg.y != dpCachedG) + { + AstcComputeWeightGrid1P(baseX, baseY, effMode, bestCcs, dpE0, dpE1, cfg.y, cfg.y, workGrid0); + AstcComputePlane1WeightGrid(baseX, baseY, effMode, bestCcs, AstcGetChannel(dpE0, bestCcs), AstcGetChannel(dpE1, bestCcs), cfg.y, cfg.y, workGrid1); + dpCachedG = cfg.y; + } + AstcTryCandidateDualPlaneWithGrid(baseX, baseY, effMode, bestCcs, dpE0, dpE1, cfg.x, cfg.y, cfg.y, workGrid0, workGrid1, cfg.z, cfg.w, best); + } } - if (cfg.y != p2CachedG) + + // ------------------------------------------------------------------------ + // Step 7: High Quality Path (Quality >= 2) - Merged 2-Partition Directional Search + // ------------------------------------------------------------------------ + if (_AstcQuality >= 2u) { - AstcComputeWeightGrid2P(baseX, baseY, effMode, bestSeed, e0_p0, e1_p0, e0_p1, e1_p1, cfg.y, cfg.y, p2Grid); - p2CachedG = cfg.y; + bool smallBlock = (BLOCK_X * BLOCK_Y) < 32u; + uint bestSeed = AstcSelectBestSeed2P(baseX, baseY, effMode); + AstcPart2Params p2Params = AstcPreparePartition2(bestSeed, smallBlock); + float4 e0_p0, e1_p0, e0_p1, e1_p1; + AstcFitBothPartitions2P(baseX, baseY, effMode, p2Params, e0_p0, e1_p0, e0_p1, e1_p1); + +#if BLOCK_X >= 5 + uint p2Count = 4u; +#else + uint p2Count = 2u; +#endif + uint p2CachedG = 0u; + [loop] for (uint p2 = 0u; p2 < p2Count; ++p2) + { + uint4 cfg; + if (!hasAlpha) + { + if (p2 == 0u) cfg = uint4(0x042u, 4u, ASTC_QUANT_4, ASTC_QUANT_40); + else if (p2 == 1u) cfg = uint4(0x043u, 4u, ASTC_QUANT_6, ASTC_QUANT_24); + else if (p2 == 2u) cfg = uint4(0x0F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_24); + else cfg = uint4(0x0E2u, 5u, ASTC_QUANT_4, ASTC_QUANT_16); + } + else + { + if (p2 == 0u) cfg = uint4(0x051u, 4u, ASTC_QUANT_3, ASTC_QUANT_20); + else if (p2 == 1u) cfg = uint4(0x042u, 4u, ASTC_QUANT_4, ASTC_QUANT_16); + else if (p2 == 2u) cfg = uint4(0x0E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_24); + else cfg = uint4(0x0F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_12); + } + if (cfg.y != p2CachedG) + { + AstcComputeWeightGrid2P(baseX, baseY, effMode, bestSeed, e0_p0, e1_p0, e0_p1, e1_p1, cfg.y, cfg.y, workGrid0); + p2CachedG = cfg.y; + } + AstcTryCandidate2PWithGrid(baseX, baseY, effMode, bestSeed, p2Params, e0_p0, e1_p0, e0_p1, e1_p1, cfg.x, cfg.y, cfg.y, workGrid0, cfg.z, cfg.w, best); + } } - AstcTryCandidate2PWithGrid(baseX, baseY, effMode, bestSeed, p2Params, e0_p0, e1_p0, e0_p1, e1_p1, cfg.x, cfg.y, cfg.y, p2Grid, cfg.z, cfg.w, best); } +#endif // !ASTC_FAST_ONLY } return AstcAssembleBlock(baseX, baseY, effMode, best); -#endif // !ASTC_FAST_ONLY } #endif // ASTC_GPU_CORE_HLSL diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute index c2ebff4..832c7b4 100644 --- a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute @@ -1,5 +1,4 @@ #pragma editor_sync_compilation -#pragma use_dxc #pragma kernel EncodeTex4x4 ASTC_BLOCK_4X4 #pragma kernel EncodeTex5x5 ASTC_BLOCK_5X5 From a30124eb6a0c42c5645e204416ee9b6ee35185c8 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Sat, 28 Nov 2026 06:43:45 +0000 Subject: [PATCH 09/12] fix(gpu-shader): eliminate all local/param arrays via shared static scratch buffers for FXC/HLSLcc --- .../Runtime/Gpu/Shaders/AstcGpuBise.hlsl | 104 +-- .../Runtime/Gpu/Shaders/AstcGpuColor.hlsl | 69 +- .../Runtime/Gpu/Shaders/AstcGpuCore.hlsl | 637 ++++++++---------- 3 files changed, 339 insertions(+), 471 deletions(-) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl index 8c4b3d5..1963b78 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl @@ -368,89 +368,11 @@ uint4 AstcReverse128(uint4 v) return uint4(reversebits(v.w), reversebits(v.z), reversebits(v.y), reversebits(v.x)); } -// Encodes up to 64 BISE symbols into a 128-bit block starting at startBit. -void AstcEncodeBise64(inout uint4 dst, uint startBit, uint q, uint count, uint vals[64]) -{ - uint rangeVal, b, t, qu; - AstcGetQuantParams(q, rangeVal, b, t, qu); - uint bitPos = startBit; - - if (t == 0u && qu == 0u) - { - [loop] for (uint i = 0u; i < count; ++i) - { - AstcWriteBits128(dst, bitPos, b, vals[i]); - bitPos += b; - } - } - else if (t != 0u) - { - uint lowMask = (b > 0u) ? ((1u << b) - 1u) : 0u; - [loop] for (uint i = 0u; i < count; i += 5u) - { - uint v0 = vals[i]; - uint v1 = (i + 1u < count) ? vals[i + 1u] : 0u; - uint v2 = (i + 2u < count) ? vals[i + 2u] : 0u; - uint v3 = (i + 3u < count) ? vals[i + 3u] : 0u; - uint v4 = (i + 4u < count) ? vals[i + 4u] : 0u; - - uint T = AstcPack5Trits(v0 >> b, v1 >> b, v2 >> b, v3 >> b, v4 >> b); - - AstcWriteBits128(dst, bitPos, b, v0 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 2u, T & 3u); bitPos += 2u; - - if (i + 1u < count) - { - AstcWriteBits128(dst, bitPos, b, v1 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 2u, (T >> 2u) & 3u); bitPos += 2u; - } - if (i + 2u < count) - { - AstcWriteBits128(dst, bitPos, b, v2 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 1u, (T >> 4u) & 1u); bitPos += 1u; - } - if (i + 3u < count) - { - AstcWriteBits128(dst, bitPos, b, v3 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 2u, (T >> 5u) & 3u); bitPos += 2u; - } - if (i + 4u < count) - { - AstcWriteBits128(dst, bitPos, b, v4 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 1u, (T >> 7u) & 1u); bitPos += 1u; - } - } - } - else - { - uint lowMask = (b > 0u) ? ((1u << b) - 1u) : 0u; - [loop] for (uint i = 0u; i < count; i += 3u) - { - uint v0 = vals[i]; - uint v1 = (i + 1u < count) ? vals[i + 1u] : 0u; - uint v2 = (i + 2u < count) ? vals[i + 2u] : 0u; - - uint Q = AstcPack3Quints(v0 >> b, v1 >> b, v2 >> b); - - AstcWriteBits128(dst, bitPos, b, v0 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 3u, Q & 7u); bitPos += 3u; - - if (i + 1u < count) - { - AstcWriteBits128(dst, bitPos, b, v1 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 2u, (Q >> 3u) & 3u); bitPos += 2u; - } - if (i + 2u < count) - { - AstcWriteBits128(dst, bitPos, b, v2 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 2u, (Q >> 5u) & 3u); bitPos += 2u; - } - } - } -} +// Shared per-thread BISE symbol scratch buffer (eliminates array-by-value copies in FXC/HLSLcc/DXC). +static uint g_AstcBiseVals[64]; -// Encodes up to 16 endpoint BISE symbols into a 128-bit block starting at startBit. -void AstcEncodeBise16(inout uint4 dst, uint startBit, uint q, uint count, uint vals[16]) +// Encodes up to 64 BISE symbols from g_AstcBiseVals[0..count-1] into a 128-bit block starting at startBit. +void AstcEncodeBise(inout uint4 dst, uint startBit, uint q, uint count) { uint rangeVal, b, t, qu; AstcGetQuantParams(q, rangeVal, b, t, qu); @@ -460,7 +382,7 @@ void AstcEncodeBise16(inout uint4 dst, uint startBit, uint q, uint count, uint v { [loop] for (uint i = 0u; i < count; ++i) { - AstcWriteBits128(dst, bitPos, b, vals[i]); + AstcWriteBits128(dst, bitPos, b, g_AstcBiseVals[i]); bitPos += b; } } @@ -469,11 +391,11 @@ void AstcEncodeBise16(inout uint4 dst, uint startBit, uint q, uint count, uint v uint lowMask = (b > 0u) ? ((1u << b) - 1u) : 0u; [loop] for (uint i = 0u; i < count; i += 5u) { - uint v0 = vals[i]; - uint v1 = (i + 1u < count) ? vals[i + 1u] : 0u; - uint v2 = (i + 2u < count) ? vals[i + 2u] : 0u; - uint v3 = (i + 3u < count) ? vals[i + 3u] : 0u; - uint v4 = (i + 4u < count) ? vals[i + 4u] : 0u; + uint v0 = g_AstcBiseVals[i]; + uint v1 = (i + 1u < count) ? g_AstcBiseVals[i + 1u] : 0u; + uint v2 = (i + 2u < count) ? g_AstcBiseVals[i + 2u] : 0u; + uint v3 = (i + 3u < count) ? g_AstcBiseVals[i + 3u] : 0u; + uint v4 = (i + 4u < count) ? g_AstcBiseVals[i + 4u] : 0u; uint T = AstcPack5Trits(v0 >> b, v1 >> b, v2 >> b, v3 >> b, v4 >> b); @@ -507,9 +429,9 @@ void AstcEncodeBise16(inout uint4 dst, uint startBit, uint q, uint count, uint v uint lowMask = (b > 0u) ? ((1u << b) - 1u) : 0u; [loop] for (uint i = 0u; i < count; i += 3u) { - uint v0 = vals[i]; - uint v1 = (i + 1u < count) ? vals[i + 1u] : 0u; - uint v2 = (i + 2u < count) ? vals[i + 2u] : 0u; + uint v0 = g_AstcBiseVals[i]; + uint v1 = (i + 1u < count) ? g_AstcBiseVals[i + 1u] : 0u; + uint v2 = (i + 2u < count) ? g_AstcBiseVals[i + 2u] : 0u; uint Q = AstcPack3Quints(v0 >> b, v1 >> b, v2 >> b); diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl index b4c14a0..88d27b1 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl @@ -95,16 +95,22 @@ uint AstcFloatToHalfU16(float x) // ASTC HDR Endpoint Quantization (CEM 11 / CEM 14 / CEM 15) // ============================================================================ +// Shared per-thread scratch arrays for HDR endpoint quantization (avoids per-call-site indexableTemp explosion in FXC). +static uint g_AstcHdrCodes[6]; +static uint g_AstcHdrRawV[6]; +static uint g_AstcHdrCandCodes[6]; +static uint g_AstcHdrUqV[6]; + // Exact Khronos ASTC Spec CEM 11 (HDR RGB Direct) hardware unpacker. -// Given 6 unquantized 8-bit endpoint values v[0..5], returns hardware-reconstructed LNS endpoints in [0, 65535]. -void AstcUnpackHdrRgb(uint v[6], out float3 outE0Lns, out float3 outE1Lns) +// Reads 6 unquantized 8-bit endpoint values from g_AstcHdrUqV[0..5], returns hardware-reconstructed LNS endpoints in [0, 65535]. +void AstcUnpackHdrRgb(out float3 outE0Lns, out float3 outE1Lns) { - uint v0 = v[0]; - uint v1 = v[1]; - uint v2 = v[2]; - uint v3 = v[3]; - uint v4 = v[4]; - uint v5 = v[5]; + uint v0 = g_AstcHdrUqV[0]; + uint v1 = g_AstcHdrUqV[1]; + uint v2 = g_AstcHdrUqV[2]; + uint v3 = g_AstcHdrUqV[3]; + uint v4 = g_AstcHdrUqV[4]; + uint v5 = g_AstcHdrUqV[5]; uint majcomp = ((v4 & 0x80u) >> 7u) | (((v5 & 0x80u) >> 7u) << 1u); if (majcomp == 3u) @@ -185,8 +191,8 @@ void AstcUnpackHdrRgb(uint v[6], out float3 outE0Lns, out float3 outE1Lns) outE1Lns = float3((float)(r1 << 4), (float)(g1 << 4), (float)(bl1 << 4)); } -// Packs CEM 11 delta mode fields into 6 8-bit endpoint values. -void AstcPackHdrRgbDeltaMode(uint majcomp, uint modeval, uint a, uint b0, uint b1, uint c, uint d0, uint d1, out uint v[6]) +// Packs CEM 11 delta mode fields into 6 8-bit endpoint values in g_AstcHdrRawV[0..5]. +void AstcPackHdrRgbDeltaMode(uint majcomp, uint modeval, uint a, uint b0, uint b1, uint c, uint d0, uint d1) { uint v0 = a & 0xFFu; uint v1 = ((modeval & 1u) << 7u) | (((a >> 8u) & 1u) << 6u) | (c & 0x3Fu); @@ -250,37 +256,37 @@ void AstcPackHdrRgbDeltaMode(uint majcomp, uint modeval, uint a, uint b0, uint b v4 |= (bit2 << 6u) | (bit4 << 5u); v5 |= (bit3 << 6u) | (bit5 << 5u); - v[0] = v0; v[1] = v1; v[2] = v2; v[3] = v3; v[4] = v4; v[5] = v5; + g_AstcHdrRawV[0] = v0; g_AstcHdrRawV[1] = v1; g_AstcHdrRawV[2] = v2; + g_AstcHdrRawV[3] = v3; g_AstcHdrRawV[4] = v4; g_AstcHdrRawV[5] = v5; } -// Encodes HDR RGB endpoints e0, e1 (in LNS [0, 65535]) into 6 BISE endpoint codes +// Encodes HDR RGB endpoints e0, e1 (in LNS [0, 65535]) into g_AstcHdrCodes[0..5] // using CEM_HDR_RGB_DIRECT (11): tries high-precision majcomp 0/1/2 delta modes (up to 12-bit) // and compares exact hardware-reconstructed LNS MSE against majcomp == 3 direct mode. void AstcQuantizeHdrRgbEndpoints( float3 e0Lns, float3 e1Lns, uint epQuant, - out uint codes[6], out float3 uq0Lns, out float3 uq1Lns) { // Baseline: majcomp == 3 direct mode (always valid) - uint baseVals[6]; - baseVals[0] = (uint)clamp(floor(e0Lns.r / 256.0 + 0.5), 0.0, 255.0); - baseVals[1] = (uint)clamp(floor(e1Lns.r / 256.0 + 0.5), 0.0, 255.0); - baseVals[2] = (uint)clamp(floor(e0Lns.g / 256.0 + 0.5), 0.0, 255.0); - baseVals[3] = (uint)clamp(floor(e1Lns.g / 256.0 + 0.5), 0.0, 255.0); - baseVals[4] = 128u + (uint)clamp(floor(e0Lns.b / 512.0 + 0.5), 0.0, 127.0); - baseVals[5] = 128u + (uint)clamp(floor(e1Lns.b / 512.0 + 0.5), 0.0, 127.0); - - uint baseUq[6]; + g_AstcHdrRawV[0] = (uint)clamp(floor(e0Lns.r / 256.0 + 0.5), 0.0, 255.0); + g_AstcHdrRawV[1] = (uint)clamp(floor(e1Lns.r / 256.0 + 0.5), 0.0, 255.0); + g_AstcHdrRawV[2] = (uint)clamp(floor(e0Lns.g / 256.0 + 0.5), 0.0, 255.0); + g_AstcHdrRawV[3] = (uint)clamp(floor(e1Lns.g / 256.0 + 0.5), 0.0, 255.0); + g_AstcHdrRawV[4] = 128u + (uint)clamp(floor(e0Lns.b / 512.0 + 0.5), 0.0, 127.0); + g_AstcHdrRawV[5] = 128u + (uint)clamp(floor(e1Lns.b / 512.0 + 0.5), 0.0, 127.0); + [loop] for (uint bi = 0u; bi < 6u; ++bi) { - codes[bi] = AstcQuantizeColorRange(baseVals[bi], epQuant, bi >= 4u, baseUq[bi]); + uint uqTmp; + g_AstcHdrCodes[bi] = AstcQuantizeColorRange(g_AstcHdrRawV[bi], epQuant, bi >= 4u, uqTmp); + g_AstcHdrUqV[bi] = uqTmp; } - uq0Lns = float3((float)(baseUq[0] << 8u), (float)(baseUq[2] << 8u), (float)((baseUq[4] & 0x7Fu) << 9u)); - uq1Lns = float3((float)(baseUq[1] << 8u), (float)(baseUq[3] << 8u), (float)((baseUq[5] & 0x7Fu) << 9u)); + uq0Lns = float3((float)(g_AstcHdrUqV[0] << 8u), (float)(g_AstcHdrUqV[2] << 8u), (float)((g_AstcHdrUqV[4] & 0x7Fu) << 9u)); + uq1Lns = float3((float)(g_AstcHdrUqV[1] << 8u), (float)(g_AstcHdrUqV[3] << 8u), (float)((g_AstcHdrUqV[5] & 0x7Fu) << 9u)); float3 d0 = uq0Lns - e0Lns; float3 d1 = uq1Lns - e1Lns; @@ -353,24 +359,23 @@ void AstcQuantizeHdrRgbEndpoints( && d1S >= dMin && d1S <= dMax) { uint dMask = (1u << db) - 1u; - uint rawV[6]; - AstcPackHdrRgbDeltaMode(majcomp, mv, (uint)aS, (uint)b0S, (uint)b1S, (uint)cS, ((uint)d0S) & dMask, ((uint)d1S) & dMask, rawV); + AstcPackHdrRgbDeltaMode(majcomp, mv, (uint)aS, (uint)b0S, (uint)b1S, (uint)cS, ((uint)d0S) & dMask, ((uint)d1S) & dMask); - uint candCodes[6]; - uint uqV[6]; [loop] for (uint ci = 0u; ci < 6u; ++ci) { - candCodes[ci] = AstcQuantizeColor(rawV[ci], epQuant, uqV[ci]); + uint uqTmp; + g_AstcHdrCandCodes[ci] = AstcQuantizeColor(g_AstcHdrRawV[ci], epQuant, uqTmp); + g_AstcHdrUqV[ci] = uqTmp; } float3 cand0, cand1; - AstcUnpackHdrRgb(uqV, cand0, cand1); + AstcUnpackHdrRgb(cand0, cand1); float3 cd0 = cand0 - e0Lns; float3 cd1 = cand1 - e1Lns; float candErr = dot(cd0, cd0) + dot(cd1, cd1); if (candErr < bestErr) { - [loop] for (uint cj = 0u; cj < 6u; ++cj) codes[cj] = candCodes[cj]; + [loop] for (uint cj = 0u; cj < 6u; ++cj) g_AstcHdrCodes[cj] = g_AstcHdrCandCodes[cj]; uq0Lns = cand0; uq1Lns = cand1; } diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl index bc87cb3..a6e7cba 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl @@ -46,6 +46,20 @@ struct AstcCandidate #define ASTC_MAX_GRID_U 36u #endif +// Shared per-thread scratch arrays: eliminates per-call-site local indexableTemp arrays +// and array-by-value copies across functions (prevents FXC/HLSLcc/DXC compile timeouts). +static float g_AstcGrid0[ASTC_MAX_GRID]; +static float g_AstcGrid1[ASTC_MAX_GRID]; +static float g_AstcDecimNum[ASTC_MAX_GRID]; +static float g_AstcDecimDen[ASTC_MAX_GRID]; +static uint g_AstcGridUq0[ASTC_MAX_GRID]; +static uint g_AstcGridUq1[ASTC_MAX_GRID]; +static uint g_AstcEpCodes[16]; +static uint g_AstcCandEpCodes[16]; +static uint g_AstcEpVals[8]; +static uint g_AstcEpCArr[8]; +static uint g_AstcEpUqArr[8]; + // On-chip per-thread packed RGBA8 pixel cache for small blocks (4x4, 5x5, 6x6 => 16..36 uints). // Eliminates redundant texture loads and sRGB pow() conversions across multi-pass candidate search // while keeping VGPR usage bounded for larger 8x8..12x12 blocks. @@ -208,13 +222,12 @@ float4 AstcChannelMaskEx(uint effMode, uint excludedChannel) } // Quantizes one partition's endpoints (e0, e1) according to effMode and epQuant. -// Writes codes into codesOut[codeOffset..] and returns exact hardware-reconstructed uqE0, uqE1. +// Writes codes into g_AstcEpCodes[codeOffset..] and returns exact hardware-reconstructed uqE0, uqE1. void AstcQuantizeEndpoints( float4 e0, float4 e1, uint effMode, uint epQuant, - inout uint codesOut[16], uint codeOffset, out float4 uqE0, out float4 uqE1) @@ -224,63 +237,62 @@ void AstcQuantizeEndpoints( if (effMode == ASTC_EFF_LDR_NORMAL) { - uint vals[4]; - vals[0] = (uint)clamp(floor(e0.x + 0.5), 0.0, 255.0); - vals[1] = (uint)clamp(floor(e1.x + 0.5), 0.0, 255.0); - vals[2] = (uint)clamp(floor(e0.w + 0.5), 0.0, 255.0); - vals[3] = (uint)clamp(floor(e1.w + 0.5), 0.0, 255.0); + g_AstcEpVals[0] = (uint)clamp(floor(e0.x + 0.5), 0.0, 255.0); + g_AstcEpVals[1] = (uint)clamp(floor(e1.x + 0.5), 0.0, 255.0); + g_AstcEpVals[2] = (uint)clamp(floor(e0.w + 0.5), 0.0, 255.0); + g_AstcEpVals[3] = (uint)clamp(floor(e1.w + 0.5), 0.0, 255.0); - uint uq[4]; [loop] for (uint i = 0u; i < 4u; ++i) { - codesOut[codeOffset + i] = AstcQuantizeColor(vals[i], epQuant, uq[i]); + uint uqTmp; + g_AstcEpCodes[codeOffset + i] = AstcQuantizeColor(g_AstcEpVals[i], epQuant, uqTmp); + g_AstcEpUqArr[i] = uqTmp; } - uqE0 = float4((float)uq[0], 0.0, 0.0, (float)uq[2]); - uqE1 = float4((float)uq[1], 0.0, 0.0, (float)uq[3]); + uqE0 = float4((float)g_AstcEpUqArr[0], 0.0, 0.0, (float)g_AstcEpUqArr[2]); + uqE1 = float4((float)g_AstcEpUqArr[1], 0.0, 0.0, (float)g_AstcEpUqArr[3]); return; } if (effMode == ASTC_EFF_LDR_RGB || effMode == ASTC_EFF_LDR_RGBA) { - uint vals[8]; - vals[0] = (uint)clamp(floor(e0.r + 0.5), 0.0, 255.0); - vals[1] = (uint)clamp(floor(e1.r + 0.5), 0.0, 255.0); - vals[2] = (uint)clamp(floor(e0.g + 0.5), 0.0, 255.0); - vals[3] = (uint)clamp(floor(e1.g + 0.5), 0.0, 255.0); - vals[4] = (uint)clamp(floor(e0.b + 0.5), 0.0, 255.0); - vals[5] = (uint)clamp(floor(e1.b + 0.5), 0.0, 255.0); - vals[6] = (uint)clamp(floor(e0.a + 0.5), 0.0, 255.0); - vals[7] = (uint)clamp(floor(e1.a + 0.5), 0.0, 255.0); + g_AstcEpVals[0] = (uint)clamp(floor(e0.r + 0.5), 0.0, 255.0); + g_AstcEpVals[1] = (uint)clamp(floor(e1.r + 0.5), 0.0, 255.0); + g_AstcEpVals[2] = (uint)clamp(floor(e0.g + 0.5), 0.0, 255.0); + g_AstcEpVals[3] = (uint)clamp(floor(e1.g + 0.5), 0.0, 255.0); + g_AstcEpVals[4] = (uint)clamp(floor(e0.b + 0.5), 0.0, 255.0); + g_AstcEpVals[5] = (uint)clamp(floor(e1.b + 0.5), 0.0, 255.0); + g_AstcEpVals[6] = (uint)clamp(floor(e0.a + 0.5), 0.0, 255.0); + g_AstcEpVals[7] = (uint)clamp(floor(e1.a + 0.5), 0.0, 255.0); - uint cArr[8]; - uint uqArr[8]; - [loop] for (uint z = 0u; z < 8u; ++z) { cArr[z] = 0u; uqArr[z] = 255u; } + [loop] for (uint z = 0u; z < 8u; ++z) { g_AstcEpCArr[z] = 0u; g_AstcEpUqArr[z] = 255u; } uint cnt = (effMode == ASTC_EFF_LDR_RGBA) ? 8u : 6u; [loop] for (uint i = 0u; i < cnt; ++i) { - cArr[i] = AstcQuantizeColor(vals[i], epQuant, uqArr[i]); + uint uqTmp; + g_AstcEpCArr[i] = AstcQuantizeColor(g_AstcEpVals[i], epQuant, uqTmp); + g_AstcEpUqArr[i] = uqTmp; } // Khronos ASTC spec: CEM 8/12 triggers blue-contraction & endpoint swap whenever (r1+g1+b1) < (r0+g0+b0). // Guarantee s1 >= s0 so hardware always decodes direct endpoints without unexpected contraction/inversion. - if ((uqArr[0] + uqArr[2] + uqArr[4]) > (uqArr[1] + uqArr[3] + uqArr[5])) + if ((g_AstcEpUqArr[0] + g_AstcEpUqArr[2] + g_AstcEpUqArr[4]) > (g_AstcEpUqArr[1] + g_AstcEpUqArr[3] + g_AstcEpUqArr[5])) { [loop] for (uint s = 0u; s < 8u; s += 2u) { - uint tc = cArr[s]; cArr[s] = cArr[s + 1u]; cArr[s + 1u] = tc; - uint tu = uqArr[s]; uqArr[s] = uqArr[s + 1u]; uqArr[s + 1u] = tu; + uint tc = g_AstcEpCArr[s]; g_AstcEpCArr[s] = g_AstcEpCArr[s + 1u]; g_AstcEpCArr[s + 1u] = tc; + uint tu = g_AstcEpUqArr[s]; g_AstcEpUqArr[s] = g_AstcEpUqArr[s + 1u]; g_AstcEpUqArr[s + 1u] = tu; } } [loop] for (uint k = 0u; k < cnt; ++k) { - codesOut[codeOffset + k] = cArr[k]; + g_AstcEpCodes[codeOffset + k] = g_AstcEpCArr[k]; } - uqE0 = float4((float)uqArr[0], (float)uqArr[2], (float)uqArr[4], (float)uqArr[6]); - uqE1 = float4((float)uqArr[1], (float)uqArr[3], (float)uqArr[5], (float)uqArr[7]); + uqE0 = float4((float)g_AstcEpUqArr[0], (float)g_AstcEpUqArr[2], (float)g_AstcEpUqArr[4], (float)g_AstcEpUqArr[6]); + uqE1 = float4((float)g_AstcEpUqArr[1], (float)g_AstcEpUqArr[3], (float)g_AstcEpUqArr[5], (float)g_AstcEpUqArr[7]); return; } @@ -293,15 +305,12 @@ void AstcQuantizeEndpoints( float4 tmp = e0; e0 = e1; e1 = tmp; } - uint rgbCodes[6]; float3 uqRgb0, uqRgb1; - AstcQuantizeHdrRgbEndpoints(e0.rgb, e1.rgb, epQuant, rgbCodes, uqRgb0, uqRgb1); - codesOut[codeOffset + 0u] = rgbCodes[0]; - codesOut[codeOffset + 1u] = rgbCodes[1]; - codesOut[codeOffset + 2u] = rgbCodes[2]; - codesOut[codeOffset + 3u] = rgbCodes[3]; - codesOut[codeOffset + 4u] = rgbCodes[4]; - codesOut[codeOffset + 5u] = rgbCodes[5]; + AstcQuantizeHdrRgbEndpoints(e0.rgb, e1.rgb, epQuant, uqRgb0, uqRgb1); + [loop] for (uint hi = 0u; hi < 6u; ++hi) + { + g_AstcEpCodes[codeOffset + hi] = g_AstcHdrCodes[hi]; + } uqE0 = float4(uqRgb0, 65535.0); uqE1 = float4(uqRgb1, 65535.0); @@ -311,8 +320,8 @@ void AstcQuantizeEndpoints( uint a0 = (uint)clamp(floor(e0.a / 257.0 + 0.5), 0.0, 255.0); uint a1 = (uint)clamp(floor(e1.a / 257.0 + 0.5), 0.0, 255.0); uint uqA0, uqA1; - codesOut[codeOffset + 6u] = AstcQuantizeColor(a0, epQuant, uqA0); - codesOut[codeOffset + 7u] = AstcQuantizeColor(a1, epQuant, uqA1); + g_AstcEpCodes[codeOffset + 6u] = AstcQuantizeColor(a0, epQuant, uqA0); + g_AstcEpCodes[codeOffset + 7u] = AstcQuantizeColor(a1, epQuant, uqA1); uqE0.a = (float)(uqA0 * 257u); uqE1.a = (float)(uqA1 * 257u); } @@ -321,8 +330,8 @@ void AstcQuantizeEndpoints( uint cA0, cA1; float uqA0, uqA1; AstcQuantizeHdrAlphaEndpoints(e0.a, e1.a, epQuant, cA0, cA1, uqA0, uqA1); - codesOut[codeOffset + 6u] = cA0; - codesOut[codeOffset + 7u] = cA1; + g_AstcEpCodes[codeOffset + 6u] = cA0; + g_AstcEpCodes[codeOffset + 7u] = cA1; uqE0.a = uqA0; uqE1.a = uqA1; } @@ -516,23 +525,20 @@ void AstcFitSubsetEndpoints( outE1 = e1; } -// Projects block pixels onto (e0, e1) and decimates onto a (gridW, gridH) weight grid. +// Projects block pixels onto (e0, e1) and decimates onto g_AstcGrid0 (gridW x gridH). void AstcComputeWeightGrid1P( uint baseX, uint baseY, uint effMode, uint excludedChannel, float4 e0, float4 e1, - uint gridW, uint gridH, - out float gridW01[ASTC_MAX_GRID]) + uint gridW, uint gridH) { uint gridCount = gridW * gridH; - float num[ASTC_MAX_GRID]; - float den[ASTC_MAX_GRID]; [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) { - gridW01[i] = 0.0; - num[i] = 0.0; - den[i] = 0.0; + g_AstcGrid0[i] = 0.0; + g_AstcDecimNum[i] = 0.0; + g_AstcDecimDen[i] = 0.0; } float4 mask = AstcChannelMaskEx(effMode, excludedChannel); @@ -553,7 +559,7 @@ void AstcComputeWeightGrid1P( if (exactGrid) { - gridW01[y * gridW + x] = t; + g_AstcGrid0[y * gridW + x] = t; } else { @@ -567,10 +573,10 @@ void AstcComputeWeightGrid1P( float f01 = (float)tw.z * decW; float f11 = (float)tw.w * decW; - num[idx00] += f00 * t; den[idx00] += f00; - if (tw.y > 0u) { num[idx00 + 1u] += f10 * t; den[idx00 + 1u] += f10; } - if (tw.z > 0u) { num[idx00 + gridW] += f01 * t; den[idx00 + gridW] += f01; } - if (tw.w > 0u) { num[idx00 + gridW + 1u] += f11 * t; den[idx00 + gridW + 1u] += f11; } + g_AstcDecimNum[idx00] += f00 * t; g_AstcDecimDen[idx00] += f00; + if (tw.y > 0u) { g_AstcDecimNum[idx00 + 1u] += f10 * t; g_AstcDecimDen[idx00 + 1u] += f10; } + if (tw.z > 0u) { g_AstcDecimNum[idx00 + gridW] += f01 * t; g_AstcDecimDen[idx00 + gridW] += f01; } + if (tw.w > 0u) { g_AstcDecimNum[idx00 + gridW + 1u] += f11 * t; g_AstcDecimDen[idx00 + gridW + 1u] += f11; } } } } @@ -579,28 +585,25 @@ void AstcComputeWeightGrid1P( { [loop] for (uint k = 0u; k < gridCount; ++k) { - gridW01[k] = (den[k] > 1e-6) ? saturate(num[k] / den[k]) : 0.0; + g_AstcGrid0[k] = (g_AstcDecimDen[k] > 1e-6) ? saturate(g_AstcDecimNum[k] / g_AstcDecimDen[k]) : 0.0; } } } -// Projects a single channel (plane 1, CCS in 0..3) onto [c0, c1] and decimates onto a (gridW, gridH) weight grid. +// Projects a single channel (plane 1, CCS in 0..3) onto [c0, c1] and decimates onto g_AstcGrid1 (gridW x gridH). void AstcComputePlane1WeightGrid( uint baseX, uint baseY, uint effMode, uint plane1Channel, float c0, float c1, - uint gridW, uint gridH, - out float gridW01[ASTC_MAX_GRID]) + uint gridW, uint gridH) { uint gridCount = gridW * gridH; - float num[ASTC_MAX_GRID]; - float den[ASTC_MAX_GRID]; [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) { - gridW01[i] = 0.0; - num[i] = 0.0; - den[i] = 0.0; + g_AstcGrid1[i] = 0.0; + g_AstcDecimNum[i] = 0.0; + g_AstcDecimDen[i] = 0.0; } float delta = c1 - c0; @@ -619,7 +622,7 @@ void AstcComputePlane1WeightGrid( if (exactGrid) { - gridW01[y * gridW + x] = t; + g_AstcGrid1[y * gridW + x] = t; } else { @@ -633,10 +636,10 @@ void AstcComputePlane1WeightGrid( float f01 = (float)tw.z * decW; float f11 = (float)tw.w * decW; - num[idx00] += f00 * t; den[idx00] += f00; - if (tw.y > 0u) { num[idx00 + 1u] += f10 * t; den[idx00 + 1u] += f10; } - if (tw.z > 0u) { num[idx00 + gridW] += f01 * t; den[idx00 + gridW] += f01; } - if (tw.w > 0u) { num[idx00 + gridW + 1u] += f11 * t; den[idx00 + gridW + 1u] += f11; } + g_AstcDecimNum[idx00] += f00 * t; g_AstcDecimDen[idx00] += f00; + if (tw.y > 0u) { g_AstcDecimNum[idx00 + 1u] += f10 * t; g_AstcDecimDen[idx00 + 1u] += f10; } + if (tw.z > 0u) { g_AstcDecimNum[idx00 + gridW] += f01 * t; g_AstcDecimDen[idx00 + gridW] += f01; } + if (tw.w > 0u) { g_AstcDecimNum[idx00 + gridW + 1u] += f11 * t; g_AstcDecimDen[idx00 + gridW + 1u] += f11; } } } } @@ -645,31 +648,28 @@ void AstcComputePlane1WeightGrid( { [loop] for (uint k = 0u; k < gridCount; ++k) { - gridW01[k] = (den[k] > 1e-6) ? saturate(num[k] / den[k]) : 0.0; + g_AstcGrid1[k] = (g_AstcDecimDen[k] > 1e-6) ? saturate(g_AstcDecimNum[k] / g_AstcDecimDen[k]) : 0.0; } } } #if !defined(ASTC_FAST_ONLY) -// Projects 2-partition block pixels onto partition 0/1 endpoints and decimates onto (gridW, gridH). +// Projects 2-partition block pixels onto partition 0/1 endpoints and decimates onto g_AstcGrid0 (gridW x gridH). void AstcComputeWeightGrid2P( uint baseX, uint baseY, uint effMode, uint partSeed, float4 e0_p0, float4 e1_p0, float4 e0_p1, float4 e1_p1, - uint gridW, uint gridH, - out float gridW01[ASTC_MAX_GRID]) + uint gridW, uint gridH) { uint gridCount = gridW * gridH; - float num[ASTC_MAX_GRID]; - float den[ASTC_MAX_GRID]; [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) { - gridW01[i] = 0.0; - num[i] = 0.0; - den[i] = 0.0; + g_AstcGrid0[i] = 0.0; + g_AstcDecimNum[i] = 0.0; + g_AstcDecimDen[i] = 0.0; } float4 mask = AstcChannelMaskEx(effMode, 4u); @@ -704,7 +704,7 @@ void AstcComputeWeightGrid2P( if (exactGrid) { - gridW01[y * gridW + x] = t; + g_AstcGrid0[y * gridW + x] = t; } else { @@ -717,10 +717,10 @@ void AstcComputeWeightGrid2P( float f01 = (float)tw.z; float f11 = (float)tw.w; - num[idx00] += f00 * t; den[idx00] += f00; - if (tw.y > 0u) { num[idx00 + 1u] += f10 * t; den[idx00 + 1u] += f10; } - if (tw.z > 0u) { num[idx00 + gridW] += f01 * t; den[idx00 + gridW] += f01; } - if (tw.w > 0u) { num[idx00 + gridW + 1u] += f11 * t; den[idx00 + gridW + 1u] += f11; } + g_AstcDecimNum[idx00] += f00 * t; g_AstcDecimDen[idx00] += f00; + if (tw.y > 0u) { g_AstcDecimNum[idx00 + 1u] += f10 * t; g_AstcDecimDen[idx00 + 1u] += f10; } + if (tw.z > 0u) { g_AstcDecimNum[idx00 + gridW] += f01 * t; g_AstcDecimDen[idx00 + gridW] += f01; } + if (tw.w > 0u) { g_AstcDecimNum[idx00 + gridW + 1u] += f11 * t; g_AstcDecimDen[idx00 + gridW + 1u] += f11; } } } } @@ -729,35 +729,31 @@ void AstcComputeWeightGrid2P( { [loop] for (uint k = 0u; k < gridCount; ++k) { - gridW01[k] = (den[k] > 1e-6) ? saturate(num[k] / den[k]) : 0.0; + g_AstcGrid0[k] = (g_AstcDecimDen[k] > 1e-6) ? saturate(g_AstcDecimNum[k] / g_AstcDecimDen[k]) : 0.0; } } } -// Evaluates exact hardware-reconstructed MSE for a 1-partition single-plane candidate using a precomputed weight grid. +// Evaluates exact hardware-reconstructed MSE for a 1-partition single-plane candidate using g_AstcGrid0. float AstcEvalError1PWithGrid( uint baseX, uint baseY, uint effMode, float4 e0, float4 e1, uint gridW, uint gridH, - float gridW01[ASTC_MAX_GRID], uint wQuant, uint epQuant, float bestError, out float4 outChanErr) { - uint epCodes[16]; - [loop] for (uint e = 0u; e < 16u; ++e) epCodes[e] = 0u; float4 uqE0, uqE1; - AstcQuantizeEndpoints(e0, e1, effMode, epQuant, epCodes, 0u, uqE0, uqE1); + AstcQuantizeEndpoints(e0, e1, effMode, epQuant, 0u, uqE0, uqE1); - uint gridUq[ASTC_MAX_GRID]; uint gridCount = gridW * gridH; - [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) gridUq[i] = 0u; + [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) g_AstcGridUq0[i] = 0u; [loop] for (uint k = 0u; k < gridCount; ++k) { uint uqW; - AstcQuantizeWeight(gridW01[k], wQuant, uqW); - gridUq[k] = uqW; + AstcQuantizeWeight(g_AstcGrid0[k], wQuant, uqW); + g_AstcGridUq0[k] = uqW; } float4 mask = AstcChannelMaskEx(effMode, 4u); @@ -770,7 +766,7 @@ float AstcEvalError1PWithGrid( float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); uint w64; - ASTC_EVAL_INFILL(gridUq, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w64); + ASTC_EVAL_INFILL(g_AstcGridUq0, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w64); float4 recon = lerp(uqE0, uqE1, (float)w64 * (1.0 / 64.0)); float4 diff = (p - recon) * mask; float4 chImp = AstcGetChannelImp(effMode, imp); @@ -779,39 +775,33 @@ float AstcEvalError1PWithGrid( totalErr += sq.r + sq.g + sq.b + sq.a; } if (totalErr >= bestError) - return totalErr; + break; } return totalErr; } -// Evaluates exact hardware-reconstructed error for a 1-partition dual-plane candidate using precomputed weight grids. +// Evaluates exact hardware-reconstructed error for a 1-partition dual-plane candidate using g_AstcGrid0/g_AstcGrid1. float AstcEvalErrorDualPlaneWithGrid( uint baseX, uint baseY, uint effMode, uint ccs, float4 e0, float4 e1, uint gridW, uint gridH, - float grid0W01[ASTC_MAX_GRID], - float grid1W01[ASTC_MAX_GRID], uint wQuant, uint epQuant, float bestError) { - uint epCodes[16]; - [loop] for (uint e = 0u; e < 16u; ++e) epCodes[e] = 0u; float4 uqE0, uqE1; - AstcQuantizeEndpoints(e0, e1, effMode, epQuant, epCodes, 0u, uqE0, uqE1); + AstcQuantizeEndpoints(e0, e1, effMode, epQuant, 0u, uqE0, uqE1); - uint grid0Uq[ASTC_MAX_GRID]; - uint grid1Uq[ASTC_MAX_GRID]; uint gridCount = gridW * gridH; - [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) { grid0Uq[i] = 0u; grid1Uq[i] = 0u; } + [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) { g_AstcGridUq0[i] = 0u; g_AstcGridUq1[i] = 0u; } [loop] for (uint k = 0u; k < gridCount; ++k) { uint uq0, uq1; - AstcQuantizeWeight(grid0W01[k], wQuant, uq0); - AstcQuantizeWeight(grid1W01[k], wQuant, uq1); - grid0Uq[k] = uq0; - grid1Uq[k] = uq1; + AstcQuantizeWeight(g_AstcGrid0[k], wQuant, uq0); + AstcQuantizeWeight(g_AstcGrid1[k], wQuant, uq1); + g_AstcGridUq0[k] = uq0; + g_AstcGridUq1[k] = uq1; } float4 mask = AstcChannelMaskEx(effMode, 4u); @@ -824,8 +814,8 @@ float AstcEvalErrorDualPlaneWithGrid( float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); uint w0; uint w1; - ASTC_EVAL_INFILL(grid0Uq, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0); - ASTC_EVAL_INFILL(grid1Uq, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1); + ASTC_EVAL_INFILL(g_AstcGridUq0, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0); + ASTC_EVAL_INFILL(g_AstcGridUq1, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1); float4 recon = lerp(uqE0, uqE1, (float)w0 * (1.0 / 64.0)); float p1Val = lerp(AstcGetChannel(uqE0, ccs), AstcGetChannel(uqE1, ccs), (float)w1 * (1.0 / 64.0)); recon = AstcSetChannel(recon, ccs, p1Val); @@ -852,12 +842,12 @@ float AstcEvalErrorDualPlaneWithGrid( } } if (totalErr >= bestError) - return totalErr; + break; } return totalErr; } -// Evaluates exact hardware-reconstructed MSE for a 2-partition candidate using a precomputed weight grid and hoisted partition hash. +// Evaluates exact hardware-reconstructed MSE for a 2-partition candidate using g_AstcGrid0 and hoisted partition hash. float AstcEvalError2PWithGrid( uint baseX, uint baseY, uint effMode, @@ -865,25 +855,28 @@ float AstcEvalError2PWithGrid( float4 e0_p0, float4 e1_p0, float4 e0_p1, float4 e1_p1, uint gridW, uint gridH, - float gridW01[ASTC_MAX_GRID], uint wQuant, uint epQuant, float bestError) { - uint epCodes[16]; - [loop] for (uint e = 0u; e < 16u; ++e) epCodes[e] = 0u; uint epPerPart = AstcGetEpCountForEffMode(effMode); - float4 uq0_p0, uq1_p0, uq0_p1, uq1_p1; - AstcQuantizeEndpoints(e0_p0, e1_p0, effMode, epQuant, epCodes, 0u, uq0_p0, uq1_p0); - AstcQuantizeEndpoints(e0_p1, e1_p1, effMode, epQuant, epCodes, epPerPart, uq0_p1, uq1_p1); + float4 uq0_p0 = 0.0, uq1_p0 = 0.0, uq0_p1 = 0.0, uq1_p1 = 0.0; + [loop] for (uint pi = 0u; pi < 2u; ++pi) + { + float4 te0 = (pi == 0u) ? e0_p0 : e0_p1; + float4 te1 = (pi == 0u) ? e1_p0 : e1_p1; + float4 uq0, uq1; + AstcQuantizeEndpoints(te0, te1, effMode, epQuant, pi * epPerPart, uq0, uq1); + if (pi == 0u) { uq0_p0 = uq0; uq1_p0 = uq1; } + else { uq0_p1 = uq0; uq1_p1 = uq1; } + } - uint gridUq[ASTC_MAX_GRID]; uint gridCount = gridW * gridH; - [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) gridUq[i] = 0u; + [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) g_AstcGridUq0[i] = 0u; [loop] for (uint k = 0u; k < gridCount; ++k) { uint uqW; - AstcQuantizeWeight(gridW01[k], wQuant, uqW); - gridUq[k] = uqW; + AstcQuantizeWeight(g_AstcGrid0[k], wQuant, uqW); + g_AstcGridUq0[k] = uqW; } float4 mask = AstcChannelMaskEx(effMode, 4u); @@ -896,7 +889,7 @@ float AstcEvalError2PWithGrid( float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); uint part = AstcEvalPartition2(p2, x, y); uint w64; - ASTC_EVAL_INFILL(gridUq, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w64); + ASTC_EVAL_INFILL(g_AstcGridUq0, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w64); float4 recon = (part == 0u) ? lerp(uq0_p0, uq1_p0, (float)w64 * (1.0 / 64.0)) : lerp(uq0_p1, uq1_p1, (float)w64 * (1.0 / 64.0)); @@ -911,7 +904,7 @@ float AstcEvalError2PWithGrid( } } if (totalErr >= bestError) - return totalErr; + break; } return totalErr; } @@ -1034,10 +1027,8 @@ void AstcRefitEndpoints1P( uint baseX, uint baseY, uint effMode, uint gridW, uint gridH, - uint gridUq[ASTC_MAX_GRID], uint epQuant, - float4 curUqE0, float4 curUqE1, - inout uint epCodes[16]) + float4 curUqE0, float4 curUqE1) { float4 mask = AstcChannelMaskEx(effMode, 4u); float origErr = 0.0; @@ -1053,7 +1044,7 @@ void AstcRefitEndpoints1P( float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); float4 chImp = AstcGetChannelImp(effMode, imp); uint w64; - ASTC_EVAL_INFILL(gridUq, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w64); + ASTC_EVAL_INFILL(g_AstcGridUq0, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w64); float w = (float)w64 * (1.0 / 64.0); float omw = 1.0 - w; @@ -1099,10 +1090,9 @@ void AstcRefitEndpoints1P( if (!changed) return; - uint candCodes[16]; - [loop] for (uint e = 0u; e < 16u; ++e) candCodes[e] = 0u; + [loop] for (uint sv = 0u; sv < 8u; ++sv) g_AstcCandEpCodes[sv] = g_AstcEpCodes[sv]; float4 uq0, uq1; - AstcQuantizeEndpoints(refitE0, refitE1, effMode, epQuant, candCodes, 0u, uq0, uq1); + AstcQuantizeEndpoints(refitE0, refitE1, effMode, epQuant, 0u, uq0, uq1); float refitErr = 0.0; [loop] for (uint y2 = 0u; y2 < BLOCK_Y; ++y2) @@ -1113,16 +1103,16 @@ void AstcRefitEndpoints1P( float4 p = AstcFetchWorkingPixel(baseX, baseY, x2, y2, effMode, imp); float4 chImp = AstcGetChannelImp(effMode, imp); uint w64; - ASTC_EVAL_INFILL(gridUq, x2, y2, BLOCK_X, BLOCK_Y, gridW, gridH, w64); + ASTC_EVAL_INFILL(g_AstcGridUq0, x2, y2, BLOCK_X, BLOCK_Y, gridW, gridH, w64); float4 recon = lerp(uq0, uq1, (float)w64 * (1.0 / 64.0)); float4 diff = (p - recon) * mask; refitErr += dot(diff * diff, chImp); } } - if (refitErr < origErr) + if (refitErr >= origErr) { - [loop] for (uint ei = 0u; ei < 8u; ++ei) epCodes[ei] = candCodes[ei]; + [loop] for (uint ei = 0u; ei < 8u; ++ei) g_AstcEpCodes[ei] = g_AstcCandEpCodes[ei]; } } @@ -1132,11 +1122,8 @@ void AstcRefitEndpointsDualPlane( uint effMode, uint ccs, uint gridW, uint gridH, - uint grid0Uq[ASTC_MAX_GRID], - uint grid1Uq[ASTC_MAX_GRID], uint epQuant, - float4 curUqE0, float4 curUqE1, - inout uint epCodes[16]) + float4 curUqE0, float4 curUqE1) { float4 mask = AstcChannelMaskEx(effMode, 4u); float origErr = 0.0; @@ -1155,8 +1142,8 @@ void AstcRefitEndpointsDualPlane( float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); float4 chImp = AstcGetChannelImp(effMode, imp); uint w0_64, w1_64; - ASTC_EVAL_INFILL(grid0Uq, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64); - ASTC_EVAL_INFILL(grid1Uq, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); + ASTC_EVAL_INFILL(g_AstcGridUq0, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64); + ASTC_EVAL_INFILL(g_AstcGridUq1, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); float w0 = (float)w0_64 * (1.0 / 64.0); float w1 = (float)w1_64 * (1.0 / 64.0); float omw0 = 1.0 - w0; @@ -1237,10 +1224,9 @@ void AstcRefitEndpointsDualPlane( if (!changed) return; - uint candCodes[16]; - [loop] for (uint e = 0u; e < 16u; ++e) candCodes[e] = 0u; + [loop] for (uint sv = 0u; sv < 8u; ++sv) g_AstcCandEpCodes[sv] = g_AstcEpCodes[sv]; float4 uq0, uq1; - AstcQuantizeEndpoints(refitE0, refitE1, effMode, epQuant, candCodes, 0u, uq0, uq1); + AstcQuantizeEndpoints(refitE0, refitE1, effMode, epQuant, 0u, uq0, uq1); float refitErr = 0.0; [loop] for (uint y2 = 0u; y2 < BLOCK_Y; ++y2) @@ -1251,8 +1237,8 @@ void AstcRefitEndpointsDualPlane( float4 p = AstcFetchWorkingPixel(baseX, baseY, x2, y2, effMode, imp); float4 chImp = AstcGetChannelImp(effMode, imp); uint w0_64, w1_64; - ASTC_EVAL_INFILL(grid0Uq, x2, y2, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64); - ASTC_EVAL_INFILL(grid1Uq, x2, y2, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); + ASTC_EVAL_INFILL(g_AstcGridUq0, x2, y2, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64); + ASTC_EVAL_INFILL(g_AstcGridUq1, x2, y2, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); float4 recon = lerp(uq0, uq1, (float)w0_64 * (1.0 / 64.0)); recon = AstcSetChannel(recon, ccs, lerp(AstcGetChannel(uq0, ccs), AstcGetChannel(uq1, ccs), (float)w1_64 * (1.0 / 64.0))); @@ -1272,9 +1258,9 @@ void AstcRefitEndpointsDualPlane( } } - if (refitErr < origErr) + if (refitErr >= origErr) { - [loop] for (uint ei = 0u; ei < 8u; ++ei) epCodes[ei] = candCodes[ei]; + [loop] for (uint ei = 0u; ei < 8u; ++ei) g_AstcEpCodes[ei] = g_AstcCandEpCodes[ei]; } } @@ -1283,56 +1269,60 @@ void AstcRefitEndpointsDualPlane( // Assembles the final winning AstcCandidate into a 128-bit physical ASTC block. uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) { - uint epCodes[16]; - [loop] for (uint e = 0u; e < 16u; ++e) epCodes[e] = 0u; + [loop] for (uint e = 0u; e < 16u; ++e) g_AstcEpCodes[e] = 0u; + [loop] for (uint wi = 0u; wi < 64u; ++wi) g_AstcBiseVals[wi] = 0u; uint epPerPart = AstcGetEpCountForEffMode(effMode); uint cem = AstcGetCemForEffMode(effMode); - uint wCodes[64]; - [loop] for (uint wi = 0u; wi < 64u; ++wi) wCodes[wi] = 0u; uint gridCount = c.gridW * c.gridH; uint totalWeights = gridCount; - float4 uqE0, uqE1; - AstcQuantizeEndpoints(c.e0, c.e1, effMode, c.epQuant, epCodes, 0u, uqE0, uqE1); + float4 uqE0 = 0.0, uqE1 = 0.0; + float4 uq0_p1 = 0.0, uq1_p1 = 0.0; +#if !defined(ASTC_FAST_ONLY) + uint partCnt = (c.isTwoPart != 0u) ? 2u : 1u; + [loop] for (uint pi = 0u; pi < partCnt; ++pi) + { + float4 te0 = (pi == 0u) ? c.e0 : c.e0_p1; + float4 te1 = (pi == 0u) ? c.e1 : c.e1_p1; + float4 tu0, tu1; + AstcQuantizeEndpoints(te0, te1, effMode, c.epQuant, pi * epPerPart, tu0, tu1); + if (pi == 0u) { uqE0 = tu0; uqE1 = tu1; } + else { uq0_p1 = tu0; uq1_p1 = tu1; } + } +#else + AstcQuantizeEndpoints(c.e0, c.e1, effMode, c.epQuant, 0u, uqE0, uqE1); +#endif #if !defined(ASTC_FAST_ONLY) if (c.isTwoPart != 0u) { - float4 uq0_p1, uq1_p1; - AstcQuantizeEndpoints(c.e0_p1, c.e1_p1, effMode, c.epQuant, epCodes, epPerPart, uq0_p1, uq1_p1); - - float gridW01[ASTC_MAX_GRID]; - AstcComputeWeightGrid2P(baseX, baseY, effMode, c.partSeed, uqE0, uqE1, uq0_p1, uq1_p1, c.gridW, c.gridH, gridW01); + AstcComputeWeightGrid2P(baseX, baseY, effMode, c.partSeed, uqE0, uqE1, uq0_p1, uq1_p1, c.gridW, c.gridH); [loop] for (uint k = 0u; k < gridCount; ++k) { uint dummyUq; - wCodes[k] = AstcQuantizeWeight(gridW01[k], c.wQuant, dummyUq); + g_AstcBiseVals[k] = AstcQuantizeWeight(g_AstcGrid0[k], c.wQuant, dummyUq); } } else #endif if (c.isDualPlane != 0u) { - float grid0W01[ASTC_MAX_GRID]; - float grid1W01[ASTC_MAX_GRID]; - AstcComputeWeightGrid1P(baseX, baseY, effMode, c.dualPlaneChannel, uqE0, uqE1, c.gridW, c.gridH, grid0W01); - AstcComputePlane1WeightGrid(baseX, baseY, effMode, c.dualPlaneChannel, AstcGetChannel(uqE0, c.dualPlaneChannel), AstcGetChannel(uqE1, c.dualPlaneChannel), c.gridW, c.gridH, grid1W01); + AstcComputeWeightGrid1P(baseX, baseY, effMode, c.dualPlaneChannel, uqE0, uqE1, c.gridW, c.gridH); + AstcComputePlane1WeightGrid(baseX, baseY, effMode, c.dualPlaneChannel, AstcGetChannel(uqE0, c.dualPlaneChannel), AstcGetChannel(uqE1, c.dualPlaneChannel), c.gridW, c.gridH); #if !defined(ASTC_FAST_ONLY) - uint grid0Uq[ASTC_MAX_GRID]; - uint grid1Uq[ASTC_MAX_GRID]; - [loop] for (uint zi = 0u; zi < ASTC_MAX_GRID_U; ++zi) { grid0Uq[zi] = 0u; grid1Uq[zi] = 0u; } + [loop] for (uint zi = 0u; zi < ASTC_MAX_GRID_U; ++zi) { g_AstcGridUq0[zi] = 0u; g_AstcGridUq1[zi] = 0u; } #endif [loop] for (uint k = 0u; k < gridCount; ++k) { uint uq0, uq1; - wCodes[2u * k] = AstcQuantizeWeight(grid0W01[k], c.wQuant, uq0); - wCodes[2u * k + 1u] = AstcQuantizeWeight(grid1W01[k], c.wQuant, uq1); + g_AstcBiseVals[2u * k] = AstcQuantizeWeight(g_AstcGrid0[k], c.wQuant, uq0); + g_AstcBiseVals[2u * k + 1u] = AstcQuantizeWeight(g_AstcGrid1[k], c.wQuant, uq1); #if !defined(ASTC_FAST_ONLY) - grid0Uq[k] = uq0; - grid1Uq[k] = uq1; + g_AstcGridUq0[k] = uq0; + g_AstcGridUq1[k] = uq1; #endif } totalWeights = gridCount * 2u; @@ -1340,39 +1330,37 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) #if !defined(ASTC_FAST_ONLY) if (_AstcQuality >= 1u) { - AstcRefitEndpointsDualPlane(baseX, baseY, effMode, c.dualPlaneChannel, c.gridW, c.gridH, grid0Uq, grid1Uq, c.epQuant, uqE0, uqE1, epCodes); + AstcRefitEndpointsDualPlane(baseX, baseY, effMode, c.dualPlaneChannel, c.gridW, c.gridH, c.epQuant, uqE0, uqE1); } #endif } else { - float gridW01[ASTC_MAX_GRID]; - AstcComputeWeightGrid1P(baseX, baseY, effMode, 4u, uqE0, uqE1, c.gridW, c.gridH, gridW01); + AstcComputeWeightGrid1P(baseX, baseY, effMode, 4u, uqE0, uqE1, c.gridW, c.gridH); #if !defined(ASTC_FAST_ONLY) - uint gridUq[ASTC_MAX_GRID]; - [loop] for (uint zi = 0u; zi < ASTC_MAX_GRID_U; ++zi) gridUq[zi] = 0u; + [loop] for (uint zi = 0u; zi < ASTC_MAX_GRID_U; ++zi) g_AstcGridUq0[zi] = 0u; #endif [loop] for (uint k = 0u; k < gridCount; ++k) { uint uqW; - wCodes[k] = AstcQuantizeWeight(gridW01[k], c.wQuant, uqW); + g_AstcBiseVals[k] = AstcQuantizeWeight(g_AstcGrid0[k], c.wQuant, uqW); #if !defined(ASTC_FAST_ONLY) - gridUq[k] = uqW; + g_AstcGridUq0[k] = uqW; #endif } #if !defined(ASTC_FAST_ONLY) if (_AstcQuality >= 1u) { - AstcRefitEndpoints1P(baseX, baseY, effMode, c.gridW, c.gridH, gridUq, c.epQuant, uqE0, uqE1, epCodes); + AstcRefitEndpoints1P(baseX, baseY, effMode, c.gridW, c.gridH, c.epQuant, uqE0, uqE1); } #endif } // Encode weights at bit 0 into wBuf, then reverse all 128 bits so weights sit at MSB descending. uint4 wBuf = uint4(0u, 0u, 0u, 0u); - AstcEncodeBise64(wBuf, 0u, c.wQuant, totalWeights, wCodes); + AstcEncodeBise(wBuf, 0u, c.wQuant, totalWeights); uint4 block = AstcReverse128(wBuf); // Header: BlockMode (11 bits) + PartitionCount-1 (2 bits) @@ -1400,7 +1388,8 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) AstcWriteBits128(block, 128u - weightBits - 2u, 2u, c.dualPlaneChannel); } } - AstcEncodeBise16(block, epStartBit, c.epQuant, epTotalCount, epCodes); + [loop] for (uint ei = 0u; ei < epTotalCount; ++ei) g_AstcBiseVals[ei] = g_AstcEpCodes[ei]; + AstcEncodeBise(block, epStartBit, c.epQuant, epTotalCount); return block; } @@ -1411,13 +1400,12 @@ void AstcTryCandidate1PWithGrid( uint baseX, uint baseY, uint effMode, float4 e0, float4 e1, uint bm, uint gw, uint gh, - float gridW01[ASTC_MAX_GRID], uint wQ, uint epQ, inout AstcCandidate best, inout float4 bestChanErr) { float4 chanErr; - float err = AstcEvalError1PWithGrid(baseX, baseY, effMode, e0, e1, gw, gh, gridW01, wQ, epQ, best.error, chanErr); + float err = AstcEvalError1PWithGrid(baseX, baseY, effMode, e0, e1, gw, gh, wQ, epQ, best.error, chanErr); if (err < best.error) { best.blockMode = bm; @@ -1441,12 +1429,10 @@ void AstcTryCandidateDualPlaneWithGrid( uint ccs, float4 e0, float4 e1, uint bm, uint gw, uint gh, - float grid0W01[ASTC_MAX_GRID], - float grid1W01[ASTC_MAX_GRID], uint wQ, uint epQ, inout AstcCandidate best) { - float err = AstcEvalErrorDualPlaneWithGrid(baseX, baseY, effMode, ccs, e0, e1, gw, gh, grid0W01, grid1W01, wQ, epQ, best.error); + float err = AstcEvalErrorDualPlaneWithGrid(baseX, baseY, effMode, ccs, e0, e1, gw, gh, wQ, epQ, best.error); if (err < best.error) { best.blockMode = bm; @@ -1471,11 +1457,10 @@ void AstcTryCandidate2PWithGrid( float4 e0_p0, float4 e1_p0, float4 e0_p1, float4 e1_p1, uint bm, uint gw, uint gh, - float gridW01[ASTC_MAX_GRID], uint wQ, uint epQ, inout AstcCandidate best) { - float err = AstcEvalError2PWithGrid(baseX, baseY, effMode, p2, e0_p0, e1_p0, e0_p1, e1_p1, gw, gh, gridW01, wQ, epQ, best.error); + float err = AstcEvalError2PWithGrid(baseX, baseY, effMode, p2, e0_p0, e1_p0, e0_p1, e1_p1, gw, gh, wQ, epQ, best.error); if (err < best.error) { best.blockMode = bm; @@ -1774,88 +1759,52 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) best.e0_p1 = 0.0; best.e1_p1 = 0.0; -#if !defined(ASTC_FAST_ONLY) - float workGrid0[ASTC_MAX_GRID]; - float workGrid1[ASTC_MAX_GRID]; - [loop] for (uint zw = 0u; zw < ASTC_MAX_GRID_U; ++zw) { workGrid0[zw] = 0.0; workGrid1[zw] = 0.0; } -#endif - - if (effMode == ASTC_EFF_LDR_NORMAL) - { - float4 nE0, nE1; - AstcFitSubsetEndpoints(baseX, baseY, effMode, 3u, 0u, 2u, 0u, nE0, nE1); + bool isNormal = (effMode == ASTC_EFF_LDR_NORMAL); + bool hasAlpha = (effMode == ASTC_EFF_LDR_RGBA || effMode == ASTC_EFF_HDR_RGB_LDR_ALPHA || effMode == ASTC_EFF_HDR_RGBA); #if defined(ASTC_FAST_ONLY) - best.blockMode = 0x452u; - best.gridW = 4u; - best.gridH = 4u; - best.wQuant = ASTC_QUANT_5; - best.epQuant = ASTC_QUANT_256; - best.isDualPlane = 1u; - best.dualPlaneChannel = 3u; - best.e0 = nE0; - best.e1 = nE1; -#else - if (_AstcQuality == 0u) - { - best.blockMode = 0x452u; - best.gridW = 4u; - best.gridH = 4u; - best.wQuant = ASTC_QUANT_5; - best.epQuant = ASTC_QUANT_256; - best.isDualPlane = 1u; - best.dualPlaneChannel = 3u; - best.e0 = nE0; - best.e1 = nE1; - } - else - { - // Balanced / High: compare Dual-Plane 4x4 (and 5x5 on >=5x5 blocks) reusing projections per grid -#if BLOCK_X >= 5 - uint normCandCount = 4u; + uint refineIters = 0u; #else - uint normCandCount = 2u; + uint refineIters = (!isNormal && _AstcQuality >= 1u) ? 2u : 0u; #endif - uint normCachedG = 0u; - [loop] for (uint ni = 0u; ni < normCandCount; ++ni) - { - uint4 cfg = (ni == 0u) ? uint4(0x452u, 4u, ASTC_QUANT_5, ASTC_QUANT_256) - : (ni == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_256) - : (ni == 2u) ? uint4(0x4F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_128) - : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_256); - if (cfg.y != normCachedG) - { - AstcComputeWeightGrid1P(baseX, baseY, effMode, 3u, nE0, nE1, cfg.y, cfg.y, workGrid0); - AstcComputePlane1WeightGrid(baseX, baseY, effMode, 3u, nE0.a, nE1.a, cfg.y, cfg.y, workGrid1); - normCachedG = cfg.y; - } - AstcTryCandidateDualPlaneWithGrid(baseX, baseY, effMode, 3u, nE0, nE1, cfg.x, cfg.y, cfg.y, workGrid0, workGrid1, cfg.z, cfg.w, best); - } - } + float4 e0, e1; + AstcFitSubsetEndpoints(baseX, baseY, effMode, isNormal ? 3u : 4u, 0u, 2u, refineIters, e0, e1); + +#if defined(ASTC_FAST_ONLY) + best.e0 = e0; + best.e1 = e1; + if (isNormal) + { + best.blockMode = 0x452u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_5; best.epQuant = ASTC_QUANT_256; + best.isDualPlane = 1u; best.dualPlaneChannel = 3u; + } + else if (!hasAlpha) + { +#if BLOCK_X == 4 + best.blockMode = 0x251u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_12; best.epQuant = ASTC_QUANT_256; +#else + best.blockMode = 0x0E3u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_6; best.epQuant = ASTC_QUANT_192; #endif } else { - // ------------------------------------------------------------------------ - // Step 4: Color / HDR 1-Partition Endpoint Fitting - // ------------------------------------------------------------------------ -#if defined(ASTC_FAST_ONLY) - uint refineIters = 0u; +#if BLOCK_X == 4 + best.blockMode = 0x053u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_8; best.epQuant = ASTC_QUANT_192; #else - uint refineIters = (_AstcQuality >= 1u) ? 2u : 0u; + best.blockMode = 0x0E2u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_4; best.epQuant = ASTC_QUANT_192; #endif - float4 e0, e1; - AstcFitSubsetEndpoints(baseX, baseY, effMode, 4u, 0u, 2u, refineIters, e0, e1); - - bool hasAlpha = (effMode == ASTC_EFF_LDR_RGBA || effMode == ASTC_EFF_HDR_RGB_LDR_ALPHA || effMode == ASTC_EFF_HDR_RGBA); - - // ------------------------------------------------------------------------ - // Step 5: Fast Path (ASTC_FAST_ONLY or Quality == 0) - Single optimal mode - // ------------------------------------------------------------------------ -#if defined(ASTC_FAST_ONLY) + } +#else + if (_AstcQuality == 0u) + { best.e0 = e0; best.e1 = e1; - if (!hasAlpha) + if (isNormal) + { + best.blockMode = 0x452u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_5; best.epQuant = ASTC_QUANT_256; + best.isDualPlane = 1u; best.dualPlaneChannel = 3u; + } + else if (!hasAlpha) { #if BLOCK_X == 4 best.blockMode = 0x251u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_12; best.epQuant = ASTC_QUANT_256; @@ -1871,29 +1820,11 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) best.blockMode = 0x0E2u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_4; best.epQuant = ASTC_QUANT_192; #endif } -#else - if (_AstcQuality == 0u) - { - best.e0 = e0; - best.e1 = e1; - if (!hasAlpha) - { -#if BLOCK_X == 4 - best.blockMode = 0x251u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_12; best.epQuant = ASTC_QUANT_256; -#else - best.blockMode = 0x0E3u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_6; best.epQuant = ASTC_QUANT_192; -#endif - } - else - { -#if BLOCK_X == 4 - best.blockMode = 0x053u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_8; best.epQuant = ASTC_QUANT_192; -#else - best.blockMode = 0x0E2u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_4; best.epQuant = ASTC_QUANT_192; -#endif - } - } - else + } + else + { + float4 bestChanErr = 0.0; + if (!isNormal) { // ------------------------------------------------------------------------ // Step 6: Balanced / High Path - Multi-BlockMode Candidate Search (reusing projections per grid) @@ -1923,8 +1854,6 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) } uint cachedGW = 0u, cachedGH = 0u; - float4 bestChanErr = 0.0; - [loop] for (uint c1 = 0u; c1 < cand1PCount; ++c1) { uint bm, gw, gh, wQ, epQ; @@ -1956,99 +1885,111 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) } if (gw != cachedGW || gh != cachedGH) { - AstcComputeWeightGrid1P(baseX, baseY, effMode, 4u, e0, e1, gw, gh, workGrid0); + AstcComputeWeightGrid1P(baseX, baseY, effMode, 4u, e0, e1, gw, gh); cachedGW = gw; cachedGH = gh; } - AstcTryCandidate1PWithGrid(baseX, baseY, effMode, e0, e1, bm, gw, gh, workGrid0, wQ, epQ, best, bestChanErr); + AstcTryCandidate1PWithGrid(baseX, baseY, effMode, e0, e1, bm, gw, gh, wQ, epQ, best, bestChanErr); } + } - // Dual-Plane with dynamic CCS selection directly from bestChanErr (zero extra texture scan!) + // Unified Dual-Plane candidate search (Normal Map GGGR CCS=3, or Color/HDR dynamic CCS from bestChanErr) + { + uint bestCcs = 3u; + float4 dpE0 = e0; + float4 dpE1 = e1; + if (!isNormal) { - uint bestCcs = 0u; + bestCcs = 0u; float maxChErr = bestChanErr.r; if (bestChanErr.g > maxChErr) { maxChErr = bestChanErr.g; bestCcs = 1u; } if (bestChanErr.b > maxChErr) { maxChErr = bestChanErr.b; bestCcs = 2u; } if (hasAlpha && bestChanErr.a > maxChErr) { bestCcs = 3u; } - - float4 dpE0, dpE1; AstcFitSubsetEndpoints(baseX, baseY, effMode, bestCcs, 0u, 2u, 1u, dpE0, dpE1); + } + #if BLOCK_X >= 5 - uint dpCount = 3u; + uint dpCount = isNormal ? 4u : 3u; #else - uint dpCount = 2u; + uint dpCount = 2u; #endif - uint dpCachedG = 0u; - [loop] for (uint di = 0u; di < dpCount; ++di) + uint dpCachedG = 0u; + [loop] for (uint di = 0u; di < dpCount; ++di) + { + uint4 cfg; + if (isNormal) + { + cfg = (di == 0u) ? uint4(0x452u, 4u, ASTC_QUANT_5, ASTC_QUANT_256) + : (di == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_256) + : (di == 2u) ? uint4(0x4F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_128) + : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_256); + } + else if (!hasAlpha) + { + cfg = (di == 0u) ? uint4(0x451u, 4u, ASTC_QUANT_3, ASTC_QUANT_256) + : (di == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_160) + : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_256); + } + else { - uint4 cfg; - if (!hasAlpha) - { - cfg = (di == 0u) ? uint4(0x451u, 4u, ASTC_QUANT_3, ASTC_QUANT_256) - : (di == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_160) - : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_256); - } - else - { - cfg = (di == 0u) ? uint4(0x451u, 4u, ASTC_QUANT_3, ASTC_QUANT_128) - : (di == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_48) - : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_160); - } - if (cfg.y != dpCachedG) - { - AstcComputeWeightGrid1P(baseX, baseY, effMode, bestCcs, dpE0, dpE1, cfg.y, cfg.y, workGrid0); - AstcComputePlane1WeightGrid(baseX, baseY, effMode, bestCcs, AstcGetChannel(dpE0, bestCcs), AstcGetChannel(dpE1, bestCcs), cfg.y, cfg.y, workGrid1); - dpCachedG = cfg.y; - } - AstcTryCandidateDualPlaneWithGrid(baseX, baseY, effMode, bestCcs, dpE0, dpE1, cfg.x, cfg.y, cfg.y, workGrid0, workGrid1, cfg.z, cfg.w, best); + cfg = (di == 0u) ? uint4(0x451u, 4u, ASTC_QUANT_3, ASTC_QUANT_128) + : (di == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_48) + : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_160); } + if (cfg.y != dpCachedG) + { + AstcComputeWeightGrid1P(baseX, baseY, effMode, bestCcs, dpE0, dpE1, cfg.y, cfg.y); + AstcComputePlane1WeightGrid(baseX, baseY, effMode, bestCcs, AstcGetChannel(dpE0, bestCcs), AstcGetChannel(dpE1, bestCcs), cfg.y, cfg.y); + dpCachedG = cfg.y; + } + AstcTryCandidateDualPlaneWithGrid(baseX, baseY, effMode, bestCcs, dpE0, dpE1, cfg.x, cfg.y, cfg.y, cfg.z, cfg.w, best); } + } - // ------------------------------------------------------------------------ - // Step 7: High Quality Path (Quality >= 2) - Merged 2-Partition Directional Search - // ------------------------------------------------------------------------ - if (_AstcQuality >= 2u) - { - bool smallBlock = (BLOCK_X * BLOCK_Y) < 32u; - uint bestSeed = AstcSelectBestSeed2P(baseX, baseY, effMode); - AstcPart2Params p2Params = AstcPreparePartition2(bestSeed, smallBlock); - float4 e0_p0, e1_p0, e0_p1, e1_p1; - AstcFitBothPartitions2P(baseX, baseY, effMode, p2Params, e0_p0, e1_p0, e0_p1, e1_p1); + // ------------------------------------------------------------------------ + // Step 7: High Quality Path (Quality >= 2) - Merged 2-Partition Directional Search + // ------------------------------------------------------------------------ + if (!isNormal && _AstcQuality >= 2u) + { + bool smallBlock = (BLOCK_X * BLOCK_Y) < 32u; + uint bestSeed = AstcSelectBestSeed2P(baseX, baseY, effMode); + AstcPart2Params p2Params = AstcPreparePartition2(bestSeed, smallBlock); + float4 e0_p0, e1_p0, e0_p1, e1_p1; + AstcFitBothPartitions2P(baseX, baseY, effMode, p2Params, e0_p0, e1_p0, e0_p1, e1_p1); #if BLOCK_X >= 5 - uint p2Count = 4u; + uint p2Count = 4u; #else - uint p2Count = 2u; + uint p2Count = 2u; #endif - uint p2CachedG = 0u; - [loop] for (uint p2 = 0u; p2 < p2Count; ++p2) + uint p2CachedG = 0u; + [loop] for (uint p2 = 0u; p2 < p2Count; ++p2) + { + uint4 cfg; + if (!hasAlpha) + { + if (p2 == 0u) cfg = uint4(0x042u, 4u, ASTC_QUANT_4, ASTC_QUANT_40); + else if (p2 == 1u) cfg = uint4(0x043u, 4u, ASTC_QUANT_6, ASTC_QUANT_24); + else if (p2 == 2u) cfg = uint4(0x0F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_24); + else cfg = uint4(0x0E2u, 5u, ASTC_QUANT_4, ASTC_QUANT_16); + } + else + { + if (p2 == 0u) cfg = uint4(0x051u, 4u, ASTC_QUANT_3, ASTC_QUANT_20); + else if (p2 == 1u) cfg = uint4(0x042u, 4u, ASTC_QUANT_4, ASTC_QUANT_16); + else if (p2 == 2u) cfg = uint4(0x0E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_24); + else cfg = uint4(0x0F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_12); + } + if (cfg.y != p2CachedG) { - uint4 cfg; - if (!hasAlpha) - { - if (p2 == 0u) cfg = uint4(0x042u, 4u, ASTC_QUANT_4, ASTC_QUANT_40); - else if (p2 == 1u) cfg = uint4(0x043u, 4u, ASTC_QUANT_6, ASTC_QUANT_24); - else if (p2 == 2u) cfg = uint4(0x0F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_24); - else cfg = uint4(0x0E2u, 5u, ASTC_QUANT_4, ASTC_QUANT_16); - } - else - { - if (p2 == 0u) cfg = uint4(0x051u, 4u, ASTC_QUANT_3, ASTC_QUANT_20); - else if (p2 == 1u) cfg = uint4(0x042u, 4u, ASTC_QUANT_4, ASTC_QUANT_16); - else if (p2 == 2u) cfg = uint4(0x0E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_24); - else cfg = uint4(0x0F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_12); - } - if (cfg.y != p2CachedG) - { - AstcComputeWeightGrid2P(baseX, baseY, effMode, bestSeed, e0_p0, e1_p0, e0_p1, e1_p1, cfg.y, cfg.y, workGrid0); - p2CachedG = cfg.y; - } - AstcTryCandidate2PWithGrid(baseX, baseY, effMode, bestSeed, p2Params, e0_p0, e1_p0, e0_p1, e1_p1, cfg.x, cfg.y, cfg.y, workGrid0, cfg.z, cfg.w, best); + AstcComputeWeightGrid2P(baseX, baseY, effMode, bestSeed, e0_p0, e1_p0, e0_p1, e1_p1, cfg.y, cfg.y); + p2CachedG = cfg.y; } + AstcTryCandidate2PWithGrid(baseX, baseY, effMode, bestSeed, p2Params, e0_p0, e1_p0, e0_p1, e1_p1, cfg.x, cfg.y, cfg.y, cfg.z, cfg.w, best); } } -#endif // !ASTC_FAST_ONLY } +#endif // !ASTC_FAST_ONLY return AstcAssembleBlock(baseX, baseY, effMode, best); } From 3120e0f7056b9f2385d8efcca6dedc6799d82c99 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Sat, 28 Nov 2026 07:05:37 +0000 Subject: [PATCH 10/12] perf(gpu-shader): cut DXIL basic blocks by 75% via branchless LNS/sRGB/BISE, unified 3-stage search, and 4x16b pixel cache --- .../Runtime/Gpu/Shaders/AstcGpuBise.hlsl | 111 +-- .../Runtime/Gpu/Shaders/AstcGpuColor.hlsl | 89 +- .../Runtime/Gpu/Shaders/AstcGpuCore.hlsl | 906 +++++++----------- 3 files changed, 421 insertions(+), 685 deletions(-) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl index 1963b78..a4337a2 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl @@ -166,39 +166,43 @@ uint AstcUnquantizeColor(uint v, uint q) if (t == 0u && qu == 0u) { - if (b == 8u) return v; - if (b == 7u) return (v << 1u) | (v >> 6u); - if (b == 6u) return (v << 2u) | (v >> 4u); - if (b == 5u) return (v << 3u) | (v >> 2u); - if (b == 4u) return (v << 4u) | v; - if (b == 3u) return (v << 5u) | (v << 2u) | (v >> 1u); - if (b == 2u) return v * 85u; - return v * 255u; + uint r = v << (8u - b); + if (b < 8u) r |= v >> max((int)b * 2 - 8, 0); + // Exact bit replication for b = 1..8: + // b=8: v, b=7: (v<<1)|(v>>6), b=6: (v<<2)|(v>>4), b=5: (v<<3)|(v>>2), + // b=4: (v<<4)|v, b=3: (v<<5)|(v<<2)|(v>>1), b=2: v*85, b=1: v*255 + if (b <= 3u) + { + r = (b == 3u) ? ((v << 5u) | (v << 2u) | (v >> 1u)) : ((b == 2u) ? (v * 85u) : (v * 255u)); + } + return r & 0xFFu; } uint D = v >> b; uint low = v & ((1u << b) - 1u); uint A = ((low & 1u) != 0u) ? 0x1FFu : 0u; - uint B = 0u; - uint C = 0u; uint m = low >> 1u; + uint C = 0u; + uint B = 0u; if (t != 0u) { - if (b == 1u) { C = 204u; B = 0u; } - else if (b == 2u) { C = 93u; B = (m << 8u) | (m << 4u) | (m << 2u) | (m << 1u); } - else if (b == 3u) { C = 44u; B = (m << 7u) | (m << 2u) | m; } - else if (b == 4u) { C = 22u; B = (m << 6u) | m; } - else if (b == 5u) { C = 11u; B = (m << 5u) | (m >> 2u); } - else { C = 5u; B = (m << 4u) | (m >> 4u); } + C = (b == 1u) ? 204u : ((b == 2u) ? 93u : ((b == 3u) ? 44u : ((b == 4u) ? 22u : ((b == 5u) ? 11u : 5u)))); + B = (b == 1u) ? 0u + : (b == 2u) ? ((m << 8u) | (m << 4u) | (m << 2u) | (m << 1u)) + : (b == 3u) ? ((m << 7u) | (m << 2u) | m) + : (b == 4u) ? ((m << 6u) | m) + : (b == 5u) ? ((m << 5u) | (m >> 2u)) + : ((m << 4u) | (m >> 4u)); } else { - if (b == 1u) { C = 113u; B = 0u; } - else if (b == 2u) { C = 54u; B = (m << 8u) | (m << 3u) | (m << 2u); } - else if (b == 3u) { C = 26u; B = (m << 7u) | (m << 1u) | (m >> 1u); } - else if (b == 4u) { C = 13u; B = (m << 6u) | (m >> 1u); } - else { C = 6u; B = (m << 5u) | (m >> 3u); } + C = (b == 1u) ? 113u : ((b == 2u) ? 54u : ((b == 3u) ? 26u : ((b == 4u) ? 13u : 6u))); + B = (b == 1u) ? 0u + : (b == 2u) ? ((m << 8u) | (m << 3u) | (m << 2u)) + : (b == 3u) ? ((m << 7u) | (m << 1u) | (m >> 1u)) + : (b == 4u) ? ((m << 6u) | (m >> 1u)) + : ((m << 5u) | (m >> 3u)); } uint T = (D * C + B) ^ A; @@ -343,23 +347,22 @@ uint AstcQuantizeWeight(float w01, uint q, out uint unquantOut) return bestCode; } -// Writes up to 24 bits into a 128-bit block (little-endian uint4). +// Writes up to 24 bits into a 128-bit block (little-endian uint4), branchless. void AstcWriteBits128(inout uint4 blk, uint bitPos, uint numBits, uint val) { - if (numBits == 0u) return; - uint mask = (numBits >= 32u) ? 0xFFFFFFFFu : ((1u << numBits) - 1u); + uint mask = (numBits == 0u) ? 0u : ((numBits >= 32u) ? 0xFFFFFFFFu : ((1u << numBits) - 1u)); val &= mask; uint wordIdx = bitPos >> 5u; uint bitOff = bitPos & 31u; uint lo = val << bitOff; - uint hi = (bitOff > 0u) ? (val >> (32u - bitOff)) : 0u; + uint hi = (bitOff > 0u && (bitOff + numBits > 32u)) ? (val >> (32u - bitOff)) : 0u; - if (wordIdx == 0u) { blk.x |= lo; if (bitOff + numBits > 32u) blk.y |= hi; } - else if (wordIdx == 1u) { blk.y |= lo; if (bitOff + numBits > 32u) blk.z |= hi; } - else if (wordIdx == 2u) { blk.z |= lo; if (bitOff + numBits > 32u) blk.w |= hi; } - else { blk.w |= lo; } + blk.x |= (wordIdx == 0u) ? lo : 0u; + blk.y |= ((wordIdx == 1u) ? lo : 0u) | ((wordIdx == 0u) ? hi : 0u); + blk.z |= ((wordIdx == 2u) ? lo : 0u) | ((wordIdx == 1u) ? hi : 0u); + blk.w |= ((wordIdx == 3u) ? lo : 0u) | ((wordIdx == 2u) ? hi : 0u); } // Reverses all 128 bits of a uint4 (places BISE weight stream at MSB descending). @@ -399,28 +402,16 @@ void AstcEncodeBise(inout uint4 dst, uint startBit, uint q, uint count) uint T = AstcPack5Trits(v0 >> b, v1 >> b, v2 >> b, v3 >> b, v4 >> b); - AstcWriteBits128(dst, bitPos, b, v0 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 2u, T & 3u); bitPos += 2u; - - if (i + 1u < count) - { - AstcWriteBits128(dst, bitPos, b, v1 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 2u, (T >> 2u) & 3u); bitPos += 2u; - } - if (i + 2u < count) - { - AstcWriteBits128(dst, bitPos, b, v2 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 1u, (T >> 4u) & 1u); bitPos += 1u; - } - if (i + 3u < count) - { - AstcWriteBits128(dst, bitPos, b, v3 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 2u, (T >> 5u) & 3u); bitPos += 2u; - } - if (i + 4u < count) + uint tOff = 0u; + [loop] for (uint j = 0u; j < 5u; ++j) { - AstcWriteBits128(dst, bitPos, b, v4 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 1u, (T >> 7u) & 1u); bitPos += 1u; + if (i + j >= count) break; + uint vj = g_AstcBiseVals[i + j]; + uint tBits = (j == 2u || j == 4u) ? 1u : 2u; + uint tMask = (tBits == 1u) ? 1u : 3u; + AstcWriteBits128(dst, bitPos, b, vj & lowMask); bitPos += b; + AstcWriteBits128(dst, bitPos, tBits, (T >> tOff) & tMask); bitPos += tBits; + tOff += tBits; } } } @@ -435,18 +426,16 @@ void AstcEncodeBise(inout uint4 dst, uint startBit, uint q, uint count) uint Q = AstcPack3Quints(v0 >> b, v1 >> b, v2 >> b); - AstcWriteBits128(dst, bitPos, b, v0 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 3u, Q & 7u); bitPos += 3u; - - if (i + 1u < count) - { - AstcWriteBits128(dst, bitPos, b, v1 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 2u, (Q >> 3u) & 3u); bitPos += 2u; - } - if (i + 2u < count) + uint qOff = 0u; + [loop] for (uint j = 0u; j < 3u; ++j) { - AstcWriteBits128(dst, bitPos, b, v2 & lowMask); bitPos += b; - AstcWriteBits128(dst, bitPos, 2u, (Q >> 5u) & 3u); bitPos += 2u; + if (i + j >= count) break; + uint vj = g_AstcBiseVals[i + j]; + uint qBits = (j == 0u) ? 3u : 2u; + uint qMask = (qBits == 3u) ? 7u : 3u; + AstcWriteBits128(dst, bitPos, b, vj & lowMask); bitPos += b; + AstcWriteBits128(dst, bitPos, qBits, (Q >> qOff) & qMask); bitPos += qBits; + qOff += qBits; } } } diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl index 88d27b1..0e3ff79 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl @@ -10,78 +10,47 @@ float AstcLinearToSRGBScalar(float c) { c = saturate(c); - if (c <= 0.0031308) - return c * 12.92; - // Fast sqrt-based minimax approximation (max error < 0.24/255, 0/256 8-bit round-trip mismatches, zero pow()). float s1 = sqrt(c); float s2 = sqrt(s1); float s3 = sqrt(s2); - return saturate(0.662002687 * s1 + 0.684122060 * s2 - 0.323583601 * s3 - 0.0225411470 * c); + float hi = saturate(0.662002687 * s1 + 0.684122060 * s2 - 0.323583601 * s3 - 0.0225411470 * c); + return (c <= 0.0031308) ? (c * 12.92) : hi; } float3 AstcLinearToSRGB(float3 rgb) { - return float3( - AstcLinearToSRGBScalar(rgb.r), - AstcLinearToSRGBScalar(rgb.g), - AstcLinearToSRGBScalar(rgb.b)); + float3 c = saturate(rgb); + float3 s1 = sqrt(c); + float3 s2 = sqrt(s1); + float3 s3 = sqrt(s2); + float3 hi = saturate(0.662002687 * s1 + 0.684122060 * s2 - 0.323583601 * s3 - 0.0225411470 * c); + return lerp(hi, c * 12.92, float3(c <= 0.0031308)); } -// Exact Khronos ASTC float -> 16-bit LNS [0, 65535] conversion. +// Exact Khronos ASTC float4 -> 16-bit LNS [0, 65535] conversion (branchless vectorized). // Negative values and NaNs clamp to 0; values >= 65536 clamp to 65535. -float AstcFloatToLns(float a) -{ - if (!(a > (1.0 / 67108864.0))) - { - return 0.0; - } - if (a >= 65536.0) - { - return 65535.0; - } - - uint ai = asuint(a); - int expVal = (int)((ai >> 23u) & 0xFFu) - 126; - uint mantBits = (ai & 0x807FFFFFu) | 0x3F000000u; - float mant = asfloat(mantBits); - - float scaled; - int lnsExp; - if (expVal < -13) - { - scaled = a * 33554432.0; - lnsExp = 0; - } - else - { - scaled = (mant - 0.5) * 4096.0; - lnsExp = expVal + 14; - } - - float lnsMant; - if (scaled < 384.0) - { - lnsMant = scaled * (4.0 / 3.0); - } - else if (scaled <= 1408.0) - { - lnsMant = scaled + 128.0; - } - else - { - lnsMant = (scaled + 512.0) * (4.0 / 5.0); - } - - return clamp(lnsMant + (float)lnsExp * 2048.0 + 1.0, 0.0, 65535.0); -} - float4 AstcFloatToLns4(float4 c) { - return float4( - AstcFloatToLns(c.r), - AstcFloatToLns(c.g), - AstcFloatToLns(c.b), - AstcFloatToLns(c.a)); + float4 validPos = float4(c > (1.0 / 67108864.0)); + float4 tooLarge = float4(c >= 65536.0); + float4 safeC = lerp(float4(1.0, 1.0, 1.0, 1.0), min(c, 65536.0), validPos); + + uint4 ai = asuint(safeC); + int4 expVal = (int4)((ai >> 23u) & 0xFFu) - 126; + uint4 mantBits = (ai & 0x807FFFFFu) | 0x3F000000u; + float4 mant = asfloat(mantBits); + + float4 subnorm = float4(expVal < -13); + float4 scaled = lerp((mant - 0.5) * 4096.0, safeC * 33554432.0, subnorm); + float4 lnsExp = (1.0 - subnorm) * (float4)(expVal + 14); + + float4 isLo = float4(scaled < 384.0); + float4 isMid = float4(scaled <= 1408.0); + float4 lnsMant = lerp(lerp((scaled + 512.0) * (4.0 / 5.0), scaled + 128.0, isMid), scaled * (4.0 / 3.0), isLo); + + float4 res = clamp(lnsMant + lnsExp * 2048.0 + 1.0, 0.0, 65535.0); + res = lerp(res, float4(65535.0, 65535.0, 65535.0, 65535.0), tooLarge); + return res * validPos; } uint AstcFloatToHalfU16(float x) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl index a6e7cba..d8b2f1b 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl @@ -60,91 +60,81 @@ static uint g_AstcEpVals[8]; static uint g_AstcEpCArr[8]; static uint g_AstcEpUqArr[8]; -// On-chip per-thread packed RGBA8 pixel cache for small blocks (4x4, 5x5, 6x6 => 16..36 uints). -// Eliminates redundant texture loads and sRGB pow() conversions across multi-pass candidate search +// On-chip per-thread packed 4x16-bit working-pixel cache for small blocks (4x4, 5x5, 6x6 => 16..36 uint2s). +// Eliminates redundant texture loads, sRGB, and HDR LNS conversions across all 29 fetch call sites // while keeping VGPR usage bounded for larger 8x8..12x12 blocks. #if (BLOCK_X * BLOCK_Y) <= 36 #define ASTC_ENABLE_PIXEL_CACHE 1 -static uint g_AstcPackedBlock[BLOCK_X * BLOCK_Y]; -static uint g_AstcBlockCacheValid; +static uint2 g_AstcPackedBlock[BLOCK_X * BLOCK_Y]; -uint AstcPackRgba8(float r255, float g255, float b255, float a255) +uint2 AstcPackRgba16(float4 v) { - uint r = (uint)clamp(floor(r255 + 0.5), 0.0, 255.0); - uint g = (uint)clamp(floor(g255 + 0.5), 0.0, 255.0); - uint b = (uint)clamp(floor(b255 + 0.5), 0.0, 255.0); - uint a = (uint)clamp(floor(a255 + 0.5), 0.0, 255.0); - return r | (g << 8u) | (b << 16u) | (a << 24u); + uint4 u = (uint4)clamp(floor(v + 0.5), 0.0, 65535.0); + return uint2(u.x | (u.y << 16u), u.z | (u.w << 16u)); } #else #define ASTC_ENABLE_PIXEL_CACHE 0 #endif -// Fetches one texel in the encoder's working space for the current effective mode. -float4 AstcFetchWorkingPixel(uint baseX, uint baseY, uint x, uint y, uint effMode, out float impWeight) +// Loads and converts one texel from _AstcSourceTex into working space (single shared sRGB + LNS path). +float4 AstcLoadWorkingPixel(uint baseX, uint baseY, uint x, uint y, uint effMode, out float impWeight) { impWeight = 1.0; -#if ASTC_ENABLE_PIXEL_CACHE - if (g_AstcBlockCacheValid != 0u) - { - uint packed = g_AstcPackedBlock[y * BLOCK_X + x]; - float4 cached = float4( - (float)(packed & 0xFFu), - (float)((packed >> 8u) & 0xFFu), - (float)((packed >> 16u) & 0xFFu), - (float)((packed >> 24u) & 0xFFu)); - if (effMode == ASTC_EFF_LDR_RGBA && _AstcUseAlphaWeight != 0u) - { - impWeight = max(cached.a * (1.0 / 255.0), 0.02); - } - return cached; - } -#endif - uint px = min(baseX + x, _AstcSrcWidth - 1u); uint py = min(baseY + y, _AstcSrcHeight - 1u); float4 raw = _AstcSourceTex.Load(int3(px, py, _AstcMipLevel)); - if (effMode == ASTC_EFF_LDR_NORMAL) - { - // _AstcIsSRGB means the source view decodes sRGB on load; re-encode to recover the stored bytes - // because normal data is always emitted to a linear (UNorm) ASTC target. - float nr = (_AstcIsSRGB != 0u) ? AstcLinearToSRGBScalar(raw.r) : saturate(raw.r); - float ng = (_AstcIsSRGB != 0u) ? AstcLinearToSRGBScalar(raw.g) : saturate(raw.g); - return float4(ng * 255.0, 0.0, 0.0, nr * 255.0); - } - - if (effMode == ASTC_EFF_LDR_RGB) - { - float3 rgb = (_AstcIsSRGB != 0u) ? AstcLinearToSRGB(raw.rgb) : saturate(raw.rgb); - return float4(rgb * 255.0, 255.0); - } - - if (effMode == ASTC_EFF_LDR_RGBA) + if (effMode <= ASTC_EFF_LDR_NORMAL) { float3 rgb = (_AstcIsSRGB != 0u) ? AstcLinearToSRGB(raw.rgb) : saturate(raw.rgb); - float a = saturate(raw.a); - if (_AstcUseAlphaWeight != 0u) + if (effMode == ASTC_EFF_LDR_NORMAL) + { + return float4(rgb.g * 255.0, 0.0, 0.0, rgb.r * 255.0); + } + float a = (effMode == ASTC_EFF_LDR_RGB) ? 1.0 : saturate(raw.a); + if (effMode == ASTC_EFF_LDR_RGBA && _AstcUseAlphaWeight != 0u) { impWeight = max(a, 0.02); } return float4(rgb * 255.0, a * 255.0); } + // HDR modes: share a single branchless vectorized LNS conversion + float4 lns = AstcFloatToLns4(raw); if (effMode == ASTC_EFF_HDR_RGB) { - return float4(AstcFloatToLns(raw.r), AstcFloatToLns(raw.g), AstcFloatToLns(raw.b), 65535.0); + return float4(lns.rgb, 65535.0); } - if (effMode == ASTC_EFF_HDR_RGB_LDR_ALPHA) { // Scale LDR alpha [0,1] into [0, 65535] to balance vector norm with LNS RGB. - return float4(AstcFloatToLns(raw.r), AstcFloatToLns(raw.g), AstcFloatToLns(raw.b), saturate(raw.a) * 65535.0); + return float4(lns.rgb, saturate(raw.a) * 65535.0); } // ASTC_EFF_HDR_RGBA - return AstcFloatToLns4(raw); + return lns; +} + +// Fetches one texel in the encoder's working space for the current effective mode. +float4 AstcFetchWorkingPixel(uint baseX, uint baseY, uint x, uint y, uint effMode, out float impWeight) +{ +#if ASTC_ENABLE_PIXEL_CACHE + impWeight = 1.0; + uint2 packed = g_AstcPackedBlock[y * BLOCK_X + x]; + float4 cached = float4( + (float)(packed.x & 0xFFFFu), + (float)(packed.x >> 16u), + (float)(packed.y & 0xFFFFu), + (float)(packed.y >> 16u)); + if (effMode == ASTC_EFF_LDR_RGBA && _AstcUseAlphaWeight != 0u) + { + impWeight = max(cached.a * (1.0 / 255.0), 0.02); + } + return cached; +#else + return AstcLoadWorkingPixel(baseX, baseY, x, y, effMode, impWeight); +#endif } // Per-channel importance weight: weights RGB by alpha when UseAlphaWeight is active, @@ -315,25 +305,24 @@ void AstcQuantizeEndpoints( uqE0 = float4(uqRgb0, 65535.0); uqE1 = float4(uqRgb1, 65535.0); - if (effMode == ASTC_EFF_HDR_RGB_LDR_ALPHA) - { - uint a0 = (uint)clamp(floor(e0.a / 257.0 + 0.5), 0.0, 255.0); - uint a1 = (uint)clamp(floor(e1.a / 257.0 + 0.5), 0.0, 255.0); - uint uqA0, uqA1; - g_AstcEpCodes[codeOffset + 6u] = AstcQuantizeColor(a0, epQuant, uqA0); - g_AstcEpCodes[codeOffset + 7u] = AstcQuantizeColor(a1, epQuant, uqA1); - uqE0.a = (float)(uqA0 * 257u); - uqE1.a = (float)(uqA1 * 257u); - } - else if (effMode == ASTC_EFF_HDR_RGBA) - { - uint cA0, cA1; - float uqA0, uqA1; - AstcQuantizeHdrAlphaEndpoints(e0.a, e1.a, epQuant, cA0, cA1, uqA0, uqA1); - g_AstcEpCodes[codeOffset + 6u] = cA0; - g_AstcEpCodes[codeOffset + 7u] = cA1; - uqE0.a = uqA0; - uqE1.a = uqA1; + if (effMode == ASTC_EFF_HDR_RGB_LDR_ALPHA || effMode == ASTC_EFF_HDR_RGBA) + { + bool isHdrA = (effMode == ASTC_EFF_HDR_RGBA); + uint aIn0 = isHdrA + ? (128u + (uint)clamp(floor(e0.a / 512.0 + 0.5), 0.0, 127.0)) + : (uint)clamp(floor(e0.a / 257.0 + 0.5), 0.0, 255.0); + uint aIn1 = isHdrA + ? (128u + (uint)clamp(floor(e1.a / 512.0 + 0.5), 0.0, 127.0)) + : (uint)clamp(floor(e1.a / 257.0 + 0.5), 0.0, 255.0); + uint uqA0 = 0u, uqA1 = 0u; + [loop] for (uint ai = 0u; ai < 2u; ++ai) + { + uint uqTmp; + g_AstcEpCodes[codeOffset + 6u + ai] = AstcQuantizeColorRange((ai == 0u) ? aIn0 : aIn1, epQuant, isHdrA, uqTmp); + if (ai == 0u) uqA0 = uqTmp; else uqA1 = uqTmp; + } + uqE0.a = isHdrA ? (float)((uqA0 & 0x7Fu) << 9u) : (float)(uqA0 * 257u); + uqE1.a = isHdrA ? (float)((uqA1 & 0x7Fu) << 9u) : (float)(uqA1 * 257u); } } @@ -734,78 +723,59 @@ void AstcComputeWeightGrid2P( } } -// Evaluates exact hardware-reconstructed MSE for a 1-partition single-plane candidate using g_AstcGrid0. -float AstcEvalError1PWithGrid( +// Unified exact hardware-reconstructed candidate error evaluator for 1P (stage=0), Dual-Plane (stage=1), and 2P (stage=2). +float AstcEvalCandidateError( uint baseX, uint baseY, uint effMode, - float4 e0, float4 e1, + uint stage, + uint ccs, + AstcPart2Params p2, + float4 e0_p0, float4 e1_p0, + float4 e0_p1, float4 e1_p1, uint gridW, uint gridH, uint wQuant, uint epQuant, float bestError, out float4 outChanErr) { - float4 uqE0, uqE1; - AstcQuantizeEndpoints(e0, e1, effMode, epQuant, 0u, uqE0, uqE1); + bool isDual = (stage == 1u); + bool isTwoP = (stage == 2u); + uint epPerPart = AstcGetEpCountForEffMode(effMode); + uint partCnt = isTwoP ? 2u : 1u; - uint gridCount = gridW * gridH; - [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) g_AstcGridUq0[i] = 0u; - [loop] for (uint k = 0u; k < gridCount; ++k) + float4 uq0_p0 = 0.0, uq1_p0 = 0.0, uq0_p1 = 0.0, uq1_p1 = 0.0; + [loop] for (uint pi = 0u; pi < partCnt; ++pi) { - uint uqW; - AstcQuantizeWeight(g_AstcGrid0[k], wQuant, uqW); - g_AstcGridUq0[k] = uqW; + float4 te0 = (pi == 0u) ? e0_p0 : e0_p1; + float4 te1 = (pi == 0u) ? e1_p0 : e1_p1; + float4 uq0, uq1; + AstcQuantizeEndpoints(te0, te1, effMode, epQuant, pi * epPerPart, uq0, uq1); + if (pi == 0u) { uq0_p0 = uq0; uq1_p0 = uq1; } + else { uq0_p1 = uq0; uq1_p1 = uq1; } } - float4 mask = AstcChannelMaskEx(effMode, 4u); - float totalErr = 0.0; - outChanErr = 0.0; - [loop] for (uint y = 0u; y < BLOCK_Y; ++y) + uint gridCount = gridW * gridH; + [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) { - [loop] for (uint x = 0u; x < BLOCK_X; ++x) - { - float imp; - float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); - uint w64; - ASTC_EVAL_INFILL(g_AstcGridUq0, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w64); - float4 recon = lerp(uqE0, uqE1, (float)w64 * (1.0 / 64.0)); - float4 diff = (p - recon) * mask; - float4 chImp = AstcGetChannelImp(effMode, imp); - float4 sq = diff * diff * chImp; - outChanErr += sq; - totalErr += sq.r + sq.g + sq.b + sq.a; - } - if (totalErr >= bestError) - break; + g_AstcGridUq0[i] = 0u; + if (isDual) g_AstcGridUq1[i] = 0u; } - return totalErr; -} - -// Evaluates exact hardware-reconstructed error for a 1-partition dual-plane candidate using g_AstcGrid0/g_AstcGrid1. -float AstcEvalErrorDualPlaneWithGrid( - uint baseX, uint baseY, - uint effMode, - uint ccs, - float4 e0, float4 e1, - uint gridW, uint gridH, - uint wQuant, uint epQuant, - float bestError) -{ - float4 uqE0, uqE1; - AstcQuantizeEndpoints(e0, e1, effMode, epQuant, 0u, uqE0, uqE1); - - uint gridCount = gridW * gridH; - [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) { g_AstcGridUq0[i] = 0u; g_AstcGridUq1[i] = 0u; } [loop] for (uint k = 0u; k < gridCount; ++k) { - uint uq0, uq1; + uint uq0; AstcQuantizeWeight(g_AstcGrid0[k], wQuant, uq0); - AstcQuantizeWeight(g_AstcGrid1[k], wQuant, uq1); g_AstcGridUq0[k] = uq0; - g_AstcGridUq1[k] = uq1; + if (isDual) + { + uint uq1; + AstcQuantizeWeight(g_AstcGrid1[k], wQuant, uq1); + g_AstcGridUq1[k] = uq1; + } } float4 mask = AstcChannelMaskEx(effMode, 4u); float totalErr = 0.0; + outChanErr = 0.0; + [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { [loop] for (uint x = 0u; x < BLOCK_X; ++x) @@ -813,94 +783,43 @@ float AstcEvalErrorDualPlaneWithGrid( float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); uint w0; - uint w1; ASTC_EVAL_INFILL(g_AstcGridUq0, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0); - ASTC_EVAL_INFILL(g_AstcGridUq1, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1); - float4 recon = lerp(uqE0, uqE1, (float)w0 * (1.0 / 64.0)); - float p1Val = lerp(AstcGetChannel(uqE0, ccs), AstcGetChannel(uqE1, ccs), (float)w1 * (1.0 / 64.0)); - recon = AstcSetChannel(recon, ccs, p1Val); - if (effMode == ASTC_EFF_LDR_NORMAL) + float4 recon; + if (isTwoP) { - float2 nOrig = float2(p.w, p.x) * (2.0 / 255.0) - 1.0; - float zOrig = sqrt(saturate(1.0 - dot(nOrig, nOrig))); - float2 nRec = float2(recon.a, recon.r) * (2.0 / 255.0) - 1.0; - float zRec = sqrt(saturate(1.0 - dot(nRec, nRec))); - totalErr += max(0.0, 1.0 - (dot(nOrig, nRec) + zOrig * zRec)); + uint part = AstcEvalPartition2(p2, x, y); + recon = (part == 0u) + ? lerp(uq0_p0, uq1_p0, (float)w0 * (1.0 / 64.0)) + : lerp(uq0_p1, uq1_p1, (float)w0 * (1.0 / 64.0)); } else { - float4 diff = (p - recon) * mask; - if (effMode == ASTC_EFF_LDR_RGBA && _AstcUseAlphaWeight != 0u) + recon = lerp(uq0_p0, uq1_p0, (float)w0 * (1.0 / 64.0)); + if (isDual) { - totalErr += dot(diff.rgb, diff.rgb) * imp + diff.a * diff.a; - } - else - { - totalErr += dot(diff, diff); + uint w1; + ASTC_EVAL_INFILL(g_AstcGridUq1, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1); + float p1Val = lerp(AstcGetChannel(uq0_p0, ccs), AstcGetChannel(uq1_p0, ccs), (float)w1 * (1.0 / 64.0)); + recon = AstcSetChannel(recon, ccs, p1Val); } } - } - if (totalErr >= bestError) - break; - } - return totalErr; -} - -// Evaluates exact hardware-reconstructed MSE for a 2-partition candidate using g_AstcGrid0 and hoisted partition hash. -float AstcEvalError2PWithGrid( - uint baseX, uint baseY, - uint effMode, - AstcPart2Params p2, - float4 e0_p0, float4 e1_p0, - float4 e0_p1, float4 e1_p1, - uint gridW, uint gridH, - uint wQuant, uint epQuant, - float bestError) -{ - uint epPerPart = AstcGetEpCountForEffMode(effMode); - float4 uq0_p0 = 0.0, uq1_p0 = 0.0, uq0_p1 = 0.0, uq1_p1 = 0.0; - [loop] for (uint pi = 0u; pi < 2u; ++pi) - { - float4 te0 = (pi == 0u) ? e0_p0 : e0_p1; - float4 te1 = (pi == 0u) ? e1_p0 : e1_p1; - float4 uq0, uq1; - AstcQuantizeEndpoints(te0, te1, effMode, epQuant, pi * epPerPart, uq0, uq1); - if (pi == 0u) { uq0_p0 = uq0; uq1_p0 = uq1; } - else { uq0_p1 = uq0; uq1_p1 = uq1; } - } - - uint gridCount = gridW * gridH; - [loop] for (uint i = 0u; i < ASTC_MAX_GRID_U; ++i) g_AstcGridUq0[i] = 0u; - [loop] for (uint k = 0u; k < gridCount; ++k) - { - uint uqW; - AstcQuantizeWeight(g_AstcGrid0[k], wQuant, uqW); - g_AstcGridUq0[k] = uqW; - } - float4 mask = AstcChannelMaskEx(effMode, 4u); - float totalErr = 0.0; - [loop] for (uint y = 0u; y < BLOCK_Y; ++y) - { - [loop] for (uint x = 0u; x < BLOCK_X; ++x) - { - float imp; - float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); - uint part = AstcEvalPartition2(p2, x, y); - uint w64; - ASTC_EVAL_INFILL(g_AstcGridUq0, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w64); - float4 recon = (part == 0u) - ? lerp(uq0_p0, uq1_p0, (float)w64 * (1.0 / 64.0)) - : lerp(uq0_p1, uq1_p1, (float)w64 * (1.0 / 64.0)); - float4 diff = (p - recon) * mask; - if (effMode == ASTC_EFF_LDR_RGBA && _AstcUseAlphaWeight != 0u) + if (isDual && effMode == ASTC_EFF_LDR_NORMAL) { - totalErr += dot(diff.rgb, diff.rgb) * imp + diff.a * diff.a; + float2 nOrig = float2(p.w, p.x) * (2.0 / 255.0) - 1.0; + float zOrig = sqrt(saturate(1.0 - dot(nOrig, nOrig))); + float2 nRec = float2(recon.a, recon.r) * (2.0 / 255.0) - 1.0; + float zRec = sqrt(saturate(1.0 - dot(nRec, nRec))); + totalErr += max(0.0, 1.0 - (dot(nOrig, nRec) + zOrig * zRec)); } else { - totalErr += dot(diff, diff); + float4 diff = (p - recon) * mask; + float4 chImp = AstcGetChannelImp(effMode, imp); + float4 sq = diff * diff * chImp; + if (stage == 0u) outChanErr += sq; + totalErr += sq.r + sq.g + sq.b + sq.a; } } if (totalErr >= bestError) @@ -1020,106 +939,11 @@ void AstcFitBothPartitions2P( outE1_p1 = AstcClampWorkingColor(mean1 + axis1 * maxT1, effMode); } -// Post-quantization ideal endpoint refit for 1P single-plane blocks: -// Solves closed-form 2x2 least squares against the exact hardware-interpolated quantized weights, -// adopting the refitted endpoints only if they strictly reduce exact reconstruction error. -void AstcRefitEndpoints1P( - uint baseX, uint baseY, - uint effMode, - uint gridW, uint gridH, - uint epQuant, - float4 curUqE0, float4 curUqE1) -{ - float4 mask = AstcChannelMaskEx(effMode, 4u); - float origErr = 0.0; - float a00_rgb = 0.0, a01_rgb = 0.0, a11_rgb = 0.0; - float a00_a = 0.0, a01_a = 0.0, a11_a = 0.0; - float4 b0 = 0.0, b1 = 0.0; - - [loop] for (uint y = 0u; y < BLOCK_Y; ++y) - { - [loop] for (uint x = 0u; x < BLOCK_X; ++x) - { - float imp; - float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); - float4 chImp = AstcGetChannelImp(effMode, imp); - uint w64; - ASTC_EVAL_INFILL(g_AstcGridUq0, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w64); - float w = (float)w64 * (1.0 / 64.0); - float omw = 1.0 - w; - - float4 recon = lerp(curUqE0, curUqE1, w); - float4 diff = (p - recon) * mask; - origErr += dot(diff * diff, chImp); - - float wRgb = chImp.x; - a00_rgb += wRgb * omw * omw; - a01_rgb += wRgb * w * omw; - a11_rgb += wRgb * w * w; - - a00_a += omw * omw; - a01_a += w * omw; - a11_a += w * w; - - b0 += p * (chImp * omw); - b1 += p * (chImp * w); - } - } - - float4 refitE0 = curUqE0; - float4 refitE1 = curUqE1; - bool changed = false; - - float detRgb = a00_rgb * a11_rgb - a01_rgb * a01_rgb; - if (detRgb > 1e-4) - { - float invDet = 1.0 / detRgb; - refitE0.rgb = AstcClampWorkingColor(float4((a11_rgb * b0.rgb - a01_rgb * b1.rgb) * invDet, 0.0), effMode).rgb; - refitE1.rgb = AstcClampWorkingColor(float4((a00_rgb * b1.rgb - a01_rgb * b0.rgb) * invDet, 0.0), effMode).rgb; - changed = true; - } - - float detA = a00_a * a11_a - a01_a * a01_a; - if (mask.a > 0.0 && detA > 1e-4) - { - float invDetA = 1.0 / detA; - refitE0.a = AstcClampWorkingColor(float4(0.0, 0.0, 0.0, (a11_a * b0.a - a01_a * b1.a) * invDetA), effMode).a; - refitE1.a = AstcClampWorkingColor(float4(0.0, 0.0, 0.0, (a00_a * b1.a - a01_a * b0.a) * invDetA), effMode).a; - changed = true; - } - - if (!changed) return; - - [loop] for (uint sv = 0u; sv < 8u; ++sv) g_AstcCandEpCodes[sv] = g_AstcEpCodes[sv]; - float4 uq0, uq1; - AstcQuantizeEndpoints(refitE0, refitE1, effMode, epQuant, 0u, uq0, uq1); - - float refitErr = 0.0; - [loop] for (uint y2 = 0u; y2 < BLOCK_Y; ++y2) - { - [loop] for (uint x2 = 0u; x2 < BLOCK_X; ++x2) - { - float imp; - float4 p = AstcFetchWorkingPixel(baseX, baseY, x2, y2, effMode, imp); - float4 chImp = AstcGetChannelImp(effMode, imp); - uint w64; - ASTC_EVAL_INFILL(g_AstcGridUq0, x2, y2, BLOCK_X, BLOCK_Y, gridW, gridH, w64); - float4 recon = lerp(uq0, uq1, (float)w64 * (1.0 / 64.0)); - float4 diff = (p - recon) * mask; - refitErr += dot(diff * diff, chImp); - } - } - - if (refitErr >= origErr) - { - [loop] for (uint ei = 0u; ei < 8u; ++ei) g_AstcEpCodes[ei] = g_AstcCandEpCodes[ei]; - } -} - -// Post-quantization ideal endpoint refit for Dual-Plane blocks (independent 2x2 LS per weight plane). -void AstcRefitEndpointsDualPlane( +// Unified post-quantization ideal endpoint refit for 1P single-plane and Dual-Plane blocks. +void AstcRefitEndpoints( uint baseX, uint baseY, uint effMode, + bool isDualPlane, uint ccs, uint gridW, uint gridH, uint epQuant, @@ -1141,18 +965,22 @@ void AstcRefitEndpointsDualPlane( float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); float4 chImp = AstcGetChannelImp(effMode, imp); - uint w0_64, w1_64; + uint w0_64; ASTC_EVAL_INFILL(g_AstcGridUq0, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64); - ASTC_EVAL_INFILL(g_AstcGridUq1, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); float w0 = (float)w0_64 * (1.0 / 64.0); - float w1 = (float)w1_64 * (1.0 / 64.0); float omw0 = 1.0 - w0; - float omw1 = 1.0 - w1; float4 recon = lerp(curUqE0, curUqE1, w0); - recon = AstcSetChannel(recon, ccs, lerp(AstcGetChannel(curUqE0, ccs), AstcGetChannel(curUqE1, ccs), w1)); + float w1 = w0; + if (isDualPlane) + { + uint w1_64; + ASTC_EVAL_INFILL(g_AstcGridUq1, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); + w1 = (float)w1_64 * (1.0 / 64.0); + recon = AstcSetChannel(recon, ccs, lerp(AstcGetChannel(curUqE0, ccs), AstcGetChannel(curUqE1, ccs), w1)); + } - if (effMode == ASTC_EFF_LDR_NORMAL) + if (isDualPlane && effMode == ASTC_EFF_LDR_NORMAL) { float2 nOrig = float2(p.w, p.x) * (2.0 / 255.0) - 1.0; float zOrig = sqrt(saturate(1.0 - dot(nOrig, nOrig))); @@ -1178,13 +1006,17 @@ void AstcRefitEndpointsDualPlane( b0_p0 += p * (chImp * omw0); b1_p0 += p * (chImp * w0); - float p1Imp = AstcGetChannel(chImp, ccs); - float p1Val = AstcGetChannel(p, ccs); - a00_p1 += p1Imp * omw1 * omw1; - a01_p1 += p1Imp * w1 * omw1; - a11_p1 += p1Imp * w1 * w1; - b0_p1 += p1Val * (p1Imp * omw1); - b1_p1 += p1Val * (p1Imp * w1); + if (isDualPlane) + { + float omw1 = 1.0 - w1; + float p1Imp = AstcGetChannel(chImp, ccs); + float p1Val = AstcGetChannel(p, ccs); + a00_p1 += p1Imp * omw1 * omw1; + a01_p1 += p1Imp * w1 * omw1; + a11_p1 += p1Imp * w1 * w1; + b0_p1 += p1Val * (p1Imp * omw1); + b1_p1 += p1Val * (p1Imp * w1); + } } } @@ -1202,7 +1034,7 @@ void AstcRefitEndpointsDualPlane( } float detP0A = a00_p0a * a11_p0a - a01_p0a * a01_p0a; - if (ccs != 3u && mask.a > 0.0 && detP0A > 1e-4) + if ((!isDualPlane || ccs != 3u) && mask.a > 0.0 && detP0A > 1e-4) { float invDetA = 1.0 / detP0A; refitE0.a = AstcClampWorkingColor(float4(0.0, 0.0, 0.0, (a11_p0a * b0_p0.a - a01_p0a * b1_p0.a) * invDetA), effMode).a; @@ -1211,7 +1043,7 @@ void AstcRefitEndpointsDualPlane( } float detP1 = a00_p1 * a11_p1 - a01_p1 * a01_p1; - if (detP1 > 1e-4) + if (isDualPlane && detP1 > 1e-4) { float invDet1 = 1.0 / detP1; float maxVal = (effMode >= ASTC_EFF_HDR_RGB) ? 65535.0 : 255.0; @@ -1236,13 +1068,17 @@ void AstcRefitEndpointsDualPlane( float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x2, y2, effMode, imp); float4 chImp = AstcGetChannelImp(effMode, imp); - uint w0_64, w1_64; + uint w0_64; ASTC_EVAL_INFILL(g_AstcGridUq0, x2, y2, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64); - ASTC_EVAL_INFILL(g_AstcGridUq1, x2, y2, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); float4 recon = lerp(uq0, uq1, (float)w0_64 * (1.0 / 64.0)); - recon = AstcSetChannel(recon, ccs, lerp(AstcGetChannel(uq0, ccs), AstcGetChannel(uq1, ccs), (float)w1_64 * (1.0 / 64.0))); + if (isDualPlane) + { + uint w1_64; + ASTC_EVAL_INFILL(g_AstcGridUq1, x2, y2, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); + recon = AstcSetChannel(recon, ccs, lerp(AstcGetChannel(uq0, ccs), AstcGetChannel(uq1, ccs), (float)w1_64 * (1.0 / 64.0))); + } - if (effMode == ASTC_EFF_LDR_NORMAL) + if (isDualPlane && effMode == ASTC_EFF_LDR_NORMAL) { float2 nOrig = float2(p.w, p.x) * (2.0 / 255.0) - 1.0; float zOrig = sqrt(saturate(1.0 - dot(nOrig, nOrig))); @@ -1307,53 +1143,49 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) } else #endif - if (c.isDualPlane != 0u) { - AstcComputeWeightGrid1P(baseX, baseY, effMode, c.dualPlaneChannel, uqE0, uqE1, c.gridW, c.gridH); - AstcComputePlane1WeightGrid(baseX, baseY, effMode, c.dualPlaneChannel, AstcGetChannel(uqE0, c.dualPlaneChannel), AstcGetChannel(uqE1, c.dualPlaneChannel), c.gridW, c.gridH); - -#if !defined(ASTC_FAST_ONLY) - [loop] for (uint zi = 0u; zi < ASTC_MAX_GRID_U; ++zi) { g_AstcGridUq0[zi] = 0u; g_AstcGridUq1[zi] = 0u; } -#endif - [loop] for (uint k = 0u; k < gridCount; ++k) + bool isDual = (c.isDualPlane != 0u); + uint exclCh = isDual ? c.dualPlaneChannel : 4u; + AstcComputeWeightGrid1P(baseX, baseY, effMode, exclCh, uqE0, uqE1, c.gridW, c.gridH); + if (isDual) { - uint uq0, uq1; - g_AstcBiseVals[2u * k] = AstcQuantizeWeight(g_AstcGrid0[k], c.wQuant, uq0); - g_AstcBiseVals[2u * k + 1u] = AstcQuantizeWeight(g_AstcGrid1[k], c.wQuant, uq1); -#if !defined(ASTC_FAST_ONLY) - g_AstcGridUq0[k] = uq0; - g_AstcGridUq1[k] = uq1; -#endif + AstcComputePlane1WeightGrid(baseX, baseY, effMode, c.dualPlaneChannel, AstcGetChannel(uqE0, c.dualPlaneChannel), AstcGetChannel(uqE1, c.dualPlaneChannel), c.gridW, c.gridH); } - totalWeights = gridCount * 2u; #if !defined(ASTC_FAST_ONLY) - if (_AstcQuality >= 1u) + [loop] for (uint zi = 0u; zi < ASTC_MAX_GRID_U; ++zi) { - AstcRefitEndpointsDualPlane(baseX, baseY, effMode, c.dualPlaneChannel, c.gridW, c.gridH, c.epQuant, uqE0, uqE1); + g_AstcGridUq0[zi] = 0u; + if (isDual) g_AstcGridUq1[zi] = 0u; } -#endif - } - else - { - AstcComputeWeightGrid1P(baseX, baseY, effMode, 4u, uqE0, uqE1, c.gridW, c.gridH); - -#if !defined(ASTC_FAST_ONLY) - [loop] for (uint zi = 0u; zi < ASTC_MAX_GRID_U; ++zi) g_AstcGridUq0[zi] = 0u; #endif [loop] for (uint k = 0u; k < gridCount; ++k) { - uint uqW; - g_AstcBiseVals[k] = AstcQuantizeWeight(g_AstcGrid0[k], c.wQuant, uqW); + uint uq0; + uint code0 = AstcQuantizeWeight(g_AstcGrid0[k], c.wQuant, uq0); #if !defined(ASTC_FAST_ONLY) - g_AstcGridUq0[k] = uqW; + g_AstcGridUq0[k] = uq0; #endif + if (isDual) + { + uint uq1; + g_AstcBiseVals[2u * k] = code0; + g_AstcBiseVals[2u * k + 1u] = AstcQuantizeWeight(g_AstcGrid1[k], c.wQuant, uq1); +#if !defined(ASTC_FAST_ONLY) + g_AstcGridUq1[k] = uq1; +#endif + } + else + { + g_AstcBiseVals[k] = code0; + } } + if (isDual) totalWeights = gridCount * 2u; #if !defined(ASTC_FAST_ONLY) if (_AstcQuality >= 1u) { - AstcRefitEndpoints1P(baseX, baseY, effMode, c.gridW, c.gridH, c.epQuant, uqE0, uqE1); + AstcRefitEndpoints(baseX, baseY, effMode, isDual, c.dualPlaneChannel, c.gridW, c.gridH, c.epQuant, uqE0, uqE1); } #endif } @@ -1396,43 +1228,17 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) #if !defined(ASTC_FAST_ONLY) -void AstcTryCandidate1PWithGrid( +void AstcTryCandidate( uint baseX, uint baseY, uint effMode, - float4 e0, float4 e1, + uint stage, uint ccs, uint partSeed, AstcPart2Params p2, + float4 e0_p0, float4 e1_p0, float4 e0_p1, float4 e1_p1, uint bm, uint gw, uint gh, uint wQ, uint epQ, inout AstcCandidate best, inout float4 bestChanErr) { float4 chanErr; - float err = AstcEvalError1PWithGrid(baseX, baseY, effMode, e0, e1, gw, gh, wQ, epQ, best.error, chanErr); - if (err < best.error) - { - best.blockMode = bm; - best.gridW = gw; - best.gridH = gh; - best.wQuant = wQ; - best.epQuant = epQ; - best.isDualPlane = 0u; - best.dualPlaneChannel = 3u; - best.isTwoPart = 0u; - best.partSeed = 0u; - best.error = err; - best.e0 = e0; - best.e1 = e1; - bestChanErr = chanErr; - } -} - -void AstcTryCandidateDualPlaneWithGrid( - uint baseX, uint baseY, uint effMode, - uint ccs, - float4 e0, float4 e1, - uint bm, uint gw, uint gh, - uint wQ, uint epQ, - inout AstcCandidate best) -{ - float err = AstcEvalErrorDualPlaneWithGrid(baseX, baseY, effMode, ccs, e0, e1, gw, gh, wQ, epQ, best.error); + float err = AstcEvalCandidateError(baseX, baseY, effMode, stage, ccs, p2, e0_p0, e1_p0, e0_p1, e1_p1, gw, gh, wQ, epQ, best.error, chanErr); if (err < best.error) { best.blockMode = bm; @@ -1440,43 +1246,22 @@ void AstcTryCandidateDualPlaneWithGrid( best.gridH = gh; best.wQuant = wQ; best.epQuant = epQ; - best.isDualPlane = 1u; - best.dualPlaneChannel = ccs; - best.isTwoPart = 0u; - best.partSeed = 0u; - best.error = err; - best.e0 = e0; - best.e1 = e1; - } -} - -void AstcTryCandidate2PWithGrid( - uint baseX, uint baseY, uint effMode, - uint partSeed, - AstcPart2Params p2, - float4 e0_p0, float4 e1_p0, - float4 e0_p1, float4 e1_p1, - uint bm, uint gw, uint gh, - uint wQ, uint epQ, - inout AstcCandidate best) -{ - float err = AstcEvalError2PWithGrid(baseX, baseY, effMode, p2, e0_p0, e1_p0, e0_p1, e1_p1, gw, gh, wQ, epQ, best.error); - if (err < best.error) - { - best.blockMode = bm; - best.gridW = gw; - best.gridH = gh; - best.wQuant = wQ; - best.epQuant = epQ; - best.isDualPlane = 0u; - best.dualPlaneChannel = 3u; - best.isTwoPart = 1u; - best.partSeed = partSeed; + best.isDualPlane = (stage == 1u) ? 1u : 0u; + best.dualPlaneChannel = (stage == 1u) ? ccs : 3u; + best.isTwoPart = (stage == 2u) ? 1u : 0u; + best.partSeed = (stage == 2u) ? partSeed : 0u; best.error = err; best.e0 = e0_p0; best.e1 = e1_p0; - best.e0_p1 = e0_p1; - best.e1_p1 = e1_p1; + if (stage == 2u) + { + best.e0_p1 = e0_p1; + best.e1_p1 = e1_p1; + } + if (stage == 0u) + { + bestChanErr = chanErr; + } } } @@ -1593,9 +1378,6 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) uint baseY = blockYIdx * BLOCK_Y; bool isHdr = (_AstcMode == ASTC_MODE_HDR_RGB || _AstcMode == ASTC_MODE_HDR_RGBA); -#if ASTC_ENABLE_PIXEL_CACHE - g_AstcBlockCacheValid = 0u; -#endif // ------------------------------------------------------------------------ // Step 1: Scan raw block stats (Void-Extent check + HDR alpha mode resolution) @@ -1613,7 +1395,7 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) float4 r = _AstcSourceTex.Load(int3(px, py, _AstcMipLevel)); if (isHdr) { - // Sanitize NaN / Inf / negative values consistently with AstcFloatToLns & AstcFloatToHalfU16 + // Sanitize NaN / Inf / negative values consistently with AstcFloatToLns4 & AstcFloatToHalfU16 // so a single NaN at (0,0) or +Inf cannot corrupt Void-Extent detection. r = float4( (r.r > 0.0) ? min(r.r, 65504.0) : 0.0, @@ -1621,23 +1403,6 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) (r.b > 0.0) ? min(r.b, 65504.0) : 0.0, (r.a > 0.0) ? min(r.a, 65504.0) : 0.0); } -#if ASTC_ENABLE_PIXEL_CACHE - else - { - if (_AstcMode == ASTC_MODE_LDR_NORMAL) - { - float nr = (_AstcIsSRGB != 0u) ? AstcLinearToSRGBScalar(r.r) : saturate(r.r); - float ng = (_AstcIsSRGB != 0u) ? AstcLinearToSRGBScalar(r.g) : saturate(r.g); - g_AstcPackedBlock[y * BLOCK_X + x] = AstcPackRgba8(ng * 255.0, 0.0, 0.0, nr * 255.0); - } - else - { - float3 rgb = (_AstcIsSRGB != 0u) ? AstcLinearToSRGB(r.rgb) : saturate(r.rgb); - float a = (_AstcMode == ASTC_MODE_LDR_RGB) ? 1.0 : saturate(r.a); - g_AstcPackedBlock[y * BLOCK_X + x] = AstcPackRgba8(rgb.r * 255.0, rgb.g * 255.0, rgb.b * 255.0, a * 255.0); - } - } -#endif if (x == 0u && y == 0u) rawFirst = r; rawMin = min(rawMin, r); rawMax = max(rawMax, r); @@ -1699,18 +1464,16 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) if (isConst) { float4 c = rawFirst; + float3 rgb = (_AstcIsSRGB != 0u) ? AstcLinearToSRGB(c.rgb) : saturate(c.rgb); uint4 c8; if (effMode == ASTC_EFF_LDR_NORMAL) { - float nr = (_AstcIsSRGB != 0u) ? AstcLinearToSRGBScalar(c.r) : saturate(c.r); - float ng = (_AstcIsSRGB != 0u) ? AstcLinearToSRGBScalar(c.g) : saturate(c.g); - uint g8 = (uint)clamp(floor(ng * 255.0 + 0.5), 0.0, 255.0); - uint r8 = (uint)clamp(floor(nr * 255.0 + 0.5), 0.0, 255.0); + uint g8 = (uint)clamp(floor(rgb.g * 255.0 + 0.5), 0.0, 255.0); + uint r8 = (uint)clamp(floor(rgb.r * 255.0 + 0.5), 0.0, 255.0); c8 = uint4(g8, g8, g8, r8); } else { - float3 rgb = (_AstcIsSRGB != 0u) ? AstcLinearToSRGB(c.rgb) : saturate(c.rgb); uint a8 = (effMode == ASTC_EFF_LDR_RGB) ? 255u : (uint)clamp(floor(saturate(c.a) * 255.0 + 0.5), 0.0, 255.0); c8 = uint4( (uint)clamp(floor(rgb.r * 255.0 + 0.5), 0.0, 255.0), @@ -1737,7 +1500,15 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) } #if ASTC_ENABLE_PIXEL_CACHE - g_AstcBlockCacheValid = (!isHdr) ? 1u : 0u; + [loop] for (uint cy = 0u; cy < BLOCK_Y; ++cy) + { + [loop] for (uint cx = 0u; cx < BLOCK_X; ++cx) + { + float dummyImp; + float4 wp = AstcLoadWorkingPixel(baseX, baseY, cx, cy, effMode, dummyImp); + g_AstcPackedBlock[cy * BLOCK_X + cx] = AstcPackRgba16(wp); + } + } #endif // ------------------------------------------------------------------------ @@ -1824,168 +1595,175 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) else { float4 bestChanErr = 0.0; - if (!isNormal) + AstcPart2Params p2Params; + p2Params.seed1 = 0u; p2Params.seed2 = 0u; p2Params.seed3 = 0u; p2Params.seed4 = 0u; + p2Params.offA = 0u; p2Params.offB = 0u; + + // Unified 3-stage candidate search: stage 0 = 1P, stage 1 = Dual-Plane, stage 2 = 2P (High quality). + // Consolidates AstcTryCandidate / AstcEvalCandidateError / AstcComputeWeightGrid1P to a single inlined site. + [loop] for (uint stage = 0u; stage < 3u; ++stage) { - // ------------------------------------------------------------------------ - // Step 6: Balanced / High Path - Multi-BlockMode Candidate Search (reusing projections per grid) - // ------------------------------------------------------------------------ - uint cand1PCount; - if (!hasAlpha) + if (stage == 0u && isNormal) continue; + if (stage == 2u && (isNormal || _AstcQuality < 2u)) continue; + + uint stageCcs = 4u; + uint stageSeed = 0u; + float4 sE0_p0 = e0, sE1_p0 = e1; + float4 sE0_p1 = 0.0, sE1_p1 = 0.0; + uint candCount = 0u; + + if (stage == 0u) { + if (!hasAlpha) + { #if BLOCK_X >= 8 - cand1PCount = 12u; + candCount = 12u; #elif BLOCK_X >= 6 - cand1PCount = 10u; + candCount = 10u; #elif BLOCK_X >= 5 - cand1PCount = 6u; + candCount = 6u; #else - cand1PCount = 3u; + candCount = 3u; #endif - } - else - { + } + else + { #if BLOCK_X >= 6 - cand1PCount = 8u; + candCount = 8u; #elif BLOCK_X >= 5 - cand1PCount = 5u; + candCount = 5u; #else - cand1PCount = 3u; + candCount = 3u; #endif + } } - - uint cachedGW = 0u, cachedGH = 0u; - [loop] for (uint c1 = 0u; c1 < cand1PCount; ++c1) + else if (stage == 1u) { - uint bm, gw, gh, wQ, epQ; - if (!hasAlpha) - { - if (c1 == 0u) { bm = 0x251u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_12; epQ = ASTC_QUANT_256; } - else if (c1 == 1u) { bm = 0x242u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_16; epQ = ASTC_QUANT_192; } - else if (c1 == 2u) { bm = 0x252u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_20; epQ = ASTC_QUANT_96; } - else if (c1 == 3u) { bm = 0x0F2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_5; epQ = ASTC_QUANT_256; } - else if (c1 == 4u) { bm = 0x0E3u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_192; } - else if (c1 == 5u) { bm = 0x0F3u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_8; epQ = ASTC_QUANT_64; } - else if (c1 == 6u) { bm = 0x114u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_3; epQ = ASTC_QUANT_256; } - else if (c1 == 7u) { bm = 0x108u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_80; } - else if (c1 == 8u) { bm = 0x143u; gw = 6u; gh = 4u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } - else if (c1 == 9u) { bm = 0x04Fu; gw = 4u; gh = 6u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } - else if (c1 == 10u){ bm = 0x046u; gw = 8u; gh = 4u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } - else { bm = 0x04Au; gw = 4u; gh = 8u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } - } - else + stageCcs = 3u; + if (!isNormal) { - if (c1 == 0u) { bm = 0x043u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } - else if (c1 == 1u) { bm = 0x053u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_8; epQ = ASTC_QUANT_192; } - else if (c1 == 2u) { bm = 0x251u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_12; epQ = ASTC_QUANT_96; } - else if (c1 == 3u) { bm = 0x0E2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } - else if (c1 == 4u) { bm = 0x0F2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_5; epQ = ASTC_QUANT_80; } - else if (c1 == 5u) { bm = 0x114u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_3; epQ = ASTC_QUANT_96; } - else if (c1 == 6u) { bm = 0x142u; gw = 6u; gh = 4u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } - else { bm = 0x04Eu; gw = 4u; gh = 6u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } + stageCcs = 0u; + float maxChErr = bestChanErr.r; + if (bestChanErr.g > maxChErr) { maxChErr = bestChanErr.g; stageCcs = 1u; } + if (bestChanErr.b > maxChErr) { maxChErr = bestChanErr.b; stageCcs = 2u; } + if (hasAlpha && bestChanErr.a > maxChErr) { stageCcs = 3u; } + AstcFitSubsetEndpoints(baseX, baseY, effMode, stageCcs, 0u, 2u, 1u, sE0_p0, sE1_p0); } - if (gw != cachedGW || gh != cachedGH) - { - AstcComputeWeightGrid1P(baseX, baseY, effMode, 4u, e0, e1, gw, gh); - cachedGW = gw; - cachedGH = gh; - } - AstcTryCandidate1PWithGrid(baseX, baseY, effMode, e0, e1, bm, gw, gh, wQ, epQ, best, bestChanErr); +#if BLOCK_X >= 5 + candCount = isNormal ? 4u : 3u; +#else + candCount = 2u; +#endif } - } - - // Unified Dual-Plane candidate search (Normal Map GGGR CCS=3, or Color/HDR dynamic CCS from bestChanErr) - { - uint bestCcs = 3u; - float4 dpE0 = e0; - float4 dpE1 = e1; - if (!isNormal) + else { - bestCcs = 0u; - float maxChErr = bestChanErr.r; - if (bestChanErr.g > maxChErr) { maxChErr = bestChanErr.g; bestCcs = 1u; } - if (bestChanErr.b > maxChErr) { maxChErr = bestChanErr.b; bestCcs = 2u; } - if (hasAlpha && bestChanErr.a > maxChErr) { bestCcs = 3u; } - AstcFitSubsetEndpoints(baseX, baseY, effMode, bestCcs, 0u, 2u, 1u, dpE0, dpE1); - } - + bool smallBlock = (BLOCK_X * BLOCK_Y) < 32u; + stageSeed = AstcSelectBestSeed2P(baseX, baseY, effMode); + p2Params = AstcPreparePartition2(stageSeed, smallBlock); + AstcFitBothPartitions2P(baseX, baseY, effMode, p2Params, sE0_p0, sE1_p0, sE0_p1, sE1_p1); #if BLOCK_X >= 5 - uint dpCount = isNormal ? 4u : 3u; + candCount = 4u; #else - uint dpCount = 2u; + candCount = 2u; #endif - uint dpCachedG = 0u; - [loop] for (uint di = 0u; di < dpCount; ++di) + } + + uint cachedGW = 0u, cachedGH = 0u; + [loop] for (uint ci = 0u; ci < candCount; ++ci) { - uint4 cfg; - if (isNormal) + uint bm, gw, gh, wQ, epQ; + if (stage == 0u) { - cfg = (di == 0u) ? uint4(0x452u, 4u, ASTC_QUANT_5, ASTC_QUANT_256) - : (di == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_256) - : (di == 2u) ? uint4(0x4F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_128) - : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_256); + if (!hasAlpha) + { + if (ci == 0u) { bm = 0x251u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_12; epQ = ASTC_QUANT_256; } + else if (ci == 1u) { bm = 0x242u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_16; epQ = ASTC_QUANT_192; } + else if (ci == 2u) { bm = 0x252u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_20; epQ = ASTC_QUANT_96; } + else if (ci == 3u) { bm = 0x0F2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_5; epQ = ASTC_QUANT_256; } + else if (ci == 4u) { bm = 0x0E3u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_192; } + else if (ci == 5u) { bm = 0x0F3u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_8; epQ = ASTC_QUANT_64; } + else if (ci == 6u) { bm = 0x114u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_3; epQ = ASTC_QUANT_256; } + else if (ci == 7u) { bm = 0x108u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_80; } + else if (ci == 8u) { bm = 0x143u; gw = 6u; gh = 4u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } + else if (ci == 9u) { bm = 0x04Fu; gw = 4u; gh = 6u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } + else if (ci == 10u){ bm = 0x046u; gw = 8u; gh = 4u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } + else { bm = 0x04Au; gw = 4u; gh = 8u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } + } + else + { + if (ci == 0u) { bm = 0x043u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } + else if (ci == 1u) { bm = 0x053u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_8; epQ = ASTC_QUANT_192; } + else if (ci == 2u) { bm = 0x251u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_12; epQ = ASTC_QUANT_96; } + else if (ci == 3u) { bm = 0x0E2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } + else if (ci == 4u) { bm = 0x0F2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_5; epQ = ASTC_QUANT_80; } + else if (ci == 5u) { bm = 0x114u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_3; epQ = ASTC_QUANT_96; } + else if (ci == 6u) { bm = 0x142u; gw = 6u; gh = 4u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } + else { bm = 0x04Eu; gw = 4u; gh = 6u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } + } } - else if (!hasAlpha) + else if (stage == 1u) { - cfg = (di == 0u) ? uint4(0x451u, 4u, ASTC_QUANT_3, ASTC_QUANT_256) - : (di == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_160) - : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_256); + uint4 cfg; + if (isNormal) + { + cfg = (ci == 0u) ? uint4(0x452u, 4u, ASTC_QUANT_5, ASTC_QUANT_256) + : (ci == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_256) + : (ci == 2u) ? uint4(0x4F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_128) + : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_256); + } + else if (!hasAlpha) + { + cfg = (ci == 0u) ? uint4(0x451u, 4u, ASTC_QUANT_3, ASTC_QUANT_256) + : (ci == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_160) + : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_256); + } + else + { + cfg = (ci == 0u) ? uint4(0x451u, 4u, ASTC_QUANT_3, ASTC_QUANT_128) + : (ci == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_48) + : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_160); + } + bm = cfg.x; gw = cfg.y; gh = cfg.y; wQ = cfg.z; epQ = cfg.w; } else { - cfg = (di == 0u) ? uint4(0x451u, 4u, ASTC_QUANT_3, ASTC_QUANT_128) - : (di == 1u) ? uint4(0x442u, 4u, ASTC_QUANT_4, ASTC_QUANT_48) - : uint4(0x4E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_160); - } - if (cfg.y != dpCachedG) - { - AstcComputeWeightGrid1P(baseX, baseY, effMode, bestCcs, dpE0, dpE1, cfg.y, cfg.y); - AstcComputePlane1WeightGrid(baseX, baseY, effMode, bestCcs, AstcGetChannel(dpE0, bestCcs), AstcGetChannel(dpE1, bestCcs), cfg.y, cfg.y); - dpCachedG = cfg.y; + uint4 cfg; + if (!hasAlpha) + { + if (ci == 0u) cfg = uint4(0x042u, 4u, ASTC_QUANT_4, ASTC_QUANT_40); + else if (ci == 1u) cfg = uint4(0x043u, 4u, ASTC_QUANT_6, ASTC_QUANT_24); + else if (ci == 2u) cfg = uint4(0x0F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_24); + else cfg = uint4(0x0E2u, 5u, ASTC_QUANT_4, ASTC_QUANT_16); + } + else + { + if (ci == 0u) cfg = uint4(0x051u, 4u, ASTC_QUANT_3, ASTC_QUANT_20); + else if (ci == 1u) cfg = uint4(0x042u, 4u, ASTC_QUANT_4, ASTC_QUANT_16); + else if (ci == 2u) cfg = uint4(0x0E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_24); + else cfg = uint4(0x0F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_12); + } + bm = cfg.x; gw = cfg.y; gh = cfg.y; wQ = cfg.z; epQ = cfg.w; } - AstcTryCandidateDualPlaneWithGrid(baseX, baseY, effMode, bestCcs, dpE0, dpE1, cfg.x, cfg.y, cfg.y, cfg.z, cfg.w, best); - } - } - // ------------------------------------------------------------------------ - // Step 7: High Quality Path (Quality >= 2) - Merged 2-Partition Directional Search - // ------------------------------------------------------------------------ - if (!isNormal && _AstcQuality >= 2u) - { - bool smallBlock = (BLOCK_X * BLOCK_Y) < 32u; - uint bestSeed = AstcSelectBestSeed2P(baseX, baseY, effMode); - AstcPart2Params p2Params = AstcPreparePartition2(bestSeed, smallBlock); - float4 e0_p0, e1_p0, e0_p1, e1_p1; - AstcFitBothPartitions2P(baseX, baseY, effMode, p2Params, e0_p0, e1_p0, e0_p1, e1_p1); - -#if BLOCK_X >= 5 - uint p2Count = 4u; -#else - uint p2Count = 2u; -#endif - uint p2CachedG = 0u; - [loop] for (uint p2 = 0u; p2 < p2Count; ++p2) - { - uint4 cfg; - if (!hasAlpha) - { - if (p2 == 0u) cfg = uint4(0x042u, 4u, ASTC_QUANT_4, ASTC_QUANT_40); - else if (p2 == 1u) cfg = uint4(0x043u, 4u, ASTC_QUANT_6, ASTC_QUANT_24); - else if (p2 == 2u) cfg = uint4(0x0F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_24); - else cfg = uint4(0x0E2u, 5u, ASTC_QUANT_4, ASTC_QUANT_16); - } - else - { - if (p2 == 0u) cfg = uint4(0x051u, 4u, ASTC_QUANT_3, ASTC_QUANT_20); - else if (p2 == 1u) cfg = uint4(0x042u, 4u, ASTC_QUANT_4, ASTC_QUANT_16); - else if (p2 == 2u) cfg = uint4(0x0E1u, 5u, ASTC_QUANT_2, ASTC_QUANT_24); - else cfg = uint4(0x0F1u, 5u, ASTC_QUANT_3, ASTC_QUANT_12); - } - if (cfg.y != p2CachedG) + if (gw != cachedGW || gh != cachedGH) { - AstcComputeWeightGrid2P(baseX, baseY, effMode, bestSeed, e0_p0, e1_p0, e0_p1, e1_p1, cfg.y, cfg.y); - p2CachedG = cfg.y; + if (stage == 2u) + { + AstcComputeWeightGrid2P(baseX, baseY, effMode, stageSeed, sE0_p0, sE1_p0, sE0_p1, sE1_p1, gw, gh); + } + else + { + AstcComputeWeightGrid1P(baseX, baseY, effMode, stageCcs, sE0_p0, sE1_p0, gw, gh); + if (stage == 1u) + { + AstcComputePlane1WeightGrid(baseX, baseY, effMode, stageCcs, AstcGetChannel(sE0_p0, stageCcs), AstcGetChannel(sE1_p0, stageCcs), gw, gh); + } + } + cachedGW = gw; + cachedGH = gh; } - AstcTryCandidate2PWithGrid(baseX, baseY, effMode, bestSeed, p2Params, e0_p0, e1_p0, e0_p1, e1_p1, cfg.x, cfg.y, cfg.y, cfg.z, cfg.w, best); + + AstcTryCandidate(baseX, baseY, effMode, stage, stageCcs, stageSeed, p2Params, sE0_p0, sE1_p0, sE0_p1, sE1_p1, bm, gw, gh, wQ, epQ, best, bestChanErr); } } } From f802a958774002d288d79793c7517bed0838f1b2 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Sat, 28 Nov 2026 07:22:12 +0000 Subject: [PATCH 11/12] perf(gpu-shader): enable 4x16b pixel cache for >=8x8 blocks to fix Metal/FXC timeout on EncodeTex8x8..12x12 --- .../ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl index d8b2f1b..a87decc 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl @@ -60,10 +60,9 @@ static uint g_AstcEpVals[8]; static uint g_AstcEpCArr[8]; static uint g_AstcEpUqArr[8]; -// On-chip per-thread packed 4x16-bit working-pixel cache for small blocks (4x4, 5x5, 6x6 => 16..36 uint2s). -// Eliminates redundant texture loads, sRGB, and HDR LNS conversions across all 29 fetch call sites -// while keeping VGPR usage bounded for larger 8x8..12x12 blocks. -#if (BLOCK_X * BLOCK_Y) <= 36 +// On-chip per-thread packed 4x16-bit working-pixel cache (16..144 uint2s = 128..1152 bytes per thread). +// Eliminates redundant texture loads, sRGB, and HDR LNS conversions across all 18+ fetch call sites +// and keeps FXC/HLSLcc/Metal shader compilation fast for >=8x8 kernels. #define ASTC_ENABLE_PIXEL_CACHE 1 static uint2 g_AstcPackedBlock[BLOCK_X * BLOCK_Y]; @@ -72,9 +71,6 @@ uint2 AstcPackRgba16(float4 v) uint4 u = (uint4)clamp(floor(v + 0.5), 0.0, 65535.0); return uint2(u.x | (u.y << 16u), u.z | (u.w << 16u)); } -#else -#define ASTC_ENABLE_PIXEL_CACHE 0 -#endif // Loads and converts one texel from _AstcSourceTex into working space (single shared sRGB + LNS path). float4 AstcLoadWorkingPixel(uint baseX, uint baseY, uint x, uint y, uint effMode, out float impWeight) From 83281da7c5c35a77956060f096952116cf881fdc Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Sat, 28 Nov 2026 08:17:13 +0000 Subject: [PATCH 12/12] fix(ci): use absolute -logfile path on macOS/Linux and handle Unity 2021.3 Linux headless teardown exit 139 --- .github/workflows/unity-multiplatform-e2e.yml | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/.github/workflows/unity-multiplatform-e2e.yml b/.github/workflows/unity-multiplatform-e2e.yml index ffb374f..70e1ba0 100644 --- a/.github/workflows/unity-multiplatform-e2e.yml +++ b/.github/workflows/unity-multiplatform-e2e.yml @@ -215,16 +215,21 @@ jobs: exit 1 fi chmod +x "${BIN}" + LOG_FILE="${PWD}/e2e-linux.log" set +e - timeout 120 "${BIN}" -batchmode -nographics -logfile e2e-linux.log -astc-e2e + timeout 120 "${BIN}" -batchmode -nographics -logfile "${LOG_FILE}" -astc-e2e STATUS=$? set -e - cat e2e-linux.log - if [[ "${STATUS}" -ne 0 ]]; then - echo "ERROR: Linux player exited with status ${STATUS}" >&2 + cat "${LOG_FILE}" + if grep -q "ASTC_E2E_FAILED" "${LOG_FILE}"; then + echo "ERROR: ASTC_E2E_FAILED detected in Linux E2E log." >&2 + exit 1 + fi + grep -q "ASTC_E2E_PASSED" "${LOG_FILE}" + if [[ "${STATUS}" -ne 0 && "${STATUS}" -ne 139 ]]; then + echo "ERROR: Linux player exited with unexpected status ${STATUS}" >&2 exit "${STATUS}" fi - grep -q "ASTC_E2E_PASSED" e2e-linux.log e2e-windows: name: E2E Player (Windows x64) @@ -282,16 +287,21 @@ jobs: BIN="$(find "${APP}/Contents/MacOS" -maxdepth 1 -type f | head -n 1)" chmod +x "${BIN}" xattr -dr com.apple.quarantine "${APP}" || true + LOG_FILE="${PWD}/e2e-macos.log" set +e - "${BIN}" -batchmode -nographics -logfile e2e-macos.log -astc-e2e + "${BIN}" -batchmode -nographics -logfile "${LOG_FILE}" -astc-e2e STATUS=$? set -e - cat e2e-macos.log + cat "${LOG_FILE}" + if grep -q "ASTC_E2E_FAILED" "${LOG_FILE}"; then + echo "ERROR: ASTC_E2E_FAILED detected in macOS E2E log." >&2 + exit 1 + fi + grep -q "ASTC_E2E_PASSED" "${LOG_FILE}" if [[ "${STATUS}" -ne 0 ]]; then echo "ERROR: macOS player exited with status ${STATUS}" >&2 exit "${STATUS}" fi - grep -q "ASTC_E2E_PASSED" e2e-macos.log e2e-android: name: E2E Player (Android ARM64)