Skip to content

Commit

Permalink
Why doesn't double buffering work??? ugh
Browse files Browse the repository at this point in the history
  • Loading branch information
nchalkley2 committed Aug 19, 2024
1 parent a905da0 commit 23e7662
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#pragma once

#include <packet2.h>
#include <array>
#include "types.hpp"

struct Matrix;
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 @@ -18,4 +22,10 @@ void wait_vsync();

void start_draw();
void end_draw();

// 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ struct inline_packet2: public inline_packet2_base
{
qword_t data[qwords] __attribute__((aligned(16)));

inline_packet2() = default;
inline_packet2()
{
assert(__is_aligned(&data[0], 16));
};

inline_packet2(enum Packet2Type type, enum Packet2Mode mode, u8 tte)
{
assert(__is_aligned(&data[0], 16));

initialize(type, mode, tte);
}

Expand Down
31 changes: 29 additions & 2 deletions dependencies/egg-ps2-graphics-lib/src/egg-ps2-graphics-lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ void init_drawing_environment(framebuffer_t* t_frame, zbuffer_t* t_z)
void vu1_set_double_buffer_settings()
{
printf("egg-ps2-graphics-lib: vu1_set_double_buffer_settings\n");
utils::inline_packet2<1> packet2(P2_TYPE_NORMAL, P2_MODE_CHAIN, 1);
//packet2_utils_vu_add_double_buffer(packet2, 0, 500);
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);
dma_channel_wait(DMA_CHANNEL_VIF1, 0);
Expand Down Expand Up @@ -139,6 +139,9 @@ void init_draw_finish()
packet2_utils_vu_add_end_tag(draw_finish_packet);
}

static u8 vif_packets_context = 0;
static std::array<vif_packet_t, 2> vif_packets;

} // namespace

namespace egg::ps2::graphics
Expand Down Expand Up @@ -166,6 +169,10 @@ void init()
init_draw_finish();

current_frame = frame;

vif_packets[0].initialize(P2_TYPE_NORMAL, P2_MODE_CHAIN, 1);
vif_packets[1].initialize(P2_TYPE_NORMAL, P2_MODE_CHAIN, 1);
vif_packets_context = 0;
}

u32 load_vu_program(void* program_start_address, void* program_end_address)
Expand Down Expand Up @@ -244,4 +251,24 @@ void end_draw()
*GS_REG_CSR |= 2;
}

std::array<vif_packet_t, 2>& get_vif_packets()
{
return vif_packets;
}

packet2_t* get_current_vif_packet()
{
return vif_packets[vif_packets_context];
}

u8 get_vif_packet_context()
{
return vif_packets_context;
}

void flip_vip_packet_context()
{
vif_packets_context ^= 1;
}

} // namespace egg::ps2::graphics
64 changes: 18 additions & 46 deletions dependencies/egg-ps2-graphics-lib/src/mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ clutbuffer_t clut;
*/
lod_t lod;

u8 context = 0;
packet2_t* vif_packets[2] __attribute__((aligned(64)));

packet2_t* curr_vif_packet;

void draw_strip(const Matrix& mesh_to_screen_matrix, const mesh_descriptor& mesh)
{
static bool initialized = false;
Expand All @@ -53,73 +48,50 @@ void draw_strip(const Matrix& mesh_to_screen_matrix, const mesh_descriptor& mesh
prim.mapping_type = PRIM_MAP_ST;
prim.colorfix = PRIM_UNFIXED;

printf("initializing vif packets...\n");
vif_packets[0] = packet2_create(4000, P2_TYPE_NORMAL, P2_MODE_CHAIN, 1);
vif_packets[1] = packet2_create(4000, P2_TYPE_NORMAL, P2_MODE_CHAIN, 1);

initialized = true;
printf("Initialized\n");
}

curr_vif_packet = vif_packets[context];

packet2_reset(curr_vif_packet, 0);
packet2_reset(get_current_vif_packet(), 0);

packet2_utils_vu_open_unpack(curr_vif_packet, 0, false);
packet2_utils_vu_open_unpack(get_current_vif_packet(), 0, true);
{
// 0
for (int i = 0; i < 4; ++i)
{
packet2_add_u128(curr_vif_packet, ((u128*)&mesh_to_screen_matrix)[i]);
packet2_add_u128(get_current_vif_packet(), ((u128*)&mesh_to_screen_matrix)[i]);
}

// 4
packet2_add_float(curr_vif_packet, 2048.0F); // scale
packet2_add_float(curr_vif_packet, 2048.0F); // scale
packet2_add_float(curr_vif_packet, ((float)0xFFFFFF) / -32.0F); // scale
packet2_add_s32(curr_vif_packet, mesh.num_verts); // vert count
packet2_add_float(get_current_vif_packet(), 2048.0F); // scale
packet2_add_float(get_current_vif_packet(), 2048.0F); // scale
packet2_add_float(get_current_vif_packet(), ((float)0xFFFFFF) / -32.0F); // scale
packet2_add_u32(get_current_vif_packet(), mesh.num_verts); // vert count

// 5
{
packet2_add_2x_s64(
curr_vif_packet,
VU_GS_GIFTAG(
mesh.num_verts, // Information for GS. Amount of loops
1,
1,
VU_GS_PRIM(
prim.type,
prim.shading,
prim.mapping,
prim.fogging,
prim.blending,
prim.antialiasing,
prim.mapping_type,
0, // context
prim.colorfix),
0,
2),
DRAW_RGBAQ_REGLIST);
}
packet2_utils_gs_add_prim_giftag(get_current_vif_packet(), &prim, mesh.num_verts, DRAW_RGBAQ_REGLIST, 2, 0);

// 6
u8 j = 0; // RGBA
for (j = 0; j < 4; j++)
packet2_add_u32(curr_vif_packet, 128);
packet2_add_u32(get_current_vif_packet(), 128);
}
packet2_utils_vu_close_unpack(curr_vif_packet);
packet2_utils_vu_close_unpack(get_current_vif_packet());

packet2_utils_vu_add_unpack_data(curr_vif_packet, 7, (void*)mesh.pos, mesh.num_verts, false);
packet2_utils_vu_add_unpack_data(get_current_vif_packet(), 8, (void*)mesh.pos, mesh.num_verts, true);

assert(mesh.color != nullptr);
{
packet2_utils_vu_add_unpack_data(curr_vif_packet, 7 + mesh.num_verts, (void*)mesh.color, mesh.num_verts, false);
packet2_utils_vu_add_unpack_data(get_current_vif_packet(), 8 + mesh.num_verts, (void*)mesh.color, mesh.num_verts, true);
}

packet2_utils_vu_add_start_program(curr_vif_packet, mesh.vu_program_addr);
packet2_utils_vu_add_end_tag(curr_vif_packet);
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(curr_vif_packet, DMA_CHANNEL_VIF1, 1);
dma_channel_send_packet2(get_current_vif_packet(), DMA_CHANNEL_VIF1, 1);
dma_wait_fast();

flip_vip_packet_context();
}

} // namespace
Expand Down
28 changes: 14 additions & 14 deletions dependencies/egg-ps2-graphics-lib/vu1/vertex_color_renderer.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@
; Updated dynamically
xtop iBase
lq matrixRow[0], 0(vi00) ; load view-projection matrix
lq matrixRow[1], 1(vi00)
lq matrixRow[2], 2(vi00)
lq matrixRow[3], 3(vi00)
lq matrixRow[0], 0(iBase) ; load view-projection matrix
lq matrixRow[1], 1(iBase)
lq matrixRow[2], 2(iBase)
lq matrixRow[3], 3(iBase)
lq.xyz scale, 4(vi00) ; load program params
lq.xyz scale, 4(iBase) ; load program params
; float : X, Y, Z - scale vector that we will use to scale the verts after projecting them.
; float : W - vert count.
ilw.w vertCount, 4(vi00)
lq primTag, 5(vi00) ; GIF tag - tell GS how many data we will send
lq rgba, 6(vi00) ; RGBA
ilw.w vertCount, 4(iBase)
lq primTag, 5(iBase) ; GIF tag - tell GS how many data we will send
lq rgba, 6(iBase) ; RGBA
; u32 : R, G, B, A (0-128)
iaddiu vertexData, vi00, 7 ; pointer to vertex data
iaddiu vertexData, iBase, 8 ; pointer to vertex data
iadd colorData, vertexData, vertCount ; pointer to color data
iadd kickAddress, colorData, vertCount ; pointer for XGKICK
iadd destAddress, colorData, vertCount ; helper pointer for data inserting
Expand All @@ -68,7 +68,7 @@
;////////////////////////////////////////////
;/////////////// --- Loop --- ///////////////
iadd vertexCounter, vi00, vertCount ; loop vertCount times
iadd vertexCounter, iBase, vertCount ; loop vertCount times
vertexLoop:
;////////// --- Load loop data --- //////////
Expand All @@ -92,14 +92,15 @@
clipw.xyz vertex, vertex ; Dr. Fortuna: This instruction checks if the vertex is outside
; the viewing frustum. If it is, then the appropriate
; clipping flags are set
fcand vi01, 0x3FFFF ; Bitwise AND the clipping flags with 0x3FFFF, this makes
fcand VI01, 0x3FFFF ; Bitwise AND the clipping flags with 0x3FFFF, this makes
; sure that we get the clipping judgement for the last three
; verts (i.e. that make up the triangle we are about to draw)
iaddiu iClipBit, vi01, 0x7FFF ; Add 0x7FFF. If any of the clipping flags were set this will
iaddiu iClipBit, VI01, 0x7FFF ; Add 0x7FFF. If any of the clipping flags were set this will
; cause the triangle not to be drawn (any values above 0x8000
; that are stored in the w component of XYZ2 will set the ADC
; bit, which tells the GS not to perform a drawing kick on this
; triangle.
isw.w iClipBit, 1(destAddress)
div q, vf00[w], vertex[w] ; perspective divide (1/vert[w]):
mul.xyz vertex, vertex, q
Expand All @@ -110,15 +111,14 @@
;//////////// --- Store data --- ////////////
sq.xyzw color, 0(destAddress)
sq.xyz vertex, 1(destAddress) ; XYZ2
isw.w iClipBit, 1(destAddress)
;////////////////////////////////////////////
iaddiu vertexData, vertexData, 1
iaddiu colorData, colorData, 1
iaddiu destAddress, destAddress, 2
iaddi vertexCounter, vertexCounter, -1 ; decrement the loop counter
ibne vertexCounter, vi00, vertexLoop ; and repeat if needed
ibne vertexCounter, iBase, vertexLoop ; and repeat if needed
;////////////////////////////////////////////
Expand Down
Binary file modified dependencies/egg-ps2-graphics-lib/vu1/vertex_color_renderer.vo
Binary file not shown.
2 changes: 2 additions & 0 deletions src/world/level.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Level::Level(Asset::Reference level_reference)

void Level::render(const GS::GSState& gs_state)
{
return;

for (int i = 0; i < level->meshes.mesh_files.num_elements(); ++i)
{
meshes[level->meshes.mesh_files[i]].draw(level->meshes.mesh_transforms[i] * gs_state.world_view);
Expand Down

0 comments on commit 23e7662

Please sign in to comment.