From 7e03eee3ed51dd3f2f618f3e01cb7bcf5d1b1ee1 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Wed, 20 Dec 2023 22:28:42 +0800 Subject: [PATCH] font_bitmap: use 4bpp palette --- example/example.mk | 8 ++--- example/{font.cpp => font_bitmap.cpp} | 4 +-- twiddle.hpp | 47 +++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) rename example/{font.cpp => font_bitmap.cpp} (99%) diff --git a/example/example.mk b/example/example.mk index 14118b8..014c764 100644 --- a/example/example.mk +++ b/example/example.mk @@ -33,8 +33,8 @@ MACAW_TWIDDLE_OBJ = \ example/macaw_twiddle.elf: LDSCRIPT = $(LIB)/alt.lds example/macaw_twiddle.elf: $(START_OBJ) $(MACAW_TWIDDLE_OBJ) -FONT_OBJ = \ - example/font.o \ +FONT_BITMAP_OBJ = \ + example/font_bitmap.o \ vga.o \ holly/core.o \ holly/region_array.o \ @@ -43,8 +43,8 @@ FONT_OBJ = \ serial.o \ sperrypc.data.o -example/font.elf: LDSCRIPT = $(LIB)/alt.lds -example/font.elf: $(START_OBJ) $(FONT_OBJ) +example/font_bitmap.elf: LDSCRIPT = $(LIB)/alt.lds +example/font_bitmap.elf: $(START_OBJ) $(FONT_BITMAP_OBJ) MACAW_MULTIPASS_OBJ = \ example/macaw_multipass.o \ diff --git a/example/font.cpp b/example/font_bitmap.cpp similarity index 99% rename from example/font.cpp rename to example/font_bitmap.cpp index 5c83385..aba0929 100644 --- a/example/font.cpp +++ b/example/font_bitmap.cpp @@ -107,7 +107,7 @@ uint32_t transform(uint32_t * ta_parameter_buf, const char * s, const uint32_t l | tsp_instruction_word::texture_u_size::_8 // 8px | tsp_instruction_word::texture_v_size::_8; // 8px - polygon.texture_control_word = texture_control_word::pixel_format::_8bpp_palette + polygon.texture_control_word = texture_control_word::pixel_format::_4bpp_palette | texture_control_word::scan_order::twiddled | texture_control_word::texture_address((texture_address + 8 * 8 * (s[string_ix] - ' ')) / 8); parameter.append() = polygon; @@ -182,7 +182,7 @@ inline void inflate_character(const uint8_t * src, const uint8_t c) } temp2; //twiddle::texture(&texture[offset], temp, 8, 8); - twiddle::texture(temp2.u8, temp, 8, 8); + twiddle::texture_4bpp(temp2.u8, temp, 8, 8); for (uint32_t i = 0; i < 8 * 8 / 4; i++) { texture[(offset / 4) + i] = temp2.u32[i]; } diff --git a/twiddle.hpp b/twiddle.hpp index 8576826..3681dda 100644 --- a/twiddle.hpp +++ b/twiddle.hpp @@ -1,4 +1,5 @@ #include +#include namespace twiddle { @@ -57,6 +58,38 @@ static_assert(from_xy(0b000, 0b101) == 17); static_assert(from_xy(0b000, 0b110) == 20); static_assert(from_xy(0b000, 0b111) == 21); +constexpr inline std::array +from_ix(uint32_t curve_ix) +{ + std::array x_y = {0, 0}; + uint32_t curve_bit = 0; + + while (curve_ix != 0) { + x_y[(curve_bit + 1) % 2] |= (curve_ix & 1) << (curve_bit / 2); + curve_ix >>= 1; + curve_bit += 1; + } + + return x_y; +} + +static_assert(from_ix(0) == std::array{{0b000, 0b000}}); +static_assert(from_ix(2) == std::array{{0b001, 0b000}}); +static_assert(from_ix(8) == std::array{{0b010, 0b000}}); +static_assert(from_ix(10) == std::array{{0b011, 0b000}}); +static_assert(from_ix(32) == std::array{{0b100, 0b000}}); +static_assert(from_ix(34) == std::array{{0b101, 0b000}}); +static_assert(from_ix(40) == std::array{{0b110, 0b000}}); +static_assert(from_ix(42) == std::array{{0b111, 0b000}}); + +static_assert(from_ix(1) == std::array{{0b000, 0b001}}); +static_assert(from_ix(4) == std::array{{0b000, 0b010}}); +static_assert(from_ix(5) == std::array{{0b000, 0b011}}); +static_assert(from_ix(16) == std::array{{0b000, 0b100}}); +static_assert(from_ix(17) == std::array{{0b000, 0b101}}); +static_assert(from_ix(20) == std::array{{0b000, 0b110}}); +static_assert(from_ix(21) == std::array{{0b000, 0b111}}); + template void texture(volatile T * dst, const T * src, const uint32_t width, const uint32_t height) { @@ -69,4 +102,18 @@ void texture(volatile T * dst, const T * src, const uint32_t width, const uint32 } } +template +void texture_4bpp(volatile T * dst, const T * src, const uint32_t width, const uint32_t height) +{ + for (uint32_t y = 0; y < height; y++) { + for (uint32_t x = 0; x < width; x++) { + uint32_t twiddle_ix = from_xy(x, y); + T value = src[y * width + x]; + uint32_t shift = (4 * (twiddle_ix & 1)); + dst[twiddle_ix / 2] &= ~(0b1111 << shift); + dst[twiddle_ix / 2] |= value << shift; + } + } +} + }