From fc8ac2170e5e8bdf1763a9f5a8b73f6da812445a Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Wed, 16 Jul 2025 21:12:04 -0500 Subject: [PATCH] demo: add font --- demo.mk | 7 +- font/ter_u12n.data | Bin 0 -> 4096 bytes font/ter_u12n.data.h | 15 ++++ src/demo/ballistics.cpp | 2 + src/demo/demo.cpp | 3 + src/demo/font.cpp | 84 +++++++++++++++++++ src/demo/font.hpp | 28 +++++++ src/demo/graphics.cpp | 24 +++--- src/demo/graphics_primitive.hpp | 5 +- src/demo/ta_parameter_presets.hpp | 130 ++++++++++++++++++++++++++++++ src/demo/texture.cpp | 55 +++++++++++++ src/demo/texture.hpp | 7 ++ src/ta_parameter_presets.hpp | 62 -------------- 13 files changed, 346 insertions(+), 76 deletions(-) create mode 100644 font/ter_u12n.data create mode 100644 font/ter_u12n.data.h create mode 100644 src/demo/font.cpp create mode 100644 src/demo/font.hpp create mode 100644 src/demo/ta_parameter_presets.hpp create mode 100644 src/demo/texture.cpp create mode 100644 src/demo/texture.hpp delete mode 100644 src/ta_parameter_presets.hpp diff --git a/demo.mk b/demo.mk index 15177c2..263cf8e 100644 --- a/demo.mk +++ b/demo.mk @@ -1,3 +1,6 @@ +FONT_OBJ = \ + font/ter_u12n.data.o + DEMO_OBJ = \ $(LIB)/holly/core.o \ $(LIB)/holly/region_array.o \ @@ -14,7 +17,9 @@ DEMO_OBJ = \ src/demo/graphics.o \ src/demo/input.o \ src/demo/ballistics.o \ + src/demo/texture.o \ + src/demo/font.o \ src/physics/particle.o \ demo.elf: LDSCRIPT = $(LIB)/main.lds -demo.elf: $(START_OBJ) $(DEMO_OBJ) $(LIBGCC) +demo.elf: $(START_OBJ) $(DEMO_OBJ) $(FONT_OBJ) $(LIBGCC) diff --git a/font/ter_u12n.data b/font/ter_u12n.data new file mode 100644 index 0000000000000000000000000000000000000000..1d01af88d40ae8cb9d24618c92d08200838cd781 GIT binary patch literal 4096 zcmc(hfpXj+3`Ea=aQ^?F?Y>en zrMDD3)s}GhvIy37-EX1jxoQOOeg_R*+9eoGv?NQZf-1WUU*^V$F@Pphv1M`TuffW< zFv`VBkzri&^09_axR&vPzcJO>Q6#Am?X5+JbzZmW&|N8@F~SFj%V8OzuJM_V@IiI@ z4jPGGP{t)3zGZ{5S_t1_XJ|acFTScXe%shS@sN1vvtI?e7iA*|KdkkBB7fN~)rLk~ z9Nm%y9A{g(EW5@vM8;;n(>}tXa?}o;k$e=xtNyhy}I|Jn!w;N8-ztp&Fv%+{Ch4oh-CZ+9{3j zZQJBF=VQgoD|cIJzWGWozu4}BmfwB*z= z`6dDy=HR3r=siL4C;}Q=OOGAi&gpS~XMJ)$L3Dm1Ct3e;ck4SIZl&zCV$4)=38lF2 z`Zb8rckdVH;2??HFY1n4A-tDUXC8Kr1#3GW?$5fvP!ShS9(^L5^(&4QjNDWGWZSWP zR1}C@rib-W3`VJYTY6l6%!;k(9BRJ{bg&Mj1LZNn5VmP#sd-N|g3j++w`A6{uSc8L zJ%*PY$g zx3u$(*E65CV{Yi!+x4s`Jf#UVA-qZK={8zJ_V_4?QWM?|>eGYTayni-9 zLf3WcY=UPy-kxiZbH8{kamLikgzx{Hee9waB0A79bH!Y>;%9A^M6!KssxG|e_a(kZ zi?3}7K_uSt-LgZu2jP+X5k}Fs?efyEFkktsM%VRgOO4cO#OW21==V`-(7p@ZzHMI+ zZ6vitB17?``HN`O;53?b$JMJN@c8|6c72`!?7jO4ITWd8_86Zh-Z1?yqEj6u;e5_U fZOJo#bsDF^53&vgUrYYN%h^e=u^~So{N3eWUQ;QM literal 0 HcmV?d00001 diff --git a/font/ter_u12n.data.h b/font/ter_u12n.data.h new file mode 100644 index 0000000..9ad122b --- /dev/null +++ b/font/ter_u12n.data.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern uint32_t _binary_font_ter_u12n_data_start __asm("_binary_font_ter_u12n_data_start"); +extern uint32_t _binary_font_ter_u12n_data_end __asm("_binary_font_ter_u12n_data_end"); +extern uint32_t _binary_font_ter_u12n_data_size __asm("_binary_font_ter_u12n_data_size"); + +#ifdef __cplusplus +} +#endif diff --git a/src/demo/ballistics.cpp b/src/demo/ballistics.cpp index 1c11e80..01e1e6e 100644 --- a/src/demo/ballistics.cpp +++ b/src/demo/ballistics.cpp @@ -91,6 +91,8 @@ namespace demo { void ballistics::draw(ta_parameter_writer& writer, const mat4x4& trans) { + draw_grid(writer, trans * translate(vec3(0, 0, 50)) * scale(vec3(20, 10, 100))); + for (int i = 0; i < max_projectiles; i++) { projectile * projectile = &projectiles[i]; if (projectile->type == projectile_type::NONE) diff --git a/src/demo/demo.cpp b/src/demo/demo.cpp index c6a4809..a6f0f5e 100644 --- a/src/demo/demo.cpp +++ b/src/demo/demo.cpp @@ -4,6 +4,7 @@ #include "demo/graphics.hpp" #include "demo/input.hpp" +#include "demo/texture.hpp" #include "printf/printf.h" void vbr100() @@ -59,6 +60,8 @@ void main() input::init(); graphics::init(); + texture::init(); + printf("texture init\n"); interrupt_init(); system.IML6NRM = istnrm::end_of_render_tsp diff --git a/src/demo/font.cpp b/src/demo/font.cpp new file mode 100644 index 0000000..1ddd953 --- /dev/null +++ b/src/demo/font.cpp @@ -0,0 +1,84 @@ +#include "holly/isp_tsp.hpp" + +#include "demo/font.hpp" +#include "demo/ta_parameter_presets.hpp" +#include "demo/texture.hpp" + +namespace font { + + static inline vec2 transform_glyph_texture(const face& face, const vec2& t, int char_code) + { + int row = char_code / face.row_stride; + int col = char_code % face.row_stride; + + return { + (float)(col * face.width + t.x * face.width) / (float)(face.texture_width), + (float)(row * face.height + t.y * face.height) / (float)(face.texture_height), + }; + } + + static inline vec3 transform_glyph_position(const face& face, const vec2& p, float x, float y, float z) + { + return { + p.x * face.width + x, + p.y * face.height + y, + z + }; + } + + void face::global(ta_parameter_writer& writer) const + { + global_polygon_textured(writer, + para_control::list_type::punch_through, + texture_offset, + texture_size | tsp_instruction_word::dst_alpha_instr::inverse_src_alpha, + texture_control_word::pixel_format::_4bpp_palette); + } + + void face::draw_glyph(ta_parameter_writer& writer, + const vec3& p, + int c, + uint32_t base_color) const + { + static const vec2 vtx[] = { + { 0, 0 }, + { 1, 0 }, + { 1, 1 }, + { 0, 1 }, + }; + + if (c <= 0x20 || c >= 0x7f) { + return; + } + c -= 0x20; + + vec3 ap = transform_glyph_position(*this, vtx[0], p.x, p.y, p.z); + vec3 bp = transform_glyph_position(*this, vtx[1], p.x, p.y, p.z); + vec3 cp = transform_glyph_position(*this, vtx[2], p.x, p.y, p.z); + vec3 dp = transform_glyph_position(*this, vtx[3], p.x, p.y, p.z); + + vec2 at = transform_glyph_texture(*this, vtx[0], c); + vec2 bt = transform_glyph_texture(*this, vtx[1], c); + vec2 ct = transform_glyph_texture(*this, vtx[2], c); + vec2 dt = transform_glyph_texture(*this, vtx[3], c); + + quad_type_3(writer, + ap, at, + bp, bt, + cp, ct, + dp, dt, + base_color); + } + + const face ter_u12n = { + .texture_size = tsp_instruction_word::texture_u_size::from_int(128) + | tsp_instruction_word::texture_v_size::from_int(64), + .texture_offset = texture::offset::ter_u12n, + .texture_width = 128, + .texture_height = 64, + .hori_advance = 6, + .width = 6, + .height = 12, + .row_stride = 21, + }; +}; diff --git a/src/demo/font.hpp b/src/demo/font.hpp new file mode 100644 index 0000000..02f3892 --- /dev/null +++ b/src/demo/font.hpp @@ -0,0 +1,28 @@ +#include + +#include "math/float_types.hpp" + +#include "holly/ta_parameter.hpp" + +namespace font { + + struct face { + uint32_t texture_size; + uint32_t texture_offset; + int16_t texture_width; + int16_t texture_height; + int8_t hori_advance; + int8_t width; + int8_t height; + int8_t row_stride; + + void global(ta_parameter_writer& writer) const; + + void draw_glyph(ta_parameter_writer& writer, + const vec3& p, + int c, + uint32_t base_color) const; + }; + + extern const face ter_u12n; +} diff --git a/src/demo/graphics.cpp b/src/demo/graphics.cpp index 50b47f1..1e408c5 100644 --- a/src/demo/graphics.cpp +++ b/src/demo/graphics.cpp @@ -14,21 +14,22 @@ #include "holly/ta_fifo_texture_memory_transfer.hpp" #include "holly/ta_fifo_polygon_converter.hpp" -#include "ta_parameter_presets.hpp" - +#include "math/float_types.hpp" #include "math/transform.hpp" +#include "demo/ta_parameter_presets.hpp" #include "demo/graphics_primitive.hpp" #include "demo/graphics.hpp" #include "demo/ballistics.hpp" #include "demo/input.hpp" +#include "demo/font.hpp" #include "maple/maple_bus_bits.hpp" namespace graphics { - static uint8_t op_buf[1024 * 1024] __attribute__((aligned(32))); - static ta_parameter_writer writer(op_buf, (sizeof (op_buf))); + static uint8_t ta_buf[1024 * 1024] __attribute__((aligned(32))); + static ta_parameter_writer writer(ta_buf, (sizeof (ta_buf))); static volatile int ta_in_use = 0; static volatile int core_in_use = 0; @@ -37,7 +38,7 @@ namespace graphics { static volatile int next_frame_ix = 0; constexpr uint32_t ta_alloc - = ta_alloc_ctrl::pt_opb::no_list + = ta_alloc_ctrl::pt_opb::_32x4byte | ta_alloc_ctrl::tm_opb::no_list | ta_alloc_ctrl::t_opb::no_list | ta_alloc_ctrl::om_opb::no_list @@ -48,7 +49,7 @@ namespace graphics { .opaque_modifier = 0, .translucent = 0, .translucent_modifier = 0, - .punch_through = 0 + .punch_through = 32 * 4 }; mat4x4 view_trans; @@ -93,10 +94,13 @@ namespace graphics { void draw() { - draw_grid(writer, view_trans); - draw_axis(writer, view_trans); + font::ter_u12n.global(writer); + font::ter_u12n.draw_glyph(writer, vec3(10, 10, 10), 'a', 0xffffffff); - //draw_cube(writer, view_trans * scale(0.1f), {1, 1, 0}); + writer.append() = + ta_global_parameter::end_of_list(para_control::para_type::end_of_list); + + draw_axis(writer, view_trans); b.draw(writer, view_trans * scale(0.05f)); @@ -185,8 +189,6 @@ namespace graphics { } } - static int tick = 0; - void step() { view_transform(); diff --git a/src/demo/graphics_primitive.hpp b/src/demo/graphics_primitive.hpp index 6d61e61..665e67d 100644 --- a/src/demo/graphics_primitive.hpp +++ b/src/demo/graphics_primitive.hpp @@ -1,7 +1,8 @@ -#include "ta_parameter_presets.hpp" #include "math/math.hpp" #include "holly/framebuffer.hpp" +#include "demo/ta_parameter_presets.hpp" + #define _fsrra(n) (1.0f / (sqrt(n))) static inline void draw_line(ta_parameter_writer& writer, @@ -47,7 +48,7 @@ static inline void draw_grid(ta_parameter_writer& writer, for (int i = 1; i < 10; i++) { float x = (float)i * 0.1f - 0.5f; - { + if (0) { vec3 p1 = screen_transform(trans * vec3(x, 0, 0.5)); vec3 p2 = screen_transform(trans * vec3(x, 0, -0.5)); draw_line(writer, p1, p2); diff --git a/src/demo/ta_parameter_presets.hpp b/src/demo/ta_parameter_presets.hpp new file mode 100644 index 0000000..160abd7 --- /dev/null +++ b/src/demo/ta_parameter_presets.hpp @@ -0,0 +1,130 @@ +#pragma once + +#include "holly/ta_parameter.hpp" +#include "holly/ta_vertex_parameter.hpp" +#include "holly/ta_global_parameter.hpp" +#include "holly/isp_tsp.hpp" +#include "holly/texture_memory_alloc9.hpp" + +#include "math/float_types.hpp" + +static inline void global_polygon_intensity(ta_parameter_writer& writer, + const vec3& color) +{ + const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume + | para_control::list_type::opaque + | obj_control::col_type::intensity_mode_1 + ; + + const uint32_t isp_tsp_instruction_word = isp_tsp_instruction_word::depth_compare_mode::greater_or_equal + | isp_tsp_instruction_word::culling_mode::no_culling; + + const uint32_t tsp_instruction_word = tsp_instruction_word::texture_shading_instruction::decal + | tsp_instruction_word::src_alpha_instr::one + | tsp_instruction_word::dst_alpha_instr::zero + | tsp_instruction_word::fog_control::no_fog; + + const uint32_t texture_control_word = 0; + + writer.append() = + ta_global_parameter::polygon_type_1(parameter_control_word, + isp_tsp_instruction_word, + tsp_instruction_word, + texture_control_word, + 1.0f, color.x, color.y, color.z); +} + +static inline void global_polygon_textured(ta_parameter_writer& writer, + uint32_t list_type, + uint32_t texture_offset, + uint32_t texture_size, + uint32_t pixel_format) +{ + const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume + | list_type + | obj_control::col_type::packed_color + | obj_control::texture + ; + + const uint32_t isp_tsp_instruction_word = isp_tsp_instruction_word::depth_compare_mode::greater_or_equal + | isp_tsp_instruction_word::culling_mode::no_culling; + + const uint32_t tsp_instruction_word = tsp_instruction_word::texture_shading_instruction::modulate + | tsp_instruction_word::src_alpha_instr::one + | tsp_instruction_word::fog_control::no_fog + | texture_size; + + const uint32_t texture_address = texture_memory_alloc.texture.start + texture_offset; + const uint32_t texture_control_word = pixel_format + | texture_control_word::scan_order::twiddled + | texture_control_word::texture_address(texture_address / 8); + + writer.append() = + ta_global_parameter::polygon_type_0(parameter_control_word, + isp_tsp_instruction_word, + tsp_instruction_word, + texture_control_word, + 0, // data_size_for_sort_dma + 0 // next_address_for_sort_dma + ); +} + +static inline void quad_type_2(ta_parameter_writer& writer, + const vec3& ap, + const vec3& bp, + const vec3& cp, + const vec3& dp, + float base_intensity) +{ + writer.append() = + ta_vertex_parameter::polygon_type_2(polygon_vertex_parameter_control_word(false), + ap.x, ap.y, ap.z, + base_intensity); + + writer.append() = + ta_vertex_parameter::polygon_type_2(polygon_vertex_parameter_control_word(false), + bp.x, bp.y, bp.z, + base_intensity); + + writer.append() = + ta_vertex_parameter::polygon_type_2(polygon_vertex_parameter_control_word(false), + dp.x, dp.y, dp.z, + base_intensity); + + writer.append() = + ta_vertex_parameter::polygon_type_2(polygon_vertex_parameter_control_word(true), + cp.x, cp.y, cp.z, + base_intensity); +} + +static inline void quad_type_3(ta_parameter_writer& writer, + const vec3& ap, const vec2& at, + const vec3& bp, const vec2& bt, + const vec3& cp, const vec2& ct, + const vec3& dp, const vec2& dt, + uint32_t base_color) +{ + writer.append() = + ta_vertex_parameter::polygon_type_3(polygon_vertex_parameter_control_word(false), + ap.x, ap.y, ap.z, + at.x, at.y, + base_color, 0); + + writer.append() = + ta_vertex_parameter::polygon_type_3(polygon_vertex_parameter_control_word(false), + bp.x, bp.y, bp.z, + bt.x, bt.y, + base_color, 0); + + writer.append() = + ta_vertex_parameter::polygon_type_3(polygon_vertex_parameter_control_word(false), + dp.x, dp.y, dp.z, + dt.x, dt.y, + base_color, 0); + + writer.append() = + ta_vertex_parameter::polygon_type_3(polygon_vertex_parameter_control_word(true), + cp.x, cp.y, cp.z, + ct.x, ct.y, + base_color, 0); +} diff --git a/src/demo/texture.cpp b/src/demo/texture.cpp new file mode 100644 index 0000000..200f7af --- /dev/null +++ b/src/demo/texture.cpp @@ -0,0 +1,55 @@ +#include "texture.hpp" + +#include "systembus.hpp" + +#include "holly/holly.hpp" +#include "holly/core_bits.hpp" +#include "holly/texture_memory_alloc9.hpp" +#include "holly/ta_fifo_texture_memory_transfer.hpp" + +#include "font/ter_u12n.data.h" + +namespace texture { + struct entry { + void * start; + int size; + int offset; + }; + + const entry textures[] = { + { + .start = reinterpret_cast(&_binary_font_ter_u12n_data_start), + .size = reinterpret_cast(&_binary_font_ter_u12n_data_size), + .offset = offset::ter_u12n, + }, + }; + + const int textures_length = (sizeof (textures)) / (sizeof (textures[0])); + + static inline void transfer_palettes() + { + holly.PAL_RAM_CTRL = pal_ram_ctrl::pixel_format::argb4444; + + holly.PALETTE_RAM[0] = 0; + holly.PALETTE_RAM[1] = 0xffff; + + holly.PT_ALPHA_REF = 0xf0; + } + + void init() + { + system.LMMODE0 = 0; // 64-bit address space + system.LMMODE1 = 0; // 64-bit address space + + for (int i = 0; i < textures_length; i++) { + uint32_t offset = texture_memory_alloc.texture.start + textures[i].offset; + void * dst = reinterpret_cast(&ta_fifo_texture_memory[offset / 4]); + void * src = textures[i].start; + int size = textures[i].size; + + ta_fifo_texture_memory_transfer::copy(dst, src, size); + } + + transfer_palettes(); + } +} diff --git a/src/demo/texture.hpp b/src/demo/texture.hpp new file mode 100644 index 0000000..b250367 --- /dev/null +++ b/src/demo/texture.hpp @@ -0,0 +1,7 @@ +namespace texture { + namespace offset { + constexpr int ter_u12n = 0; + } + + void init(); +} diff --git a/src/ta_parameter_presets.hpp b/src/ta_parameter_presets.hpp deleted file mode 100644 index d9cbd2f..0000000 --- a/src/ta_parameter_presets.hpp +++ /dev/null @@ -1,62 +0,0 @@ -#pragma once - -#include "holly/ta_parameter.hpp" -#include "holly/ta_vertex_parameter.hpp" -#include "holly/ta_global_parameter.hpp" -#include "holly/isp_tsp.hpp" - -#include "math/float_types.hpp" - -static inline void global_polygon_intensity(ta_parameter_writer& writer, - const vec3& color) -{ - const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume - | para_control::list_type::opaque - | obj_control::col_type::intensity_mode_1 - ; - - const uint32_t isp_tsp_instruction_word = isp_tsp_instruction_word::depth_compare_mode::greater_or_equal - | isp_tsp_instruction_word::culling_mode::no_culling; - - const uint32_t tsp_instruction_word = tsp_instruction_word::texture_shading_instruction::decal - | tsp_instruction_word::src_alpha_instr::one - | tsp_instruction_word::dst_alpha_instr::zero - | tsp_instruction_word::fog_control::no_fog; - - const uint32_t texture_control_word = 0; - - writer.append() = - ta_global_parameter::polygon_type_1(parameter_control_word, - isp_tsp_instruction_word, - tsp_instruction_word, - texture_control_word, - 1.0f, color.x, color.y, color.z); -} - -static inline void quad_type_2(ta_parameter_writer& writer, - const vec3& ap, - const vec3& bp, - const vec3& cp, - const vec3& dp, - float base_intensity) -{ - writer.append() = - ta_vertex_parameter::polygon_type_2(polygon_vertex_parameter_control_word(false), - ap.x, ap.y, ap.z, - base_intensity); - - writer.append() = - ta_vertex_parameter::polygon_type_2(polygon_vertex_parameter_control_word(false), - bp.x, bp.y, bp.z, - base_intensity); - - writer.append() = - ta_vertex_parameter::polygon_type_2(polygon_vertex_parameter_control_word(false), - dp.x, dp.y, dp.z, - base_intensity); - - writer.append() = - ta_vertex_parameter::polygon_type_2(polygon_vertex_parameter_control_word(true), - cp.x, cp.y, cp.z, - base_intensity); -}