#include "util.hpp" #include #include #include <3ds.h> #include struct C3DFixture { C3DFixture() { C3D_Init(C3D_DEFAULT_CMDBUF_SIZE); } ~C3DFixture() { C3D_Fini(); } }; constexpr auto gradient_quad = TestQuadBuilder() .AtBR(RESULT_COLOR, 1, 0, 0, 0) .AtTR(RESULT_COLOR, 1, 1, 0, 0) .AtTL(RESULT_COLOR, 0, 0, 0, 0) .AtBL(RESULT_COLOR, 0, 1, 0, 0); TEST_CASE_METHOD(C3DFixture, "tex env", "") { auto quad = gradient_quad.Build(512, 512); // Consistency check: Color passthrough should yield the interpolated color value SECTION("Consistency") { quad.Begin(); C3D_TexEnv *env = C3D_GetTexEnv(0); C3D_TexEnvInit(env); env->color = 0x00ffffff; C3D_TexEnvInit(env); C3D_TexEnvSrc(env, C3D_Both, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR); C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); quad.DrawAndFinish(); // TODO: Consider using multiplier to increase resolution? for (int y = 0; y < quad.quad_width; ++y) { INFO(y); CHECK(quad.GetResult(0, y).r() == (int)quad.InterpolatedColorAt(0, y)); } } // Intra-stage precision: Intermediate results of tev stages are rounded to integer results. Rounding is applied before multiplication with the scale parameter. // We test this by multiplying the input color by 0.5 and using the scale parameter in a single stage. SECTION("Intra-stage precision") { quad.Begin(); C3D_TexEnv *env = C3D_GetTexEnv(0); C3D_TexEnvInit(env); env->color = 0x00ffffff; // Produce 0.5 as a constant. If tev-stages use higher bitwidths internally, this will be more accurate than the 8-bit constant input C3D_TexEnvSrc(env, C3D_Both, GPU_CONSTANT, GPU_CONSTANT, GPU_PRIMARY_COLOR); C3D_TexEnvOpRgb(env, GPU_TEVOP_RGB_SRC_COLOR, GPU_TEVOP_RGB_SRC_ALPHA); C3D_TexEnvFunc(env, C3D_Both, GPU_ADD_SIGNED); env = C3D_GetTexEnv(1); C3D_TexEnvInit(env); C3D_TexEnvSrc(env, C3D_Both, GPU_PRIMARY_COLOR, GPU_PREVIOUS, GPU_PRIMARY_COLOR); C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE); C3D_TexEnvScale(env, C3D_Both, GPU_TEVSCALE_2); quad.DrawAndFinish(); for (int y = 0; y < quad.quad_width; ++y) { INFO(y); CHECK(quad.GetResult(0, y).r() == ((int)quad.InterpolatedColorAt(0, y)) / 2 * 2); } } // When a stage produces 0.5 as a result, you could imagine it being represented as either of: // * Fixed-point 0x7f // * Fixed-point 0x80 // * A higher-precision inter-stage representation closer (or equal) to the "true" 0.5 // // To distinguish these three potential values, you can multiply the result by 2: // * 0x7f * 2 = 0xfe // * 0x80 * 2 = 0x100 (clamped to 0xff) // * perfect 0.5 * 2 = 0xff // To distinguish the last two cases, we can subtract 1 before doubling the value. // For 0.5 computed within a tev stage, the observation indicates the representation 0x7f. // For 0.5 fetched from the vertex color, the observation suggests 0x80. // // Generally, it seems vertex colors are rounded towards the nearest integer before entering tev. SECTION("0.5 is 0x7f or 0x80") { auto stage_setup = GENERATE(0, 1, 2); auto config = stage_setup != 2 ? gradient_quad : TestQuadBuilder().Constant(RESULT_COLOR, 0.5f); auto quad = config.Build(512, 512); quad.Begin(); C3D_TexEnv *env = C3D_GetTexEnv(0); C3D_TexEnvInit(env); env->color = 0x00ffffff; // aabbggrr if (stage_setup == 0) { // Produce 0.5 as a constant, and double it immediately after C3D_TexEnvSrc(env, C3D_Both, GPU_CONSTANT, GPU_CONSTANT, GPU_CONSTANT); C3D_TexEnvOpRgb(env, GPU_TEVOP_RGB_SRC_COLOR, GPU_TEVOP_RGB_SRC_ALPHA); C3D_TexEnvFunc(env, C3D_Both, GPU_ADD_SIGNED); C3D_TexEnvScale(env, C3D_Both, GPU_TEVSCALE_2); } else if (stage_setup == 1) { // Produce 0.5 as a constant, and double it in the next stage C3D_TexEnvSrc(env, C3D_Both, GPU_CONSTANT, GPU_CONSTANT, GPU_CONSTANT); C3D_TexEnvOpRgb(env, GPU_TEVOP_RGB_SRC_COLOR, GPU_TEVOP_RGB_SRC_ALPHA); C3D_TexEnvFunc(env, C3D_Both, GPU_ADD_SIGNED); env = C3D_GetTexEnv(1); C3D_TexEnvInit(env); C3D_TexEnvSrc(env, C3D_Both, GPU_PREVIOUS, GPU_PREVIOUS, GPU_PREVIOUS); C3D_TexEnvFunc(env, C3D_Both, GPU_ADD); } else { // Read 0.5 from render target. // This actually gets turned into fixed-point 0x80, so we'll have // to subtract one to avoid overflows C3D_TexEnvSrc(env, C3D_Both, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR); C3D_TexEnvOpRgb(env, GPU_TEVOP_RGB_SRC_COLOR, GPU_TEVOP_RGB_SRC_ALPHA); C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); env = C3D_GetTexEnv(1); C3D_TexEnvInit(env); env->color = 0x01010101; C3D_TexEnvSrc(env, C3D_Both, GPU_PREVIOUS, GPU_CONSTANT, GPU_PRIMARY_COLOR); C3D_TexEnvFunc(env, C3D_Both, GPU_SUBTRACT); C3D_TexEnvScale(env, C3D_Both, GPU_TEVSCALE_2); } quad.DrawAndFinish(); CHECK(quad.GetResult(0, 0).r() == 0x7f * 2); } } TEST_CASE_METHOD(C3DFixture, "Combiner operations", "") { auto quad = TestQuadBuilder() .AtBR(RESULT_COLOR, 1, 0, 0.5, 0) // bottom-right .AtTR(RESULT_COLOR, 1, 1, 0.5, 0) // top-right .AtTL(RESULT_COLOR, 0, 1, 0.5, 0) // top-left .AtBL(RESULT_COLOR, 0, 0, 0.5, 0) // bottom-left .Build(512, 512); auto op = GENERATE(GPU_MODULATE, GPU_ADD, GPU_ADD_SIGNED, GPU_SUBTRACT); for (int col2 = 0; col2 < 0x100; ++col2) { quad.Begin(); C3D_TexEnv *env = C3D_GetTexEnv(0); C3D_TexEnvInit(env); env->color = col2 * 0x01010101u; C3D_TexEnvSrc(env, C3D_Both, GPU_PRIMARY_COLOR, GPU_CONSTANT, GPU_PRIMARY_COLOR); C3D_TexEnvFunc(env, C3D_Both, op); quad.DrawAndFinish(); for (int y = 0; y < quad.quad_width; ++y) { int expected = std::invoke([=](int color) { switch (op) { case GPU_MODULATE: return (color * col2 + 127) / 255; case GPU_ADD: return color + col2; case GPU_ADD_SIGNED: return color + col2 - 128; case GPU_SUBTRACT: return color - col2; default: return 0; } }, (int)quad.InterpolatedColorAt(0, y)); // TODO: Actually should round towards nearest here expected = std::max(0, std::min(255, expected)); INFO(std::to_string((int)quad.InterpolatedColorAt(0, y)) + " op" + std::to_string(op) + " " + std::to_string(col2) + " at y=" + std::to_string(y)); CHECK(quad.GetResult(0, y).r() == expected); } } }