VertexShaderGen: Remove the need for an extra UID.

This commit is contained in:
Jules Blok
2017-01-05 14:40:37 +01:00
parent ad84b904e4
commit f866748006
3 changed files with 39 additions and 41 deletions

View File

@ -30,10 +30,6 @@ VertexShaderUid GetVertexShaderUid()
uid_data->msaa = g_ActiveConfig.iMultisamples > 1; uid_data->msaa = g_ActiveConfig.iMultisamples > 1;
uid_data->ssaa = g_ActiveConfig.iMultisamples > 1 && g_ActiveConfig.bSSAA; uid_data->ssaa = g_ActiveConfig.iMultisamples > 1 && g_ActiveConfig.bSSAA;
uid_data->numColorChans = xfmem.numChan.numColorChans; uid_data->numColorChans = xfmem.numChan.numColorChans;
uid_data->vertex_depth =
g_ActiveConfig.backend_info.bSupportsDepthClamp &&
((fabs(xfmem.viewport.zRange) > 16777215.0f || fabs(xfmem.viewport.farZ) > 16777215.0f) ||
(xfmem.viewport.zRange < 0.0f && !g_ActiveConfig.backend_info.bSupportsReversedDepthRange));
GetLightingShaderUid(uid_data->lighting); GetLightingShaderUid(uid_data->lighting);
@ -438,23 +434,15 @@ ShaderCode GenerateVertexShaderCode(APIType api_type, const vertex_shader_uid_da
// override it with the correct values if not then early z culling will improve speed. // override it with the correct values if not then early z culling will improve speed.
// There are two different ways to do this, when the depth range is oversized, we process // There are two different ways to do this, when the depth range is oversized, we process
// the depth range in the vertex shader, if not we let the host driver handle it. // the depth range in the vertex shader, if not we let the host driver handle it.
if (uid_data->vertex_depth) //
{ // Adjust z for the depth range. We're using an equation which incorperates a depth inversion,
// Adjust z for the depth range. We're using an equation which incorperates a depth inversion, // so we can map the console -1..0 range to the 0..1 range used in the depth buffer.
// so we can map the console -1..0 range to the 0..1 range used in the depth buffer. // We have to handle the depth range in the vertex shader instead of after the perspective
// We have to handle the depth range in the vertex shader instead of after the perspective // divide, because some games will use a depth range larger than what is allowed by the
// divide, because some games will use a depth range larger than what is allowed by the // graphics API. These large depth ranges will still be clipped to the 0..1 range, so these
// graphics API. These large depth ranges will still be clipped to the 0..1 range, so these // games effectively add a depth bias to the values written to the depth buffer.
// games effectively add a depth bias to the values written to the depth buffer. out.Write("o.pos.z = o.pos.w * " I_PIXELCENTERCORRECTION ".w - "
out.Write("o.pos.z = o.pos.w * " I_PIXELCENTERCORRECTION ".w - " "o.pos.z * " I_PIXELCENTERCORRECTION ".z;\n");
"o.pos.z * " I_PIXELCENTERCORRECTION ".z;\n");
}
else
{
// Here we rely on the host driver to process the depth range, however we still need to invert
// the console -1..0 range to the 0..1 range used in the depth buffer.
out.Write("o.pos.z = -o.pos.z;\n");
}
if (!g_ActiveConfig.backend_info.bSupportsClipControl) if (!g_ActiveConfig.backend_info.bSupportsClipControl)
{ {

View File

@ -43,8 +43,7 @@ struct vertex_shader_uid_data
u32 texMtxInfo_n_projection : 16; // Stored separately to guarantee that the texMtxInfo struct is u32 texMtxInfo_n_projection : 16; // Stored separately to guarantee that the texMtxInfo struct is
// 8 bits wide // 8 bits wide
u32 ssaa : 1; u32 ssaa : 1;
u32 vertex_depth : 1; u32 pad : 15;
u32 pad : 14;
struct struct
{ {

View File

@ -387,30 +387,41 @@ void VertexShaderManager::SetConstants()
constants.pixelcentercorrection[0] = pixel_center_correction * pixel_size_x; constants.pixelcentercorrection[0] = pixel_center_correction * pixel_size_x;
constants.pixelcentercorrection[1] = pixel_center_correction * pixel_size_y; constants.pixelcentercorrection[1] = pixel_center_correction * pixel_size_y;
// The depth range is handled in the vertex shader. We need to reverse if (g_ActiveConfig.backend_info.bSupportsDepthClamp &&
// the far value to get a reversed depth range mapping. This is necessary ((fabs(xfmem.viewport.zRange) > 16777215.0f || fabs(xfmem.viewport.farZ) > 16777215.0f) ||
// because the standard depth range equation pushes all depth values towards (xfmem.viewport.zRange < 0.0f &&
// the back of the depth buffer where conventionally depth buffers have the !g_ActiveConfig.backend_info.bSupportsReversedDepthRange)))
// least precision.
if (g_ActiveConfig.backend_info.bSupportsReversedDepthRange)
{ {
// For backends that support reversing the depth range we also support cases // The depth range is handled in the vertex shader. We need to reverse
// where the console also uses reversed depth with the same accuracy. We need // the far value to get a reversed depth range mapping. This is necessary
// to make sure the depth range is positive here and then reverse the depth in // because the standard depth range equation pushes all depth values towards
// the backend viewport. // the back of the depth buffer where conventionally depth buffers have the
constants.pixelcentercorrection[2] = fabs(xfmem.viewport.zRange) / 16777215.0f; // least precision.
if (xfmem.viewport.zRange < 0.0f) if (g_ActiveConfig.backend_info.bSupportsReversedDepthRange)
constants.pixelcentercorrection[3] = xfmem.viewport.farZ / 16777215.0f; {
// For backends that support reversing the depth range we also support cases
// where the console also uses reversed depth with the same accuracy. We need
// to make sure the depth range is positive here and then reverse the depth in
// the backend viewport.
constants.pixelcentercorrection[2] = fabs(xfmem.viewport.zRange) / 16777215.0f;
if (xfmem.viewport.zRange < 0.0f)
constants.pixelcentercorrection[3] = xfmem.viewport.farZ / 16777215.0f;
else
constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f;
}
else else
{
// For backends that don't support reversing the depth range we can still render
// cases where the console uses reversed depth correctly. But we simply can't
// provide the same accuracy as the console.
constants.pixelcentercorrection[2] = xfmem.viewport.zRange / 16777215.0f;
constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f; constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f;
}
} }
else else
{ {
// For backends that don't support reversing the depth range we can still render constants.pixelcentercorrection[2] = 1.0f;
// cases where the console uses reversed depth correctly. But we simply can't constants.pixelcentercorrection[3] = 0.0f;
// provide the same accuracy as the console.
constants.pixelcentercorrection[2] = xfmem.viewport.zRange / 16777215.0f;
constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f;
} }
dirty = true; dirty = true;