From c705c3627c087e506536478521d69fa4e6cc2804 Mon Sep 17 00:00:00 2001 From: Jaby Date: Mon, 12 Jun 2023 21:47:56 +0200 Subject: [PATCH] Support basic linked elements --- examples/PoolBox/application/src/main.cpp | 4 ++ .../PSX/GPU/Primitives/linked_elements.hpp | 56 +++++++++++++++++++ .../Primitives/primitive_rectangle_types.hpp | 11 ++-- .../Primitives/primitive_support_types.hpp | 7 --- include/PSX/GPU/gpu.hpp | 6 ++ include/PSX/GPU/gpu_types.hpp | 12 ++++ include/PSX/System/IOPorts/dma_io.hpp | 4 ++ src/Library/src/GPU/gpu.cpp | 9 +++ 8 files changed, 97 insertions(+), 12 deletions(-) create mode 100644 include/PSX/GPU/Primitives/linked_elements.hpp diff --git a/examples/PoolBox/application/src/main.cpp b/examples/PoolBox/application/src/main.cpp index 0eedd2b7..40f3bd06 100644 --- a/examples/PoolBox/application/src/main.cpp +++ b/examples/PoolBox/application/src/main.cpp @@ -121,6 +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 void load_assets() { static const CDFile Assets[] = { CDFileBuilder::simple_tim(LBA::FONT, SimpleTIM(320, 0, 320, 511)), @@ -180,6 +182,8 @@ void main() { GPU::render(line3); GPU::render(line4); + GPU::render(rect9); + GPU::swap_buffers_vsync(2); } } diff --git a/include/PSX/GPU/Primitives/linked_elements.hpp b/include/PSX/GPU/Primitives/linked_elements.hpp new file mode 100644 index 00000000..bc5f513a --- /dev/null +++ b/include/PSX/GPU/Primitives/linked_elements.hpp @@ -0,0 +1,56 @@ +#ifndef __JABYENGINE_LINKED_ELEMENTS_HPP__ +#define __JABYENGINE_LINKED_ELEMENTS_HPP__ +#include "../../Auxiliary/bits.hpp" + +namespace JabyEngine { + namespace GPU { + struct Link { + static constexpr auto AdrRange = BitRange::from_to(0, 23); + static constexpr auto SizeRange = BitRange::from_to(24, 31); + + static constexpr uint32_t TerminationValue = 0x00FFFFFF; + + uint32_t value = TerminationValue; + + constexpr Link() = default; + constexpr Link(size_t size) : value(SizeRange.as_value(size >> 2) | TerminationValue) { + } + + //void add + + constexpr void terminate() { + this->value |= TerminationValue; + } + }; + + template + struct LinkedElement : public Link { + T element; + + constexpr LinkedElement() : Link(sizeof(T)), element() { + } + + constexpr LinkedElement(const T& element) : Link(sizeof(T)), element(element) { + } + + constexpr const T* operator->() const { + return &this->element; + } + + constexpr T* operator->() { + return &this->element; + } + }; + + namespace internal { + template + struct LinkedElementCreator { + constexpr LinkedElement linked() { + return LinkedElement(*static_cast(this)); + } + }; + } + } +} + +#endif // !__JABYENGINE_LINKED_ELEMENTS_HPP__ \ No newline at end of file diff --git a/include/PSX/GPU/Primitives/primitive_rectangle_types.hpp b/include/PSX/GPU/Primitives/primitive_rectangle_types.hpp index 6e43b9a5..775702b6 100644 --- a/include/PSX/GPU/Primitives/primitive_rectangle_types.hpp +++ b/include/PSX/GPU/Primitives/primitive_rectangle_types.hpp @@ -1,5 +1,6 @@ #ifndef __JABYENGINE_PRIMITIVE_RECTANGLE_TYPES_HPP__ #define __JABYENGINE_PRIMITIVE_RECTANGLE_TYPES_HPP__ +#include "linked_elements.hpp" #include "primitive_support_types.hpp" namespace JabyEngine { @@ -67,21 +68,21 @@ namespace JabyEngine { typedef internal::RECT_F TILE_8; typedef internal::RECT_F TILE_16; struct TILE : public internal::RECT_F { - Vertex position_bottom_right; + SizeI16 size; constexpr TILE() = default; - constexpr TILE(const AreaI16& area, const Color24& color) : RECT_F(area.position, color), position_bottom_right(area.get_bottom_left()) { + constexpr TILE(const AreaI16& area, const Color24& color) : RECT_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 { - Vertex position_bottom_right; + struct SPRT : public internal::RECT_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), position_bottom_right(area.get_bottom_left()) { + constexpr SPRT(const AreaI16& area, const PagePositionClut& page, const Color24& color = Color24::Grey()) : RECT_T(area.position, page, color), size(area.size) { } }; diff --git a/include/PSX/GPU/Primitives/primitive_support_types.hpp b/include/PSX/GPU/Primitives/primitive_support_types.hpp index 315c4fe5..da3f3ae7 100644 --- a/include/PSX/GPU/Primitives/primitive_support_types.hpp +++ b/include/PSX/GPU/Primitives/primitive_support_types.hpp @@ -52,13 +52,6 @@ namespace JabyEngine { } }; - // Concept for now - template - struct Hooked { - uint32_t hook; - T primitive; - }; - template struct is_render_primitive { static constexpr bool value = false; diff --git a/include/PSX/GPU/gpu.hpp b/include/PSX/GPU/gpu.hpp index 782c8a58..62cf6f15 100644 --- a/include/PSX/GPU/gpu.hpp +++ b/include/PSX/GPU/gpu.hpp @@ -38,6 +38,12 @@ namespace JabyEngine { namespace internal { void render(const uint32_t* data, size_t words); + void render_dma(const uint32_t* data); + } + + template + static void render(const LinkedElement& linked_primitives) { + internal::render_dma(&linked_primitives.value); } template diff --git a/include/PSX/GPU/gpu_types.hpp b/include/PSX/GPU/gpu_types.hpp index 68e320e0..0ef84a80 100644 --- a/include/PSX/GPU/gpu_types.hpp +++ b/include/PSX/GPU/gpu_types.hpp @@ -15,6 +15,10 @@ namespace JabyEngine { return *static_cast(this); } + constexpr T add(S dx, S dy) const { + return T(static_cast(this)->x, static_cast(this)->y).add(dx, dy); + } + constexpr T& sub(S dx, S dy) { static_cast(this)->x -= dx; static_cast(this)->y -= dy; @@ -22,6 +26,10 @@ namespace JabyEngine { return *static_cast(this); } + constexpr T sub(S dx, S dy) const { + return T(static_cast(this)->x, static_cast(this)->y).sub(dx, dy); + } + constexpr T& move(S dx, S dy) { return this->add(dx, dy); } @@ -156,6 +164,10 @@ namespace JabyEngine { constexpr Area(T position_x, T position_y, T size_width, T size_height) : position{position_x, position_y}, size{size_width, size_height} { } + constexpr Area centered() const { + return Area(this->position.sub(this->size.width/2, this->size.height/2), this->size); + } + constexpr Position get_bottom_left() const { return this->position.move(this->size.width, this->size.height); } diff --git a/include/PSX/System/IOPorts/dma_io.hpp b/include/PSX/System/IOPorts/dma_io.hpp index 7f7a1e69..9092e8a3 100644 --- a/include/PSX/System/IOPorts/dma_io.hpp +++ b/include/PSX/System/IOPorts/dma_io.hpp @@ -68,6 +68,10 @@ namespace JabyEngine { return Self{0x01000201}; } + static constexpr Self StartGPULinked() { + return Self{0x01000401}; + } + static constexpr Self StartCDROM() { return Self{0x11000000}; } diff --git a/src/Library/src/GPU/gpu.cpp b/src/Library/src/GPU/gpu.cpp index 56808ad2..fd2f7578 100644 --- a/src/Library/src/GPU/gpu.cpp +++ b/src/Library/src/GPU/gpu.cpp @@ -63,6 +63,15 @@ namespace JabyEngine { GPU_IO::GP0 = data[n]; } } + + void render_dma(const uint32_t* data) { + wait_ready_for_CMD(); + // DPCR already enabled + GPU_IO::GP1 = GPU_IO::Command::DMADirection(GPU_IO::DMADirection::CPU2GPU); + DMA_IO::GPU.set_adr(reinterpret_cast(data)); + DMA_IO::GPU.block_ctrl = 0; + DMA_IO::GPU.channel_ctrl = DMA_IO::CHCHR_t::StartGPULinked(); + } } #ifndef USE_NO$PSX