citra/src/tests/video_core/shader/shader_jit_compiler.cpp

709 lines
28 KiB
C++
Raw Normal View History

video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
// Copyright 2023 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include "common/arch.h"
video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
#if CITRA_ARCH(x86_64) || CITRA_ARCH(arm64)
#include <algorithm>
#include <cmath>
#include <memory>
#include <span>
#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
#include <fmt/format.h>
#include <nihstro/inline_assembly.h>
#include "video_core/shader/shader_interpreter.h"
video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
#if CITRA_ARCH(x86_64)
#include "video_core/shader/shader_jit_x64_compiler.h"
video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
#elif CITRA_ARCH(arm64)
#include "video_core/shader/shader_jit_a64_compiler.h"
#endif
using JitShader = Pica::Shader::JitShader;
using ShaderInterpreter = Pica::Shader::InterpreterEngine;
using DestRegister = nihstro::DestRegister;
using OpCode = nihstro::OpCode;
using SourceRegister = nihstro::SourceRegister;
using Type = nihstro::InlineAsm::Type;
static constexpr Common::Vec4f vec4_inf = Common::Vec4f::AssignToAll(INFINITY);
static constexpr Common::Vec4f vec4_nan = Common::Vec4f::AssignToAll(NAN);
static constexpr Common::Vec4f vec4_one = Common::Vec4f::AssignToAll(1.0f);
static constexpr Common::Vec4f vec4_zero = Common::Vec4f::AssignToAll(0.0f);
namespace Catch {
template <>
video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
struct StringMaker<Common::Vec2f> {
static std::string convert(Common::Vec2f value) {
return fmt::format("({}, {})", value.x, value.y);
}
};
template <>
struct StringMaker<Common::Vec3f> {
static std::string convert(Common::Vec3f value) {
return fmt::format("({}, {}, {})", value.r(), value.g(), value.b());
}
};
template <>
struct StringMaker<Common::Vec4f> {
static std::string convert(Common::Vec4f value) {
return fmt::format("({}, {}, {}, {})", value.r(), value.g(), value.b(), value.a());
}
};
} // namespace Catch
static std::unique_ptr<Pica::Shader::ShaderSetup> CompileShaderSetup(
std::initializer_list<nihstro::InlineAsm> code) {
const auto shbin = nihstro::InlineAsm::CompileToRawBinary(code);
auto shader = std::make_unique<Pica::Shader::ShaderSetup>();
std::transform(shbin.program.begin(), shbin.program.end(), shader->program_code.begin(),
[](const auto& x) { return x.hex; });
std::transform(shbin.swizzle_table.begin(), shbin.swizzle_table.end(),
shader->swizzle_data.begin(), [](const auto& x) { return x.hex; });
return shader;
}
class ShaderTest {
public:
explicit ShaderTest(std::initializer_list<nihstro::InlineAsm> code)
: shader_setup(CompileShaderSetup(code)) {
shader_jit.Compile(&shader_setup->program_code, &shader_setup->swizzle_data);
}
video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
explicit ShaderTest(std::unique_ptr<Pica::Shader::ShaderSetup> input_shader_setup)
: shader_setup(std::move(input_shader_setup)) {
shader_jit.Compile(&shader_setup->program_code, &shader_setup->swizzle_data);
}
Common::Vec4f Run(std::span<const Common::Vec4f> inputs) {
Pica::Shader::UnitState shader_unit;
RunJit(shader_unit, inputs);
return {shader_unit.registers.output[0].x.ToFloat32(),
shader_unit.registers.output[0].y.ToFloat32(),
shader_unit.registers.output[0].z.ToFloat32(),
shader_unit.registers.output[0].w.ToFloat32()};
}
Common::Vec4f Run(std::initializer_list<float> inputs) {
std::vector<Common::Vec4f> input_vecs;
for (const float& input : inputs) {
input_vecs.emplace_back(input, 0.0f, 0.0f, 0.0f);
}
return Run(input_vecs);
}
Common::Vec4f Run(float input) {
return Run({input});
}
Common::Vec4f Run(std::initializer_list<Common::Vec4f> inputs) {
return Run(std::vector<Common::Vec4f>{inputs});
}
void RunJit(Pica::Shader::UnitState& shader_unit, std::span<const Common::Vec4f> inputs) {
for (std::size_t i = 0; i < inputs.size(); ++i) {
const Common::Vec4f& input = inputs[i];
shader_unit.registers.input[i].x = Pica::f24::FromFloat32(input.x);
shader_unit.registers.input[i].y = Pica::f24::FromFloat32(input.y);
shader_unit.registers.input[i].z = Pica::f24::FromFloat32(input.z);
shader_unit.registers.input[i].w = Pica::f24::FromFloat32(input.w);
}
shader_unit.registers.temporary.fill(
Common::Vec4<Pica::f24>::AssignToAll(Pica::f24::Zero()));
shader_jit.Run(*shader_setup, shader_unit, 0);
}
void RunJit(Pica::Shader::UnitState& shader_unit, float input) {
const Common::Vec4f input_vec(input, 0, 0, 0);
RunJit(shader_unit, {&input_vec, 1});
}
void RunInterpreter(Pica::Shader::UnitState& shader_unit,
std::span<const Common::Vec4f> inputs) {
for (std::size_t i = 0; i < inputs.size(); ++i) {
const Common::Vec4f& input = inputs[i];
shader_unit.registers.input[i].x = Pica::f24::FromFloat32(input.x);
shader_unit.registers.input[i].y = Pica::f24::FromFloat32(input.y);
shader_unit.registers.input[i].z = Pica::f24::FromFloat32(input.z);
shader_unit.registers.input[i].w = Pica::f24::FromFloat32(input.w);
}
shader_unit.registers.temporary.fill(
Common::Vec4<Pica::f24>::AssignToAll(Pica::f24::Zero()));
shader_interpreter.Run(*shader_setup, shader_unit);
}
void RunInterpreter(Pica::Shader::UnitState& shader_unit, float input) {
const Common::Vec4f input_vec(input, 0, 0, 0);
RunInterpreter(shader_unit, {&input_vec, 1});
}
public:
JitShader shader_jit;
ShaderInterpreter shader_interpreter;
std::unique_ptr<Pica::Shader::ShaderSetup> shader_setup;
};
TEST_CASE("ADD", "[video_core][shader][shader_jit]") {
const auto sh_input1 = SourceRegister::MakeInput(0);
const auto sh_input2 = SourceRegister::MakeInput(1);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::ADD, sh_output, sh_input1, sh_input2},
{OpCode::Id::END},
});
REQUIRE(shader.Run({+1.0f, -1.0f}).x == +0.0f);
REQUIRE(shader.Run({+0.0f, -0.0f}).x == -0.0f);
REQUIRE(std::isnan(shader.Run({+INFINITY, -INFINITY}).x));
REQUIRE(std::isinf(shader.Run({INFINITY, +1.0f}).x));
REQUIRE(std::isinf(shader.Run({INFINITY, -1.0f}).x));
}
video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
TEST_CASE("CALL", "[video_core][shader][shader_jit]") {
const auto sh_input = SourceRegister::MakeInput(0);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader_setup = CompileShaderSetup({
{OpCode::Id::NOP}, // call foo
{OpCode::Id::END},
// .proc foo
{OpCode::Id::NOP}, // call ex2
{OpCode::Id::END},
// .proc ex2
{OpCode::Id::EX2, sh_output, sh_input},
{OpCode::Id::END},
});
// nihstro does not support the CALL* instructions, so the instruction-binary must be manually
// inserted here:
nihstro::Instruction CALL = {};
CALL.opcode = nihstro::OpCode(nihstro::OpCode::Id::CALL);
// call foo
CALL.flow_control.dest_offset = 2;
CALL.flow_control.num_instructions = 1;
shader_setup->program_code[0] = CALL.hex;
// call ex2
CALL.flow_control.dest_offset = 4;
CALL.flow_control.num_instructions = 1;
shader_setup->program_code[2] = CALL.hex;
auto shader = ShaderTest(std::move(shader_setup));
REQUIRE(shader.Run(0.f).x == Catch::Approx(1.f));
}
TEST_CASE("DP3", "[video_core][shader][shader_jit]") {
const auto sh_input1 = SourceRegister::MakeInput(0);
const auto sh_input2 = SourceRegister::MakeInput(1);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::DP3, sh_output, sh_input1, sh_input2},
{OpCode::Id::END},
});
REQUIRE(shader.Run({vec4_inf, vec4_zero}).x == 0.0f);
REQUIRE(std::isnan(shader.Run({vec4_nan, vec4_zero}).x));
REQUIRE(shader.Run({vec4_one, vec4_one}).x == 3.0f);
}
TEST_CASE("DP4", "[video_core][shader][shader_jit]") {
const auto sh_input1 = SourceRegister::MakeInput(0);
const auto sh_input2 = SourceRegister::MakeInput(1);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::DP4, sh_output, sh_input1, sh_input2},
{OpCode::Id::END},
});
REQUIRE(shader.Run({vec4_inf, vec4_zero}).x == 0.0f);
REQUIRE(std::isnan(shader.Run({vec4_nan, vec4_zero}).x));
REQUIRE(shader.Run({vec4_one, vec4_one}).x == 4.0f);
}
TEST_CASE("DPH", "[video_core][shader][shader_jit]") {
const auto sh_input1 = SourceRegister::MakeInput(0);
const auto sh_input2 = SourceRegister::MakeInput(1);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::DPH, sh_output, sh_input1, sh_input2},
{OpCode::Id::END},
});
REQUIRE(shader.Run({vec4_inf, vec4_zero}).x == 0.0f);
REQUIRE(std::isnan(shader.Run({vec4_nan, vec4_zero}).x));
REQUIRE(shader.Run({vec4_one, vec4_one}).x == 4.0f);
REQUIRE(shader.Run({vec4_zero, vec4_one}).x == 1.0f);
}
TEST_CASE("LG2", "[video_core][shader][shader_jit]") {
const auto sh_input = SourceRegister::MakeInput(0);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::LG2, sh_output, sh_input},
{OpCode::Id::END},
});
REQUIRE(std::isnan(shader.Run(NAN).x));
REQUIRE(std::isnan(shader.Run(-1.f).x));
REQUIRE(std::isinf(shader.Run(0.f).x));
REQUIRE(shader.Run(4.f).x == Catch::Approx(2.f));
REQUIRE(shader.Run(64.f).x == Catch::Approx(6.f));
REQUIRE(shader.Run(1.e24f).x == Catch::Approx(79.7262742773f));
}
TEST_CASE("EX2", "[video_core][shader][shader_jit]") {
const auto sh_input = SourceRegister::MakeInput(0);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::EX2, sh_output, sh_input},
{OpCode::Id::END},
});
REQUIRE(std::isnan(shader.Run(NAN).x));
REQUIRE(shader.Run(-800.f).x == Catch::Approx(0.f));
REQUIRE(shader.Run(0.f).x == Catch::Approx(1.f));
REQUIRE(shader.Run(2.f).x == Catch::Approx(4.f));
REQUIRE(shader.Run(6.f).x == Catch::Approx(64.f));
REQUIRE(shader.Run(79.7262742773f).x == Catch::Approx(1.e24f));
REQUIRE(std::isinf(shader.Run(800.f).x));
}
TEST_CASE("MUL", "[video_core][shader][shader_jit]") {
const auto sh_input1 = SourceRegister::MakeInput(0);
const auto sh_input2 = SourceRegister::MakeInput(1);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::MUL, sh_output, sh_input1, sh_input2},
{OpCode::Id::END},
});
REQUIRE(shader.Run({+1.0f, -1.0f}).x == -1.0f);
REQUIRE(shader.Run({-1.0f, +1.0f}).x == -1.0f);
REQUIRE(shader.Run({INFINITY, 0.0f}).x == 0.0f);
REQUIRE(std::isnan(shader.Run({NAN, 0.0f}).x));
REQUIRE(shader.Run({+INFINITY, +INFINITY}).x == INFINITY);
REQUIRE(shader.Run({+INFINITY, -INFINITY}).x == -INFINITY);
}
TEST_CASE("SGE", "[video_core][shader][shader_jit]") {
const auto sh_input1 = SourceRegister::MakeInput(0);
const auto sh_input2 = SourceRegister::MakeInput(1);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::SGE, sh_output, sh_input1, sh_input2},
{OpCode::Id::END},
});
REQUIRE(shader.Run({INFINITY, 0.0f}).x == 1.0f);
REQUIRE(shader.Run({0.0f, INFINITY}).x == 0.0f);
REQUIRE(shader.Run({NAN, 0.0f}).x == 0.0f);
REQUIRE(shader.Run({0.0f, NAN}).x == 0.0f);
REQUIRE(shader.Run({+INFINITY, +INFINITY}).x == 1.0f);
REQUIRE(shader.Run({+INFINITY, -INFINITY}).x == 1.0f);
REQUIRE(shader.Run({-INFINITY, +INFINITY}).x == 0.0f);
REQUIRE(shader.Run({+1.0f, -1.0f}).x == 1.0f);
REQUIRE(shader.Run({-1.0f, +1.0f}).x == 0.0f);
}
TEST_CASE("SLT", "[video_core][shader][shader_jit]") {
const auto sh_input1 = SourceRegister::MakeInput(0);
const auto sh_input2 = SourceRegister::MakeInput(1);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::SLT, sh_output, sh_input1, sh_input2},
{OpCode::Id::END},
});
REQUIRE(shader.Run({INFINITY, 0.0f}).x == 0.0f);
REQUIRE(shader.Run({0.0f, INFINITY}).x == 1.0f);
REQUIRE(shader.Run({NAN, 0.0f}).x == 0.0f);
REQUIRE(shader.Run({0.0f, NAN}).x == 0.0f);
REQUIRE(shader.Run({+INFINITY, +INFINITY}).x == 0.0f);
REQUIRE(shader.Run({+INFINITY, -INFINITY}).x == 0.0f);
REQUIRE(shader.Run({-INFINITY, +INFINITY}).x == 1.0f);
REQUIRE(shader.Run({+1.0f, -1.0f}).x == 0.0f);
REQUIRE(shader.Run({-1.0f, +1.0f}).x == 1.0f);
}
TEST_CASE("FLR", "[video_core][shader][shader_jit]") {
const auto sh_input1 = SourceRegister::MakeInput(0);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::FLR, sh_output, sh_input1},
{OpCode::Id::END},
});
REQUIRE(shader.Run({0.5}).x == 0.0f);
REQUIRE(shader.Run({-0.5}).x == -1.0f);
REQUIRE(shader.Run({1.5}).x == 1.0f);
REQUIRE(shader.Run({-1.5}).x == -2.0f);
REQUIRE(std::isnan(shader.Run({NAN}).x));
REQUIRE(std::isinf(shader.Run({INFINITY}).x));
}
TEST_CASE("MAX", "[video_core][shader][shader_jit]") {
const auto sh_input1 = SourceRegister::MakeInput(0);
const auto sh_input2 = SourceRegister::MakeInput(1);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::MAX, sh_output, sh_input1, sh_input2},
{OpCode::Id::END},
});
REQUIRE(shader.Run({1.0f, 0.0f}).x == 1.0f);
REQUIRE(shader.Run({0.0f, 1.0f}).x == 1.0f);
REQUIRE(shader.Run({0.0f, +INFINITY}).x == +INFINITY);
// REQUIRE(shader.Run({0.0f, -INFINITY}).x == -INFINITY); // TODO: 3dbrew says this is -INFINITY
REQUIRE(std::isnan(shader.Run({0.0f, NAN}).x));
REQUIRE(shader.Run({NAN, 0.0f}).x == 0.0f);
REQUIRE(shader.Run({-INFINITY, +INFINITY}).x == +INFINITY);
}
TEST_CASE("MIN", "[video_core][shader][shader_jit]") {
const auto sh_input1 = SourceRegister::MakeInput(0);
const auto sh_input2 = SourceRegister::MakeInput(1);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::MIN, sh_output, sh_input1, sh_input2},
{OpCode::Id::END},
});
REQUIRE(shader.Run({1.0f, 0.0f}).x == 0.0f);
REQUIRE(shader.Run({0.0f, 1.0f}).x == 0.0f);
REQUIRE(shader.Run({0.0f, +INFINITY}).x == 0.0f);
REQUIRE(shader.Run({0.0f, -INFINITY}).x == -INFINITY);
REQUIRE(std::isnan(shader.Run({0.0f, NAN}).x));
REQUIRE(shader.Run({NAN, 0.0f}).x == 0.0f);
REQUIRE(shader.Run({-INFINITY, +INFINITY}).x == -INFINITY);
}
TEST_CASE("RCP", "[video_core][shader][shader_jit]") {
const auto sh_input = SourceRegister::MakeInput(0);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::RCP, sh_output, sh_input},
{OpCode::Id::END},
});
// REQUIRE(shader.Run({-0.0f}).x == INFINITY); // Violates IEEE
REQUIRE(shader.Run({0.0f}).x == INFINITY);
REQUIRE(shader.Run({INFINITY}).x == 0.0f);
REQUIRE(std::isnan(shader.Run({NAN}).x));
REQUIRE(shader.Run({16.0f}).x == Catch::Approx(0.0625f).margin(0.001f));
REQUIRE(shader.Run({8.0f}).x == Catch::Approx(0.125f).margin(0.001f));
REQUIRE(shader.Run({4.0f}).x == Catch::Approx(0.25f).margin(0.001f));
REQUIRE(shader.Run({2.0f}).x == Catch::Approx(0.5f).margin(0.001f));
REQUIRE(shader.Run({1.0f}).x == Catch::Approx(1.0f).margin(0.001f));
REQUIRE(shader.Run({0.5f}).x == Catch::Approx(2.0f).margin(0.001f));
REQUIRE(shader.Run({0.25f}).x == Catch::Approx(4.0f).margin(0.001f));
REQUIRE(shader.Run({0.125f}).x == Catch::Approx(8.0f).margin(0.002f));
REQUIRE(shader.Run({0.0625f}).x == Catch::Approx(16.0f).margin(0.004f));
}
TEST_CASE("RSQ", "[video_core][shader][shader_jit]") {
const auto sh_input = SourceRegister::MakeInput(0);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
{OpCode::Id::RSQ, sh_output, sh_input},
{OpCode::Id::END},
});
// REQUIRE(shader.Run({-0.0f}).x == INFINITY); // Violates IEEE
REQUIRE(std::isnan(shader.Run({-2.0f}).x));
REQUIRE(shader.Run({INFINITY}).x == 0.0f);
REQUIRE(std::isnan(shader.Run({-INFINITY}).x));
REQUIRE(std::isnan(shader.Run({NAN}).x));
REQUIRE(shader.Run({16.0f}).x == Catch::Approx(0.25f).margin(0.001f));
REQUIRE(shader.Run({8.0f}).x == Catch::Approx(1.0f / std::sqrt(8.0f)).margin(0.001f));
REQUIRE(shader.Run({4.0f}).x == Catch::Approx(0.5f).margin(0.001f));
REQUIRE(shader.Run({2.0f}).x == Catch::Approx(1.0f / std::sqrt(2.0f)).margin(0.001f));
REQUIRE(shader.Run({1.0f}).x == Catch::Approx(1.0f).margin(0.001f));
REQUIRE(shader.Run({0.5f}).x == Catch::Approx(1.0f / std::sqrt(0.5f)).margin(0.001f));
REQUIRE(shader.Run({0.25f}).x == Catch::Approx(2.0f).margin(0.001f));
REQUIRE(shader.Run({0.125f}).x == Catch::Approx(1.0 / std::sqrt(0.125)).margin(0.002f));
REQUIRE(shader.Run({0.0625f}).x == Catch::Approx(4.0f).margin(0.004f));
}
video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
TEST_CASE("Uniform Read", "[video_core][shader][shader_jit]") {
const auto sh_input = SourceRegister::MakeInput(0);
const auto sh_c0 = SourceRegister::MakeFloat(0);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
// mova a0.x, sh_input.x
{OpCode::Id::MOVA, DestRegister{}, "x", sh_input, "x", SourceRegister{}, "",
nihstro::InlineAsm::RelativeAddress::A1},
// mov sh_output.xyzw, c0[a0.x].xyzw
{OpCode::Id::MOV, sh_output, "xyzw", sh_c0, "xyzw", SourceRegister{}, "",
nihstro::InlineAsm::RelativeAddress::A1},
{OpCode::Id::END},
});
// Prepare shader uniforms
std::array<Common::Vec4f, 96> f_uniforms = {};
for (u32 i = 0; i < 96; ++i) {
const float color = (i * 2.0f) / 255.0f;
const auto color_f24 = Pica::f24::FromFloat32(color);
shader.shader_setup->uniforms.f[i] = {color_f24, color_f24, color_f24, Pica::f24::One()};
f_uniforms[i] = {color, color, color, 1.0f};
}
for (u32 i = 0; i < 96; ++i) {
const float index = static_cast<float>(i);
// Add some fractional values to test proper float->integer truncation
const float fractional = (i % 17) / 17.0f;
REQUIRE(shader.Run(index + fractional) == f_uniforms[i]);
}
}
TEST_CASE("Address Register Offset", "[video_core][shader][shader_jit]") {
const auto sh_input = SourceRegister::MakeInput(0);
const auto sh_c40 = SourceRegister::MakeFloat(40);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader = ShaderTest({
// mova a0.x, sh_input.x
{OpCode::Id::MOVA, DestRegister{}, "x", sh_input, "x", SourceRegister{}, "",
nihstro::InlineAsm::RelativeAddress::A1},
// mov sh_output.xyzw, c40[a0.x].xyzw
{OpCode::Id::MOV, sh_output, "xyzw", sh_c40, "xyzw", SourceRegister{}, "",
nihstro::InlineAsm::RelativeAddress::A1},
{OpCode::Id::END},
});
// Prepare shader uniforms
const bool inverted = true;
std::array<Common::Vec4f, 96> f_uniforms;
for (u32 i = 0; i < 0x80; i++) {
if (i >= 0x00 && i < 0x60) {
const u32 base = inverted ? (0x60 - i) : i;
const auto color = (base * 2.f) / 255.0f;
const auto color_f24 = Pica::f24::FromFloat32(color);
shader.shader_setup->uniforms.f[i] = {color_f24, color_f24, color_f24,
Pica::f24::One()};
f_uniforms[i] = {color, color, color, 1.f};
} else if (i >= 0x60 && i < 0x64) {
const u8 color = static_cast<u8>((i - 0x60) * 0x10);
shader.shader_setup->uniforms.i[i - 0x60] = {color, color, color, 255};
} else if (i >= 0x70 && i < 0x80) {
shader.shader_setup->uniforms.b[i - 0x70] = i >= 0x78;
}
}
REQUIRE(shader.Run(0.f) == f_uniforms[40]);
REQUIRE(shader.Run(13.f) == f_uniforms[53]);
REQUIRE(shader.Run(50.f) == f_uniforms[90]);
REQUIRE(shader.Run(60.f) == vec4_one);
REQUIRE(shader.Run(74.f) == vec4_one);
REQUIRE(shader.Run(87.f) == vec4_one);
REQUIRE(shader.Run(88.f) == f_uniforms[0]);
REQUIRE(shader.Run(128.f) == f_uniforms[40]);
REQUIRE(shader.Run(-40.f) == f_uniforms[0]);
REQUIRE(shader.Run(-42.f) == vec4_one);
REQUIRE(shader.Run(-70.f) == vec4_one);
REQUIRE(shader.Run(-73.f) == f_uniforms[95]);
REQUIRE(shader.Run(-127.f) == f_uniforms[41]);
REQUIRE(shader.Run(-129.f) == f_uniforms[40]);
}
video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
TEST_CASE("Dest Mask", "[video_core][shader][shader_jit]") {
const auto sh_input = SourceRegister::MakeInput(0);
const auto sh_output = DestRegister::MakeOutput(0);
video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
const auto shader = [&sh_input, &sh_output](const char* dest_mask) {
return std::unique_ptr<ShaderTest>(new ShaderTest{
{OpCode::Id::MOV, sh_output, dest_mask, sh_input, "xyzw", SourceRegister{}, ""},
{OpCode::Id::END},
});
};
const Common::Vec4f iota_vec = {1.0f, 2.0f, 3.0f, 4.0f};
REQUIRE(shader("x")->Run({iota_vec}).x == iota_vec.x);
REQUIRE(shader("y")->Run({iota_vec}).y == iota_vec.y);
REQUIRE(shader("z")->Run({iota_vec}).z == iota_vec.z);
REQUIRE(shader("w")->Run({iota_vec}).w == iota_vec.w);
REQUIRE(shader("xy")->Run({iota_vec}).xy() == iota_vec.xy());
REQUIRE(shader("xz")->Run({iota_vec}).xz() == iota_vec.xz());
REQUIRE(shader("xw")->Run({iota_vec}).xw() == iota_vec.xw());
REQUIRE(shader("yz")->Run({iota_vec}).yz() == iota_vec.yz());
REQUIRE(shader("yw")->Run({iota_vec}).yw() == iota_vec.yw());
REQUIRE(shader("zw")->Run({iota_vec}).zw() == iota_vec.zw());
REQUIRE(shader("xyz")->Run({iota_vec}).xyz() == iota_vec.xyz());
REQUIRE(shader("xyw")->Run({iota_vec}).xyw() == iota_vec.xyw());
REQUIRE(shader("xzw")->Run({iota_vec}).xzw() == iota_vec.xzw());
REQUIRE(shader("yzw")->Run({iota_vec}).yzw() == iota_vec.yzw());
REQUIRE(shader("xyzw")->Run({iota_vec}) == iota_vec);
}
video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
TEST_CASE("MAD", "[video_core][shader][shader_jit]") {
const auto sh_input1 = SourceRegister::MakeInput(0);
const auto sh_input2 = SourceRegister::MakeInput(1);
const auto sh_input3 = SourceRegister::MakeInput(2);
const auto sh_output = DestRegister::MakeOutput(0);
video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
auto shader_setup = CompileShaderSetup({
// TODO: Requires fix from https://github.com/neobrain/nihstro/issues/68
// {OpCode::Id::MAD, sh_output, sh_input1, sh_input2, sh_input3},
{OpCode::Id::NOP},
{OpCode::Id::END},
});
// nihstro does not support the MAD* instructions, so the instruction-binary must be manually
// inserted here:
nihstro::Instruction MAD = {};
MAD.opcode = nihstro::OpCode::Id::MAD;
MAD.mad.operand_desc_id = 0;
MAD.mad.src1 = sh_input1;
MAD.mad.src2 = sh_input2;
MAD.mad.src3 = sh_input3;
MAD.mad.dest = sh_output;
shader_setup->program_code[0] = MAD.hex;
nihstro::SwizzlePattern swizzle = {};
swizzle.dest_mask = 0b1111;
swizzle.SetSelectorSrc1(0, SwizzlePattern::Selector::x);
swizzle.SetSelectorSrc1(1, SwizzlePattern::Selector::y);
swizzle.SetSelectorSrc1(2, SwizzlePattern::Selector::z);
swizzle.SetSelectorSrc1(3, SwizzlePattern::Selector::w);
swizzle.SetSelectorSrc2(0, SwizzlePattern::Selector::x);
swizzle.SetSelectorSrc2(1, SwizzlePattern::Selector::y);
swizzle.SetSelectorSrc2(2, SwizzlePattern::Selector::z);
swizzle.SetSelectorSrc2(3, SwizzlePattern::Selector::w);
swizzle.SetSelectorSrc3(0, SwizzlePattern::Selector::x);
swizzle.SetSelectorSrc3(1, SwizzlePattern::Selector::y);
swizzle.SetSelectorSrc3(2, SwizzlePattern::Selector::z);
swizzle.SetSelectorSrc3(3, SwizzlePattern::Selector::w);
shader_setup->swizzle_data[0] = swizzle.hex;
auto shader = ShaderTest(std::move(shader_setup));
REQUIRE(shader.Run({vec4_zero, vec4_zero, vec4_zero}) == vec4_zero);
REQUIRE(shader.Run({vec4_one, vec4_one, vec4_one}) == (vec4_one * 2.0f));
REQUIRE(shader.Run({vec4_inf, vec4_zero, vec4_zero}) == vec4_zero);
REQUIRE(shader.Run({vec4_nan, vec4_zero, vec4_zero}) == vec4_nan);
}
TEST_CASE("Nested Loop", "[video_core][shader][shader_jit]") {
const auto sh_input = SourceRegister::MakeInput(0);
const auto sh_temp = SourceRegister::MakeTemporary(0);
const auto sh_output = DestRegister::MakeOutput(0);
auto shader_test = ShaderTest({
// clang-format off
{OpCode::Id::MOV, sh_temp, sh_input},
{OpCode::Id::LOOP, 0},
{OpCode::Id::LOOP, 1},
{OpCode::Id::ADD, sh_temp, sh_temp, sh_input},
{Type::EndLoop},
{Type::EndLoop},
{OpCode::Id::MOV, sh_output, sh_temp},
{OpCode::Id::END},
// clang-format on
});
{
shader_test.shader_setup->uniforms.i[0] = {4, 0, 1, 0};
shader_test.shader_setup->uniforms.i[1] = {4, 0, 1, 0};
Common::Vec4<u8> loop_parms{shader_test.shader_setup->uniforms.i[0]};
const int expected_aL = loop_parms[1] + ((loop_parms[0] + 1) * loop_parms[2]);
const float input = 1.0f;
const float expected_out = (((shader_test.shader_setup->uniforms.i[0][0] + 1) *
(shader_test.shader_setup->uniforms.i[1][0] + 1)) *
input) +
input;
Pica::Shader::UnitState shader_unit_jit;
shader_test.RunJit(shader_unit_jit, input);
REQUIRE(shader_unit_jit.address_registers[2] == expected_aL);
REQUIRE(shader_unit_jit.registers.output[0].x.ToFloat32() == Catch::Approx(expected_out));
}
{
shader_test.shader_setup->uniforms.i[0] = {9, 0, 2, 0};
shader_test.shader_setup->uniforms.i[1] = {7, 0, 1, 0};
const Common::Vec4<u8> loop_parms{shader_test.shader_setup->uniforms.i[0]};
const int expected_aL = loop_parms[1] + ((loop_parms[0] + 1) * loop_parms[2]);
const float input = 1.0f;
const float expected_out = (((shader_test.shader_setup->uniforms.i[0][0] + 1) *
(shader_test.shader_setup->uniforms.i[1][0] + 1)) *
input) +
input;
Pica::Shader::UnitState shader_unit_jit;
shader_test.RunJit(shader_unit_jit, input);
REQUIRE(shader_unit_jit.address_registers[2] == expected_aL);
REQUIRE(shader_unit_jit.registers.output[0].x.ToFloat32() == Catch::Approx(expected_out));
}
}
video_core: Implement an arm64 shader-jit backend (#7002) * externals: Add oaksim submodule Used for emitting ARM64 assembly * common: Implement aarch64 ABI Utilize oaknut to implement a stack frame. * tests: Allow shader-jit tests for x64 and a64 Run the shader-jit tests for both x86_64 and arm64 targets * video_core: Initialize arm64 shader-jit backend Passes all current unit tests! * shader_jit_a64: protect/unprotect memory when jit-ing Required on MacOS. Memory needs to be fully unprotected and then re-protected when writing or there will be memory access errors on MacOS. * shader_jit_a64: Fix ARM64-Imm overflow These conditionals were throwing exceptions since the immediate values were overflowing the available space in the `EOR` instructions. Instead they are generated from `MOV` and then `EOR`-ed after. * shader_jit_a64: Fix Geometry shader conditional * shader_jit_a64: Replace `ADRL` with `MOVP2R` Fixes some immediate-generation exceptions. * common/aarch64: Fix CallFarFunction * shader_jit_a64: Optimize `SantitizedMul` Co-authored-by: merryhime <merryhime@users.noreply.github.com> * shader_jit_a64: Fix address register offset behavior Based on https://github.com/citra-emu/citra/pull/6942 Passes unit tests. * shader_jit_a64: Fix `RET` address offset A64 stack is 16-byte aligned rather than 8. So a direct port of the x64 code won't work. Fixes weird branches into invalid memory for any shaders with subroutines. * shader_jit_a64: Increase max program size Tuned for A64 program size. * shader_jit_a64: Use `UBFX` for extracting loop-state Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `SUB+CMP` to `SUBS` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Optimize `CMP+B` to `CBNZ` Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Use `FMOV` for `ONE` vector Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove x86-specific documentation * shader_jit_a64: Use `UBFX` to extract exponent Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit_a64: Remove redundant MIN/MAX `SRC2`-NaN check Special handling only needs to check SRC1 for NaN, not SRC2. It would work as follows in the four possible cases: No NaN: No special handling needed. Only SRC1 is NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Only SRC2 is NaN: FMAX automatically picks SRC2 because it always picks the NaN if there is one. Both SRC1 and SRC2 are NaN: The special handling is triggered because SRC1 is NaN, and SRC2 is picked. Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests:: Add catch-stringifier for vec2f/vec3f * shader_jit/tests: Add Dest Mask unit test * shader_jit_a64: Fix Dest-Mask `BSL` operand order Passes the dest-mask unit tests now. * shader_jit_a64: Use `MOVI` for DestEnable mask Accelerate certain cases of masking with MOVI as well Co-authored-by: JosJuice <JosJuice@users.noreply.github.com> * shader_jit/tests: Add source-swizzle unit test This is not expansive. Generating all `4^4` cases seems to make Catch2 crash. So I've added some component-masking(non-reordering) tests based on the Dest-Mask unit-test and some additional ones to test broadcasts/splats and component re-ordering. * shader_jit_a64: Fix swizzle index generation This was still generating `SHUFPS` indices and not the ones that we wanted for the `TBL` instruction. Passes all unit tests now. * shader_jit/tests: Add `ShaderSetup` constructor to `ShaderTest` Rather than using the direct output of `CompileShaderSetup` allow a `ShaderSetup` object to be passed in directly. This enabled the ability emit assembly that is not directly supported by nihstro. * shader_jit/tests: Add `CALL` unit-test Tests nested `CALL` instructions to eventually reach an `EX2` instruction. EX2 is picked in particular since it is implemented as an even deeper dispatch and ensures subroutines are properly implemented between `CALL` instructions and implementation-calls. * shader_jit_a64: Fix nested `BL` subroutines `lr` was getting writen over by nested calls to `BL`, causing undefined behavior with mixtures of `CALL`, `EX2`, and `LG2` instructions. Each usage of `BL` is now protected with a stach push/pop to preserve and restore teh `lr` register to allow nested subroutines to work properly. * shader_jit/tests: Allocate generated tests on heap Each of these generated shader-test objects were causing the stack to overflow. Allocate each of the generated tests on the heap and use unique_ptr so they only exist within the life-time of the `REQUIRE` statement. * shader_jit_a64: Preserve `lr` register from external function calls `EMIT` makes an external function call, and should be preserving `lr` * shader_jit/tests: Add `MAD` unit-test The Inline Asm version requires an upstream fix: https://github.com/neobrain/nihstro/issues/68 Instead, the program code is manually configured and added. * shader_jit/tests: Fix uninitialized instructions These `union`-type instruction-types were uninitialized, causing tests to indeterminantly fail at times. * shader_jit_a64: Remove unneeded `MOV` Residue from the direct-port of x64 code. * shader_jit_a64: Use `std::array` for `instr_table` Add some type-safety and const-correctness around this type as well. * shader_jit_a64: Avoid c-style offset casting Add some more const-correctness to this function as well. * video_core: Add arch preprocessor comments * common/aarch64: Use X16 as the veneer register https://developer.arm.com/documentation/102374/0101/Procedure-Call-Standard * shader_jit/tests: Add uniform reading unit-test Particularly to ensure that addresses are being properly truncated * common/aarch64: Use `X0` as `ABI_RETURN` `X8` is used as the indirect return result value in the case that the result is bigger than 128-bits. Principally `X0` is the general-case return register though. * common/aarch64: Add veneer register note `LR` is generally overwritten by `BLR` anyways, and would also be a safe veneer to utilize for far-calls. * shader_jit_a64: Remove unneeded scratch register from `SanitizedMul` * shader_jit_a64: Fix CALLU condition Should be `EQ` not `NE`. Fixes the regression on Kid Icarus. No known regressions anymore! --------- Co-authored-by: merryhime <merryhime@users.noreply.github.com> Co-authored-by: JosJuice <JosJuice@users.noreply.github.com>
2023-11-05 20:40:31 +00:00
TEST_CASE("Source Swizzle", "[video_core][shader][shader_jit]") {
const auto sh_input = SourceRegister::MakeInput(0);
const auto sh_output = DestRegister::MakeOutput(0);
const auto shader = [&sh_input, &sh_output](const char* swizzle) {
return std::unique_ptr<ShaderTest>(new ShaderTest{
{OpCode::Id::MOV, sh_output, "xyzw", sh_input, swizzle, SourceRegister{}, ""},
{OpCode::Id::END},
});
};
const Common::Vec4f iota_vec = {1.0f, 2.0f, 3.0f, 4.0f};
REQUIRE(shader("x")->Run({iota_vec}).x == iota_vec.x);
REQUIRE(shader("y")->Run({iota_vec}).x == iota_vec.y);
REQUIRE(shader("z")->Run({iota_vec}).x == iota_vec.z);
REQUIRE(shader("w")->Run({iota_vec}).x == iota_vec.w);
REQUIRE(shader("xy")->Run({iota_vec}).xy() == iota_vec.xy());
REQUIRE(shader("xz")->Run({iota_vec}).xy() == iota_vec.xz());
REQUIRE(shader("xw")->Run({iota_vec}).xy() == iota_vec.xw());
REQUIRE(shader("yz")->Run({iota_vec}).xy() == iota_vec.yz());
REQUIRE(shader("yw")->Run({iota_vec}).xy() == iota_vec.yw());
REQUIRE(shader("zw")->Run({iota_vec}).xy() == iota_vec.zw());
REQUIRE(shader("yy")->Run({iota_vec}).xy() == iota_vec.yy());
REQUIRE(shader("wx")->Run({iota_vec}).xy() == iota_vec.wx());
REQUIRE(shader("xyz")->Run({iota_vec}).xyz() == iota_vec.xyz());
REQUIRE(shader("xyw")->Run({iota_vec}).xyz() == iota_vec.xyw());
REQUIRE(shader("xzw")->Run({iota_vec}).xyz() == iota_vec.xzw());
REQUIRE(shader("yzw")->Run({iota_vec}).xyz() == iota_vec.yzw());
REQUIRE(shader("yyy")->Run({iota_vec}).xyz() == iota_vec.yyy());
REQUIRE(shader("yxw")->Run({iota_vec}).xyz() == iota_vec.yxw());
REQUIRE(shader("xyzw")->Run({iota_vec}) == iota_vec);
REQUIRE(shader("wzxy")->Run({iota_vec}) ==
Common::Vec4f(iota_vec.w, iota_vec.z, iota_vec.x, iota_vec.y));
REQUIRE(shader("yyyy")->Run({iota_vec}) ==
Common::Vec4f(iota_vec.y, iota_vec.y, iota_vec.y, iota_vec.y));
}
#endif // CITRA_ARCH(x86_64) || CITRA_ARCH(arm64)