Add depth fix to convert from GL to Metal

Vertex shaders need to convert from GL's -1 to +1 z range
to Metal's 0 to +1 z range with:

    z = (z + w) * 0.5

This code is being upstreamed from WebKit.

It fixes these end to end tests.

BlitFramebufferTest.MultisampleDepthClear/ES3_Metal
BlitFramebufferTest.MultisampleDepthClear/ES3_Metal_NoStencilOutput
BlitFramebufferTest.BlitDepthStencilPixelByPixel/ES3_Metal
BlitFramebufferTest.BlitDepthStencilPixelByPixel/ES3_Metal_NoStencilOutput
ClearTest.ClearStencilMask/ES2_Metal
ClearTest.ClearStencilMask/ES3_Metal
ClearTestES3.ClearMultipleAttachmentsIndividually/ES3_Metal
ClearTestES3.MaskedClearHeterogeneousAttachments/ES3_Metal
ClearTestES3.ScissoredClearHeterogeneousAttachments/ES3_Metal
ClearTestES3.ClearBufferivStencilMask/ES3_Metal
ClearTestES3.ClearBufferfiNoStencilAttachment/ES3_Metal
MaskedScissoredClearTest.Test/ES2_Metal__clear_s
MaskedScissoredClearTest.Test/ES2_Metal__clear_s_mask_s
MaskedScissoredClearTest.Test/ES2_Metal__scissored_clear_s_mask_s
MaskedScissoredClearTest.Test/ES2_Metal__clear_d
MaskedScissoredClearTest.Test/ES2_Metal__scissored_clear_d
MaskedScissoredClearTest.Test/ES2_Metal__clear_ds
MaskedScissoredClearTest.Test/ES2_Metal__scissored_clear_ds
MaskedScissoredClearTest.Test/ES2_Metal__clear_ds_mask_s
MaskedScissoredClearTest.Test/ES2_Metal__scissored_clear_ds_mask_s
MaskedScissoredClearTest.Test/ES2_Metal__clear_cs
MaskedScissoredClearTest.Test/ES2_Metal__clear_cs_mask_s
MaskedScissoredClearTest.Test/ES2_Metal__scissored_clear_cs_mask_s
MaskedScissoredClearTest.Test/ES2_Metal__clear_cd
MaskedScissoredClearTest.Test/ES2_Metal__scissored_clear_cd
MaskedScissoredClearTest.Test/ES2_Metal__clear_cds
MaskedScissoredClearTest.Test/ES2_Metal__scissored_clear_cds
MaskedScissoredClearTest.Test/ES2_Metal__clear_cds_mask_s
MaskedScissoredClearTest.Test/ES2_Metal__scissored_clear_cds_mask_s
MaskedScissoredClearTest.Test/ES2_Metal__clear_cs_mask_cs
MaskedScissoredClearTest.Test/ES2_Metal__scissored_clear_cs_mask_cs
MaskedScissoredClearTest.Test/ES2_Metal__clear_cd_mask_c
MaskedScissoredClearTest.Test/ES2_Metal__scissored_clear_cd_mask_c
MaskedScissoredClearTest.Test/ES2_Metal__clear_cds_mask_c
MaskedScissoredClearTest.Test/ES2_Metal__scissored_clear_cds_mask_c
MaskedScissoredClearTest.Test/ES2_Metal__clear_cds_mask_cs
MaskedScissoredClearTest.Test/ES2_Metal__scissored_clear_cds_mask_cs
MaskedScissoredClearTest.Test/ES3_Metal__clear_s
MaskedScissoredClearTest.Test/ES3_Metal__clear_s_mask_s
MaskedScissoredClearTest.Test/ES3_Metal__scissored_clear_s_mask_s
MaskedScissoredClearTest.Test/ES3_Metal__clear_d
MaskedScissoredClearTest.Test/ES3_Metal__scissored_clear_d
MaskedScissoredClearTest.Test/ES3_Metal__clear_ds
MaskedScissoredClearTest.Test/ES3_Metal__scissored_clear_ds
MaskedScissoredClearTest.Test/ES3_Metal__clear_ds_mask_s
MaskedScissoredClearTest.Test/ES3_Metal__scissored_clear_ds_mask_s
MaskedScissoredClearTest.Test/ES3_Metal__clear_cs
MaskedScissoredClearTest.Test/ES3_Metal__clear_cs_mask_s
MaskedScissoredClearTest.Test/ES3_Metal__scissored_clear_cs_mask_s
MaskedScissoredClearTest.Test/ES3_Metal__clear_cd
MaskedScissoredClearTest.Test/ES3_Metal__scissored_clear_cd
MaskedScissoredClearTest.Test/ES3_Metal__clear_cds
MaskedScissoredClearTest.Test/ES3_Metal__scissored_clear_cds
MaskedScissoredClearTest.Test/ES3_Metal__clear_cds_mask_s
MaskedScissoredClearTest.Test/ES3_Metal__scissored_clear_cds_mask_s
MaskedScissoredClearTest.Test/ES3_Metal__clear_cs_mask_cs
MaskedScissoredClearTest.Test/ES3_Metal__scissored_clear_cs_mask_cs
MaskedScissoredClearTest.Test/ES3_Metal__clear_cd_mask_c
MaskedScissoredClearTest.Test/ES3_Metal__scissored_clear_cd_mask_c
MaskedScissoredClearTest.Test/ES3_Metal__clear_cds_mask_c
MaskedScissoredClearTest.Test/ES3_Metal__scissored_clear_cds_mask_c
MaskedScissoredClearTest.Test/ES3_Metal__clear_cds_mask_cs
MaskedScissoredClearTest.Test/ES3_Metal__scissored_clear_cds_mask_cs
DepthStencilFormatsTest.DepthTextureRender/ES2_Metal
DepthStencilFormatsTest.DepthBuffer16/ES2_Metal
DepthStencilFormatsTest.DepthBuffer24/ES2_Metal
DepthStencilFormatsTest.VerifyDepth32UploadData/ES2_Metal
DepthStencilFormatsTest.VerifyDepth16UploadData/ES2_Metal
TinyDepthStencilWorkaroundTest.DepthTexturesStick/ES3_Metal
DiscardFramebufferEXTTest.ClearDepthThenDrawWithDepthTestThenDiscard/ES2_Metal
DiscardFramebufferEXTTest.ClearDepthThenDrawWithDepthTestThenDiscard/ES3_Metal

Bug: angleproject:5505
Change-Id: I9b52bb46569678636afc6a243bfa8779f101b6af
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3097161
Reviewed-by: Kenneth Russell <kbr@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Kenneth Russell <kbr@chromium.org>
This commit is contained in:
Gregg Tavares 2021-08-16 16:30:32 -07:00 коммит произвёл Angle LUCI CQ
Родитель 162847ca3a
Коммит 63bb0e5368
2 изменённых файлов: 39 добавлений и 0 удалений

Просмотреть файл

@ -609,6 +609,38 @@ bool TranslatorMetalDirect::transformDepthBeforeCorrection(TIntermBlock *root,
return RunAtTheEndOfShader(this, root, assignment, &getSymbolTable());
}
// This operation performs the viewport depth translation needed by Metal. GL uses a
// clip space z range of -1 to +1 where as Metal uses 0 to 1. The translation becomes
// this expression
//
// z_metal = 0.5 * (w_gl + z_gl)
//
// where z_metal is the depth output of a Metal vertex shader and z_gl is the same for GL.
bool TranslatorMetalDirect::appendVertexShaderDepthCorrectionToMain(TIntermBlock *root)
{
const TVariable *position = BuiltInVariable::gl_Position();
TIntermSymbol *positionRef = new TIntermSymbol(position);
TVector<int> swizzleOffsetZ = {2};
TIntermSwizzle *positionZ = new TIntermSwizzle(positionRef, swizzleOffsetZ);
TIntermConstantUnion *oneHalf = CreateFloatNode(0.5f);
TVector<int> swizzleOffsetW = {3};
TIntermSwizzle *positionW = new TIntermSwizzle(positionRef->deepCopy(), swizzleOffsetW);
// Create the expression "(gl_Position.z + gl_Position.w) * 0.5".
TIntermBinary *zPlusW = new TIntermBinary(EOpAdd, positionZ->deepCopy(), positionW->deepCopy());
TIntermBinary *halfZPlusW = new TIntermBinary(EOpMul, zPlusW, oneHalf->deepCopy());
// Create the assignment "gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5"
TIntermTyped *positionZLHS = positionZ->deepCopy();
TIntermBinary *assignment = new TIntermBinary(TOperator::EOpAssign, positionZLHS, halfZPlusW);
// Append the assignment as a statement at the end of the shader.
return RunAtTheEndOfShader(this, root, assignment, &getSymbolTable());
}
static std::set<ImmutableString> GetMslKeywords()
{
std::set<ImmutableString> keywords;
@ -1062,6 +1094,11 @@ bool TranslatorMetalDirect::translateImpl(TInfoSinkBase &sink,
return false;
}
if (!appendVertexShaderDepthCorrectionToMain(root))
{
return false;
}
if ((compileOptions & SH_ADD_PRE_ROTATION) != 0 &&
!AppendPreRotation(this, root, &getSymbolTable(), specConst, driverUniforms))
{

Просмотреть файл

@ -177,6 +177,8 @@ class TranslatorMetalDirect : public TCompiler
ANGLE_NO_DISCARD bool transformDepthBeforeCorrection(TIntermBlock *root,
const DriverUniform *driverUniforms);
ANGLE_NO_DISCARD bool appendVertexShaderDepthCorrectionToMain(TIntermBlock *root);
ANGLE_NO_DISCARD bool insertSampleMaskWritingLogic(TIntermBlock &root,
DriverUniform &driverUniforms);
ANGLE_NO_DISCARD bool insertRasterizationDiscardLogic(TIntermBlock &root);