From de4ba5e1f38e81a683f6615df61583316855a638 Mon Sep 17 00:00:00 2001 From: Jaby Date: Mon, 1 May 2023 22:37:41 +0200 Subject: [PATCH] Fix image conversion and CLUT placement --- include/PSX/File/file_types.hpp | 19 ++++++--- .../src/File/Processor/tim_processor.cpp | 41 ++++++++++++------- .../src/images/reduced_tim/mod.rs | 6 +-- .../src/images/reduced_tim/types.rs | 4 ++ src/Tools/tool_helper/Cargo.toml | 2 +- src/Tools/tool_helper/src/bits.rs | 2 +- 6 files changed, 50 insertions(+), 24 deletions(-) diff --git a/include/PSX/File/file_types.hpp b/include/PSX/File/file_types.hpp index bba6c5f5..fe077595 100644 --- a/include/PSX/File/file_types.hpp +++ b/include/PSX/File/file_types.hpp @@ -1,6 +1,7 @@ #ifndef __JABYENGINE_FILE_TYPES_HPP__ #define __JABYENGINE_FILE_TYPES_HPP__ #include "../Auxiliary/bits.hpp" +#include "../GPU/gpu_types.hpp" #include "../jabyengine_defines.h" namespace JabyEngine { @@ -19,24 +20,32 @@ namespace JabyEngine { this->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(clutY)) { + 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))) { } - constexpr uint16_t getTextureX() const { + constexpr uint16_t get_texture_x() const { return bit::value::get_normalized(this->raw, TextureX) << 1; } - constexpr uint16_t getTextureY() const { + constexpr uint16_t get_texture_y() const { return bit::value::get_normalized(this->raw, TextureY) << 1; } - constexpr uint16_t getClutX() const { + constexpr GPU::PositionU16 get_texture_position() const { + return {SimpleTIM::get_texture_x(), SimpleTIM::get_texture_y()}; + } + + constexpr uint16_t get_clut_x() const { return bit::value::get_normalized(this->raw, SimpleTIM::ClutX) << 4; } - constexpr uint16_t getClutY() const { + constexpr uint16_t get_clut_y() const { return bit::value::get_normalized(this->raw, ClutY); } + + constexpr GPU::PositionU16 get_clut_position() const { + return {SimpleTIM::get_clut_x(), SimpleTIM::get_clut_y()}; + } }; struct __no_align CopyTo { diff --git a/src/Library/src/File/Processor/tim_processor.cpp b/src/Library/src/File/Processor/tim_processor.cpp index f0528270..0ff40dbf 100644 --- a/src/Library/src/File/Processor/tim_processor.cpp +++ b/src/Library/src/File/Processor/tim_processor.cpp @@ -1,10 +1,12 @@ #include "../../../internal-include/GPU/gpu_internal.hpp" #include "simplehelper.hpp" +#include #include #include namespace JabyEngine { namespace FileProcessor { + using GPU::AreaU16; using GPU::PositionU16; using GPU::SizeU16; @@ -12,20 +14,28 @@ namespace JabyEngine { constexpr SimpleTIMSize() { } - constexpr uint16_t getTextureWidth() const { - return SimpleTIM::getTextureX(); + constexpr uint16_t get_texture_width() const { + return SimpleTIM::get_texture_x(); } - constexpr uint16_t getTextureHeight() const { - return SimpleTIM::getTextureY(); + constexpr uint16_t get_texture_height() const { + return SimpleTIM::get_texture_y(); } - constexpr uint16_t getClutWidth() const { - return SimpleTIM::getClutX(); + constexpr SizeU16 get_texture_size() const { + return {SimpleTIMSize::get_texture_width(), SimpleTIMSize::get_texture_height()}; } - constexpr uint16_t getClutHeight() const { - return SimpleTIM::getClutY(); + constexpr uint16_t get_clut_width() const { + return SimpleTIM::get_clut_x(); + } + + constexpr uint16_t get_clut_height() const { + return SimpleTIM::get_clut_y(); + } + + constexpr SizeU16 get_clut_size() const { + return {SimpleTIMSize::get_clut_width(), SimpleTIMSize::get_clut_height()}; } }; @@ -44,9 +54,12 @@ namespace JabyEngine { GPU::internal::DMA::Receive::set_src(reinterpret_cast(src)); } - static void set_gpu_receive_data(const uint32_t* src, SimpleTIMState& state, uint16_t width, uint16_t height) { - state.words_left = (width*height)/2; - set_gpu_receive(src, state.dst_info.getTextureX(), state.dst_info.getTextureY(), width, height); + static size_t set_gpu_receive_data(const uint32_t* src, const AreaU16& dst) { + const auto width = dst.size.width; + const auto height = dst.size.height; + + set_gpu_receive(src, dst.position.x, dst.position.y, width, height); + return (width*height)/2; } static Progress parse_data(State::Configuration& config, SimpleTIMState& state) { @@ -91,7 +104,7 @@ namespace JabyEngine { } static Progress switch_state_parse_data(State::Configuration& config, SimpleTIMState& state) { - set_gpu_receive_data(reinterpret_cast(config.data_adr), state, state.size_info.getTextureWidth(), state.size_info.getTextureHeight()); + state.words_left = set_gpu_receive_data(reinterpret_cast(config.data_adr), {state.dst_info.get_texture_position(), state.size_info.get_texture_size()}); return Helper::exchange_and_execute_process_function(parse_data, config, state); } @@ -110,9 +123,9 @@ namespace JabyEngine { Helper::simple_read(state.size_info, config); //Check if we have a clut to care about - if(state.size_info.getClutWidth() > 0) { + if(state.size_info.get_clut_width() > 0) { //CLUTs are 16bit full color anyway - set_gpu_receive_data(reinterpret_cast(config.data_adr), state, state.size_info.getClutWidth(), state.size_info.getClutHeight()); + state.words_left = set_gpu_receive_data(reinterpret_cast(config.data_adr), {state.dst_info.get_clut_position(), state.size_info.get_clut_size()}); return Helper::exchange_and_execute_process_function(parse_clut, config, state); } diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs index 53a80907..415105c6 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs @@ -64,18 +64,18 @@ fn encode(image: T, color_depth: ColorType, clut_align: Cl if let Some(palette) = palette { let mut color_count = pal_width*pal_height; for color in palette { - tool_helper::raw::write_generic(output, color)?; + tool_helper::raw::write_generic(output, color.get_raw())?; color_count -= 1; } while color_count > 0 { - tool_helper::raw::write_generic(output, PSXColor::black())?; + tool_helper::raw::write_generic(output, PSXColor::black().get_raw())?; color_count -= 1; } } for color in image { - tool_helper::raw::write_generic(output, color)?; + tool_helper::raw::write_generic(output, color.get_raw())?; } Ok(()) diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs index 5da937e0..e9600a38 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/types.rs @@ -102,6 +102,10 @@ impl Color { Color{value} } + pub const fn get_raw(&self) -> u16 { + self.value.to_le() + } + const fn new(stp: u8, red: u8, green: u8, blue: u8) -> Color { let value = set_member_value!(set_member_value!(set_member_value!(set_member_value!(0, stp, 0, u16), diff --git a/src/Tools/tool_helper/Cargo.toml b/src/Tools/tool_helper/Cargo.toml index eda5d603..ae2fa4c8 100644 --- a/src/Tools/tool_helper/Cargo.toml +++ b/src/Tools/tool_helper/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tool_helper" -version = "0.8.0" +version = "0.8.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/Tools/tool_helper/src/bits.rs b/src/Tools/tool_helper/src/bits.rs index 7dec95a7..f68fc377 100644 --- a/src/Tools/tool_helper/src/bits.rs +++ b/src/Tools/tool_helper/src/bits.rs @@ -57,7 +57,7 @@ macro_rules! create_bit_functions { } pub const fn [< get_value_ $type_val >](src: $type_val, range: &BitRange) -> $type_val { - (src & [< get_mask_ $type_val >](range)) >> range.start + (src & ([< get_mask_ $type_val >](range) << (range.start as $type_val))) >> range.start } pub const fn [< bit_set_ $type_val >](src: $type_val, bit: usize) -> bool {