Skip to content

Commit 8c211b1

Browse files
kkinnunen-appleAngle LUCI CQ
authored andcommitted
Metal: Clear depth pbuffers to 1 for robust init
Robust resource initialization would init depth pbuffers as 0.0 instead of 1.0. This would differ from the FBO initialization, where the uninitialized depth buffers would be initialized to 1.0. Bug: angleproject:348199042 Change-Id: I0f251e85d6e08ce992fe323b257a0906d237f5b1 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5642763 Reviewed-by: Geoff Lang <[email protected]> Commit-Queue: Kimmo Kinnunen <[email protected]> Auto-Submit: Kimmo Kinnunen <[email protected]> Reviewed-by: Quyen Le <[email protected]>
1 parent e2afdd3 commit 8c211b1

File tree

8 files changed

+72
-23
lines changed

8 files changed

+72
-23
lines changed

extensions/EGL_ANGLE_robust_resource_initialization.txt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ Additions to the EGL 1.5 Specification
7878
Add a new paragrah to section 3.5.1 "Creating On-Screen Rendering
7979
Surfaces" and section 3.5.2 "Creating Off-Screen Rendering Surfaces":
8080

81-
"EGL_ROBUST_RESOURCE_INITIALIZATION_ANGLE specifies if the pixel
82-
values of the surface are initialized. If its value is EGL_TRUE then all
83-
pixel values in the surface are initialized to zero, otherwise the contents
84-
are undefined. The default value of
85-
EGL_ROBUST_RESOURCE_INITIALIZATION_ANGLE is EGL_FALSE."
81+
"EGL_ROBUST_RESOURCE_INITIALIZATION_ANGLE specifies if the pixel values of
82+
the surface are initialized. If its value is EGL_TRUE then all pixel and
83+
stencil values in the surface are initialized to zero, depth values are
84+
initialized to 1.0. Otherwise the contents are undefined. The default value
85+
of EGL_ROBUST_RESOURCE_INITIALIZATION_ANGLE is EGL_FALSE."
8686

8787
Add a new paragrah to section 3.5.6 "Surface Attributes" under
8888
eglQuerySurface:
@@ -92,10 +92,15 @@ Additions to the EGL 1.5 Specification
9292

9393
Modify the 2nd paragraph of section 3.10.1 "Posting to a Window":
9494

95-
"... The contents of the color buffer are undefined if the value of the
96-
EGL_SWAP_BEHAVIOR attribute of surface is not EGL_BUFFER_PRESERVED and
97-
the value of the EGL_ROBUST_RESOURCE_INITIALIZATION_ANGLE is
98-
EGL_FALSE. ..."
95+
"... If EGL_ROBUST_RESOURCE_INITIALIZATION_ANGLE is EGL_TRUE, following
96+
conditions apply. If EGL_SWAP_BEHAVIOR attribute is not
97+
EGL_BUFFER_PRESERVED, color and stencil buffer values are set to zero, depth
98+
buffer values are set to 1.0. Otherwise, color and ancillary buffer values
99+
preserved.
100+
101+
If EGL_ROBUST_RESOURCE_INITIALIZATION_ANGLE is EGL_FALSE, the contents of
102+
ancillary buffers are always undefined after calling eglSwapBuffers. ...
103+
"
99104

100105
Issues
101106

@@ -118,4 +123,6 @@ Revision History
118123
Version 2, 2017/03/01 - renamed extension and enum.
119124
Version 3, 2017/05/31 - renamed extension and made into display extension.
120125
Version 4, 2017/09/19 - renamed extension and changed into a
121-
context/surface creation attribute.
126+
context/surface creation attribute.
127+
Version 5, 2024/09/21 - specified that depth values are initialized to 1.0
128+
specified that stencil values are initialized to 0.0

src/libANGLE/renderer/metal/IOSurfaceSurfaceMtl.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ int FindIOSurfaceFormatIndex(GLenum internalFormat, GLenum type)
199199
// Disable subsequent rendering to alpha channel.
200200
mColorTexture->setColorWritableMask(MTLColorWriteMaskAll & (~MTLColorWriteMaskAlpha));
201201
}
202+
// Robust resource init: currently we do not allow passing contents with IOSurfaces.
203+
mColorTextureInitialized = false;
202204

203205
return angle::Result::Continue;
204206
}

src/libANGLE/renderer/metal/SurfaceMtl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ class SurfaceMtl : public SurfaceImpl
111111
bool mAutoResolveMSColorTexture = false;
112112

113113
bool mRobustResourceInit = false;
114-
bool mContentInitialized = false;
114+
bool mColorTextureInitialized = true;
115+
bool mDepthStencilTexturesInitialized = true;
115116

116117
mtl::Format mColorFormat;
117118
mtl::Format mDepthFormat;

src/libANGLE/renderer/metal/SurfaceMtl.mm

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,6 @@
254254
{
255255
ASSERT(mColorTexture);
256256

257-
if (mContentInitialized)
258-
{
259-
return angle::Result::Continue;
260-
}
261-
262257
ContextMtl *contextMtl = mtl::GetImpl(context);
263258

264259
// Use loadAction=clear
@@ -269,17 +264,26 @@
269264
{
270265
case GL_BACK:
271266
{
267+
if (mColorTextureInitialized)
268+
{
269+
return angle::Result::Continue;
270+
}
272271
rpDesc.numColorAttachments = 1;
273272
mColorRenderTarget.toRenderPassAttachmentDesc(&rpDesc.colorAttachments[0]);
274273
rpDesc.colorAttachments[0].loadAction = MTLLoadActionClear;
275274
MTLClearColor black = {};
276275
rpDesc.colorAttachments[0].clearColor =
277276
mtl::EmulatedAlphaClearColor(black, mColorTexture->getColorWritableMask());
277+
mColorTextureInitialized = true;
278278
break;
279279
}
280280
case GL_DEPTH:
281281
case GL_STENCIL:
282282
{
283+
if (mDepthStencilTexturesInitialized)
284+
{
285+
return angle::Result::Continue;
286+
}
283287
if (mDepthTexture)
284288
{
285289
mDepthRenderTarget.toRenderPassAttachmentDesc(&rpDesc.depthAttachment);
@@ -290,6 +294,7 @@
290294
mStencilRenderTarget.toRenderPassAttachmentDesc(&rpDesc.stencilAttachment);
291295
rpDesc.stencilAttachment.loadAction = MTLLoadActionClear;
292296
}
297+
mDepthStencilTexturesInitialized = true;
293298
break;
294299
}
295300
default:
@@ -298,7 +303,6 @@
298303
}
299304
mtl::RenderCommandEncoder *encoder = contextMtl->getRenderPassCommandEncoder(rpDesc);
300305
encoder->setStoreAction(MTLStoreActionStore);
301-
mContentInitialized = true;
302306

303307
return angle::Result::Continue;
304308
}
@@ -374,6 +378,8 @@
374378
/** renderTargetOnly */ false, &mDepthTexture));
375379

376380
mDepthRenderTarget.set(mDepthTexture, mtl::kZeroNativeMipLevel, 0, mDepthFormat);
381+
// Robust resource init: should initialize depth to 1.0.
382+
mDepthStencilTexturesInitialized = false;
377383
}
378384

379385
if (mStencilFormat.valid() && (!mStencilTexture || mStencilTexture->sizeAt0() != size))
@@ -390,6 +396,8 @@
390396
}
391397

392398
mStencilRenderTarget.set(mStencilTexture, mtl::kZeroNativeMipLevel, 0, mStencilFormat);
399+
// Robust resource init: should initialize stencil to zero.
400+
mDepthStencilTexturesInitialized = false;
393401
}
394402

395403
return angle::Result::Continue;
@@ -671,7 +679,6 @@
671679
mMetalLayer.get().allowsNextDrawableTimeout = NO;
672680
mCurrentDrawable.retainAssign([mMetalLayer nextDrawable]);
673681
mMetalLayer.get().allowsNextDrawableTimeout = YES;
674-
mContentInitialized = false;
675682
}
676683

677684
if (!mColorTexture)
@@ -685,6 +692,7 @@
685692
{
686693
mColorTexture->set(mCurrentDrawable.get().texture);
687694
}
695+
mColorTextureInitialized = false;
688696

689697
ANGLE_MTL_LOG("Current metal drawable size=%d,%d", mColorTexture->width(),
690698
mColorTexture->height());
@@ -718,7 +726,11 @@
718726
mColorTexture->set(nil);
719727
mCurrentDrawable = nil;
720728
}
721-
729+
// Robust resource init: should initialize stencil zero and depth to 1.0 after swap.
730+
if (mDepthTexture || mStencilTexture)
731+
{
732+
mDepthStencilTexturesInitialized = false;
733+
}
722734
return angle::Result::Continue;
723735
}
724736

src/libANGLE/renderer/metal/mtl_state_cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ struct RenderPassDepthAttachmentDesc : public RenderPassAttachmentDesc
349349
return !(*this == other);
350350
}
351351

352-
double clearDepth = 0;
352+
double clearDepth = 1.0;
353353
};
354354

355355
struct RenderPassStencilAttachmentDesc : public RenderPassAttachmentDesc

src/libANGLE/renderer/metal/mtl_utils.mm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,6 @@ bool PreferStagedTextureUploads(const gl::Context *context,
659659
{
660660
rtMTL.toRenderPassAttachmentDesc(&rpDesc.depthAttachment);
661661
rpDesc.depthAttachment.loadAction = MTLLoadActionClear;
662-
rpDesc.depthAttachment.clearDepth = 1.0;
663662
}
664663
if (angleFormat.stencilBits)
665664
{

src/tests/angle_end2end_tests_expectations.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,6 @@ b/273271471 WIN INTEL VULKAN : ShaderAlgorithmTest.rgb_to_hsl_vertex_shader/* =
378378
40644761 MAC METAL NVIDIA : OcclusionQueriesTest.CopyNotCounted/* = SKIP
379379
40644783 MAC METAL NVIDIA : OcclusionQueriesTestES3.BlitNotCounted/* = SKIP
380380

381-
40644831 MAC METAL AMD : RobustResourceInitTest.SurfaceInitializedAfterSwap/* = SKIP
382-
383381
40644762 MAC METAL AMD : SRGBTextureTestES3.GenerateMipmaps/* = SKIP
384382

385383
40644833 MAC METAL NVIDIA : Texture2DBaseMaxTestES3.Fuzz545ImmutableTexRenderFeedback/* = SKIP
@@ -501,6 +499,7 @@ b/273271471 WIN INTEL VULKAN : ShaderAlgorithmTest.rgb_to_hsl_vertex_shader/* =
501499
42264513 PIXEL4ORXL GLES : MipmapTest.TextureCubeRenderToLevelZero/* = SKIP
502500
42264513 PIXEL4ORXL GLES : PbufferTest.ClearAndBindTexImageSrgb/* = SKIP
503501
42264513 PIXEL4ORXL GLES : ReadPixelsPBOTest.ExistingDataPreserved/* = SKIP
502+
42264981 PIXEL4ORXL GLES : RobustResourceInitTestES3.CheckDefaultDepthStencilRenderbufferIsCleared/* = SKIP
504503
42264981 PIXEL4ORXL GLES : RobustResourceInitTestES3.CheckDepthStencilRenderbufferIsCleared/* = SKIP
505504
42264981 PIXEL4ORXL GLES : RobustResourceInitTestES3.CheckMultisampleDepthStencilRenderbufferIsCleared/* = SKIP
506505
42264513 PIXEL4ORXL GLES : SimpleStateChangeTestES31.ClearThenSampleWithCompute/* = SKIP

src/tests/gl_tests/RobustResourceInitTest.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,35 @@ TEST_P(RobustResourceInitTestES3, CheckDepthStencilRenderbufferIsCleared)
24002400
ASSERT_GL_NO_ERROR();
24012401
}
24022402

2403+
// Test that default framebuffer depth and stencil are cleared to values that
2404+
// are consistent with non-default framebuffer clear values. Depth 1.0, stencil 0.0.
2405+
TEST_P(RobustResourceInitTestES3, CheckDefaultDepthStencilRenderbufferIsCleared)
2406+
{
2407+
ANGLE_SKIP_TEST_IF(!hasRobustSurfaceInit());
2408+
2409+
// Render a quad at Z = 1.0 with depth test on and depth function set to GL_EQUAL.
2410+
// If the depth buffer is not cleared to 1.0 this will fail
2411+
glEnable(GL_DEPTH_TEST);
2412+
glDepthFunc(GL_EQUAL);
2413+
2414+
ANGLE_GL_PROGRAM(drawGreen, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
2415+
drawQuad(drawGreen, essl1_shaders::PositionAttrib(), 1.0f, 1.0f, true);
2416+
EXPECT_PIXEL_RECT_EQ(0, 0, kWidth, kHeight, GLColor::green);
2417+
2418+
// Render with stencil test on and stencil function set to GL_EQUAL
2419+
// If the stencil is not zero this will fail.
2420+
glDisable(GL_DEPTH_TEST);
2421+
glEnable(GL_STENCIL_TEST);
2422+
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2423+
glStencilFunc(GL_EQUAL, 0, 0xFF);
2424+
2425+
ANGLE_GL_PROGRAM(drawRed, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
2426+
drawQuad(drawRed, essl1_shaders::PositionAttrib(), 1.0f, 1.0f, true);
2427+
EXPECT_PIXEL_RECT_EQ(0, 0, kWidth, kHeight, GLColor::red);
2428+
2429+
ASSERT_GL_NO_ERROR();
2430+
}
2431+
24032432
TEST_P(RobustResourceInitTestES3, CheckMultisampleDepthStencilRenderbufferIsCleared)
24042433
{
24052434
ANGLE_SKIP_TEST_IF(!hasRobustSurfaceInit());

0 commit comments

Comments
 (0)