Skip to content

Commit 45e5e5c

Browse files
authored
Experimental interpolation (#309)
* Experimental 60 fps * Fix compile error * Fix compile error * Fix compile error
1 parent bcd57f4 commit 45e5e5c

43 files changed

Lines changed: 1204 additions & 47 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,12 @@ static int game_framebuffer_msaa_resolved;
190190

191191
uint32_t gfx_msaa_level = 1;
192192

193+
static bool has_drawn_imgui_menu;
194+
193195
static bool dropped_frame;
194196

197+
static const std::unordered_map<Mtx *, MtxF> *current_mtx_replacements;
198+
195199
static float buf_vbo[MAX_BUFFERED * (32 * 3)]; // 3 vertices in a triangle and 32 floats per vtx
196200
static size_t buf_vbo_len;
197201
static size_t buf_vbo_num_tris;
@@ -916,20 +920,31 @@ static void gfx_matrix_mul(float res[4][4], const float a[4][4], const float b[4
916920

917921
static void gfx_sp_matrix(uint8_t parameters, const int32_t *addr) {
918922
float matrix[4][4];
923+
924+
if (auto it = current_mtx_replacements->find((Mtx *)addr); it != current_mtx_replacements->end()) {
925+
for (int i = 0; i < 4; i++) {
926+
for (int j = 0; j < 4; j++) {
927+
float v = it->second.mf[i][j];
928+
int as_int = (int)(v * 65536.0f);
929+
matrix[i][j] = as_int * (1.0f / 65536.0f);
930+
}
931+
}
932+
} else {
919933
#ifndef GBI_FLOATS
920-
// Original GBI where fixed point matrices are used
921-
for (int i = 0; i < 4; i++) {
922-
for (int j = 0; j < 4; j += 2) {
923-
int32_t int_part = addr[i * 2 + j / 2];
924-
uint32_t frac_part = addr[8 + i * 2 + j / 2];
925-
matrix[i][j] = (int32_t)((int_part & 0xffff0000) | (frac_part >> 16)) / 65536.0f;
926-
matrix[i][j + 1] = (int32_t)((int_part << 16) | (frac_part & 0xffff)) / 65536.0f;
934+
// Original GBI where fixed point matrices are used
935+
for (int i = 0; i < 4; i++) {
936+
for (int j = 0; j < 4; j += 2) {
937+
int32_t int_part = addr[i * 2 + j / 2];
938+
uint32_t frac_part = addr[8 + i * 2 + j / 2];
939+
matrix[i][j] = (int32_t)((int_part & 0xffff0000) | (frac_part >> 16)) / 65536.0f;
940+
matrix[i][j + 1] = (int32_t)((int_part << 16) | (frac_part & 0xffff)) / 65536.0f;
941+
}
927942
}
928-
}
929943
#else
930-
// For a modified GBI where fixed point values are replaced with floats
931-
memcpy(matrix, addr, sizeof(matrix));
944+
// For a modified GBI where fixed point values are replaced with floats
945+
memcpy(matrix, addr, sizeof(matrix));
932946
#endif
947+
}
933948

934949
if (parameters & G_MTX_PROJECTION) {
935950
if (parameters & G_MTX_LOAD) {
@@ -2708,6 +2723,7 @@ void gfx_start_frame(void) {
27082723
gfx_wapi->handle_events();
27092724
gfx_wapi->get_dimensions(&gfx_current_window_dimensions.width, &gfx_current_window_dimensions.height);
27102725
SohImGui::DrawMainMenuAndCalculateGameSize();
2726+
has_drawn_imgui_menu = true;
27112727
if (gfx_current_dimensions.height == 0) {
27122728
// Avoid division by zero
27132729
gfx_current_dimensions.height = 1;
@@ -2746,7 +2762,7 @@ void gfx_start_frame(void) {
27462762
fbActive = 0;
27472763
}
27482764

2749-
void gfx_run(Gfx *commands) {
2765+
void gfx_run(Gfx *commands, const std::unordered_map<Mtx *, MtxF>& mtx_replacements) {
27502766
gfx_sp_reset();
27512767

27522768
//puts("New frame");
@@ -2755,12 +2771,21 @@ void gfx_run(Gfx *commands) {
27552771

27562772
if (!gfx_wapi->start_frame()) {
27572773
dropped_frame = true;
2758-
SohImGui::DrawFramebufferAndGameInput();
2759-
SohImGui::CancelFrame();
2774+
if (has_drawn_imgui_menu) {
2775+
SohImGui::DrawFramebufferAndGameInput();
2776+
SohImGui::CancelFrame();
2777+
has_drawn_imgui_menu = false;
2778+
}
27602779
return;
27612780
}
27622781
dropped_frame = false;
27632782

2783+
if (!has_drawn_imgui_menu) {
2784+
SohImGui::DrawMainMenuAndCalculateGameSize();
2785+
}
2786+
2787+
current_mtx_replacements = &mtx_replacements;
2788+
27642789
double t0 = gfx_wapi->get_time();
27652790
gfx_rapi->update_framebuffer_parameters(0, gfx_current_window_dimensions.width, gfx_current_window_dimensions.height, 1, false, true, true, !game_renders_to_framebuffer);
27662791
gfx_rapi->start_frame();
@@ -2792,6 +2817,7 @@ void gfx_run(Gfx *commands) {
27922817
//printf("Process %f %f\n", t1, t1 - t0);
27932818
gfx_rapi->end_frame();
27942819
gfx_wapi->swap_buffers_begin();
2820+
has_drawn_imgui_menu = false;
27952821
}
27962822

27972823
void gfx_end_frame(void) {

libultraship/libultraship/Lib/Fast3D/gfx_pc.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <unordered_map>
77
#include <list>
88

9+
#include "U64/PR/ultra64/types.h"
10+
911
struct GfxRenderingAPI;
1012
struct GfxWindowManagerAPI;
1113

@@ -52,26 +54,24 @@ struct TextureCacheValue {
5254
#endif
5355
};
5456

55-
#ifdef __cplusplus
5657
extern "C" {
57-
#endif
58+
5859
extern struct GfxDimensions gfx_current_window_dimensions; // The dimensions of the window
5960
extern struct GfxDimensions gfx_current_dimensions; // The dimensions of the draw area the game draws to, before scaling (if applicable)
6061
extern struct XYWidthHeight gfx_current_game_window_viewport; // The area of the window the game is drawn to, (0, 0) is top-left corner
6162
extern uint32_t gfx_msaa_level;
6263

64+
}
65+
6366
void gfx_init(struct GfxWindowManagerAPI* wapi, struct GfxRenderingAPI* rapi, const char* game_name, bool start_in_fullscreen);
6467
struct GfxRenderingAPI* gfx_get_current_rendering_api(void);
6568
void gfx_start_frame(void);
66-
void gfx_run(Gfx* commands);
69+
void gfx_run(Gfx* commands, const std::unordered_map<Mtx*, MtxF>& mtx_replacements);
6770
void gfx_end_frame(void);
6871
void gfx_set_framedivisor(int);
6972
void gfx_texture_cache_clear();
70-
int gfx_create_framebuffer(uint32_t width, uint32_t height);
73+
extern "C" int gfx_create_framebuffer(uint32_t width, uint32_t height);
7174
void gfx_get_pixel_depth_prepare(float x, float y);
7275
uint16_t gfx_get_pixel_depth(float x, float y);
7376

74-
#ifdef __cplusplus
75-
}
76-
#endif
7777
#endif

libultraship/libultraship/SohImGuiImpl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ namespace SohImGui {
681681

682682
EXPERIMENTAL();
683683

684+
EnhancementCheckbox("60 fps interpolation", "g60FPS");
684685
EnhancementCheckbox("Disable LOD", "gDisableLOD");
685686

686687
ImGui::EndMenu();

libultraship/libultraship/Window.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,12 @@ namespace Ship {
282282
gfx_start_frame();
283283
}
284284

285-
void Window::RunCommands(Gfx* Commands) {
286-
gfx_run(Commands);
285+
void Window::RunCommands(Gfx* Commands, const std::vector<std::unordered_map<Mtx*, MtxF>>& mtx_replacements) {
286+
for (const auto& m : mtx_replacements) {
287+
gfx_run(Commands, m);
288+
gfx_end_frame();
289+
}
290+
gfx_run(Commands, {});
287291
gfx_end_frame();
288292
}
289293

libultraship/libultraship/Window.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Ship {
1919
void MainLoop(void (*MainFunction)(void));
2020
void Init();
2121
void StartFrame();
22-
void RunCommands(Gfx* Commands);
22+
void RunCommands(Gfx* Commands, const std::vector<std::unordered_map<Mtx*, MtxF>>& mtx_replacements);
2323
void SetFrameDivisor(int divisor);
2424
void GetPixelDepthPrepare(float x, float y);
2525
uint16_t GetPixelDepth(float x, float y);

soh/include/macros.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ extern GraphicsContext* __gfxCtx;
138138
#ifndef NDEBUG
139139
#define OPEN_DISPS(gfxCtx, file, line) \
140140
{ \
141+
void FrameInterpolation_RecordOpenChild(const void* a, int b); \
142+
FrameInterpolation_RecordOpenChild(file, line); \
141143
GraphicsContext* __gfxCtx; \
142144
Gfx* dispRefs[4]; \
143145
__gfxCtx = gfxCtx; \
@@ -146,18 +148,24 @@ extern GraphicsContext* __gfxCtx;
146148
#else
147149
#define OPEN_DISPS(gfxCtx, file, line) \
148150
{ \
151+
void FrameInterpolation_RecordOpenChild(const void* a, int b); \
152+
FrameInterpolation_RecordOpenChild(file, line); \
149153
GraphicsContext* __gfxCtx; \
150154
__gfxCtx = gfxCtx; \
151155
(void)__gfxCtx;
152156
#endif
153157

154158
#ifndef NDEBUG
155159
#define CLOSE_DISPS(gfxCtx, file, line) \
160+
{void FrameInterpolation_RecordCloseChild(void); \
161+
FrameInterpolation_RecordCloseChild();} \
156162
Graph_CloseDisps(dispRefs, gfxCtx, file, line); \
157163
} \
158164
(void)0
159165
#else
160166
#define CLOSE_DISPS(gfxCtx, file, line) \
167+
{void FrameInterpolation_RecordCloseChild(void); \
168+
FrameInterpolation_RecordCloseChild();} \
161169
(void)0; \
162170
} \
163171
(void)0

soh/include/z64effect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ typedef struct EffectSs {
225225
/* 0x5C */ s16 life; // -1 means this entry is free
226226
/* 0x5E */ u8 priority; // Lower value means higher priority
227227
/* 0x5F */ u8 type;
228+
u32 epoch;
228229
} EffectSs; // size = 0x60
229230

230231
typedef struct {

soh/soh.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
</Link>
174174
</ItemDefinitionGroup>
175175
<ItemGroup>
176+
<ClCompile Include="soh\frame_interpolation.cpp" />
176177
<ClCompile Include="soh\Enhancements\bootcommands.c" />
177178
<ClCompile Include="soh\Enhancements\debugconsole.cpp" />
178179
<ClCompile Include="soh\Enhancements\debugger\colViewer.cpp" />
@@ -878,6 +879,7 @@
878879
<ClCompile Include="src\overlays\misc\ovl_map_mark_data\z_map_mark_data.c" />
879880
</ItemGroup>
880881
<ItemGroup>
882+
<ClInclude Include="soh\frame_interpolation.h" />
881883
<ClInclude Include="include\alloca.h" />
882884
<ClInclude Include="include\bgm.h" />
883885
<ClInclude Include="include\color.h" />

soh/soh.vcxproj.filters

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,6 +2186,8 @@
21862186
</ClCompile>
21872187
<ClCompile Include="soh\Enhancements\debugger\ImGuiHelpers.cpp">
21882188
<Filter>Source Files\soh\Enhancements\debugger</Filter>
2189+
<ClCompile Include="soh\frame_interpolation.cpp">
2190+
<Filter>Source Files\soh</Filter>
21892191
</ClCompile>
21902192
<ClCompile Include="src\code\z_cheap_proc.c">
21912193
<Filter>Source Files</Filter>
@@ -3748,6 +3750,8 @@
37483750
</ClInclude>
37493751
<ClInclude Include="soh\Enhancements\debugger\ImGuiHelpers.h">
37503752
<Filter>Header Files\soh\Enhancements\debugger</Filter>
3753+
<ClInclude Include="soh\frame_interpolation.h">
3754+
<Filter>Header Files\soh</Filter>
37513755
</ClInclude>
37523756
<ClInclude Include="soh\Enhancements\savestates.h">
37533757
<Filter>Source Files\soh\Enhancements</Filter>

soh/soh/Enhancements/debugger/colViewer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "colViewer.h"
22
#include "../libultraship/SohImGuiImpl.h"
33
#include "ImGuiHelpers.h"
4+
#include "../../frame_interpolation.h"
45

56
#include <vector>
67
#include <string>

0 commit comments

Comments
 (0)