Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'viewportCorrection'
  • Loading branch information
degasus committed Oct 29, 2013
2 parents 35824aa + 92fdac4 commit 498d491
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 86 deletions.
65 changes: 18 additions & 47 deletions Source/Core/VideoBackends/D3D/Src/Render.cpp
Expand Up @@ -477,30 +477,9 @@ u32 Renderer::AccessEFB(EFBAccessType type, u32 x, u32 y, u32 poke_data)
}
}

// Viewport correction:
// Say you want a viewport at (ix, iy) with size (iw, ih),
// but your viewport must be clamped at (ax, ay) with size (aw, ah).
// Just multiply the projection matrix with the following to get the same
// effect:
// [ (iw/aw) 0 0 ((iw - 2*(ax-ix)) / aw - 1) ]
// [ 0 (ih/ah) 0 ((-ih + 2*(ay-iy)) / ah + 1) ]
// [ 0 0 1 0 ]
// [ 0 0 0 1 ]
static void ViewportCorrectionMatrix(Matrix44& result,
float ix, float iy, float iw, float ih, // Intended viewport (x, y, width, height)
float ax, float ay, float aw, float ah) // Actual viewport (x, y, width, height)
{
Matrix44::LoadIdentity(result);
if (aw == 0.f || ah == 0.f)
return;
result.data[4*0+0] = iw / aw;
result.data[4*0+3] = (iw - 2.f * (ax - ix)) / aw - 1.f;
result.data[4*1+1] = ih / ah;
result.data[4*1+3] = (-ih + 2.f * (ay - iy)) / ah + 1.f;
}

// Called from VertexShaderManager
void Renderer::UpdateViewport(Matrix44& vpCorrection)
void Renderer::UpdateViewport()
{
// reversed gxsetviewport(xorig, yorig, width, height, nearz, farz)
// [0] = width/2
Expand All @@ -513,34 +492,26 @@ void Renderer::UpdateViewport(Matrix44& vpCorrection)
int scissorXOff = bpmem.scissorOffset.x * 2;
int scissorYOff = bpmem.scissorOffset.y * 2;

float intendedX = Renderer::EFBToScaledXf(xfregs.viewport.xOrig - xfregs.viewport.wd - scissorXOff);
float intendedY = Renderer::EFBToScaledYf(xfregs.viewport.yOrig + xfregs.viewport.ht - scissorYOff);
float intendedWd = Renderer::EFBToScaledXf(2.0f * xfregs.viewport.wd);
float intendedHt = Renderer::EFBToScaledYf(-2.0f * xfregs.viewport.ht);
if (intendedWd < 0.f)
float X = Renderer::EFBToScaledXf(xfregs.viewport.xOrig - xfregs.viewport.wd - scissorXOff);
float Y = Renderer::EFBToScaledYf(xfregs.viewport.yOrig + xfregs.viewport.ht - scissorYOff);
float Wd = Renderer::EFBToScaledXf(2.0f * xfregs.viewport.wd);
float Ht = Renderer::EFBToScaledYf(-2.0f * xfregs.viewport.ht);
if (Wd < 0.0f)
{
intendedX += intendedWd;
intendedWd = -intendedWd;
X += Wd;
Wd = -Wd;
}
if (intendedHt < 0.f)
if (Ht < 0.0f)
{
intendedY += intendedHt;
intendedHt = -intendedHt;
Y += Ht;
Ht = -Ht;
}

// In D3D, the viewport rectangle must fit within the render target.
float X = (intendedX >= 0.f) ? intendedX : 0.f;
float Y = (intendedY >= 0.f) ? intendedY : 0.f;
float Wd = (X + intendedWd <= GetTargetWidth()) ? intendedWd : (GetTargetWidth() - X);
float Ht = (Y + intendedHt <= GetTargetHeight()) ? intendedHt : (GetTargetHeight() - Y);

// If GX viewport is off the render target, we must clamp our viewport
// within the bounds. Use the correction matrix to compensate.
ViewportCorrectionMatrix(vpCorrection,
intendedX, intendedY,
intendedWd, intendedHt,
X, Y,
Wd, Ht);
X = (X >= 0.f) ? X : 0.f;
Y = (Y >= 0.f) ? Y : 0.f;
Wd = (X + Wd <= GetTargetWidth()) ? Wd : (GetTargetWidth() - X);
Ht = (Y + Ht <= GetTargetHeight()) ? Ht : (GetTargetHeight() - Y);

// Some games set invalid values for z-min and z-max so fix them to the max and min allowed and let the shaders do this work
D3D11_VIEWPORT vp = CD3D11_VIEWPORT(X, Y,
Expand Down Expand Up @@ -1055,10 +1026,10 @@ void Renderer::Swap(u32 xfbAddr, u32 fbWidth, u32 fbHeight,const EFBRectangle& r
}

// begin next frame
Renderer::RestoreAPIState();
RestoreAPIState();
D3D::BeginFrame();
D3D::context->OMSetRenderTargets(1, &FramebufferManager::GetEFBColorTexture()->GetRTV(), FramebufferManager::GetEFBDepthTexture()->GetDSV());
VertexShaderManager::SetViewportChanged();
UpdateViewport();

Core::Callback_VideoCopiedToXFB(XFBWrited || (g_ActiveConfig.bUseXFB && g_ActiveConfig.bUseRealXFB));
XFBWrited = false;
Expand All @@ -1078,7 +1049,7 @@ void Renderer::RestoreAPIState()
D3D::stateman->PopBlendState();
D3D::stateman->PopDepthState();
D3D::stateman->PopRasterizerState();
VertexShaderManager::SetViewportChanged();
UpdateViewport();
BPFunctions::SetScissor();
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoBackends/D3D/Src/Render.h
Expand Up @@ -46,7 +46,7 @@ class Renderer : public ::Renderer

void ReinterpretPixelData(unsigned int convtype);

void UpdateViewport(Matrix44& vpCorrection);
void UpdateViewport();

bool SaveScreenshot(const std::string &filename, const TargetRectangle &rc);

Expand Down
1 change: 1 addition & 0 deletions Source/Core/VideoBackends/D3D/Src/main.cpp
Expand Up @@ -90,6 +90,7 @@ void InitBackendInfo()
g_Config.backend_info.bSupportsFormatReinterpretation = true;
g_Config.backend_info.bSupportsPixelLighting = true;
g_Config.backend_info.bSupportsPrimitiveRestart = true;
g_Config.backend_info.bSupportsOversizedViewports = false;

IDXGIFactory* factory;
IDXGIAdapter* ad;
Expand Down
1 change: 0 additions & 1 deletion Source/Core/VideoBackends/OGL/Src/GLUtil.cpp
Expand Up @@ -10,7 +10,6 @@
#include "ConfigManager.h"

#include "Render.h"
#include "VertexShaderManager.h"

#include "GLUtil.h"

Expand Down
8 changes: 2 additions & 6 deletions Source/Core/VideoBackends/OGL/Src/Render.cpp
Expand Up @@ -33,7 +33,6 @@
#include "RasterFont.h"
#include "VertexShaderGen.h"
#include "DLCache.h"
#include "PixelShaderManager.h"
#include "ProgramShaderCache.h"
#include "VertexShaderManager.h"
#include "VertexLoaderManager.h"
Expand Down Expand Up @@ -1093,7 +1092,7 @@ u32 Renderer::AccessEFB(EFBAccessType type, u32 x, u32 y, u32 poke_data)
}

// Called from VertexShaderManager
void Renderer::UpdateViewport(Matrix44& vpCorrection)
void Renderer::UpdateViewport()
{
// reversed gxsetviewport(xorig, yorig, width, height, nearz, farz)
// [0] = width/2
Expand Down Expand Up @@ -1124,9 +1123,6 @@ void Renderer::UpdateViewport(Matrix44& vpCorrection)
Height *= -1;
}

// OpenGL does not require any viewport correct
Matrix44::LoadIdentity(vpCorrection);

// Update the view port
if(g_ogl_config.bSupportViewportFloat)
{
Expand Down Expand Up @@ -1664,7 +1660,7 @@ void Renderer::RestoreAPIState()
SetDepthMode();
SetBlendMode(true);
SetLogicOpMode();
VertexShaderManager::SetViewportChanged();
UpdateViewport();

#ifndef USE_GLES3
glPolygonMode(GL_FRONT_AND_BACK, g_ActiveConfig.bWireFrame ? GL_LINE : GL_FILL);
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoBackends/OGL/Src/Render.h
Expand Up @@ -78,7 +78,7 @@ class Renderer : public ::Renderer

void ReinterpretPixelData(unsigned int convtype);

void UpdateViewport(Matrix44& vpCorrection);
void UpdateViewport();

bool SaveScreenshot(const std::string &filename, const TargetRectangle &rc);

Expand Down
3 changes: 0 additions & 3 deletions Source/Core/VideoBackends/OGL/Src/TextureCache.cpp
Expand Up @@ -30,14 +30,12 @@
#include "ImageWrite.h"
#include "MemoryUtil.h"
#include "ProgramShaderCache.h"
#include "PixelShaderManager.h"
#include "Render.h"
#include "Statistics.h"
#include "StringUtil.h"
#include "TextureCache.h"
#include "TextureConverter.h"
#include "TextureDecoder.h"
#include "VertexShaderManager.h"
#include "VideoConfig.h"

namespace OGL
Expand Down Expand Up @@ -391,7 +389,6 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo
}

FramebufferManager::SetFramebuffer(0);
VertexShaderManager::SetViewportChanged();

GL_REPORT_ERRORD();

Expand Down
4 changes: 0 additions & 4 deletions Source/Core/VideoBackends/OGL/Src/TextureConverter.cpp
Expand Up @@ -9,7 +9,6 @@
#include "TextureConversionShader.h"
#include "TextureCache.h"
#include "ProgramShaderCache.h"
#include "VertexShaderManager.h"
#include "FramebufferManager.h"
#include "Globals.h"
#include "VideoConfig.h"
Expand Down Expand Up @@ -350,7 +349,6 @@ void EncodeToRamYUYV(GLuint srcTexture, const TargetRectangle& sourceRc, u8* des

EncodeToRamUsingShader(srcTexture, sourceRc, destAddr, dstWidth / 2, dstHeight, 0, false, false);
FramebufferManager::SetFramebuffer(0);
VertexShaderManager::SetViewportChanged();
TextureCache::DisableStage(0);
g_renderer->RestoreAPIState();
GL_REPORT_ERRORD();
Expand Down Expand Up @@ -426,8 +424,6 @@ void DecodeToTexture(u32 xfbAddr, int srcWidth, int srcHeight, GLuint destTextur

GL_REPORT_ERRORD();

VertexShaderManager::SetViewportChanged();

FramebufferManager::SetFramebuffer(0);

g_renderer->RestoreAPIState();
Expand Down
1 change: 1 addition & 0 deletions Source/Core/VideoBackends/OGL/Src/main.cpp
Expand Up @@ -146,6 +146,7 @@ void InitBackendInfo()
g_Config.backend_info.bSupportsFormatReinterpretation = true;
g_Config.backend_info.bSupportsPixelLighting = true;
//g_Config.backend_info.bSupportsEarlyZ = true; // is gpu dependent and must be set in renderer
g_Config.backend_info.bSupportsOversizedViewports = true;

// aamodes
const char* caamodes[] = {_trans("None"), "2x", "4x", "8x", "8x CSAA", "8xQ CSAA", "16x CSAA", "16xQ CSAA", "4x SSAA"};
Expand Down
2 changes: 0 additions & 2 deletions Source/Core/VideoCommon/Src/BPFunctions.cpp
Expand Up @@ -49,8 +49,6 @@ void SetScissor()

TargetRectangle trc = g_renderer->ConvertEFBRectangle(rc);
g_renderer->SetScissorRect(trc);

UpdateViewportWithCorrection();
}

void SetLineWidth()
Expand Down
1 change: 1 addition & 0 deletions Source/Core/VideoCommon/Src/BPStructs.cpp
Expand Up @@ -185,6 +185,7 @@ void BPWritten(const BPCmd& bp)
case BPMEM_SCISSORBR: // Scissor Rectable Bottom, Right
case BPMEM_SCISSOROFFSET: // Scissor Offset
SetScissor();
VertexShaderManager::SetViewportChanged();
break;
case BPMEM_LINEPTWIDTH: // Line Width
SetLineWidth();
Expand Down
6 changes: 2 additions & 4 deletions Source/Core/VideoCommon/Src/RenderBase.cpp
Expand Up @@ -30,7 +30,6 @@
#include "XFMemory.h"
#include "FifoPlayer/FifoRecorder.h"
#include "AVIDump.h"
#include "VertexShaderManager.h"

#include <cmath>
#include <string>
Expand Down Expand Up @@ -231,7 +230,6 @@ bool Renderer::CalculateTargetSize(unsigned int framebuffer_width, unsigned int
{
s_target_width = newEFBWidth;
s_target_height = newEFBHeight;
VertexShaderManager::SetViewportChanged();
return true;
}
return false;
Expand Down Expand Up @@ -520,8 +518,8 @@ void Renderer::RecordVideoMemory()
FifoRecorder::GetInstance().SetVideoMemory(bpMem, cpMem, xfMem, xfRegs, sizeof(XFRegisters) / 4);
}

void UpdateViewport(Matrix44& vpCorrection)
void UpdateViewport()
{
if (xfregs.viewport.wd != 0 && xfregs.viewport.ht != 0)
g_renderer->UpdateViewport(vpCorrection);
g_renderer->UpdateViewport();
}
4 changes: 2 additions & 2 deletions Source/Core/VideoCommon/Src/RenderBase.h
Expand Up @@ -108,7 +108,7 @@ class Renderer
// Finish up the current frame, print some stats
virtual void Swap(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& rc,float Gamma = 1.0f) = 0;

virtual void UpdateViewport(Matrix44& vpCorrection) = 0;
virtual void UpdateViewport() = 0;

virtual bool SaveScreenshot(const std::string &filename, const TargetRectangle &rc) = 0;

Expand Down Expand Up @@ -163,6 +163,6 @@ class Renderer

extern Renderer *g_renderer;

void UpdateViewport(Matrix44& vpCorrection);
void UpdateViewport();

#endif // _COMMON_RENDERBASE_H_
1 change: 0 additions & 1 deletion Source/Core/VideoCommon/Src/TextureConversionShader.cpp
Expand Up @@ -12,7 +12,6 @@

#include "TextureConversionShader.h"
#include "TextureDecoder.h"
#include "PixelShaderManager.h"
#include "PixelShaderGen.h"
#include "BPMemory.h"
#include "RenderBase.h"
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoCommon/Src/VertexShaderGen.h
Expand Up @@ -39,7 +39,7 @@
#define I_TRANSFORMMATRICES "ctrmtx"
#define I_NORMALMATRICES "cnmtx"
#define I_POSTTRANSFORMMATRICES "cpostmtx"
#define I_DEPTHPARAMS "cDepth" // farZ, zRange, scaled viewport width, scaled viewport height
#define I_DEPTHPARAMS "cDepth" // farZ, zRange

//TODO: get rid of them, they aren't used at all
#define C_POSNORMALMATRIX 0
Expand Down

0 comments on commit 498d491

Please sign in to comment.