Skip to content

Commit

Permalink
Move ComputeDrawRectangle() to Renderer::UpdateDrawRectangle().
Browse files Browse the repository at this point in the history
  • Loading branch information
neobrain committed Nov 16, 2012
1 parent 035840e commit 78031c2
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 180 deletions.
123 changes: 123 additions & 0 deletions Source/Core/VideoCommon/Src/RenderBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ int Renderer::s_backbuffer_height;
float Renderer::xScale;
float Renderer::yScale;

TargetRectangle Renderer::target_rc;

int Renderer::s_LastEFBScale;

bool Renderer::s_skipSwap;
Expand Down Expand Up @@ -331,6 +333,127 @@ void Renderer::CalculateXYScale(const TargetRectangle& dst_rect)
}
}

// TODO: remove
extern bool g_aspect_wide;

void Renderer::UpdateDrawRectangle(int backbuffer_width, int backbuffer_height)
{
float FloatGLWidth = (float)backbuffer_width;
float FloatGLHeight = (float)backbuffer_height;
float FloatXOffset = 0;
float FloatYOffset = 0;

// The rendering window size
const float WinWidth = FloatGLWidth;
const float WinHeight = FloatGLHeight;

// Handle aspect ratio.
// Default to auto.
bool use16_9 = g_aspect_wide;

// Update aspect ratio hack values
// Won't take effect until next frame
// Don't know if there is a better place for this code so there isn't a 1 frame delay
if ( g_ActiveConfig.bWidescreenHack )
{
float source_aspect = use16_9 ? (16.0f / 9.0f) : (4.0f / 3.0f);
float target_aspect;

switch ( g_ActiveConfig.iAspectRatio )
{
case ASPECT_FORCE_16_9 :
target_aspect = 16.0f / 9.0f;
break;
case ASPECT_FORCE_4_3 :
target_aspect = 4.0f / 3.0f;
break;
case ASPECT_STRETCH :
target_aspect = WinWidth / WinHeight;
break;
default :
// ASPECT_AUTO == no hacking
target_aspect = source_aspect;
break;
}

float adjust = source_aspect / target_aspect;
if ( adjust > 1 )
{
// Vert+
g_Config.fAspectRatioHackW = 1;
g_Config.fAspectRatioHackH = 1/adjust;
}
else
{
// Hor+
g_Config.fAspectRatioHackW = adjust;
g_Config.fAspectRatioHackH = 1;
}
}
else
{
// Hack is disabled
g_Config.fAspectRatioHackW = 1;
g_Config.fAspectRatioHackH = 1;
}

// Check for force-settings and override.
if (g_ActiveConfig.iAspectRatio == ASPECT_FORCE_16_9)
use16_9 = true;
else if (g_ActiveConfig.iAspectRatio == ASPECT_FORCE_4_3)
use16_9 = false;

if (g_ActiveConfig.iAspectRatio != ASPECT_STRETCH)
{
// The rendering window aspect ratio as a proportion of the 4:3 or 16:9 ratio
float Ratio = (WinWidth / WinHeight) / (!use16_9 ? (4.0f / 3.0f) : (16.0f / 9.0f));
// Check if height or width is the limiting factor. If ratio > 1 the picture is too wide and have to limit the width.
if (Ratio > 1.0f)
{
// Scale down and center in the X direction.
FloatGLWidth /= Ratio;
FloatXOffset = (WinWidth - FloatGLWidth) / 2.0f;
}
// The window is too high, we have to limit the height
else
{
// Scale down and center in the Y direction.
FloatGLHeight *= Ratio;
FloatYOffset = FloatYOffset + (WinHeight - FloatGLHeight) / 2.0f;
}
}

// -----------------------------------------------------------------------
// Crop the picture from 4:3 to 5:4 or from 16:9 to 16:10.
// Output: FloatGLWidth, FloatGLHeight, FloatXOffset, FloatYOffset
// ------------------
if (g_ActiveConfig.iAspectRatio != ASPECT_STRETCH && g_ActiveConfig.bCrop)
{
float Ratio = !use16_9 ? ((4.0f / 3.0f) / (5.0f / 4.0f)) : (((16.0f / 9.0f) / (16.0f / 10.0f)));
// The width and height we will add (calculate this before FloatGLWidth and FloatGLHeight is adjusted)
float IncreasedWidth = (Ratio - 1.0f) * FloatGLWidth;
float IncreasedHeight = (Ratio - 1.0f) * FloatGLHeight;
// The new width and height
FloatGLWidth = FloatGLWidth * Ratio;
FloatGLHeight = FloatGLHeight * Ratio;
// Adjust the X and Y offset
FloatXOffset = FloatXOffset - (IncreasedWidth * 0.5f);
FloatYOffset = FloatYOffset - (IncreasedHeight * 0.5f);
}

int XOffset = (int)(FloatXOffset + 0.5f);
int YOffset = (int)(FloatYOffset + 0.5f);
int iWhidth = (int)ceil(FloatGLWidth);
int iHeight = (int)ceil(FloatGLHeight);
iWhidth -= iWhidth % 4; // ensure divisibility by 4 to make it compatible with all the video encoders
iHeight -= iHeight % 4;

target_rc.left = XOffset;
target_rc.top = YOffset;
target_rc.right = XOffset + iWhidth;
target_rc.bottom = YOffset + iHeight;
}

void Renderer::SetWindowSize(int width, int height)
{
if (width < 1)
Expand Down
6 changes: 6 additions & 0 deletions Source/Core/VideoCommon/Src/RenderBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class Renderer
// Use this to convert a whole native EFB rect to backbuffer coordinates
virtual TargetRectangle ConvertEFBRectangle(const EFBRectangle& rc) = 0;

static const TargetRectangle& GetTargetRectangle() { return target_rc; }
static void UpdateDrawRectangle(int backbuffer_width, int backbuffer_height);


// Use this to upscale native EFB coordinates to IDEAL internal resolution
static unsigned int EFBToScaledX(int x) { return x * GetTargetWidth() / EFB_WIDTH; }
static unsigned int EFBToScaledY(int y) { return y * GetTargetHeight() / EFB_HEIGHT; }
Expand Down Expand Up @@ -163,6 +167,8 @@ class Renderer
static float xScale;
static float yScale;

static TargetRectangle target_rc;

// can probably eliminate this static var
static int s_LastEFBScale;

Expand Down
122 changes: 0 additions & 122 deletions Source/Core/VideoCommon/Src/VideoConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,125 +290,3 @@ void VideoConfig::GameIniSave(const char* default_ini, const char* game_ini)

iniFile.Save(game_ini);
}


// TODO: remove
extern bool g_aspect_wide;

// TODO: Figure out a better place for this function.
void ComputeDrawRectangle(int backbuffer_width, int backbuffer_height, bool flip, TargetRectangle *rc)
{
float FloatGLWidth = (float)backbuffer_width;
float FloatGLHeight = (float)backbuffer_height;
float FloatXOffset = 0;
float FloatYOffset = 0;

// The rendering window size
const float WinWidth = FloatGLWidth;
const float WinHeight = FloatGLHeight;

// Handle aspect ratio.
// Default to auto.
bool use16_9 = g_aspect_wide;

// Update aspect ratio hack values
// Won't take effect until next frame
// Don't know if there is a better place for this code so there isn't a 1 frame delay
if ( g_ActiveConfig.bWidescreenHack )
{
float source_aspect = use16_9 ? (16.0f / 9.0f) : (4.0f / 3.0f);
float target_aspect;

switch ( g_ActiveConfig.iAspectRatio )
{
case ASPECT_FORCE_16_9 :
target_aspect = 16.0f / 9.0f;
break;
case ASPECT_FORCE_4_3 :
target_aspect = 4.0f / 3.0f;
break;
case ASPECT_STRETCH :
target_aspect = WinWidth / WinHeight;
break;
default :
// ASPECT_AUTO == no hacking
target_aspect = source_aspect;
break;
}

float adjust = source_aspect / target_aspect;
if ( adjust > 1 )
{
// Vert+
g_Config.fAspectRatioHackW = 1;
g_Config.fAspectRatioHackH = 1/adjust;
}
else
{
// Hor+
g_Config.fAspectRatioHackW = adjust;
g_Config.fAspectRatioHackH = 1;
}
}
else
{
// Hack is disabled
g_Config.fAspectRatioHackW = 1;
g_Config.fAspectRatioHackH = 1;
}

// Check for force-settings and override.
if (g_ActiveConfig.iAspectRatio == ASPECT_FORCE_16_9)
use16_9 = true;
else if (g_ActiveConfig.iAspectRatio == ASPECT_FORCE_4_3)
use16_9 = false;

if (g_ActiveConfig.iAspectRatio != ASPECT_STRETCH)
{
// The rendering window aspect ratio as a proportion of the 4:3 or 16:9 ratio
float Ratio = (WinWidth / WinHeight) / (!use16_9 ? (4.0f / 3.0f) : (16.0f / 9.0f));
// Check if height or width is the limiting factor. If ratio > 1 the picture is too wide and have to limit the width.
if (Ratio > 1.0f)
{
// Scale down and center in the X direction.
FloatGLWidth /= Ratio;
FloatXOffset = (WinWidth - FloatGLWidth) / 2.0f;
}
// The window is too high, we have to limit the height
else
{
// Scale down and center in the Y direction.
FloatGLHeight *= Ratio;
FloatYOffset = FloatYOffset + (WinHeight - FloatGLHeight) / 2.0f;
}
}

// -----------------------------------------------------------------------
// Crop the picture from 4:3 to 5:4 or from 16:9 to 16:10.
// Output: FloatGLWidth, FloatGLHeight, FloatXOffset, FloatYOffset
// ------------------
if (g_ActiveConfig.iAspectRatio != ASPECT_STRETCH && g_ActiveConfig.bCrop)
{
float Ratio = !use16_9 ? ((4.0f / 3.0f) / (5.0f / 4.0f)) : (((16.0f / 9.0f) / (16.0f / 10.0f)));
// The width and height we will add (calculate this before FloatGLWidth and FloatGLHeight is adjusted)
float IncreasedWidth = (Ratio - 1.0f) * FloatGLWidth;
float IncreasedHeight = (Ratio - 1.0f) * FloatGLHeight;
// The new width and height
FloatGLWidth = FloatGLWidth * Ratio;
FloatGLHeight = FloatGLHeight * Ratio;
// Adjust the X and Y offset
FloatXOffset = FloatXOffset - (IncreasedWidth * 0.5f);
FloatYOffset = FloatYOffset - (IncreasedHeight * 0.5f);
}

int XOffset = (int)(FloatXOffset + 0.5f);
int YOffset = (int)(FloatYOffset + 0.5f);
int iWhidth = (int)ceil(FloatGLWidth);
int iHeight = (int)ceil(FloatGLHeight);
iWhidth -= iWhidth % 4; // ensure divisibility by 4 to make it compatible with all the video encoders
iHeight -= iHeight % 4;
rc->left = XOffset;
rc->top = flip ? (int)(YOffset + iHeight) : YOffset;
rc->right = XOffset + iWhidth;
rc->bottom = flip ? YOffset : (int)(YOffset + iHeight);
}
2 changes: 0 additions & 2 deletions Source/Core/VideoCommon/Src/VideoConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,4 @@ extern VideoConfig g_ActiveConfig;
// Called every frame.
void UpdateActiveConfig();

void ComputeDrawRectangle(int backbuffer_width, int backbuffer_height, bool flip, TargetRectangle *rc);

#endif // _VIDEO_CONFIG_H_
30 changes: 13 additions & 17 deletions Source/Plugins/Plugin_VideoDX11/Src/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,8 @@ Renderer::Renderer()
FramebufferManagerBase::SetLastXfbWidth(MAX_XFB_WIDTH);
FramebufferManagerBase::SetLastXfbHeight(MAX_XFB_HEIGHT);

TargetRectangle dst_rect;
ComputeDrawRectangle(s_backbuffer_width, s_backbuffer_height, false, &dst_rect);

CalculateXYScale(dst_rect);
UpdateDrawRectangle(s_backbuffer_width, s_backbuffer_height);
CalculateXYScale(GetTargetRectangle());

s_LastAA = g_ActiveConfig.iMultisampleMode;
s_LastEFBScale = g_ActiveConfig.iEFBScale;
Expand Down Expand Up @@ -924,13 +922,12 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons
ResetAPIState();

// Prepare to copy the XFBs to our backbuffer
TargetRectangle dst_rect;
ComputeDrawRectangle(s_backbuffer_width, s_backbuffer_height, false, &dst_rect);
UpdateDrawRectangle(s_backbuffer_width, s_backbuffer_height);

int X = dst_rect.left;
int Y = dst_rect.top;
int Width = dst_rect.right - dst_rect.left;
int Height = dst_rect.bottom - dst_rect.top;
int X = GetTargetRectangle().left;
int Y = GetTargetRectangle().top;
int Width = GetTargetRectangle().right - GetTargetRectangle().left;
int Height = GetTargetRectangle().bottom - GetTargetRectangle().top;

// TODO: Redundant checks...
if (X < 0) X = 0;
Expand Down Expand Up @@ -1018,7 +1015,7 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons
// done with drawing the game stuff, good moment to save a screenshot
if (s_bScreenshot)
{
SaveScreenshot(s_sScreenshotName, dst_rect);
SaveScreenshot(s_sScreenshotName, GetTargetRectangle());
s_bScreenshot = false;
}

Expand All @@ -1035,8 +1032,8 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons
D3D::context->CopyResource(s_screenshot_texture, (ID3D11Resource*)D3D::GetBackBuffer()->GetTex());
if (!bLastFrameDumped)
{
s_recordWidth = dst_rect.GetWidth();
s_recordHeight = dst_rect.GetHeight();
s_recordWidth = GetTargetRectangle().GetWidth();
s_recordHeight = GetTargetRectangle().GetHeight();
bAVIDumping = AVIDump::Start(EmuWindow::GetParentWnd(), s_recordWidth, s_recordHeight);
if (!bAVIDumping)
{
Expand All @@ -1062,7 +1059,7 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons
w = s_recordWidth;
h = s_recordHeight;
}
char* source_ptr = (char*)map.pData + dst_rect.left*4 + dst_rect.top*map.RowPitch;
char* source_ptr = (char*)map.pData + GetTargetRectangle().left*4 + GetTargetRectangle().top*map.RowPitch;
formatBufferDump(source_ptr, frame_data, s_recordWidth, s_recordHeight, map.RowPitch);
AVIDump::AddFrame(frame_data);
D3D::context->Unmap(s_screenshot_texture, 0);
Expand Down Expand Up @@ -1178,9 +1175,8 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons
s_backbuffer_height = D3D::GetBackBufferHeight();
}

ComputeDrawRectangle(s_backbuffer_width, s_backbuffer_height, false, &dst_rect);

CalculateXYScale(dst_rect);
UpdateDrawRectangle(s_backbuffer_width, s_backbuffer_height);
CalculateXYScale(GetTargetRectangle());

s_LastEFBScale = g_ActiveConfig.iEFBScale;
CalculateTargetSize();
Expand Down

0 comments on commit 78031c2

Please sign in to comment.