Skip to content

Commit

Permalink
Merge pull request #306 from neobrain/pixel_center_correction
Browse files Browse the repository at this point in the history
VertexShaderGen: Correct vertex shader output to consider shifted pixel centers.
  • Loading branch information
delroth committed Apr 25, 2014
2 parents 28db739 + c47c32d commit 25f5598
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Source/Core/VideoCommon/VertexShaderGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,14 @@ static inline void GenerateVertexShader(T& out, u32 components, API_TYPE api_typ
//seems to get rather complicated
}

// The console GPU places the pixel center at 7/12 in screen space unless
// antialiasing is enabled, while D3D and OpenGL place it at 0.5. This results
// in some primitives being placed one pixel too far to the bottom-right,
// which in turn can be critical if it happens for clear quads.
// Hence, we compensate for this pixel center difference so that primitives
// get rasterized correctly.
out.Write("o.pos.xy = o.pos.xy - " I_DEPTHPARAMS".zw;\n");

if (api_type == API_OPENGL)
{
// Bit ugly here
Expand Down
11 changes: 11 additions & 0 deletions Source/Core/VideoCommon/VertexShaderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,17 @@ void VertexShaderManager::SetConstants()
bViewportChanged = false;
constants.depthparams[0] = xfregs.viewport.farZ / 16777216.0f;
constants.depthparams[1] = xfregs.viewport.zRange / 16777216.0f;

// The console GPU places the pixel center at 7/12 unless antialiasing
// is enabled, while D3D and OpenGL place it at 0.5. See the comment
// in VertexShaderGen.cpp for details.
// NOTE: If we ever emulate antialiasing, the sample locations set by
// BP registers 0x01-0x04 need to be considered here.
const float pixel_center_correction = 7.0f / 12.0f - 0.5f;
const float pixel_size_x = 2.f / Renderer::EFBToScaledXf(2.f * xfregs.viewport.wd);
const float pixel_size_y = 2.f / Renderer::EFBToScaledXf(2.f * xfregs.viewport.ht);
constants.depthparams[2] = pixel_center_correction * pixel_size_x;
constants.depthparams[3] = pixel_center_correction * pixel_size_y;
dirty = true;
// This is so implementation-dependent that we can't have it here.
g_renderer->SetViewport();
Expand Down

0 comments on commit 25f5598

Please sign in to comment.