Skip to content

Commit e7109e7

Browse files
committed
Reland "WIP: Optimize default framebuffer's drawable obtaining step"
Defer the actual obtaining until first framebuffer's drawing/reading. Change-Id: I1bd658cc5350cf52d6462016399455de57702ccf
1 parent ab8ca84 commit e7109e7

14 files changed

+246
-210
lines changed

src/libANGLE/renderer/metal/ContextMtl.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class DisplayMtl;
2727
class FramebufferMtl;
2828
class VertexArrayMtl;
2929
class ProgramMtl;
30+
class RenderTargetMtl;
3031

3132
class ContextMtl : public ContextImpl, public mtl::Context
3233
{
@@ -231,25 +232,20 @@ class ContextMtl : public ContextImpl, public mtl::Context
231232

232233
// Check whether compatible render pass has been started.
233234
bool hasStartedRenderPass(const mtl::RenderPassDesc &desc);
234-
bool hasStartedRenderPass(FramebufferMtl *framebuffer);
235235

236236
// Get current render encoder. May be nullptr if no render pass has been started.
237237
mtl::RenderCommandEncoder *getRenderCommandEncoder();
238238

239-
mtl::RenderCommandEncoder *getCurrentFramebufferRenderCommandEncoder();
240-
241239
// Will end current command encoder if it is valid, then start new encoder.
242240
// Unless hasStartedRenderPass(desc) returns true.
243241
mtl::RenderCommandEncoder *getRenderCommandEncoder(const mtl::RenderPassDesc &desc);
244242

245-
// Utilities to quickly create render command enconder to a specific texture:
243+
// Utilities to quickly create render command encoder to a specific texture:
246244
// The previous content of texture will be loaded if clearColor is not provided
247-
mtl::RenderCommandEncoder *getRenderCommandEncoder(const mtl::TextureRef &textureTarget,
248-
const gl::ImageIndex &index,
245+
mtl::RenderCommandEncoder *getRenderCommandEncoder(const RenderTargetMtl &renderTarget,
249246
const Optional<MTLClearColor> &clearColor);
250247
// The previous content of texture will be loaded
251-
mtl::RenderCommandEncoder *getRenderCommandEncoder(const mtl::TextureRef &textureTarget,
252-
const gl::ImageIndex &index);
248+
mtl::RenderCommandEncoder *getRenderCommandEncoder(const RenderTargetMtl &renderTarget);
253249

254250
// Will end current command encoder and start new blit command encoder. Unless a blit comamnd
255251
// encoder is already started.

src/libANGLE/renderer/metal/ContextMtl.mm

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@
728728
// Framebuffer creation
729729
FramebufferImpl *ContextMtl::createFramebuffer(const gl::FramebufferState &state)
730730
{
731-
return new FramebufferMtl(state, false);
731+
return new FramebufferMtl(state, false, nullptr);
732732
}
733733

734734
// Texture creation
@@ -1003,7 +1003,7 @@
10031003
ensureCommandBufferValid();
10041004

10051005
// Always discard default FBO's depth stencil buffers at the end of the frame:
1006-
if (mDrawFramebufferIsDefault && hasStartedRenderPass(mDrawFramebuffer))
1006+
if (mDrawFramebufferIsDefault && mDrawFramebuffer->renderPassHasStarted(this))
10071007
{
10081008
constexpr GLenum dsAttachments[] = {GL_DEPTH, GL_STENCIL};
10091009
(void)mDrawFramebuffer->invalidate(context, 2, dsAttachments);
@@ -1037,11 +1037,6 @@
10371037
mRenderEncoder.renderPassDesc().equalIgnoreLoadStoreOptions(desc);
10381038
}
10391039

1040-
bool ContextMtl::hasStartedRenderPass(FramebufferMtl *framebuffer)
1041-
{
1042-
return framebuffer && hasStartedRenderPass(framebuffer->getRenderPassDesc(this));
1043-
}
1044-
10451040
// Get current render encoder
10461041
mtl::RenderCommandEncoder *ContextMtl::getRenderCommandEncoder()
10471042
{
@@ -1053,16 +1048,6 @@
10531048
return &mRenderEncoder;
10541049
}
10551050

1056-
mtl::RenderCommandEncoder *ContextMtl::getCurrentFramebufferRenderCommandEncoder()
1057-
{
1058-
if (!mDrawFramebuffer)
1059-
{
1060-
return nullptr;
1061-
}
1062-
1063-
return getRenderCommandEncoder(mDrawFramebuffer->getRenderPassDesc(this));
1064-
}
1065-
10661051
mtl::RenderCommandEncoder *ContextMtl::getRenderCommandEncoder(const mtl::RenderPassDesc &desc)
10671052
{
10681053
if (hasStartedRenderPass(desc))
@@ -1080,36 +1065,31 @@
10801065
return &mRenderEncoder.restart(desc);
10811066
}
10821067

1083-
// Utilities to quickly create render command enconder to a specific texture:
1068+
// Utilities to quickly create render command encoder to a specific texture:
10841069
// The previous content of texture will be loaded if clearColor is not provided
10851070
mtl::RenderCommandEncoder *ContextMtl::getRenderCommandEncoder(
1086-
const mtl::TextureRef &textureTarget,
1087-
const gl::ImageIndex &index,
1071+
const RenderTargetMtl &renderTarget,
10881072
const Optional<MTLClearColor> &clearColor)
10891073
{
1090-
ASSERT(textureTarget && textureTarget->valid());
1074+
ASSERT(renderTarget.getTexture());
10911075

10921076
mtl::RenderPassDesc rpDesc;
1093-
1094-
rpDesc.colorAttachments[0].texture = textureTarget;
1095-
rpDesc.colorAttachments[0].level = index.getLevelIndex();
1096-
rpDesc.colorAttachments[0].slice = index.hasLayer() ? index.getLayerIndex() : 0;
1097-
rpDesc.numColorAttachments = 1;
1077+
renderTarget.toRenderPassAttachmentDesc(&rpDesc.colorAttachments[0]);
1078+
rpDesc.numColorAttachments = 1;
10981079

10991080
if (clearColor.valid())
11001081
{
11011082
rpDesc.colorAttachments[0].loadAction = MTLLoadActionClear;
1102-
rpDesc.colorAttachments[0].clearColor =
1103-
mtl::EmulatedAlphaClearColor(clearColor.value(), textureTarget->getColorWritableMask());
1083+
rpDesc.colorAttachments[0].clearColor = mtl::EmulatedAlphaClearColor(
1084+
clearColor.value(), renderTarget.getTexture()->getColorWritableMask());
11041085
}
11051086

11061087
return getRenderCommandEncoder(rpDesc);
11071088
}
11081089
// The previous content of texture will be loaded
1109-
mtl::RenderCommandEncoder *ContextMtl::getRenderCommandEncoder(const mtl::TextureRef &textureTarget,
1110-
const gl::ImageIndex &index)
1090+
mtl::RenderCommandEncoder *ContextMtl::getRenderCommandEncoder(const RenderTargetMtl &renderTarget)
11111091
{
1112-
return getRenderCommandEncoder(textureTarget, index, Optional<MTLClearColor>());
1092+
return getRenderCommandEncoder(renderTarget, Optional<MTLClearColor>());
11131093
}
11141094

11151095
mtl::BlitCommandEncoder *ContextMtl::getBlitCommandEncoder()
@@ -1354,8 +1334,7 @@
13541334
if (mDirtyBits.test(DIRTY_BIT_DRAW_FRAMEBUFFER))
13551335
{
13561336
// Start new render command encoder
1357-
const mtl::RenderPassDesc &rpDesc = mDrawFramebuffer->getRenderPassDesc(this);
1358-
ANGLE_MTL_TRY(this, getRenderCommandEncoder(rpDesc));
1337+
ANGLE_MTL_TRY(this, mDrawFramebuffer->ensureRenderPassStarted(context));
13591338

13601339
// re-apply everything
13611340
invalidateState(context);
@@ -1552,15 +1531,15 @@
15521531

15531532
// Need to handle the case when render pass doesn't have depth/stencil attachment.
15541533
mtl::DepthStencilDesc dsDesc = mDepthStencilDesc;
1555-
const mtl::RenderPassDesc &renderPassDesc = mDrawFramebuffer->getRenderPassDesc(this);
1534+
const mtl::RenderPassDesc &renderPassDesc = mRenderEncoder.renderPassDesc();
15561535

1557-
if (!renderPassDesc.depthAttachment.texture)
1536+
if (!renderPassDesc.depthAttachment.texture())
15581537
{
15591538
dsDesc.depthWriteEnabled = false;
15601539
dsDesc.depthCompareFunction = MTLCompareFunctionAlways;
15611540
}
15621541

1563-
if (!renderPassDesc.stencilAttachment.texture)
1542+
if (!renderPassDesc.stencilAttachment.texture())
15641543
{
15651544
dsDesc.frontFaceStencil.reset();
15661545
dsDesc.backFaceStencil.reset();
@@ -1606,7 +1585,7 @@
16061585

16071586
if (rppChange)
16081587
{
1609-
const mtl::RenderPassDesc &renderPassDesc = mDrawFramebuffer->getRenderPassDesc(this);
1588+
const mtl::RenderPassDesc &renderPassDesc = mRenderEncoder.renderPassDesc();
16101589
// Obtain RenderPipelineDesc's output descriptor.
16111590
renderPassDesc.populateRenderPipelineOutputDesc(mBlendDesc,
16121591
&mRenderPipelineDesc.outputDescriptor);

src/libANGLE/renderer/metal/FrameBufferMtl.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818

1919
namespace rx
2020
{
21+
namespace mtl
22+
{
23+
class RenderCommandEncoder;
24+
}
2125
class ContextMtl;
2226
class SurfaceMtl;
2327

2428
class FramebufferMtl : public FramebufferImpl
2529
{
2630
public:
27-
explicit FramebufferMtl(const gl::FramebufferState &state, bool flipY);
31+
explicit FramebufferMtl(const gl::FramebufferState &state, bool flipY, SurfaceMtl *backbuffer);
2832
~FramebufferMtl() override;
2933
void destroy(const gl::Context *context) override;
3034

@@ -81,19 +85,20 @@ class FramebufferMtl : public FramebufferImpl
8185
size_t index,
8286
GLfloat *xy) const override;
8387

84-
RenderTargetMtl *getColorReadRenderTarget() const;
88+
RenderTargetMtl *getColorReadRenderTarget(const gl::Context *context) const;
8589

8690
bool flipY() const { return mFlipY; }
8791

8892
gl::Rectangle getCompleteRenderArea() const;
8993

90-
const mtl::RenderPassDesc &getRenderPassDesc(ContextMtl *context);
94+
bool renderPassHasStarted(ContextMtl *contextMtl) const;
95+
mtl::RenderCommandEncoder *ensureRenderPassStarted(const gl::Context *context);
9196

9297
// Call this to notify FramebufferMtl whenever its render pass has started.
9398
void onStartedDrawingToFrameBuffer(const gl::Context *context);
9499

95100
// The actual area will be adjusted based on framebuffer flipping property.
96-
gl::Rectangle getReadPixelArea(const gl::Rectangle &glArea);
101+
gl::Rectangle getReadPixelArea(const gl::Context *context, const gl::Rectangle &glArea);
97102

98103
// NOTE: this method doesn't do the flipping of area. Caller must do it if needed before
99104
// callling this. See getReadPixelsArea().
@@ -122,6 +127,9 @@ class FramebufferMtl : public FramebufferImpl
122127
gl::DrawBufferMask drawColorBuffers,
123128
mtl::RenderPassDesc *descOut);
124129

130+
mtl::RenderCommandEncoder *ensureRenderPassStarted(const gl::Context *context,
131+
const mtl::RenderPassDesc &desc);
132+
125133
void overrideClearColor(const mtl::TextureRef &texture,
126134
MTLClearColor clearColor,
127135
MTLClearColor *colorOut);
@@ -140,7 +148,9 @@ class FramebufferMtl : public FramebufferImpl
140148
RenderTargetMtl *mDepthRenderTarget = nullptr;
141149
RenderTargetMtl *mStencilRenderTarget = nullptr;
142150
mtl::RenderPassDesc mRenderPassDesc;
143-
const bool mFlipY = false;
151+
152+
SurfaceMtl *mBackbuffer = nullptr;
153+
const bool mFlipY = false;
144154
};
145155
} // namespace rx
146156

0 commit comments

Comments
 (0)