From 246d30ce04c396554c2676c40229d625bffaa26e Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 7 Jan 2023 17:08:30 +1000 Subject: [PATCH] RoomMtx: Prevent cache fighting in multiplayer and remove need to tick roommtxes --- ld/gamefiles.ntsc-final.inc | 1 - src/game/lv.c | 1 - src/game/room.c | 86 ++++++++++++++----------------------- src/game/roomreset.c | 22 +++++----- src/game/roomtick.c | 23 ---------- src/include/bss.h | 3 +- src/include/constants.h | 1 - src/include/game/room.h | 2 - src/include/types.h | 6 +-- src/lib/profile.c | 4 +- 10 files changed, 50 insertions(+), 99 deletions(-) delete mode 100644 src/game/roomtick.c diff --git a/ld/gamefiles.ntsc-final.inc b/ld/gamefiles.ntsc-final.inc index fa362e455..6ce826c4a 100644 --- a/ld/gamefiles.ntsc-final.inc +++ b/ld/gamefiles.ntsc-final.inc @@ -58,7 +58,6 @@ build/ROMID/game/menustop.o (section); \ build/ROMID/game/menutick.o (section); \ build/ROMID/game/weathertick.o (section); \ - build/ROMID/game/roomtick.o (section); \ build/ROMID/game/skytick.o (section); \ build/ROMID/game/casingtick.o (section); \ build/ROMID/game/shardstick.o (section); \ diff --git a/src/game/lv.c b/src/game/lv.c index c85bec0ce..7ba8edb76 100644 --- a/src/game/lv.c +++ b/src/game/lv.c @@ -2041,7 +2041,6 @@ void lvTick(void) lvUpdateCutsceneTime(); PROFILE(PROFILEMARKER_LVT_VTXSTORE, vtxstoreTick()); lvUpdateSoloHandicaps(); - PROFILE(PROFILEMARKER_LVT_ROOMS, roomsTick()); skyTick(); PROFILE(PROFILEMARKER_LVT_CASINGS, casingsTick()); diff --git a/src/game/room.c b/src/game/room.c index e9c9479fc..71c22e499 100644 --- a/src/game/room.c +++ b/src/game/room.c @@ -7,10 +7,9 @@ #include "types.h" struct roommtx *g_RoomMtxes; -struct roommtx *g_RoomMtxesUsed; -struct roommtx *g_RoomMtxesFree; s32 var80082050 = 0; +s32 g_NextRoomMtx = 0; void roomSetLastForOffset(s32 room) { @@ -19,13 +18,13 @@ void roomSetLastForOffset(s32 room) void room0f1668f0(struct roommtx *roommtx, s32 roomnum) { - g_Rooms[roomnum].unk10 = roommtx->index; + g_Rooms[roomnum].unk10[g_Vars.currentplayernum] = roommtx->index; roommtx->room1 = roomnum; } void room0f16692c(struct roommtx *roommtx, s32 roomnum) { - g_Rooms[roomnum].unk10 = -1; + g_Rooms[roomnum].unk10[roommtx->playernum] = -1; roommtx->room1 = -1; } @@ -35,28 +34,10 @@ void room0f16696c(struct roommtx *roommtx) room0f16692c(roommtx, roommtx->room1); } - roommtx->count = 2; + roommtx->lvframe = 0; + roommtx->playernum = -1; roommtx->room2 = -1; roommtx->somefloat = 1; - - // Move from used to free - if (roommtx == g_RoomMtxesUsed) { - g_RoomMtxesUsed = roommtx->next; - } else { - struct roommtx *iter = g_RoomMtxesUsed; - - while (iter) { - if (iter->next == roommtx) { - iter->next = roommtx->next; - break; - } - - iter = iter->next; - } - } - - roommtx->next = g_RoomMtxesFree; - g_RoomMtxesFree = roommtx; } /** @@ -64,30 +45,29 @@ void room0f16696c(struct roommtx *roommtx) */ struct roommtx *room0f1669fc(void) { - struct roommtx *roommtx = g_RoomMtxesFree; + s32 i; - if (!roommtx) { - return g_RoomMtxesUsed; + for (i = 0; i < var80082050; i++) { + struct roommtx *roommtx = &g_RoomMtxes[g_NextRoomMtx]; + + g_NextRoomMtx++; + + if (g_NextRoomMtx == var80082050) { + g_NextRoomMtx = 0; + } + + if (roommtx->lvframe < g_Vars.lvframenum - 2) { + return roommtx; + } } - g_RoomMtxesFree = roommtx->next; - - roommtx->next = g_RoomMtxesUsed; - g_RoomMtxesUsed = roommtx; - - return roommtx; + return &g_RoomMtxes[0]; } void room0f166a6c(Mtxf *mtx, s32 roomnum) { - s32 stagenum = g_Vars.stagenum; - mtx4LoadIdentity(mtx); - mtx->m[0][0] = 1; - mtx->m[1][1] = 1; - mtx->m[2][2] = 1; - if (roomnum == g_BgAlwaysRoom) { mtx->m[3][0] = g_BgRooms[roomnum].pos.x; mtx->m[3][1] = g_BgRooms[roomnum].pos.y; @@ -101,27 +81,27 @@ void room0f166a6c(Mtxf *mtx, s32 roomnum) struct roommtx *room0f166c20(s32 roomnum) { - s32 index = g_Rooms[roomnum].unk10; + s32 index = g_Rooms[roomnum].unk10[g_Vars.currentplayernum]; struct roommtx *roommtx; Mtxf mtx; - if (index == -1 - || g_Vars.currentplayer->lastroomforoffset != g_RoomMtxes[index].room2 - || g_RoomMtxes[index].somefloat != var8005ef10[0]) { - if (index != -1) { - room0f16692c(&g_RoomMtxes[index], roomnum); - } - - roommtx = room0f1669fc(); - - room0f1668f0(roommtx, roomnum); - roommtx->count = 0; - } else { + if (index != -1 + && g_Vars.currentplayer->lastroomforoffset == g_RoomMtxes[index].room2 + && g_RoomMtxes[index].somefloat == var8005ef10[0]) { roommtx = &g_RoomMtxes[index]; - roommtx->count = 0; + roommtx->lvframe = g_Vars.lvframenum; return roommtx; } + if (index != -1) { + room0f16692c(&g_RoomMtxes[index], roomnum); + } + + roommtx = room0f1669fc(); + + room0f1668f0(roommtx, roomnum); + roommtx->lvframe = g_Vars.lvframenum; + roommtx->playernum = g_Vars.currentplayernum; roommtx->room2 = g_Vars.currentplayer->lastroomforoffset; roommtx->somefloat = var8005ef10[0]; diff --git a/src/game/roomreset.c b/src/game/roomreset.c index 5b419ec3a..fa6839f14 100644 --- a/src/game/roomreset.c +++ b/src/game/roomreset.c @@ -15,25 +15,25 @@ void roomsReset(void) var80082050 = PLAYERCOUNT() >= 2 ? 200 : 120; g_RoomMtxes = mempAlloc(ALIGN16(var80082050 * sizeof(struct roommtx)), MEMPOOL_STAGE); - g_RoomMtxesUsed = NULL; - g_RoomMtxesFree = g_RoomMtxes; + g_NextRoomMtx = 0; for (i = 0; i < PLAYERCOUNT(); i++) { g_Vars.players[i]->lastroomforoffset = -1; } for (i = 0; i < var80082050; i++) { - g_RoomMtxesFree[i].index = i; - g_RoomMtxesFree[i].count = 2; - g_RoomMtxesFree[i].room1 = -1; - g_RoomMtxesFree[i].room2 = -1; - g_RoomMtxesFree[i].somefloat = 1; - g_RoomMtxesFree[i].next = &g_RoomMtxes[i + 1]; + g_RoomMtxes[i].index = i; + g_RoomMtxes[i].playernum = -1; + g_RoomMtxes[i].lvframe = -10; + g_RoomMtxes[i].room1 = -1; + g_RoomMtxes[i].room2 = -1; + g_RoomMtxes[i].somefloat = 1; } - g_RoomMtxesFree[var80082050 - 1].next = NULL; - for (i = 0; i < g_Vars.roomcount; i++) { - g_Rooms[i].unk10 = -1; + g_Rooms[i].unk10[0] = -1; + g_Rooms[i].unk10[1] = -1; + g_Rooms[i].unk10[2] = -1; + g_Rooms[i].unk10[3] = -1; } } diff --git a/src/game/roomtick.c b/src/game/roomtick.c deleted file mode 100644 index d48fe16d1..000000000 --- a/src/game/roomtick.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include "constants.h" -#include "game/room.h" -#include "bss.h" -#include "data.h" -#include "types.h" - -void roomsTick(void) -{ - struct roommtx *roommtx = g_RoomMtxesUsed; - - while (roommtx) { - struct roommtx *next = roommtx->next; - - roommtx->count++; - - if (roommtx->count > 1) { - room0f16696c(roommtx); - } - - roommtx = next; - } -} diff --git a/src/include/bss.h b/src/include/bss.h index 24c1580f0..e1055222f 100644 --- a/src/include/bss.h +++ b/src/include/bss.h @@ -295,8 +295,7 @@ extern s32 g_LasersightsActive; extern bool g_StarsActive; extern u8 g_HudmsgsActive; extern struct roommtx *g_RoomMtxes; -extern struct roommtx *g_RoomMtxesUsed; -extern struct roommtx *g_RoomMtxesFree; +extern s32 g_NextRoomMtx; extern s32 g_BgAlwaysRoom; #endif diff --git a/src/include/constants.h b/src/include/constants.h index c82c2b2b9..61ad42809 100644 --- a/src/include/constants.h +++ b/src/include/constants.h @@ -3461,7 +3461,6 @@ enum profilemarker { PROFILEMARKER_LVRENDER, PROFILEMARKER_LVT_HUDMSGS, PROFILEMARKER_LVT_VTXSTORE, - PROFILEMARKER_LVT_ROOMS, PROFILEMARKER_LVT_CASINGS, PROFILEMARKER_LVT_SHARDS, PROFILEMARKER_LVT_SPARKS, diff --git a/src/include/game/room.h b/src/include/game/room.h index eb94d30cd..12efd9b06 100644 --- a/src/include/game/room.h +++ b/src/include/game/room.h @@ -6,8 +6,6 @@ void roomsReset(void); -void roomsTick(void); - void roomSetLastForOffset(s32 room); void room0f1668f0(struct roommtx *roommtx, s32 roomnum); void room0f16692c(struct roommtx *roommtx, s32 roomnum); diff --git a/src/include/types.h b/src/include/types.h index 9dfb58e83..2743110bf 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -3560,7 +3560,7 @@ struct room { /*0x0a*/ u16 lightindex; // index of start of this room's lights in data file /*0x0c*/ u16 firstwaypoint; // offset into g_Vars.waypoints /*0x0e*/ s16 roomportallistoffset; - /*0x10*/ s16 unk10; + /*0x10*/ s16 unk10[4]; /*0x14*/ struct roomgfxdata *gfxdata; /*0x18*/ f32 bbmin[3]; /*0x24*/ f32 bbmax[3]; @@ -6363,11 +6363,11 @@ struct xz { struct roommtx { Mtxf mtx; u8 index; - u8 count; + s8 playernum; + s32 lvframe; s16 room1; s16 room2; f32 somefloat; - struct roommtx *next; }; #endif diff --git a/src/lib/profile.c b/src/lib/profile.c index 4caaefc09..500e7e258 100644 --- a/src/lib/profile.c +++ b/src/lib/profile.c @@ -93,6 +93,7 @@ void profileStartDynamic(char *file, s32 line) void profileEndDynamic(char *file, s32 line) { + u32 count = osGetCount(); s32 i; struct profileslot *slot = NULL; @@ -104,7 +105,7 @@ void profileEndDynamic(char *file, s32 line) } if (slot) { - slot->ticks = osGetCount() - slot->startticks; + slot->ticks += count - slot->startticks; slot->startticks = 0; g_ProfileCurrentSlot = slot->parent; @@ -266,7 +267,6 @@ Gfx *profileRender(Gfx *gdl) gdl = profileRenderCpuLine(gdl, x, &y, " lvTick", PROFILEMARKER_LVTICK); gdl = profileRenderCpuLine(gdl, x, &y, " hudmsgs ", PROFILEMARKER_LVT_HUDMSGS ); gdl = profileRenderCpuLine(gdl, x, &y, " vtxstore ", PROFILEMARKER_LVT_VTXSTORE ); - gdl = profileRenderCpuLine(gdl, x, &y, " rooms ", PROFILEMARKER_LVT_ROOMS ); gdl = profileRenderCpuLine(gdl, x, &y, " casings ", PROFILEMARKER_LVT_CASINGS ); gdl = profileRenderCpuLine(gdl, x, &y, " shards ", PROFILEMARKER_LVT_SHARDS ); gdl = profileRenderCpuLine(gdl, x, &y, " sparks ", PROFILEMARKER_LVT_SPARKS );