From e74cba6dfe2d2fd9d2d3c13584581063b9730d2f Mon Sep 17 00:00:00 2001 From: Jaby Date: Mon, 15 May 2023 21:14:37 +0200 Subject: [PATCH] Support textured triangles --- examples/PoolBox/application/src/main.cpp | 16 ++-- include/PSX/GPU/gpu_primitives.hpp | 97 ++++++++++++++++++----- include/PSX/GPU/gpu_types.hpp | 13 +++ 3 files changed, 101 insertions(+), 25 deletions(-) diff --git a/examples/PoolBox/application/src/main.cpp b/examples/PoolBox/application/src/main.cpp index 5ca09653..6c7efab9 100644 --- a/examples/PoolBox/application/src/main.cpp +++ b/examples/PoolBox/application/src/main.cpp @@ -39,17 +39,19 @@ static void load_assets() { } void main() { - const JabyEngine::GPU::POLY_F3 triangle( - JabyEngine::GPU::Color24(0x0, 0xFF, 0xFF), - { - {0, 0}, {320*2, 127}, {0, 255} - } - ); - printf("Hello PoolBox!\n"); + const JabyEngine::GPU::POLY_F3 triangle({{0, 0}, {64, 64}, {0, 64}}, JabyEngine::GPU::Color24(0x0, 0xFF, 0xFF)); + const JabyEngine::GPU::POLY_FT3 triangle2( + {{0, 0}, {64, 0}, {64, 64}}, {{0, 0}, {64, 0}, {64, 64}}, + JabyEngine::GPU::TPage(320, 0, JabyEngine::GPU::SemiTransparency::B_Half_add_F_Half, JabyEngine::GPU::TexturePageColor::$4bit), + JabyEngine::GPU::PageClut(320, 256), + JabyEngine::GPU::Color24(0xFF, 0xFF, 0xFF)); + load_assets(); while(true) { JabyEngine::GPU::render(triangle); + JabyEngine::GPU::render(triangle2); + JabyEngine::GPU::swap_buffers_vsync(2); } } diff --git a/include/PSX/GPU/gpu_primitives.hpp b/include/PSX/GPU/gpu_primitives.hpp index 5c890324..35f16e48 100644 --- a/include/PSX/GPU/gpu_primitives.hpp +++ b/include/PSX/GPU/gpu_primitives.hpp @@ -1,13 +1,14 @@ #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 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); @@ -24,6 +25,8 @@ namespace JabyEngine { 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); @@ -36,8 +39,11 @@ namespace JabyEngine { } }; - struct IsPrimitive { - static constexpr bool is_primitive = true; + // Concept for now + template + struct Hooked { + uint32_t hook; + T primitive; }; template @@ -63,27 +69,82 @@ namespace JabyEngine { } }; - // Concept for now - template - struct Hooked { - uint32_t hook; - T primitive; + struct IsPrimitive { + static constexpr bool is_primitive = true; + + typedef JabyEngine::GPU::internal::Code Code; }; } - struct POLY_F3 : public internal::IsPrimitive, public internal::CodeInterface { - static constexpr auto IdentityCode = internal::Code().set(internal::Code::FlatShading).set(internal::Code::TriVertics).set(internal::Code::Untextured).set(internal::Code::NonTransparent).set(internal::Code::NoBlendTexture); - - Color24 color; - internal::Code code = IdentityCode; - PositionI16 vertex[3]; + // Reexport for easier use + typedef JabyEngine::GPU_IO::SemiTransparency SemiTransparency; + typedef JabyEngine::GPU_IO::TexturePageColor TexturePageColor; - constexpr POLY_F3() = default; - constexpr POLY_F3(Color24 color, const PositionI16 (&verticies)[3]) : color(color), code(IdentityCode), vertex{verticies[0], verticies[1], verticies[2]} { + struct PageClut { + uint16_t value = 0; + + constexpr PageClut() = default; + constexpr PageClut(uint16_t x, uint16_t y) : value((y <<6 ) | ((x >> 4) & 0x3f)) { } }; - static_assert(sizeof(POLY_F3) == 16); + + 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; + Code code = IdentityCode; + Vertex vertex0; + Vertex vertex1; + Vertex vertex2; + + constexpr POLY_F3() = default; + constexpr POLY_F3(const PositionI16 (&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 { + static constexpr auto IdentityCode = Code(POLY_F3::IdentityCode).set(Code::Textured); + + Color24 color; + Code code = IdentityCode; + Vertex vertex0; + PagePosition page0; + PageClut page_clut; + Vertex vertex1; + PagePosition page1; + TPage tpage; + Vertex vertex2; + PagePosition page2; + uint16_t padded; + + constexpr POLY_FT3() = default; + constexpr POLY_FT3(const PositionI16 (&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]) { + } + }; + + static_assert(sizeof(POLY_F3) == 16); + static_assert(sizeof(POLY_FT3) == 28); } } diff --git a/include/PSX/GPU/gpu_types.hpp b/include/PSX/GPU/gpu_types.hpp index 302cf55d..f2318882 100644 --- a/include/PSX/GPU/gpu_types.hpp +++ b/include/PSX/GPU/gpu_types.hpp @@ -105,6 +105,16 @@ namespace JabyEngine { } }; + // Type used for primitives + struct PagePosition { + uint8_t u = 0; + uint8_t v = 0; + + constexpr PagePosition() = default; + constexpr PagePosition(uint8_t u, uint8_t v) : u(u), v(v) { + } + }; + typedef Position PositionI16; typedef Position PositionU16; @@ -113,6 +123,9 @@ namespace JabyEngine { typedef Area AreaI16; typedef Area AreaU16; + + // Type used for primitives + typedef PositionI16 Vertex; } } #endif //!__JABYENGINE_GPU_TYPES_HPP__ \ No newline at end of file