From dcd64802a354db828085c4d2190951011ab245cc Mon Sep 17 00:00:00 2001 From: Wunkolo Date: Tue, 19 Mar 2024 10:13:04 -0700 Subject: [PATCH] Refactor `ShaderJITTest` into a specialization of `ShaderInterpreterTest` --- tests/shader.cpp | 62 +++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/tests/shader.cpp b/tests/shader.cpp index edb2743f..29c07481 100644 --- a/tests/shader.cpp +++ b/tests/shader.cpp @@ -7,6 +7,7 @@ #include #include #include +#include using namespace Floats; static const nihstro::SourceRegister input0 = nihstro::SourceRegister::MakeInput(0); @@ -28,48 +29,59 @@ static std::unique_ptr assembleVertexShader(std::initializer_list shader = {}; + virtual void runShader() { shader->run(); } + public: explicit ShaderInterpreterTest(std::initializer_list code) : shader(assembleVertexShader(code)) {} - // Multiple inputs, singular scalar output - float runScalar(std::initializer_list inputs) { - usize inputIndex = 0; - for (const float& input : inputs) { - const std::array input_vec = std::array{f24::fromFloat32(input), f24::zero(), f24::zero(), f24::zero()}; - shader->inputs[inputIndex++] = input_vec; - } - shader->run(); - return shader->outputs[0][0]; + std::span> runTest(std::span> inputs) { + std::copy(inputs.begin(), inputs.end(), shader->inputs.begin()); + runShader(); + return shader->outputs; } + // Each input is written to the x component of sequential input registers + // The first output vector is returned + const std::array& runVector(std::initializer_list inputs) { + std::vector> inputsVec; + for (const float& input : inputs) { + const std::array inputVec = { + f24::fromFloat32(input), + f24::zero(), + f24::zero(), + f24::zero(), + }; + inputsVec.emplace_back(inputVec); + } + return runTest(inputsVec)[0]; + } + + // Each input is written to the x component of sequential input registers + // The x component of the first output + float runScalar(std::initializer_list inputs) { return runVector(inputs)[0].toFloat32(); } + + [[nodiscard]] std::array, 96>& floatUniforms() const { return shader->floatUniforms; } + [[nodiscard]] std::array, 4>& intUniforms() const { return shader->intUniforms; } + [[nodiscard]] u32& boolUniform() const { return shader->boolUniform; } + static std::unique_ptr assembleTest(std::initializer_list code) { return std::make_unique(code); } }; #if defined(PANDA3DS_SHADER_JIT_SUPPORTED) -class ShaderJITTest final { +class ShaderJITTest final : public ShaderInterpreterTest { private: - std::unique_ptr shader = {}; ShaderJIT shaderJit = {}; - public: - explicit ShaderJITTest(std::initializer_list code) : shader(assembleVertexShader(code)) { shaderJit.prepare(*shader.get()); } + void runShader() override { shaderJit.run(*shader); } - // Multiple inputs, singular scalar output - float runScalar(std::initializer_list inputs) { - usize inputIndex = 0; - for (const float& input : inputs) { - const std::array input_vec = std::array{f24::fromFloat32(input), f24::zero(), f24::zero(), f24::zero()}; - shader->inputs[inputIndex++] = input_vec; - } - shaderJit.run(*shader.get()); - return shader->outputs[0][0]; - } + public: + explicit ShaderJITTest(std::initializer_list code) : ShaderInterpreterTest(code) { shaderJit.prepare(*shader); } static std::unique_ptr assembleTest(std::initializer_list code) { return std::make_unique(code);