cube: a perspective cube

This commit is contained in:
Zack Buhman 2023-12-17 17:04:34 +08:00
parent 2f7f0a6bdf
commit 59f8c1b3ff
7 changed files with 207 additions and 21 deletions

View File

@ -2,7 +2,7 @@ MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
DIR := $(dir $(MAKEFILE_PATH)) DIR := $(dir $(MAKEFILE_PATH))
LIB ?= . LIB ?= .
OPT ?= -O3 OPT ?= -Os
DEBUG ?= -g -gdwarf-4 DEBUG ?= -g -gdwarf-4
GENERATED ?= GENERATED ?=

View File

@ -1,12 +1,197 @@
#include <cstdint> #include <cstdint>
#include "align.hpp"
#include "vga.hpp" #include "vga.hpp"
#include "holly/texture_memory_alloc.hpp"
#include "holly.hpp"
#include "holly/core.hpp"
#include "holly/core_bits.hpp"
#include "holly/ta_fifo_polygon_converter.hpp"
#include "holly/ta_parameter.hpp"
#include "holly/ta_bits.hpp"
#include "holly/region_array.hpp"
#include "holly/background.hpp"
#include "memorymap.hpp" #include "memorymap.hpp"
#include "rgb.hpp"
#include "serial.hpp"
#include "macaw.hpp"
struct vertex {
float x;
float y;
float z;
float u;
float v;
};
vertex cube_faces[][4] = {
{ // front
{ -1.0f, -1.0f, 1.0f, 0.f, 0.f},
{ -1.0f, 1.0f, 1.0f, 0.f, 1.f},
{ 1.0f, -1.0f, 1.0f, 1.f, 0.f},
{ 1.0f, 1.0f, 1.0f, 1.f, 1.f},
},
{ // back
{ -1.0f, -1.0f, -1.0f, 1.f, 0.f},
{ -1.0f, 1.0f, -1.0f, 1.f, 1.f},
{ 1.0f, -1.0f, -1.0f, 0.f, 0.f},
{ 1.0f, 1.0f, -1.0f, 0.f, 1.f},
},
{ // right side
{ 1.0f, -1.0f, 1.0f, 0.f, 0.f},
{ 1.0f, 1.0f, 1.0f, 0.f, 1.f},
{ 1.0f, -1.0f, -1.0f, 1.f, 0.f},
{ 1.0f, 1.0f, -1.0f, 1.f, 1.f},
},
{ // left side
{ -1.0f, -1.0f, 1.0f, 1.f, 0.f},
{ -1.0f, 1.0f, 1.0f, 1.f, 1.f},
{ -1.0f, -1.0f, -1.0f, 0.f, 0.f},
{ -1.0f, 1.0f, -1.0f, 0.f, 1.f},
},
};
constexpr uint32_t num_faces = (sizeof (cube_faces)) / (sizeof (cube_faces[0]));
constexpr uint32_t color = 0xffff00ff;
static float theta = 0;
constexpr float half_degree = 0.01745329f / 2.f;
void transform(ta_parameter_writer& parameter,
const vertex * strip_vertices,
const uint32_t strip_length)
{
uint32_t texture_address = (offsetof (struct texture_memory_alloc, texture));
parameter.append<global_polygon_type_0>() = global_polygon_type_0(texture_address);
for (uint32_t i = 0; i < strip_length; i++) {
bool end_of_strip = i == strip_length - 1;
float x = strip_vertices[i].x;
float y = strip_vertices[i].y;
float z = strip_vertices[i].z;
float t;
t = y * __builtin_cosf(theta) - z * __builtin_sinf(theta);
z = y * __builtin_sinf(theta) + z * __builtin_cosf(theta);
y = t;
float theta2 = 3.14 * __builtin_sinf(theta / 2);
t = x * __builtin_cosf(theta2) - z * __builtin_sinf(theta2);
z = x * __builtin_sinf(theta2) + z * __builtin_cosf(theta2);
x = t;
z += 3;
// perspective
x = x / z;
y = y / z;
x *= 240.f;
y *= 240.f;
x += 320.f;
y += 240.f;
z = 1 / z;
parameter.append<vertex_polygon_type_3>() =
vertex_polygon_type_3(x, y, z,
strip_vertices[i].u,
strip_vertices[i].v,
color,
end_of_strip);
}
}
void init_texture_memory(const struct opb_size& opb_size)
{
volatile texture_memory_alloc * mem = reinterpret_cast<volatile texture_memory_alloc *>(texture_memory);
background_parameter(mem->background);
region_array2(mem->region_array,
(offsetof (struct texture_memory_alloc, object_list)),
640 / 32, // width
480 / 32, // height
opb_size
);
}
void copy_macaw_texture()
{
auto src = reinterpret_cast<const uint8_t *>(&_binary_macaw_data_start);
auto size = reinterpret_cast<const uint32_t>(&_binary_macaw_data_size);
auto mem = reinterpret_cast<texture_memory_alloc *>(0xa400'0000);
for (uint32_t px = 0; px < size / 3; px++) {
uint8_t r = src[px * 3 + 0];
uint8_t g = src[px * 3 + 1];
uint8_t b = src[px * 3 + 2];
uint16_t rgb565 = ((r / 8) << 11) | ((g / 4) << 5) | ((b / 8) << 0);
mem->texture[px] = rgb565;
}
}
uint32_t _ta_parameter_buf[((32 * (5 * 6 + 1)) + 32) / 4];
void main() void main()
{ {
vga(); vga();
copy_macaw_texture();
while (1); // The address of `ta_parameter_buf` must be a multiple of 32 bytes.
// This is mandatory for ch2-dma to the ta fifo polygon converter.
uint32_t * ta_parameter_buf = align_32byte(_ta_parameter_buf);
constexpr uint32_t ta_alloc = ta_alloc_ctrl::pt_opb::no_list
| ta_alloc_ctrl::tm_opb::no_list
| ta_alloc_ctrl::t_opb::no_list
| ta_alloc_ctrl::om_opb::no_list
| ta_alloc_ctrl::o_opb::_16x4byte;
constexpr struct opb_size opb_size = { .opaque = 16 * 4
, .opaque_modifier = 0
, .translucent = 0
, .translucent_modifier = 0
, .punch_through = 0
};
constexpr uint32_t tiles = (640 / 32) * (320 / 32);
holly.SOFTRESET = softreset::pipeline_soft_reset
| softreset::ta_soft_reset;
holly.SOFTRESET = 0;
core_init();
init_texture_memory(opb_size);
uint32_t frame_ix = 0;
constexpr uint32_t num_frames = 1;
while (1) {
ta_polygon_converter_init(opb_size.total() * tiles, ta_alloc);
auto parameter = ta_parameter_writer(ta_parameter_buf);
for (uint32_t i = 0; i < num_faces; i++) {
transform(parameter, cube_faces[i], 4);
}
parameter.append<global_end_of_list>() = global_end_of_list();
ta_polygon_converter_transfer(ta_parameter_buf, parameter.offset);
ta_wait_opaque_list();
core_start_render(frame_ix, num_frames);
v_sync_out();
v_sync_in();
core_wait_end_of_render_video(frame_ix, num_frames);
theta += half_degree;
frame_ix += 1;
}
} }

View File

@ -74,7 +74,10 @@ CUBE_OBJ = \
example/cube.o \ example/cube.o \
vga.o \ vga.o \
holly/core.o \ holly/core.o \
holly/ta_fifo_polygon_converter.o holly/region_array.o \
holly/background.o \
holly/ta_fifo_polygon_converter.o \
macaw.data.o
example/cube.elf: LDSCRIPT = $(LIB)/alt.lds example/cube.elf: LDSCRIPT = $(LIB)/alt.lds
example/cube.elf: $(START_OBJ) $(CUBE_OBJ) example/cube.elf: $(START_OBJ) $(CUBE_OBJ)

View File

@ -89,12 +89,8 @@ void init_texture_memory(const struct opb_size& opb_size)
); );
} }
uint32_t _ta_parameter_buf[((32 * (strip_length + 2)) + 32) / 4]; void copy_macaw_texture()
void main()
{ {
vga();
auto src = reinterpret_cast<const uint8_t *>(&_binary_macaw_data_start); auto src = reinterpret_cast<const uint8_t *>(&_binary_macaw_data_start);
auto size = reinterpret_cast<const uint32_t>(&_binary_macaw_data_size); auto size = reinterpret_cast<const uint32_t>(&_binary_macaw_data_size);
auto mem = reinterpret_cast<texture_memory_alloc *>(0xa400'0000); auto mem = reinterpret_cast<texture_memory_alloc *>(0xa400'0000);
@ -106,6 +102,14 @@ void main()
uint16_t rgb565 = ((r / 8) << 11) | ((g / 4) << 5) | ((b / 8) << 0); uint16_t rgb565 = ((r / 8) << 11) | ((g / 4) << 5) | ((b / 8) << 0);
mem->texture[px] = rgb565; mem->texture[px] = rgb565;
} }
}
uint32_t _ta_parameter_buf[((32 * (strip_length + 2)) + 32) / 4];
void main()
{
vga();
copy_macaw_texture();
// The address of `ta_parameter_buf` must be a multiple of 32 bytes. // The address of `ta_parameter_buf` must be a multiple of 32 bytes.
// This is mandatory for ch2-dma to the ta fifo polygon converter. // This is mandatory for ch2-dma to the ta fifo polygon converter.

View File

@ -13,14 +13,13 @@
void core_init() void core_init()
{ {
holly.ISP_FEED_CFG = isp_feed_cfg::cache_size_for_translucency(0x200) holly.ISP_FEED_CFG = isp_feed_cfg::cache_size_for_translucency(0x200);
| isp_feed_cfg::pre_sort_mode;
holly.FPU_SHAD_SCALE = fpu_shad_scale::scale_factor_for_shadows(1); holly.FPU_SHAD_SCALE = fpu_shad_scale::scale_factor_for_shadows(1);
holly.FPU_CULL_VAL = _i(1.f); holly.FPU_CULL_VAL = _i(1.f);
holly.FPU_PERP_VAL = _i(0.f); holly.FPU_PERP_VAL = _i(0.f);
holly.SPAN_SORT_CFG = span_sort_cfg::span_sort_enable holly.SPAN_SORT_CFG = 0; /*span_sort_cfg::span_sort_enable
| span_sort_cfg::offset_sort_enable; | span_sort_cfg::offset_sort_enable;*/
holly.FOG_COL_RAM = fog_col_ram::red(127) holly.FOG_COL_RAM = fog_col_ram::red(127)
| fog_col_ram::green(127) | fog_col_ram::green(127)

View File

@ -95,8 +95,6 @@ void region_array2(volatile uint32_t * buf,
region_array[ix].tile |= REGION_ARRAY__LAST_REGION; region_array[ix].tile |= REGION_ARRAY__LAST_REGION;
uint32_t tile_index = y * width + x; uint32_t tile_index = y * width + x;
constexpr uint32_t opaque_list_opb_size = 16 * 4; // in bytes; this must match O_OPB in TA_ALLOC_CTRL
constexpr uint32_t translucent_list_opb_size = 16 * 4; // in bytes; this must match O_OPB in TA_ALLOC_CTRL
region_array[ix].opaque_list_pointer = (opb_size.opaque == 0) ? REGION_ARRAY__LIST_POINTER__EMPTY : region_array[ix].opaque_list_pointer = (opb_size.opaque == 0) ? REGION_ARRAY__LIST_POINTER__EMPTY :
(ol_base + (opb_size.opaque * tile_index) (ol_base + (opb_size.opaque * tile_index)
); );

View File

@ -174,17 +174,14 @@ struct global_polygon_type_0 {
// untextured // untextured
global_polygon_type_0() global_polygon_type_0()
: parameter_control_word( para_control::para_type::polygon_or_modifier_volume : parameter_control_word( para_control::para_type::polygon_or_modifier_volume
| para_control::list_type::translucent | obj_control::col_type::packed_color )
| obj_control::col_type::packed_color
| obj_control::gouraud )
, isp_tsp_instruction_word( isp_tsp_instruction_word::depth_compare_mode::always , isp_tsp_instruction_word( isp_tsp_instruction_word::depth_compare_mode::always
| isp_tsp_instruction_word::culling_mode::no_culling ) | isp_tsp_instruction_word::culling_mode::no_culling )
, tsp_instruction_word( tsp_instruction_word::src_alpha_instr::src_alpha , tsp_instruction_word( tsp_instruction_word::src_alpha_instr::src_alpha
| tsp_instruction_word::dst_alpha_instr::inverse_src_alpha | tsp_instruction_word::dst_alpha_instr::inverse_src_alpha
| tsp_instruction_word::fog_control::no_fog | tsp_instruction_word::fog_control::no_fog )
| tsp_instruction_word::use_alpha )
, texture_control_word( 0 ) , texture_control_word( 0 )
@ -201,7 +198,7 @@ struct global_polygon_type_0 {
| obj_control::col_type::packed_color | obj_control::col_type::packed_color
| obj_control::texture ) | obj_control::texture )
, isp_tsp_instruction_word( isp_tsp_instruction_word::depth_compare_mode::always , isp_tsp_instruction_word( isp_tsp_instruction_word::depth_compare_mode::greater
| isp_tsp_instruction_word::culling_mode::no_culling ) | isp_tsp_instruction_word::culling_mode::no_culling )
// <Note> Because a value of "0.0" is invalid for [MIP-Map] D [adjust], it must not be specified. // <Note> Because a value of "0.0" is invalid for [MIP-Map] D [adjust], it must not be specified.