#include <3ds.h> #include #include #include #include struct Color { uint32_t r() const { return comp_r; } uint32_t g() const { return comp_g; } uint32_t b() const { return comp_b; } uint32_t a() const { return comp_a; } uint32_t operator[](int index) const { if (index == 0) { return comp_r; } else if (index == 1) { return comp_g; } else if (index == 2) { return comp_b; } else { return comp_a; } } private: // Catch attempts to print these as "char" values, so only allow access through the member functions instead uint8_t comp_a; uint8_t comp_b; uint8_t comp_g; uint8_t comp_r; }; struct TestQuad; struct TestMosaicQuad; struct TestQuadBuilder { struct AttributeSlot { constexpr AttributeSlot() = default; std::array x0y0 {}; // bottom-left (as displayed on LCD) std::array x1y0 {}; // bottom-right (as displayed on LCD) std::array x0y1 {}; // top-left (as displayed on LCD) std::array x1y1 {}; // top-right (as displayed on LCD) int coords_set = 0; }; std::array attributes; constexpr TestQuadBuilder() { AtBR(RESULT_POSITION, -1.0f, -1.0f, -1.0f, 1.0f); // bottom-right AtTR(RESULT_POSITION, +1.0f, -1.0f, -1.0f, 1.0f); // top-right AtTL(RESULT_POSITION, +1.0f, +1.0f, -1.0f, 1.0f); // top-left AtBL(RESULT_POSITION, -1.0f, +1.0f, -1.0f, 1.0f); // bottom-left } /* x0y1->x1y1 ^ ^ | | x0y0->x1y0 */ constexpr TestQuadBuilder& AtBR(DVLE_outputAttribute_t type, float a, float b = 0.0, float c = 0.0, float d = 0.0) { return SetAttr(type, a, b, c, d, 0b10); } constexpr TestQuadBuilder& AtTR(DVLE_outputAttribute_t type, float a, float b = 0.0, float c = 0.0, float d = 0.0) { return SetAttr(type, a, b, c, d, 0b11); } constexpr TestQuadBuilder& AtTL(DVLE_outputAttribute_t type, float a, float b = 0.0, float c = 0.0, float d = 0.0) { return SetAttr(type, a, b, c, d, 0b01); } constexpr TestQuadBuilder& AtBL(DVLE_outputAttribute_t type, float a, float b = 0.0, float c = 0.0, float d = 0.0) { return SetAttr(type, a, b, c, d, 0b00); } constexpr TestQuadBuilder& Constant(DVLE_outputAttribute_t type, float a, float b = 0.0, float c = 0.0, float d = 0.0) { SetAttr(type, a, b, c, d, 0b00); SetAttr(type, a, b, c, d, 0b01); SetAttr(type, a, b, c, d, 0b10); return SetAttr(type, a, b, c, d, 0b11); } TestQuad Build(int width, int height) const; TestMosaicQuad BuildMosaic(int width, int height) const; private: constexpr TestQuadBuilder& SetAttr(DVLE_outputAttribute_t type, float a, float b, float c, float d, int coords) { if (type > RESULT_VIEW) { throw std::runtime_error("Invalid attribute type for TestQuad"); } auto& slot = (coords == 0b00) ? attributes[type].x0y0 : (coords == 0b01) ? attributes[type].x0y1 : (coords == 0b10) ? attributes[type].x1y0 : attributes[type].x1y1; slot = { a, b, c, d }; attributes[type].coords_set |= 1 << coords; return *this; } }; struct TestQuadBase { int quad_height; int quad_width; protected: // TODO: Rename C3D_RenderTarget* test_target; void* test_untiled; void* untiled_rgb565; DVLB_s *vshader_dvlb; shaderProgram_s program; TestQuadBase(const TestQuadBuilder&, int width, int height); ~TestQuadBase(); void Begin(); void DrawAndFinish(); private: void InitShader(const TestQuadBuilder&); }; struct TestQuad : TestQuadBase { C3D_AttrInfo attr_info; C3D_BufInfo buf_info; void *vertex_data; TestQuad(const TestQuadBuilder&, int width = 64, int height = 64); TestQuad(const TestQuad&) = delete; TestQuad& operator=(const TestQuad&) = delete; void Modify(const TestQuadBuilder&); /** * Returns the interpolated color value used for processing the given * pixel, in the range [0..255]. * * This is nontrivial to compute since colors are interpolated at pixel * centers instead of pixel edges. * * TODO: Add support for generic colors * TODO: Actually, this only works when interpolating along one of the quad edges */ float InterpolatedColorAt(int x, int y) { float pixel_center = 2 * y + 1.f + 2/1024.f; return (pixel_center * 255.f + 15.0f * (y / 128) - 15.0f * (y / 400)) / (float)(quad_width * 2); // tested up to 512x512 } // TODO: this starts at the top! Color GetResult(int x, int y) { Color ret; memcpy(&ret, (char*)test_untiled + 4 * x + 4 * y * quad_height, sizeof(ret)); return ret; } void Begin(); /** * Finalizes the test frame and copies the output to the un-tiled staging image. * * For debugging, the image is also copied onto the framebuffer console. */ void DrawAndFinish(); ~TestQuad(); }; // A mosaic of 1x1 quads building a bigger quad. // This can be used instead of TestQuad if each pixel must be rendered with // precisely controlled vertex attribute values. struct TestMosaicQuad : TestQuadBase { C3D_AttrInfo attr_info; C3D_BufInfo buf_info; void *vertex_data; std::array attributes; TestMosaicQuad(const TestQuadBuilder&, int width = 64, int height = 64); TestMosaicQuad(const TestQuad&) = delete; TestMosaicQuad& operator=(const TestQuad&) = delete; void Modify(const TestQuadBuilder&); std::array InterpolatedAttributeAt(DVLE_outputAttribute_t attr_index, int x, int y, bool normalize = false) { // Setup other attributes as constants across the 1x1 quad const float factor_x = (float)x / (quad_width - 1); const float factor_y = (float)y / (quad_height - 1); auto& attr = attributes[attr_index]; std::array ret; for (int i = 0; i < 4; ++i) { float mean_bot = attr.x0y0[i] * (1.0 - factor_x) + attr.x1y0[i] * factor_x; float mean_top = attr.x0y1[i] * (1.0 - factor_x) + attr.x1y1[i] * factor_x; ret[i] = mean_top * (1.0 - factor_y) + mean_bot * factor_y; } if (normalize) { float length = 0; for (int i = 0; i < 4; ++i) { length += ret[i] * ret[i]; } length = sqrt(length); if (length == 0.f) { length = 1.f; } for (int i = 0; i < 4; ++i) { ret[i] /= length; } } return ret; } // TODO: this starts at the top! // TODO: x and y are swapped Color GetResult(int x, int y) { Color ret; memcpy(&ret, (char*)test_untiled + 4 * x + 4 * y * quad_height, sizeof(ret)); return ret; } void Begin(); /** * Finalizes the test frame and copies the output to the un-tiled staging image. * * For debugging, the image is also copied onto the framebuffer console. */ void DrawAndFinish(); ~TestMosaicQuad(); };