From c7fd3a05f942c8ce382f909cb91649a18acf048b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 25 Apr 2024 18:36:49 +1000 Subject: [PATCH] Simplify viewport setting --- src/Game.c | 6 +----- src/Graphics.h | 3 +-- src/Graphics_3DS.c | 2 +- src/Graphics_D3D11.c | 18 +++++++----------- src/Graphics_D3D9.c | 2 +- src/Graphics_Dreamcast.c | 10 +++++----- src/Graphics_GCWii.c | 2 +- src/Graphics_N64.c | 2 +- src/Graphics_NDS.c | 2 +- src/Graphics_PS1.c | 2 +- src/Graphics_PS2.c | 2 +- src/Graphics_PS3.c | 20 ++++++++++---------- src/Graphics_PSP.c | 2 +- src/Graphics_PSVita.c | 2 +- src/Graphics_Saturn.c | 2 +- src/Graphics_SoftGPU.c | 2 +- src/Graphics_WiiU.c | 2 +- src/Graphics_Xbox.c | 2 +- src/Graphics_Xbox360.c | 2 +- src/_GLShared.h | 6 +++--- 20 files changed, 41 insertions(+), 50 deletions(-) diff --git a/src/Game.c b/src/Game.c index 3bc164776..4474a8162 100644 --- a/src/Game.c +++ b/src/Game.c @@ -639,11 +639,7 @@ static CC_INLINE void Game_DrawFrame(double delta, float t) { #ifdef CC_BUILD_SPLITSCREEN static void DrawSplitscreen(double delta, float t, int i, int x, int y, int w, int h) { - Gfx.ViewportX = x; - Gfx.ViewportY = y; - Gfx.ViewportWidth = w; - Gfx.ViewportHeight = h; - Gfx_UpdateViewport(); + Gfx_SetViewport(x, y, w, h); Entities.CurPlayer = &LocalPlayer_Instances[i]; LocalPlayer_SetInterpPosition(Entities.CurPlayer, t); diff --git a/src/Graphics.h b/src/Graphics.h index 86642684c..29b5487d1 100644 --- a/src/Graphics.h +++ b/src/Graphics.h @@ -66,7 +66,6 @@ CC_VAR extern struct _GfxData { int MinTexWidth, MinTexHeight; cc_bool ReducedPerfMode; cc_uint8 ReducedPerfModeCooldown; - int ViewportX, ViewportY, ViewportWidth, ViewportHeight; } Gfx; extern GfxResourceID Gfx_defaultIb; @@ -254,7 +253,7 @@ void Gfx_GetApiInfo(cc_string* info); /* Updates state when the window's dimensions have changed */ /* NOTE: This may require recreating the context depending on the backend */ void Gfx_OnWindowResize(void); -void Gfx_UpdateViewport(void); +void Gfx_SetViewport(int x, int y, int w, int h); enum Screen3DS { TOP_SCREEN, BOTTOM_SCREEN }; #ifdef CC_BUILD_DUALSCREEN diff --git a/src/Graphics_3DS.c b/src/Graphics_3DS.c index 94116177d..36c8d53ec 100644 --- a/src/Graphics_3DS.c +++ b/src/Graphics_3DS.c @@ -575,7 +575,7 @@ void Gfx_EndFrame(void) { void Gfx_OnWindowResize(void) { } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } /*########################################################################################################################* diff --git a/src/Graphics_D3D11.c b/src/Graphics_D3D11.c index 9d5c4ce89..1896d0a48 100644 --- a/src/Graphics_D3D11.c +++ b/src/Graphics_D3D11.c @@ -646,12 +646,12 @@ static void RS_CreateRasterState(void) { ID3D11Device_CreateRasterizerState(device, &desc, &rs_states[1]); } -static void RS_UpdateViewport(void) { +void Gfx_SetViewport(int x, int y, int w, int h) { D3D11_VIEWPORT viewport; - viewport.TopLeftX = Gfx.ViewportX; - viewport.TopLeftY = Gfx.ViewportY; - viewport.Width = Gfx.ViewportWidth; - viewport.Height = Gfx.ViewportHeight; + viewport.TopLeftX = x; + viewport.TopLeftY = y; + viewport.Width = w; + viewport.Height = h; viewport.MinDepth = 0.0f; viewport.MaxDepth = 1.0f; ID3D11DeviceContext_RSSetViewports(context, 1, &viewport); @@ -670,7 +670,7 @@ static void RS_FreeRasterStates(void) { static void RS_Init(void) { RS_CreateRasterState(); - RS_UpdateViewport(); + Gfx_SetViewport(0, 0, Game.Width, Game.Height); RS_UpdateRasterState(); } @@ -1173,11 +1173,7 @@ void Gfx_OnWindowResize(void) { if (hr) Logger_Abort2(hr, "Failed to resize swapchain"); OM_InitTargets(); - RS_UpdateViewport(); -} - -void Gfx_UpdateViewport(void) { - RS_UpdateViewport(); + Gfx_SetViewport(0, 0, Game.Width, Game.Height); } static void InitPipeline(void) { diff --git a/src/Graphics_D3D9.c b/src/Graphics_D3D9.c index e3649b1bf..3d3a4c60d 100644 --- a/src/Graphics_D3D9.c +++ b/src/Graphics_D3D9.c @@ -887,5 +887,5 @@ void Gfx_OnWindowResize(void) { UpdateSwapchain(" (resizing window)"); } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } #endif diff --git a/src/Graphics_Dreamcast.c b/src/Graphics_Dreamcast.c index 962608044..e1a91a258 100644 --- a/src/Graphics_Dreamcast.c +++ b/src/Graphics_Dreamcast.c @@ -30,7 +30,7 @@ static void InitGLState(void) { void Gfx_Create(void) { if (!Gfx.Created) glKosInit(); - Gfx_UpdateViewport(); + Gfx_SetViewport(0, 0, Game.Width, Game.Height); InitGLState(); Gfx.MinTexWidth = 8; @@ -555,11 +555,11 @@ void Gfx_EndFrame(void) { } void Gfx_OnWindowResize(void) { - Gfx_UpdateViewport(); + Gfx_SetViewport(0, 0, Game.Width, Game.Height); } -void Gfx_UpdateViewport(void) { - glViewport(Gfx.ViewportX, Gfx.ViewportY, Gfx.ViewportWidth, Gfx.ViewportHeight); - glScissor (Gfx.ViewportX, Gfx.ViewportY, Gfx.ViewportWidth, Gfx.ViewportHeight); +void Gfx_SetViewport(int x, int y, int w, int h) { + glViewport(x, y, w, h); + glScissor (x, y, w, h); } #endif diff --git a/src/Graphics_GCWii.c b/src/Graphics_GCWii.c index 3864baa0e..3c522b5df 100644 --- a/src/Graphics_GCWii.c +++ b/src/Graphics_GCWii.c @@ -312,7 +312,7 @@ void Gfx_EndFrame(void) { void Gfx_OnWindowResize(void) { } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } cc_bool Gfx_WarnIfNecessary(void) { return false; } diff --git a/src/Graphics_N64.c b/src/Graphics_N64.c index 70849b0c4..ab106de45 100644 --- a/src/Graphics_N64.c +++ b/src/Graphics_N64.c @@ -79,7 +79,7 @@ void Gfx_SetFpsLimit(cc_bool vsync, float minFrameMs) { void Gfx_OnWindowResize(void) { } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } void Gfx_BeginFrame(void) { diff --git a/src/Graphics_NDS.c b/src/Graphics_NDS.c index 372628105..82f42f163 100644 --- a/src/Graphics_NDS.c +++ b/src/Graphics_NDS.c @@ -66,7 +66,7 @@ void Gfx_SetFpsLimit(cc_bool vsync, float minFrameMs) { void Gfx_OnWindowResize(void) { } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } void Gfx_BeginFrame(void) { } diff --git a/src/Graphics_PS1.c b/src/Graphics_PS1.c index ec2db31b9..203eed67e 100644 --- a/src/Graphics_PS1.c +++ b/src/Graphics_PS1.c @@ -727,7 +727,7 @@ void Gfx_OnWindowResize(void) { // TODO } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } void Gfx_GetApiInfo(cc_string* info) { String_AppendConst(info, "-- Using PS1 --\n"); diff --git a/src/Graphics_PS2.c b/src/Graphics_PS2.c index d5c5caa37..ba7b1d050 100644 --- a/src/Graphics_PS2.c +++ b/src/Graphics_PS2.c @@ -671,7 +671,7 @@ void Gfx_OnWindowResize(void) { // TODO } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } void Gfx_GetApiInfo(cc_string* info) { String_AppendConst(info, "-- Using PS2 --\n"); diff --git a/src/Graphics_PS3.c b/src/Graphics_PS3.c index b68299eca..7c0bfbf3c 100644 --- a/src/Graphics_PS3.c +++ b/src/Graphics_PS3.c @@ -414,7 +414,7 @@ static void ResetFrameState(void) { GCM_USER_CLIP_PLANE_DISABLE); // NOTE: Must be called each frame, otherwise renders upside down at 4x zoom - Gfx_OnWindowResize(); + Gfx_SetViewport(0, 0, Game.Width, Game.Height); } // https://github.com/ps3dev/PSL1GHT/blob/master/ppu/include/rsx/rsx.h#L30 @@ -450,24 +450,26 @@ void Gfx_EndFrame(void) { } void Gfx_OnWindowResize(void) { + Gfx_SetViewport(0, 0, Game.Width, Game.Height); +} + +void Gfx_SetViewport(int x, int y, int w, int h) { f32 scale[4], offset[4]; - - u16 w = DisplayInfo.Width; - u16 h = DisplayInfo.Height; f32 zmin = 0.0f; f32 zmax = 1.0f; + y = Game.Height - y - h; scale[0] = w * 0.5f; scale[1] = h * -0.5f; scale[2] = (zmax - zmin) * 0.5f; scale[3] = 0.0f; - offset[0] = w * 0.5f; - offset[1] = h * 0.5f; + offset[0] = x + w * 0.5f; + offset[1] = x + h * 0.5f; offset[2] = (zmax + zmin) * 0.5f; offset[3] = 0.0f; - rsxSetViewport(context, 0, 0, w, h, zmin, zmax, scale, offset); - rsxSetScissor(context, 0, 0, w, h); + rsxSetViewport(context, x, y, w, h, zmin, zmax, scale, offset); + rsxSetScissor(context, x, y, w, h); // TODO: even needed? for (int i = 0; i < 8; i++) @@ -476,8 +478,6 @@ void Gfx_OnWindowResize(void) { } } -void Gfx_UpdateViewport(void) { } - /*########################################################################################################################* *-------------------------------------------------------Index buffers-----------------------------------------------------* diff --git a/src/Graphics_PSP.c b/src/Graphics_PSP.c index 1fa21ba0b..47d40e936 100644 --- a/src/Graphics_PSP.c +++ b/src/Graphics_PSP.c @@ -283,7 +283,7 @@ void Gfx_EndFrame(void) { void Gfx_OnWindowResize(void) { } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } static cc_uint8* gfx_vertices; diff --git a/src/Graphics_PSVita.c b/src/Graphics_PSVita.c index fcb18f990..ce3932ee5 100644 --- a/src/Graphics_PSVita.c +++ b/src/Graphics_PSVita.c @@ -811,7 +811,7 @@ void Gfx_EndFrame(void) { void Gfx_OnWindowResize(void) { } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } /*########################################################################################################################* diff --git a/src/Graphics_Saturn.c b/src/Graphics_Saturn.c index a34c4b466..febb4f4af 100644 --- a/src/Graphics_Saturn.c +++ b/src/Graphics_Saturn.c @@ -435,7 +435,7 @@ void Gfx_OnWindowResize(void) { // TODO } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } void Gfx_GetApiInfo(cc_string* info) { String_AppendConst(info, "-- Using Saturn --\n"); diff --git a/src/Graphics_SoftGPU.c b/src/Graphics_SoftGPU.c index 4557cbc1b..669e46fc5 100644 --- a/src/Graphics_SoftGPU.c +++ b/src/Graphics_SoftGPU.c @@ -491,7 +491,7 @@ void Gfx_OnWindowResize(void) { colorBuffer = fb_bmp.scan0; } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } void Gfx_GetApiInfo(cc_string* info) { int pointerSize = sizeof(void*) * 8; diff --git a/src/Graphics_WiiU.c b/src/Graphics_WiiU.c index 2e71341a8..521e77c46 100644 --- a/src/Graphics_WiiU.c +++ b/src/Graphics_WiiU.c @@ -459,7 +459,7 @@ void Gfx_OnWindowResize(void) { } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } void Gfx_3DS_SetRenderScreen1(enum Screen3DS screen) { GX2ContextState* tv_state = WHBGfxGetTVContextState(); diff --git a/src/Graphics_Xbox.c b/src/Graphics_Xbox.c index 69d4c4e76..605a7608f 100644 --- a/src/Graphics_Xbox.c +++ b/src/Graphics_Xbox.c @@ -535,7 +535,7 @@ void Gfx_CalcPerspectiveMatrix(struct Matrix* matrix, float fov, float aspect, f void Gfx_OnWindowResize(void) { } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } static struct Matrix _view, _proj, _mvp; diff --git a/src/Graphics_Xbox360.c b/src/Graphics_Xbox360.c index f95c75c52..d485b4915 100644 --- a/src/Graphics_Xbox360.c +++ b/src/Graphics_Xbox360.c @@ -403,5 +403,5 @@ void Gfx_OnWindowResize(void) { } -void Gfx_UpdateViewport(void) { } +void Gfx_SetViewport(int x, int y, int w, int h) { } #endif diff --git a/src/_GLShared.h b/src/_GLShared.h index 87fa52547..70f365e9b 100644 --- a/src/_GLShared.h +++ b/src/_GLShared.h @@ -325,7 +325,7 @@ void Gfx_EndFrame(void) { } void Gfx_OnWindowResize(void) { - Gfx_UpdateViewport(); + Gfx_SetViewport(0, 0, Game.Width, Game.Height); /* With cocoa backend, in some cases [NSOpenGLContext update] will actually */ /* call glViewport with the size of the window framebuffer */ /* https://github.com/glfw/glfw/issues/80 */ @@ -336,6 +336,6 @@ void Gfx_OnWindowResize(void) { GLContext_Update(); } -void Gfx_UpdateViewport(void) { - glViewport(Gfx.ViewportX, Gfx.ViewportY, Gfx.ViewportWidth, Gfx.ViewportHeight); +void Gfx_SetViewport(int x, int y, int w, int h) { + glViewport(x, y, w, h); }