Skip to content

Commit

Permalink
add dormant anaglyph code
Browse files Browse the repository at this point in the history
  • Loading branch information
skyfloogle committed Apr 14, 2024
1 parent 30f7949 commit 754362d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions include/vb_gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ extern Thread save_thread;

bool guiShouldPause();
void toggleVsync(bool enable);
void toggleAnaglyph(bool enable, bool also_update_vsync);
void aptBacklight(APT_HookType hook, void* param);
int guiGetInput(bool do_switching);

Expand Down
1 change: 1 addition & 0 deletions include/vb_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ typedef struct VB_OPT {
bool PERF_INFO;
bool VSYNC;
bool N3DS_SPEEDUP;
bool ANAGLYPH;
} VB_OPT;

void setDefaults(void);
Expand Down
19 changes: 17 additions & 2 deletions source/3ds/gui_hard.c
Original file line number Diff line number Diff line change
Expand Up @@ -1381,11 +1381,11 @@ void toggleVsync(bool enable) {
u32 vtotal_top, vtotal_bottom;
if (enable) {
// 990 is closer to 50Hz but capture cards don't like when the two screens are out of sync
vtotal_top = old_2ds ? 494 : 989;
vtotal_top = old_2ds || tVBOpt.ANAGLYPH ? 494 : 989;
vtotal_bottom = 494;
startPeriodicVsync(frame_pacer_thread);
} else {
vtotal_top = old_2ds ? 413 : 827;
vtotal_top = old_2ds || tVBOpt.ANAGLYPH ? 413 : 827;
vtotal_bottom = 413;
startPeriodic(frame_pacer_thread, 20000000);
}
Expand All @@ -1407,6 +1407,21 @@ void toggleVsync(bool enable) {
GSPGPU_WriteHWRegs(0x400524, &vtotal_bottom, 4);
}

void toggleAnaglyph(bool enable, bool also_update_vsync) {
tVBOpt.ANAGLYPH = enable;
gfxSet3D(!enable);
if (!also_update_vsync) return;
// updating 3d mode resets VTotal, so turn off VSync in advance to fix the cache
toggleVsync(false);
// push 1 frame and wait for VBlank to reset VTotal
C3D_FrameBegin(0);
video_flush(true);
C3D_FrameEnd(0);
gspWaitForVBlank();
// re-enable VSync if applicable
toggleVsync(tVBOpt.VSYNC);
}

void aptBacklight(APT_HookType hook, void* param) {
if (hook == APTHOOK_ONSLEEP && !inMenu) {
// don't save if we saved in the last 10 secs (unsure if this works)
Expand Down
2 changes: 2 additions & 0 deletions source/3ds/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ int main() {

replay_init();

toggleAnaglyph(tVBOpt.ANAGLYPH, false);

guiop = 0;
openMenu();
if (guiop & GUIEXIT) {
Expand Down
28 changes: 19 additions & 9 deletions source/3ds/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void video_render(int alt_buf) {
AttrInfo_AddLoader(attrInfo, 0, GPU_SHORT, 4);
AttrInfo_AddLoader(attrInfo, 1, GPU_SHORT, 3);

eye_count = CONFIG_3D_SLIDERSTATE > 0.0f ? 2 : 1;
eye_count = tVBOpt.ANAGLYPH || CONFIG_3D_SLIDERSTATE > 0.0f ? 2 : 1;

if (tVIPREG.XPCTRL & 0x0002) {
if (tDSPCACHE.CharCacheInvalid) {
Expand Down Expand Up @@ -323,31 +323,35 @@ void video_flush(bool default_for_both) {

C3D_TexEnv *env = C3D_GetTexEnv(0);
C3D_TexEnvInit(env);
C3D_TexEnvColor(env, tVBOpt.TINT);
C3D_TexEnvColor(env, tVBOpt.ANAGLYPH ? 0xff0000ff : tVBOpt.TINT);
C3D_TexEnvSrc(env, C3D_RGB, GPU_TEXTURE0, GPU_CONSTANT, 0);
C3D_TexEnvFunc(env, C3D_RGB, GPU_MODULATE);
C3D_TexEnvSrc(env, C3D_Alpha, GPU_CONSTANT, 0, 0);
env = C3D_GetTexEnv(1);
if (minRepeat != maxRepeat) {
env = C3D_GetTexEnv(1);
C3D_TexEnvInit(env);
C3D_TexEnvSrc(env, C3D_Both, GPU_PREVIOUS, GPU_TEXTURE1, 0);
C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE);
#ifdef COLTABLESCALE
C3D_TexEnvScale(env, C3D_RGB, maxRepeat <= 2 ? maxRepeat - 1 : GPU_TEVSCALE_4);
#endif
} else {
env = C3D_GetTexEnv(1);
C3D_TexEnvInit(env);
}

int viewportX = (TOP_SCREEN_WIDTH - VIEWPORT_WIDTH) / 2;
int viewportY = (TOP_SCREEN_HEIGHT - VIEWPORT_HEIGHT) / 2;

for (int dst_eye = 0; dst_eye < (default_for_both ? 2 : eye_count); dst_eye++) {
int src_eye = default_for_both ? orig_eye : CONFIG_3D_SLIDERSTATE == 0 ? tVBOpt.DEFAULT_EYE : dst_eye;
int src_eye = default_for_both ? orig_eye : !tVBOpt.ANAGLYPH && CONFIG_3D_SLIDERSTATE == 0 ? tVBOpt.DEFAULT_EYE : dst_eye;
if (tVBOpt.ANAGLYPH && src_eye) {
C3D_TexEnvColor(C3D_GetTexEnv(0), 0xffffff00);
C3D_AlphaBlend(GPU_BLEND_ADD, 0, GPU_ONE, GPU_ONE, 0, 0);
}
float depthOffset = getDepthOffset(default_for_both, dst_eye, tVBOpt.SLIDERMODE);
C3D_RenderTargetClear(finalScreen[dst_eye], C3D_CLEAR_ALL, 0, 0);
C3D_FrameDrawOn(finalScreen[dst_eye]);
C3D_RenderTarget *target = finalScreen[dst_eye && !tVBOpt.ANAGLYPH];
C3D_RenderTargetClear(target, C3D_CLEAR_ALL, 0, 0);
C3D_FrameDrawOn(target);
C3D_SetViewport(viewportY, viewportX+depthOffset, VIEWPORT_HEIGHT, VIEWPORT_WIDTH);
C3D_TexBind(1, &columnTableTexture[src_eye]);
C3D_ImmDrawBegin(GPU_GEOMETRY_PRIM);
Expand All @@ -357,10 +361,16 @@ void video_flush(bool default_for_both) {
C3D_ImmSendAttrib(384.0 / 512, (src_eye ? 0.5 : 0) + 224.0 / 512, 0, 0);
C3D_ImmDrawEnd();
}

if (tVBOpt.ANAGLYPH) {
C3D_AlphaBlend(GPU_BLEND_ADD, GPU_BLEND_ADD, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA);
}

if (minRepeat != maxRepeat) {
env = C3D_GetTexEnv(1);
C3D_TexEnvInit(env);
C3D_TexEnvInit(C3D_GetTexEnv(1));
}
if (tVBOpt.ANAGLYPH) {
for (int i = 2; i <= 4; i++) C3D_TexEnvInit(C3D_GetTexEnv(i));
}
}

Expand Down
1 change: 0 additions & 1 deletion source/3ds/video_hard.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ void video_hard_init() {
C3D_ColorLogicOp(GPU_LOGICOP_COPY);

C3D_DepthTest(false, GPU_ALWAYS, GPU_WRITE_ALL);
C3D_AlphaTest(true, GPU_GREATER, 0);

vbuf = linearAlloc(sizeof(vertex) * VBUF_SIZE);
avbuf = linearAlloc(sizeof(avertex) * AVBUF_SIZE);
Expand Down
1 change: 1 addition & 0 deletions source/common/vb_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void setDefaults(void) {
tVBOpt.ROM_PATH = NULL;
tVBOpt.VSYNC = true;
tVBOpt.N3DS_SPEEDUP = true;
tVBOpt.ANAGLYPH = false;

// Default keys
#ifdef __3DS__
Expand Down

0 comments on commit 754362d

Please sign in to comment.