From 09b16dae165a0660110f91fd88fb0774fa01c763 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 7 May 2023 00:07:20 +0200 Subject: [PATCH] Draw first triangle --- examples/PoolBox/application/src/main.cpp | 8 ++ include/PSX/Auxiliary/type_traits.hpp | 14 +++ include/PSX/File/file_types.hpp | 4 +- include/PSX/GPU/gpu.hpp | 13 ++- include/PSX/GPU/gpu_primitives.hpp | 90 +++++++++++++++++++ include/PSX/GPU/gpu_types.hpp | 1 + include/PSX/System/IOPorts/gpu_io.hpp | 33 ++++--- lib/Makefile | 4 +- .../internal-include/GPU/gpu_internal.hpp | 26 ++++-- .../src/File/Processor/tim_processor.cpp | 2 +- src/Library/src/GPU/gpu.cpp | 26 ++++-- src/Library/src/run.cpp | 1 + 12 files changed, 190 insertions(+), 32 deletions(-) create mode 100644 include/PSX/Auxiliary/type_traits.hpp create mode 100644 include/PSX/GPU/gpu_primitives.hpp diff --git a/examples/PoolBox/application/src/main.cpp b/examples/PoolBox/application/src/main.cpp index 8c498a8c..5ca09653 100644 --- a/examples/PoolBox/application/src/main.cpp +++ b/examples/PoolBox/application/src/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include enum LBA { @@ -38,10 +39,17 @@ 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"); load_assets(); while(true) { + JabyEngine::GPU::render(triangle); JabyEngine::GPU::swap_buffers_vsync(2); } } diff --git a/include/PSX/Auxiliary/type_traits.hpp b/include/PSX/Auxiliary/type_traits.hpp new file mode 100644 index 00000000..5b667ef1 --- /dev/null +++ b/include/PSX/Auxiliary/type_traits.hpp @@ -0,0 +1,14 @@ +#ifndef __JABYENGINE_TYPE_TRAITS_HPP__ +#define __JABYENGINE_TYPE_TRAITS_HPP__ + +namespace JabyEngine { + template + struct enable_if {}; + + template + struct enable_if { + typedef T type; + }; +} + +#endif //! __JABYENGINE_TYPE_TRAITS_HPP__ \ No newline at end of file diff --git a/include/PSX/File/file_types.hpp b/include/PSX/File/file_types.hpp index fe077595..c2df1d56 100644 --- a/include/PSX/File/file_types.hpp +++ b/include/PSX/File/file_types.hpp @@ -16,9 +16,7 @@ namespace JabyEngine { uint32_t raw; - constexpr SimpleTIM() { - this->raw = 0; - } + constexpr SimpleTIM() : raw(0) {} constexpr SimpleTIM(uint16_t texX, uint16_t texY, uint16_t clutX, uint16_t clutY) : raw(TextureX.as_value(texX >> 1) | TextureY.as_value(texY >> 1) | ClutX.as_value(clutX >> 4) | ClutY.as_value(static_cast(clutY))) { } diff --git a/include/PSX/GPU/gpu.hpp b/include/PSX/GPU/gpu.hpp index c864052c..1ac7fa16 100644 --- a/include/PSX/GPU/gpu.hpp +++ b/include/PSX/GPU/gpu.hpp @@ -1,6 +1,8 @@ #ifndef __JABYENGINE_GPU_HPP__ #define __JABYENGINE_GPU_HPP__ +#include "../Auxiliary/type_traits.hpp" #include "../System/IOPorts/gpu_io.hpp" +#include "gpu_primitives.hpp" #if !defined(JABYENGINE_NTSC) && !defined(JABYENGINE_PAL) #error "JABYENGINE_NTSC or JABYENGINE_PAL must be defined" @@ -34,7 +36,16 @@ namespace JabyEngine { static void set_offset(uint16_t x, uint16_t y); }; - uint8_t swap_buffers_vsync(uint8_t syncs); + namespace internal { + void render(const uint32_t* data, size_t words); + } + + template + static enable_if::type render(const T& primitive) { + internal::render(reinterpret_cast(&primitive), sizeof(T)/sizeof(uint32_t)); + } + + uint8_t swap_buffers_vsync(uint8_t syncs, bool clear_screen = true); } } #endif //!__JABYENGINE_GPU_HPP__ \ No newline at end of file diff --git a/include/PSX/GPU/gpu_primitives.hpp b/include/PSX/GPU/gpu_primitives.hpp new file mode 100644 index 00000000..5c890324 --- /dev/null +++ b/include/PSX/GPU/gpu_primitives.hpp @@ -0,0 +1,90 @@ +#ifndef __JABYENGINE_GPU_PRIMITIVES_HPP__ +#define __JABYENGINE_GPU_PRIMITIVES_HPP__ +#include "gpu_types.hpp" + +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& 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; + } + }; + + struct IsPrimitive { + static constexpr bool is_primitive = true; + }; + + 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); + } + }; + + // Concept for now + template + struct Hooked { + uint32_t hook; + T primitive; + }; + } + + 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]; + + constexpr POLY_F3() = default; + constexpr POLY_F3(Color24 color, const PositionI16 (&verticies)[3]) : color(color), code(IdentityCode), vertex{verticies[0], verticies[1], verticies[2]} { + } + }; + + static_assert(sizeof(POLY_F3) == 16); + } +} + +#endif // !__JABYENGINE_GPU_PRIMITIVES_HPP__ \ No newline at end of file diff --git a/include/PSX/GPU/gpu_types.hpp b/include/PSX/GPU/gpu_types.hpp index 187b9f9f..302cf55d 100644 --- a/include/PSX/GPU/gpu_types.hpp +++ b/include/PSX/GPU/gpu_types.hpp @@ -1,5 +1,6 @@ #ifndef __JABYENGINE_GPU_TYPES_HPP__ #define __JABYENGINE_GPU_TYPES_HPP__ +#include "../Auxiliary/bits.hpp" #include "../jabyengine_defines.h" namespace JabyEngine { diff --git a/include/PSX/System/IOPorts/gpu_io.hpp b/include/PSX/System/IOPorts/gpu_io.hpp index b26059c3..a6473a9d 100644 --- a/include/PSX/System/IOPorts/gpu_io.hpp +++ b/include/PSX/System/IOPorts/gpu_io.hpp @@ -89,16 +89,16 @@ namespace JabyEngine { struct Command { struct Helper { + static constexpr uint32_t construct_cmd(uint8_t cmd, uint32_t value) { + return ((cmd << 24) | value); + } + static constexpr GP0_t DrawAreaTemplate(uint8_t code, uint16_t x, uint16_t y) { constexpr auto Command = BitRange::from_to(24, 31); constexpr auto Y = BitRange::from_to(10, 18); constexpr auto X = BitRange::from_to(0, 9); - return GP0_t::from(Command.with(code), Y.with(y), X.with(x)); - } - - static constexpr uint32_t construct_cmd(uint8_t cmd, uint32_t value) { - return ((cmd << 24) | value); + return {construct_cmd(code, Y.as_value(static_cast(y)) | X.as_value(static_cast(x)))}; } }; @@ -122,12 +122,19 @@ namespace JabyEngine { return Helper::DrawAreaTemplate(0xE4, x, y); } + static GP0_t SetDrawOffset(int16_t x, int16_t y) { + constexpr auto X = BitRange::from_to(0, 10); + constexpr auto Y = BitRange::from_to(11, 21); + + return {Helper::construct_cmd(0xE5, X.as_value(static_cast(x)) | Y.as_value(static_cast(y)))}; + } + static constexpr GP0_t TopLeftPosition(uint16_t x, uint16_t y) { - return {static_cast((y << 16u) | x)}; + return {(static_cast(y) << 16u) | x}; } static constexpr GP0_t WidthHeight(uint16_t w, uint16_t h) { - return {static_cast((h << 16u) | w)}; + return {(static_cast(h) << 16u) | w}; } static constexpr GP1_t Reset() { @@ -146,25 +153,25 @@ namespace JabyEngine { return {Helper::construct_cmd(0x04, static_cast(dir))}; } - static constexpr GP1_t DisplayArea(uint32_t x, uint32_t y) { + static constexpr GP1_t DisplayArea(uint16_t x, uint16_t y) { constexpr auto X = BitRange::from_to(0, 9); constexpr auto Y = BitRange::from_to(10, 18); - return {Helper::construct_cmd(0x05, X.as_value(x) | Y.as_value(y))}; + return {Helper::construct_cmd(0x05, X.as_value(static_cast(x)) | Y.as_value(static_cast(y)))}; } - static constexpr GP1_t HorizontalDisplayRange(uint32_t x1, uint32_t x2) { + static constexpr GP1_t HorizontalDisplayRange(uint16_t x1, uint16_t x2) { constexpr auto X1 = BitRange::from_to(0, 11); constexpr auto X2 = BitRange::from_to(12, 23); - return {Helper::construct_cmd(0x06, X1.as_value(x1) | X2.as_value(x2))}; + return {Helper::construct_cmd(0x06, X1.as_value(static_cast(x1)) | X2.as_value(static_cast(x2)))}; } - static constexpr GP1_t VerticalDisplayRange(uint32_t y1, uint32_t y2) { + static constexpr GP1_t VerticalDisplayRange(uint16_t y1, uint16_t y2) { constexpr auto Y1 = BitRange::from_to(0, 9); constexpr auto Y2 = BitRange::from_to(10, 19); - return {Helper::construct_cmd(0x07, Y1.as_value(y1) | Y2.as_value(y2))}; + return {Helper::construct_cmd(0x07, Y1.as_value(static_cast(y1)) | Y2.as_value(static_cast(y2)))}; } static constexpr GP1_t DisplayMode(DisplayMode_t mode) { diff --git a/lib/Makefile b/lib/Makefile index 51db17ba..f59f3bd0 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -37,7 +37,7 @@ LD = $(CXX) AR = ar #architecture flags -ARCHFLAGS = -march=mips1 -mabi=32 -EL -fno-pic -mno-shared -nostdinc -nostdinc++ -mno-abicalls -mfp32 +ARCHFLAGS = -march=r2000 -mtune=r2000 -mabi=32 -EL -fno-pic -mno-shared -nostdinc -nostdinc++ -mno-abicalls -mfp32 -mno-llsc ARCHFLAGS += -fno-stack-protector -nostdlib -ffreestanding #Compiler flags for build profiles @@ -46,7 +46,7 @@ CCFLAGS_debug += -O0 CXXFLAGS += -fno-exceptions -fno-rtti -CCFLAGS += -mno-gpopt -fomit-frame-pointer -ffunction-sections +CCFLAGS += -mno-gpopt -fomit-frame-pointer -ffunction-sections -fdata-sections CCFLAGS += -fno-builtin -fno-strict-aliasing -Wno-attributes CCFLAGS += -std=c++20 CCFLAGS += $(ARCHFLAGS) diff --git a/src/Library/internal-include/GPU/gpu_internal.hpp b/src/Library/internal-include/GPU/gpu_internal.hpp index 74b57864..2bacb80b 100644 --- a/src/Library/internal-include/GPU/gpu_internal.hpp +++ b/src/Library/internal-include/GPU/gpu_internal.hpp @@ -21,17 +21,27 @@ namespace JabyEngine { static constexpr uint16_t ScanlinesV = 240; #endif //JABYENGINE_PAL - static void exchange_buffer_and_display(); + static uint32_t exchange_buffer_and_display(); }; void wait_vsync(uint8_t syncs); + static void wait_ready_for_CMD() { + while(!GPU_IO::GPUSTAT.is_set(GPU_IO::GPUSTAT_t::GP0ReadyForCMD)); + } + static void set_draw_area(uint16_t x, uint16_t y) { - GPU_IO::GP0 = GPU_IO::Command::DrawAreaTopLeft(x, y); - GPU_IO::GP0 = GPU_IO::Command::DrawAreaBottomRight((x + Display::Width), (y + Display::Height)); + const auto top_left = GPU_IO::Command::DrawAreaTopLeft(x, y); + const auto bottom_right = GPU_IO::Command::DrawAreaBottomRight((x + Display::Width - 1), (y + Display::Height - 1)); + + wait_ready_for_CMD(); + GPU_IO::GP0 = top_left; + wait_ready_for_CMD(); + GPU_IO::GP0 = bottom_right; } static void copy_vram_to_vram(const AreaU16& dst, const PositionU16& src) { + wait_ready_for_CMD(); GPU_IO::GP0 = GPU_IO::Command::VRAM2VRAM_Blitting(); GPU_IO::GP0 = GPU_IO::Command::TopLeftPosition(src.x, src.y); GPU_IO::GP0 = GPU_IO::Command::TopLeftPosition(dst.position.x, dst.position.y); @@ -39,17 +49,19 @@ namespace JabyEngine { } static void quick_fill_fast(const Color24& color, const AreaU16& area) { + wait_ready_for_CMD(); GPU_IO::GP0 = GPU_IO::Command::QuickFill(color); GPU_IO::GP0 = GPU_IO::Command::TopLeftPosition(area.position.x, area.position.y); GPU_IO::GP0 = GPU_IO::Command::WidthHeight(area.size.width, area.size.height); } - static void reset_cmd_buffer() { - GPU_IO::GP1 = GPU_IO::Command::ResetCMDBufer(); + static void set_draw_offset(int16_t x, int16_t y) { + wait_ready_for_CMD(); + GPU_IO::GP0 = GPU_IO::Command::SetDrawOffset(x, y); } - static void wait_ready_for_CMD() { - while(!GPU_IO::GPUSTAT.is_set(GPU_IO::GPUSTAT_t::GP0ReadyForCMD)); + static void reset_cmd_buffer() { + GPU_IO::GP1 = GPU_IO::Command::ResetCMDBufer(); } namespace DMA { diff --git a/src/Library/src/File/Processor/tim_processor.cpp b/src/Library/src/File/Processor/tim_processor.cpp index 0ff40dbf..d8fe3caf 100644 --- a/src/Library/src/File/Processor/tim_processor.cpp +++ b/src/Library/src/File/Processor/tim_processor.cpp @@ -44,7 +44,7 @@ namespace JabyEngine { SimpleTIMSize size_info; size_t words_left; //32bit values - constexpr SimpleTIMState(const SimpleTIM& dst_info) : dst_info(dst_info) { + constexpr SimpleTIMState(const SimpleTIM& dst_info) : dst_info(dst_info), words_left(0) { } }; diff --git a/src/Library/src/GPU/gpu.cpp b/src/Library/src/GPU/gpu.cpp index 94ffa438..c8cadcc1 100644 --- a/src/Library/src/GPU/gpu.cpp +++ b/src/Library/src/GPU/gpu.cpp @@ -37,10 +37,13 @@ namespace JabyEngine { __syscall_ReturnFromException(); } - void Display :: exchange_buffer_and_display() { - GPU::internal::set_draw_area(0, (PublicDisplay::Height*PublicDisplay::current_id)); + uint32_t Display :: exchange_buffer_and_display() { + const auto draw_area_y = (PublicDisplay::Height*PublicDisplay::current_id); + + GPU::internal::set_draw_area(0, draw_area_y); PublicDisplay::current_id ^= 1; GPU_IO::GP1 = GPU_IO::Command::DisplayArea(0, (PublicDisplay::Height*PublicDisplay::current_id)); + return draw_area_y; } void wait_vsync(uint8_t syncs) { @@ -53,6 +56,13 @@ namespace JabyEngine { while(vsync_count < syncs); vsync_count = 0; } + + void render(const uint32_t* data, size_t words) { + wait_ready_for_CMD(); + for(size_t n = 0; n < words; n++) { + GPU_IO::GP0 = data[n]; + } + } } #ifndef USE_NO$PSX @@ -70,10 +80,16 @@ namespace JabyEngine { } #endif //USE_NO$PSX - uint8_t swap_buffers_vsync(uint8_t syncs) { - // Wait finish draw + uint8_t swap_buffers_vsync(uint8_t syncs, bool clear_screen) { + // Waits for finish FIFO + internal::wait_ready_for_CMD(); + internal::wait_vsync(syncs); - internal::Display::exchange_buffer_and_display(); + const auto draw_offset_y = internal::Display::exchange_buffer_and_display(); + internal::set_draw_offset(0, draw_offset_y); + if(clear_screen) { + internal::quick_fill_fast(Color24::Black(), AreaU16{0, static_cast(draw_offset_y), Display::Width, Display::Height}); + } return Display::current_id; } } diff --git a/src/Library/src/run.cpp b/src/Library/src/run.cpp index 37038004..d0c9940e 100644 --- a/src/Library/src/run.cpp +++ b/src/Library/src/run.cpp @@ -1,3 +1,4 @@ +#include #include extern void main();