#ifndef __JABYENGINE_GPU_PRIMITIVES_HPP__ #define __JABYENGINE_GPU_PRIMITIVES_HPP__ #include "gpu_types.hpp" #include namespace JabyEngine { namespace GPU { namespace internal { struct Code { static constexpr uint8_t CmdValue = 0b001; static constexpr auto BitCorrection = 24; static constexpr auto CmdID = BitRange::from_to(29 - BitCorrection, 31 - BitCorrection); static constexpr auto GouraudShading = Bit(28 - BitCorrection); static constexpr auto FlatShading = !GouraudShading; static constexpr auto QuardVertics = Bit(27 - BitCorrection); static constexpr auto TriVertics = !QuardVertics; static constexpr auto Textured = Bit(26 - BitCorrection); static constexpr auto Untextured = !Textured; static constexpr auto SemiTransparent = Bit(25 - BitCorrection); static constexpr auto NonTransparent = !SemiTransparent; static constexpr auto BlendTexture = Bit(24 - BitCorrection); static constexpr auto NoBlendTexture = !BlendTexture; uint8_t value = bit::value::set_normalized(0u, CmdID.with(CmdValue)); constexpr Code() = default; constexpr Code(const Code& code) : value(code.value) { } constexpr Code& set(Bit bit) { this->value = bit::set(this->value, bit); return *this; } constexpr Code& set(ClearBit bit) { this->value = bit::set(this->value, bit); return *this; } }; // Concept for now template struct Hooked { uint32_t hook; T primitive; }; template struct CodeInterface { constexpr T& set_semi_transparent() { static_cast(this)->code.set(Code::SemiTransparent); return *static_cast(this); } constexpr T& set_non_transparent() { static_cast(this)->code.set(Code::NonTransparent); return *static_cast(this); } constexpr T& set_texture_blending() { static_cast(this)->code.set(Code::BlendTexture); return *static_cast(this); } constexpr T& set_non_texture_blening() { static_cast(this)->code.set(Code::NoBlendTexture); return *static_cast(this); } }; struct IsPrimitive { static constexpr bool is_primitive = true; typedef JabyEngine::GPU::internal::Code Code; }; } // Reexport for easier use typedef JabyEngine::GPU_IO::SemiTransparency SemiTransparency; typedef JabyEngine::GPU_IO::TexturePageColor TexturePageColor; struct PageClut { uint16_t value = 0; constexpr PageClut() = default; constexpr PageClut(uint16_t x, uint16_t y) : value((y <<6 ) | ((x >> 4) & 0x3f)) { } }; struct TPage { static constexpr auto TexturePageX = BitRange::from_to(0, 3); static constexpr auto TexturePageY = BitRange::from_to(4, 4); static constexpr auto SemiTransparency = BitRange::from_to(5, 6); static constexpr auto TextureClut = BitRange::from_to(7, 8); uint16_t value = 0; constexpr TPage() = default; constexpr TPage(uint16_t x, uint16_t y, ::JabyEngine::GPU::SemiTransparency transparency, TexturePageColor clut_color) : value(TexturePageX.as_value(x >> 6) | TexturePageY.as_value(y >> 8) | SemiTransparency.as_value(static_cast(transparency)) | TextureClut.as_value(static_cast(clut_color))) { } }; /* 1 / \ 3 - 2 */ struct POLY_F3 : public internal::IsPrimitive, public internal::CodeInterface { static constexpr auto IdentityCode = Code().set(Code::FlatShading).set(Code::TriVertics).set(Code::Untextured).set(Code::NonTransparent).set(Code::NoBlendTexture); Color24 color; // a Code code = IdentityCode; // a Vertex vertex0; // b Vertex vertex1; // c Vertex vertex2; // d constexpr POLY_F3() = default; constexpr POLY_F3(const Vertex (&verticies)[3], Color24 color) : color(color), code(IdentityCode), vertex0(verticies[0]), vertex1(verticies[1]), vertex2(verticies[2]) { } }; struct POLY_FT3 : public internal::IsPrimitive, public internal::CodeInterface { struct VertexEx { Vertex position; PagePosition page; }; static constexpr auto IdentityCode = Code(POLY_F3::IdentityCode).set(Code::Textured); Color24 color; // a Code code = IdentityCode; // a Vertex vertex0; // b PagePosition page0; // c PageClut page_clut; // c Vertex vertex1; // d PagePosition page1; // e TPage tpage; // e Vertex vertex2; // f PagePosition page2; // g uint16_t padded; // g constexpr POLY_FT3() = default; constexpr POLY_FT3(const Vertex (&verticies)[3], const PagePosition (&page_pos)[3], TPage tpage, PageClut clut, Color24 color) : color(color), code(IdentityCode), vertex0(verticies[0]), page0(page_pos[0]), page_clut(clut), vertex1(verticies[1]), page1(page_pos[1]), tpage(tpage), vertex2(verticies[2]), page2(page_pos[2]) {} constexpr POLY_FT3(const VertexEx (&vertices_ex)[3], TPage tpage, PageClut clut, Color24 color) : color(color), code(IdentityCode), vertex0(vertices_ex[0].position), page0(vertices_ex[0].page), page_clut(clut), vertex1(vertices_ex[1].position), page1(vertices_ex[1].page), tpage(tpage), vertex2(vertices_ex[2].position), page2(vertices_ex[2].page) {} }; struct POLY_G3 : public internal::IsPrimitive, public internal::CodeInterface { struct VertexEx { Vertex position; Color24 color; }; static constexpr auto IdentityCode = Code(POLY_F3::IdentityCode).set(Code::GouraudShading); Color24 color0; // a Code code = IdentityCode; // a Vertex vertex0; // b Color24 color1; // c uint8_t pad1; // c Vertex vertex1; // d Color24 color2; // e uint8_t pad2; // e Vertex vertex2; // f constexpr POLY_G3() = default; constexpr POLY_G3(const Vertex (&verticies)[3], const Color24 (&color)[3]) : color0(color[0]), code(IdentityCode), vertex0(verticies[0]), color1(color[1]), vertex1(verticies[1]), color2(color[2]), vertex2(verticies[2]) {} constexpr POLY_G3(const VertexEx (&verticies_ex)[3]) : color0(verticies_ex[0].color), code(IdentityCode), vertex0(verticies_ex[0].position), color1(verticies_ex[1].color), vertex1(verticies_ex[1].position), color2(verticies_ex[2].color), vertex2(verticies_ex[2].position) {} }; typedef POLY_F3 FlatTriangle; typedef POLY_FT3 FlatTexturedTriangle; static_assert(sizeof(POLY_F3) == 16); static_assert(sizeof(POLY_FT3) == 28); static_assert(sizeof(POLY_G3) == 24); } } #endif // !__JABYENGINE_GPU_PRIMITIVES_HPP__