From b2886dfdac33b3e9013820d719c5db9fe674fa6a Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 13 Jun 2023 21:21:02 +0200 Subject: [PATCH] Support linking primitives --- examples/PoolBox/application/src/main.cpp | 5 ++- .../PSX/GPU/Primitives/linked_elements.hpp | 25 ++++++++++++- .../GPU/Primitives/primitive_gpu_commands.hpp | 3 +- .../GPU/Primitives/primitive_line_types.hpp | 5 +-- .../GPU/Primitives/primitive_poly_types.hpp | 17 ++++----- .../Primitives/primitive_rectangle_types.hpp | 36 ++++++++++++------- 6 files changed, 65 insertions(+), 26 deletions(-) diff --git a/examples/PoolBox/application/src/main.cpp b/examples/PoolBox/application/src/main.cpp index 40f3bd06..ca776915 100644 --- a/examples/PoolBox/application/src/main.cpp +++ b/examples/PoolBox/application/src/main.cpp @@ -121,7 +121,8 @@ static constexpr const auto rect6 = GPU::SPRT_16({0, GPU::Display::Height - 16 static constexpr const auto rect7 = GPU::SPRT_8({0, GPU::Display::Height - 8}, {{0, 0}, TriangleClut}, GPU::Color24::Yellow()); static constexpr const auto rect8 = GPU::SPRT_1({0, GPU::Display::Height - 1}, {{0, 0}, TriangleClut}, GPU::Color24::Red()); -static constexpr const auto rect9 = GPU::SPRT(GPU::AreaI16({GPU::Display::Width/2, GPU::Display::Height/2}, {32, 32}).centered(), {{0, 0}, TriangleClut}, GPU::Color24::Grey()).linked(); +static auto rect9 = GPU::SPRT(GPU::AreaI16({GPU::Display::Width/2, GPU::Display::Height/2}, {32, 32}).centered(), {{0, 0}, TriangleClut}, GPU::Color24::Grey()).linked(); +static auto rect10 = GPU::SPRT(GPU::AreaI16({GPU::Display::Width/2, GPU::Display::Height/2 - 32}, {32, 32}).centered(), {{0, 0}, TriangleClut}, GPU::Color24::Grey()).linked(); static void load_assets() { static const CDFile Assets[] = { @@ -155,6 +156,8 @@ static void load_assets() { void main() { load_assets(); + rect9.concat(rect10); + while(true) { GPU::render(triangle1); GPU::render(triangle2); diff --git a/include/PSX/GPU/Primitives/linked_elements.hpp b/include/PSX/GPU/Primitives/linked_elements.hpp index bc5f513a..5a1b2c13 100644 --- a/include/PSX/GPU/Primitives/linked_elements.hpp +++ b/include/PSX/GPU/Primitives/linked_elements.hpp @@ -16,7 +16,28 @@ namespace JabyEngine { constexpr Link(size_t size) : value(SizeRange.as_value(size >> 2) | TerminationValue) { } - //void add + void set_adr(const void* adr) { + this->value = bit::value::set_normalized(this->value, AdrRange.with(reinterpret_cast(adr))); + } + + void* get_adr() const { + return reinterpret_cast(bit::value::get_normalized(this->value, AdrRange)); + } + + template + T& insert_after(T& obj) { + const auto adr = Link::get_adr(); + + Link::set_adr(&obj); + obj.set_adr(adr); + return obj; + } + + template + const T& concat(const T& obj) { + Link::set_adr(&obj); + return obj; + } constexpr void terminate() { this->value |= TerminationValue; @@ -45,6 +66,8 @@ namespace JabyEngine { namespace internal { template struct LinkedElementCreator { + typedef LinkedElement Linked; + constexpr LinkedElement linked() { return LinkedElement(*static_cast(this)); } diff --git a/include/PSX/GPU/Primitives/primitive_gpu_commands.hpp b/include/PSX/GPU/Primitives/primitive_gpu_commands.hpp index 693855a2..275d6cc9 100644 --- a/include/PSX/GPU/Primitives/primitive_gpu_commands.hpp +++ b/include/PSX/GPU/Primitives/primitive_gpu_commands.hpp @@ -1,11 +1,12 @@ #ifndef __JABYENGINE_PRIMITIVE_GPU_COMMANDS_HPP__ #define __JABYENGINE_PRIMITIVE_GPU_COMMANDS_HPP__ #include "../../System/IOPorts/gpu_io.hpp" +#include "linked_elements.hpp" #include "primitive_support_types.hpp" namespace JabyEngine { namespace GPU { - struct TexPage { + struct TexPage : public internal::LinkedElementCreator { GPU_IO::GP0_t value; constexpr TexPage(const PositionU16& tex_pos, TexturePageColor tex_color, SemiTransparency transparency = SemiTransparency::B_Half_add_F_Half, bool dither = false) : value{ diff --git a/include/PSX/GPU/Primitives/primitive_line_types.hpp b/include/PSX/GPU/Primitives/primitive_line_types.hpp index 5cb254e3..2a8ce37c 100644 --- a/include/PSX/GPU/Primitives/primitive_line_types.hpp +++ b/include/PSX/GPU/Primitives/primitive_line_types.hpp @@ -1,5 +1,6 @@ #ifndef __JABYENGINE_PRIMITIVE_LINE_TYPES_HPP__ #define __JABYENGINE_PRIMITIVE_LINE_TYPES_HPP__ +#include "linked_elements.hpp" #include "primitive_support_types.hpp" namespace JabyEngine { @@ -53,14 +54,14 @@ namespace JabyEngine { }; template - struct SingleLine : public LineCodeInterface> { + struct SingleLine : public LineCodeInterface>, public internal::LinkedElementCreator> { LineHead head; Vertex start_point; Body end_point; }; template - struct MultiLine : public LineCodeInterface> { + struct MultiLine : public LineCodeInterface>, public internal::LinkedElementCreator> { LineHead head; Vertex start_point; Body points[N]; diff --git a/include/PSX/GPU/Primitives/primitive_poly_types.hpp b/include/PSX/GPU/Primitives/primitive_poly_types.hpp index 65520a77..efbc5a06 100644 --- a/include/PSX/GPU/Primitives/primitive_poly_types.hpp +++ b/include/PSX/GPU/Primitives/primitive_poly_types.hpp @@ -1,5 +1,6 @@ #ifndef __JABYENGINE_PRIMITIVE_POLY_TYPES_HPP__ #define __JABYENGINE_PRIMITIVE_POLY_TYPES_HPP__ +#include "linked_elements.hpp" #include "primitive_support_types.hpp" namespace JabyEngine { @@ -35,7 +36,7 @@ namespace JabyEngine { / \ 3 - 2 */ - struct POLY_F3 : public internal::PolyCodeInterface { + struct POLY_F3 : public internal::PolyCodeInterface, public internal::LinkedElementCreator { static constexpr auto IdentityCode = Code().set(Code::FlatShading).set(Code::TriVertics).set(Code::Untextured).set(Code::NonTransparent); Color24 color; // a @@ -53,7 +54,7 @@ namespace JabyEngine { } }; - struct POLY_FT3 : public internal::PolyCodeInterface { + struct POLY_FT3 : public internal::PolyCodeInterface, public internal::LinkedElementCreator { struct VertexEx { Vertex position; PagePosition page; @@ -84,7 +85,7 @@ namespace JabyEngine { vertex2(vertices_ex[2].position), page2(vertices_ex[2].page), padded2(0) {} }; - struct POLY_G3 : public internal::PolyCodeInterface { + struct POLY_G3 : public internal::PolyCodeInterface, public internal::LinkedElementCreator { static constexpr auto IdentityCode = Code(POLY_F3::IdentityCode).set(Code::GouraudShading); Color24 color0; // a @@ -108,7 +109,7 @@ namespace JabyEngine { color2(verticies_ex[2].color), pad2(0), vertex2(verticies_ex[2].position) {} }; - struct POLY_GT3 : public internal::PolyCodeInterface { + struct POLY_GT3 : public internal::PolyCodeInterface, public internal::LinkedElementCreator { struct VertexEx { Vertex position; PagePosition page; @@ -148,7 +149,7 @@ namespace JabyEngine { | | 3 - 4 */ - struct POLY_F4 : public internal::PolyCodeInterface { + struct POLY_F4 : public internal::PolyCodeInterface, public internal::LinkedElementCreator { static constexpr auto IdentityCode = Code(POLY_F3::IdentityCode).set(Code::QuadVertics); Color24 color; // a @@ -173,7 +174,7 @@ namespace JabyEngine { color) {} }; - struct POLY_FT4 : public internal::PolyCodeInterface { + struct POLY_FT4 : public internal::PolyCodeInterface, public internal::LinkedElementCreator { typedef POLY_FT3::VertexEx VertexEx; static constexpr auto IdentityCode = Code(POLY_FT3::IdentityCode).set(Code::QuadVertics); @@ -213,7 +214,7 @@ namespace JabyEngine { ) {} }; - struct POLY_G4 : public internal::PolyCodeInterface { + struct POLY_G4 : public internal::PolyCodeInterface, public internal::LinkedElementCreator { static constexpr auto IdentityCode = Code(POLY_G3::IdentityCode).set(Code::QuadVertics); Color24 color0; // a @@ -249,7 +250,7 @@ namespace JabyEngine { ) {} }; - struct POLY_GT4 : public internal::PolyCodeInterface { + struct POLY_GT4 : public internal::PolyCodeInterface, public internal::LinkedElementCreator { typedef POLY_GT3::VertexEx VertexEx; static constexpr auto IdentityCode = Code(POLY_GT3::IdentityCode).set(Code::QuadVertics); diff --git a/include/PSX/GPU/Primitives/primitive_rectangle_types.hpp b/include/PSX/GPU/Primitives/primitive_rectangle_types.hpp index 775702b6..247ab38e 100644 --- a/include/PSX/GPU/Primitives/primitive_rectangle_types.hpp +++ b/include/PSX/GPU/Primitives/primitive_rectangle_types.hpp @@ -34,23 +34,23 @@ namespace JabyEngine { }; template - struct RECT_F : public RectCodeInterface> { - typedef RECT_F::Code Code; + struct RECT_BASE_F : public RectCodeInterface> { + typedef RECT_BASE_F::Code Code; static constexpr auto IdentityCode = Code().set(Code::Size.with(static_cast(Size))).set(Code::Untextured).set(Code::NonTransparent); Color24 color; Code code; Vertex position; - constexpr RECT_F() = default; - constexpr RECT_F(const Vertex& position, const Color24& color) : color(color), code(IdentityCode), position(position) { + constexpr RECT_BASE_F() = default; + constexpr RECT_BASE_F(const Vertex& position, const Color24& color) : color(color), code(IdentityCode), position(position) { } }; template - struct RECT_T : public RectCodeInterface> { - typedef RECT_T::Code Code; - static constexpr auto IdentityCode = Code(RECT_F::IdentityCode).set(Code::Textured); + struct RECT_BASE_T : public RectCodeInterface> { + typedef RECT_BASE_T::Code Code; + static constexpr auto IdentityCode = Code(RECT_BASE_F::IdentityCode).set(Code::Textured); Color24 color; Code code; @@ -58,31 +58,41 @@ namespace JabyEngine { PagePosition page; PageClut clut; - constexpr RECT_T() = default; - constexpr RECT_T(const Vertex& position, const PagePositionClut& page, const Color24& color = Color24::Grey()) : color(color), code(IdentityCode), position(position), page(page.page), clut(page.clut) { + constexpr RECT_BASE_T() = default; + constexpr RECT_BASE_T(const Vertex& position, const PagePositionClut& page, const Color24& color = Color24::Grey()) : color(color), code(IdentityCode), position(position), page(page.page), clut(page.clut) { } }; + + template + struct RECT_F : public RECT_BASE_F, public internal::LinkedElementCreator> { + using RECT_BASE_F::RECT_BASE_F; + }; + + template + struct RECT_T : public RECT_BASE_T, public internal::LinkedElementCreator> { + using RECT_BASE_T::RECT_BASE_T; + }; } typedef internal::RECT_F TILE_1; typedef internal::RECT_F TILE_8; typedef internal::RECT_F TILE_16; - struct TILE : public internal::RECT_F { + struct TILE : public internal::RECT_BASE_F, public internal::LinkedElementCreator { SizeI16 size; constexpr TILE() = default; - constexpr TILE(const AreaI16& area, const Color24& color) : RECT_F(area.position, color), size(area.size) { + constexpr TILE(const AreaI16& area, const Color24& color) : RECT_BASE_F(area.position, color), size(area.size) { } }; typedef internal::RECT_T SPRT_1; typedef internal::RECT_T SPRT_8; typedef internal::RECT_T SPRT_16; - struct SPRT : public internal::RECT_T, public internal::LinkedElementCreator { + struct SPRT : public internal::RECT_BASE_T, public internal::LinkedElementCreator { SizeI16 size; constexpr SPRT() = default; - constexpr SPRT(const AreaI16& area, const PagePositionClut& page, const Color24& color = Color24::Grey()) : RECT_T(area.position, page, color), size(area.size) { + constexpr SPRT(const AreaI16& area, const PagePositionClut& page, const Color24& color = Color24::Grey()) : RECT_BASE_T(area.position, page, color), size(area.size) { } };