Skip to content

Commit

Permalink
Removing mesh compile code, double buffering is still fucked up
Browse files Browse the repository at this point in the history
  • Loading branch information
nchalkley2 committed Aug 19, 2024
1 parent 23e7662 commit 531498c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ struct Vector;

namespace egg::ps2::graphics
{
using vif_packet_t = utils::inline_packet2<1024>;

void init();

// Uploads a VU program, returns the address of the loaded program
Expand All @@ -23,9 +21,16 @@ void wait_vsync();
void start_draw();
void end_draw();


using vif_packet_t = utils::inline_packet2<1024>;

// Returns the two vif packets in use
std::array<vif_packet_t, 2>& get_vif_packets();

packet2_t* get_current_vif_packet();

u8 get_vif_packet_context();

void flip_vip_packet_context();

} // namespace egg::ps2::graphics
25 changes: 12 additions & 13 deletions dependencies/egg-ps2-graphics-lib/src/egg-ps2-graphics-lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,29 @@ void init_drawing_environment(framebuffer_t* t_frame, zbuffer_t* t_z)

// Now send the packet, no need to wait since it's the first.
dma_channel_send_packet2(packet2, DMA_CHANNEL_GIF, 1);
dma_wait_fast();
dma_channel_wait(DMA_CHANNEL_GIF, 0);
}

void vu1_set_double_buffer_settings()
{
printf("egg-ps2-graphics-lib: vu1_set_double_buffer_settings\n");
utils::inline_packet2<2> packet2(P2_TYPE_NORMAL, P2_MODE_CHAIN, 1);
//packet2_utils_vu_add_double_buffer(packet2, 0, 512);
packet2_utils_vu_add_end_tag(packet2);
dma_channel_send_packet2(packet2, DMA_CHANNEL_VIF1, 1);
utils::inline_packet2<2> double_buffer_pkt(P2_TYPE_NORMAL, P2_MODE_CHAIN, 1);
packet2_utils_vu_add_double_buffer(double_buffer_pkt, 8, 496);
packet2_utils_vu_add_end_tag(double_buffer_pkt);

dma_channel_send_packet2(double_buffer_pkt, DMA_CHANNEL_VIF1, 1);
dma_channel_wait(DMA_CHANNEL_VIF1, 0);
}

void flip_buffers(framebuffer_t* t_frame)
{
utils::inline_packet2<3> flip(P2_TYPE_NORMAL, P2_MODE_NORMAL, 0);
static utils::inline_packet2<8> flip(P2_TYPE_UNCACHED_ACCL, P2_MODE_NORMAL, 0);
packet2_reset(flip, 0);
packet2_update(flip, draw_framebuffer(flip->next, 0, t_frame));
packet2_update(flip, draw_finish(flip->next));

dma_wait_fast();
dma_channel_send_packet2(flip, DMA_CHANNEL_GIF, 1);
dma_channel_wait(DMA_CHANNEL_GIF, 0);
dma_channel_send_packet2(flip, DMA_CHANNEL_GIF, 0);

draw_wait_finish();
}
Expand Down Expand Up @@ -166,7 +168,7 @@ void init()

init_drawing_environment(frame, &z);

init_draw_finish();
//init_draw_finish();

current_frame = frame;

Expand Down Expand Up @@ -212,16 +214,13 @@ void clear_screen(int r, int g, int b)
packet2_update(clear, draw_finish(clear->next));

// Now send our current dma chain.
dma_wait_fast();
dma_channel_send_packet2(clear, DMA_CHANNEL_GIF, 1);

// Wait for scene to finish drawing
draw_wait_finish();
}

void wait_vsync()
{
//printf("waiting vsync.........\n");
draw_wait_finish();
graph_wait_vsync();

graph_set_framebuffer_filtered(current_frame->address, current_frame->width, current_frame->psm, 0, 0);
Expand Down
64 changes: 24 additions & 40 deletions dependencies/egg-ps2-graphics-lib/src/mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,23 @@ namespace
/** Set GS primitive type of drawing. */
prim_t prim;

/**
* Color look up table.
* Needed for texture.
*/
clutbuffer_t clut;

/**
* Level of details.
* Needed for texture.
*/
lod_t lod;

void draw_strip(const Matrix& mesh_to_screen_matrix, const mesh_descriptor& mesh)
{
static bool initialized = false;
assert(mesh.is_valid());

if (!initialized)
{
// Define the triangle primitive we want to use.
prim.type = PRIM_TRIANGLE_STRIP;
prim.shading = PRIM_SHADE_GOURAUD;
prim.mapping = DRAW_DISABLE;
prim.fogging = DRAW_DISABLE;
prim.blending = DRAW_DISABLE;
prim.antialiasing = DRAW_DISABLE;
prim.mapping_type = PRIM_MAP_ST;
prim.colorfix = PRIM_UNFIXED;

initialized = true;
printf("Initialized\n");
}
// Define the triangle primitive we want to use.
prim.type = PRIM_TRIANGLE_STRIP;
prim.shading = PRIM_SHADE_GOURAUD;
prim.mapping = DRAW_DISABLE;
prim.fogging = DRAW_DISABLE;
prim.blending = DRAW_DISABLE;
prim.antialiasing = DRAW_DISABLE;
prim.mapping_type = PRIM_MAP_ST;
prim.colorfix = PRIM_UNFIXED;

packet2_reset(get_current_vif_packet(), 0);

packet2_utils_vu_open_unpack(get_current_vif_packet(), 0, true);
packet2_utils_vu_open_unpack(get_current_vif_packet(), 0, 1);
{
// 0
for (int i = 0; i < 4; ++i)
Expand All @@ -78,18 +60,20 @@ void draw_strip(const Matrix& mesh_to_screen_matrix, const mesh_descriptor& mesh
}
packet2_utils_vu_close_unpack(get_current_vif_packet());

packet2_utils_vu_add_unpack_data(get_current_vif_packet(), 8, (void*)mesh.pos, mesh.num_verts, true);
// Position data
packet2_utils_vu_add_unpack_data(get_current_vif_packet(), 8, (void*)mesh.pos, mesh.num_verts, 1);

assert(mesh.color != nullptr);
{
packet2_utils_vu_add_unpack_data(get_current_vif_packet(), 8 + mesh.num_verts, (void*)mesh.color, mesh.num_verts, true);
}
// Color data
packet2_utils_vu_add_unpack_data(get_current_vif_packet(), 8 + mesh.num_verts, (void*)mesh.color, mesh.num_verts, 1);

//assert((8 + (mesh.num_verts * 4)) < 496);

packet2_utils_vu_add_start_program(get_current_vif_packet(), mesh.vu_program_addr);
packet2_utils_vu_add_end_tag(get_current_vif_packet());

dma_channel_wait(DMA_CHANNEL_VIF1, 0);
dma_channel_send_packet2(get_current_vif_packet(), DMA_CHANNEL_VIF1, 1);
dma_wait_fast();
dma_channel_wait(DMA_CHANNEL_VIF1, 0);

flip_vip_packet_context();
}
Expand All @@ -103,7 +87,7 @@ void draw_mesh(const Matrix& mesh_to_screen_matrix, const mesh_descriptor& mesh)
assert(mesh.is_valid());

// TODO: calculate this dynamically based on how much stuff is being put into vu mem
static constexpr s32 verts_per_call = 50;
static constexpr s32 verts_per_call = 96;

for (u32 i = 0;;)
{
Expand Down Expand Up @@ -142,20 +126,20 @@ void draw_mesh(const Matrix& mesh_to_screen_matrix, const mesh_descriptor& mesh)
bool mesh_descriptor::is_valid(bool print_why_invalid) const
{
bool all_valid = true;
if (!__is_aligned(pos, 16))
if ((__uintptr_t)pos == 0)
{
if (print_why_invalid)
{
printf("Mesh invalid! Pos pointer not aligned to a 16 byte address!\n");
printf("Mesh invalid! Pos pointer is nullptr!\n");
}
all_valid &= false;
}

if ((__uintptr_t)pos == 0)
if (!__is_aligned(pos, 16))
{
if (print_why_invalid)
{
printf("Mesh invalid! Pos pointer is nullptr!\n");
printf("Mesh invalid! Pos pointer not aligned to a 16 byte address!\n");
}
all_valid &= false;
}
Expand Down
1 change: 0 additions & 1 deletion include/objects/mesh_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class MeshObject: public Renderable, public Debuggable, public RootComponentInte
MeshObject();
MeshObject(const Filesystem::Path& mesh_path);
MeshObject(Asset::Reference mesh_reference);
virtual void on_gs_init() override;
virtual void render(const GS::GSState& gs_state) override;
TransformComponent transform;
class Mesh* mesh;
Expand Down
3 changes: 0 additions & 3 deletions include/renderer/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ class Mesh: public Renderable, public Debuggable
std::unique_ptr<std::byte[]> mesh_bytes;
size_t mesh_size;

void compile();
void draw(const Matrix& render_matrix, bool flush = false);

bool is_valid() const { return mesh != nullptr && list >= 0; }
int get_triangle_count() const;

virtual void on_gs_init() override;

// for debug
const Filesystem::Path* path;
bool auto_compile;
Expand Down
1 change: 0 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ OUTPUT_DIR = build
ISO_FOLDER_DIR = build/iso

include src/Makefile.iso
# include src/Makefile.vuprogs

.PHONY: compile cleancompile
compile: $(EE_BIN)
Expand Down
14 changes: 0 additions & 14 deletions src/Makefile.vuprogs

This file was deleted.

8 changes: 0 additions & 8 deletions src/objects/mesh_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ MeshObject::MeshObject(Asset::Reference mesh_reference)
{
}

void MeshObject::on_gs_init()
{
if (mesh)
{
mesh->compile();
}
}

void MeshObject::render(const GS::GSState& gs_state)
{
if (mesh)
Expand Down
30 changes: 0 additions & 30 deletions src/renderer/mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,13 @@ Mesh::Mesh(const Filesystem::Path& in_path, bool in_auto_compile)
checkf(Filesystem::load_file(in_path, mesh_bytes, mesh_size, 16), in_path.data());
debug_name = in_path.data();
mesh = reinterpret_cast<MeshFileHeader*>(mesh_bytes.get());

auto_compile = in_auto_compile;
if (auto_compile && GS::has_gs_initialized())
{
compile();
}
}

Mesh::Mesh(Asset::Reference mesh_asset, bool in_auto_compile)
: Mesh(Asset::lookup_path(mesh_asset), in_auto_compile)
{
}

void Mesh::on_gs_init()
{
if (auto_compile)
{
compile();
}
}

void Mesh::compile()
{
if (is_valid())
{
printf("Trying to compile already compiled mesh!\nMesh list: %d\n", list);
return;
}

check(!is_valid());

if (mesh == nullptr)
{
printf("Trying to compile null mesh!\n");
return;
}
}

void Mesh::draw(const Matrix& render_matrix, bool flush)
{
Expand Down

0 comments on commit 531498c

Please sign in to comment.