Metal: use ResourceProvider for MSAA creation.

Also fixes GPU sizes to use new attachment scheme.

Bug: skia:12186
Change-Id: I4259cd26ac6b41eeda3a6d8bb42e1f283ae9e4d3
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/429197
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
This commit is contained in:
Jim Van Verth 2021-07-16 11:29:58 -04:00 committed by Skia Commit-Bot
parent 226a7cd7c1
commit 3339e570a1
6 changed files with 105 additions and 68 deletions

View File

@ -268,9 +268,7 @@ private:
sk_sp<GrAttachment> makeMSAAAttachment(SkISize dimensions,
const GrBackendFormat& format,
int numSamples,
GrProtected isProtected) override {
return nullptr;
}
GrProtected isProtected) override;
bool createMtlTextureForBackendSurface(MTLPixelFormat,
SkISize dimensions,

View File

@ -563,6 +563,19 @@ sk_sp<GrAttachment> GrMtlGpu::makeStencilAttachment(const GrBackendFormat& /*col
return GrMtlAttachment::GrMtlAttachment::MakeStencil(this, dimensions, numStencilSamples, sFmt);
}
sk_sp<GrAttachment> GrMtlGpu::makeMSAAAttachment(SkISize dimensions,
const GrBackendFormat& format,
int numSamples,
GrProtected /*isProtected*/) {
MTLPixelFormat pixelFormat = (MTLPixelFormat) format.asMtlFormat();
SkASSERT(pixelFormat != MTLPixelFormatInvalid);
SkASSERT(!GrMtlFormatIsCompressed(pixelFormat));
SkASSERT(this->mtlCaps().isFormatRenderable(pixelFormat, numSamples));
fStats.incMSAAAttachmentCreates();
return GrMtlAttachment::MakeMSAA(this, dimensions, numSamples, pixelFormat);
}
sk_sp<GrTexture> GrMtlGpu::onCreateTexture(SkISize dimensions,
const GrBackendFormat& format,
GrRenderable renderable,

View File

@ -39,6 +39,16 @@ public:
GrMtlAttachment* resolveAttachment() const { return fResolveAttachment.get(); }
id<MTLTexture> resolveMTLTexture() const { return fResolveAttachment->mtlTexture(); }
// Returns the GrMtlAttachment of the non-msaa attachment. If the color attachment has 1 sample,
// then the color attachment will be returned. Otherwise, the resolve attachment is returned.
GrMtlAttachment* nonMSAAAttachment() const {
if (fColorAttachment->numSamples() == 1) {
return fColorAttachment.get();
} else {
return fResolveAttachment.get();
}
}
GrBackendRenderTarget getBackendRenderTarget() const override;
GrBackendFormat backendFormat() const override;
@ -54,18 +64,8 @@ protected:
void onAbandon() override;
void onRelease() override;
// This accounts for the texture's memory and any MSAA renderbuffer's memory.
size_t onGpuMemorySize() const override {
int numColorSamples = this->numSamples();
// TODO: When used as render targets certain formats may actually have a larger size than
// the base format size. Check to make sure we are reporting the correct value here.
// The plus 1 is to account for the resolve texture or if not using msaa the RT itself
if (numColorSamples > 1) {
++numColorSamples;
}
return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
numColorSamples, GrMipmapped::kNo);
}
// This returns zero since the memory should all be handled by the attachments
size_t onGpuMemorySize() const override { return 0; }
sk_sp<GrMtlAttachment> fColorAttachment;
sk_sp<GrMtlAttachment> fResolveAttachment;

View File

@ -7,6 +7,8 @@
#include "src/gpu/mtl/GrMtlRenderTarget.h"
#include "src/gpu/GrDirectContextPriv.h"
#include "src/gpu/GrResourceProvider.h"
#include "src/gpu/mtl/GrMtlGpu.h"
#include "src/gpu/mtl/GrMtlUtil.h"
@ -62,13 +64,14 @@ sk_sp<GrMtlRenderTarget> GrMtlRenderTarget::MakeWrappedRenderTarget(GrMtlGpu* gp
if (!gpu->mtlCaps().isFormatRenderable(format, sampleCnt)) {
return nullptr;
}
sk_sp<GrMtlAttachment> colorAttachment = GrMtlAttachment::MakeMSAA(gpu,
dimensions,
sampleCnt,
format);
if (!colorAttachment) {
auto rp = gpu->getContext()->priv().resourceProvider();
sk_sp<GrAttachment> msaaAttachment = rp->makeMSAAAttachment(
dimensions, GrBackendFormat::MakeMtl(format), sampleCnt, GrProtected::kNo);
if (!msaaAttachment) {
return nullptr;
}
sk_sp<GrMtlAttachment> colorAttachment =
sk_sp<GrMtlAttachment>(static_cast<GrMtlAttachment*>(msaaAttachment.release()));
mtlRT = new GrMtlRenderTarget(
gpu, dimensions, std::move(colorAttachment), std::move(textureAttachment),
kWrapped);

View File

@ -58,18 +58,7 @@ private:
GrMipmapStatus,
GrWrapCacheable cacheable);
size_t onGpuMemorySize() const override {
// TODO: When used as render targets certain formats may actually have a larger size than
// the base format size. Check to make sure we are reporting the correct value here.
// The plus 1 is to account for the resolve texture or if not using msaa the RT itself
int numColorSamples = this->numSamples();
if (numColorSamples > 1) {
++numColorSamples;
}
return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
numColorSamples, GrMipmapped::kNo);
}
size_t onGpuMemorySize() const override;
};
#endif

View File

@ -5,6 +5,8 @@
* found in the LICENSE file.
*/
#include "src/gpu/GrDirectContextPriv.h"
#include "src/gpu/GrResourceProvider.h"
#include "src/gpu/mtl/GrMtlGpu.h"
#include "src/gpu/mtl/GrMtlTextureRenderTarget.h"
#include "src/gpu/mtl/GrMtlUtil.h"
@ -43,6 +45,25 @@ GrMtlTextureRenderTarget::GrMtlTextureRenderTarget(GrMtlGpu* gpu,
this->registerWithCacheWrapped(cacheable);
}
bool create_rt_attachments(GrMtlGpu* gpu, SkISize dimensions, MTLPixelFormat format, int sampleCnt,
sk_sp<GrMtlAttachment> texture, sk_sp<GrMtlAttachment>* colorAttachment,
sk_sp<GrMtlAttachment>* resolveAttachment) {
if (sampleCnt > 1) {
auto rp = gpu->getContext()->priv().resourceProvider();
sk_sp<GrAttachment> msaaAttachment = rp->makeMSAAAttachment(
dimensions, GrBackendFormat::MakeMtl(format), sampleCnt, GrProtected::kNo);
if (!msaaAttachment) {
return false;
}
*colorAttachment =
sk_sp<GrMtlAttachment>(static_cast<GrMtlAttachment*>(msaaAttachment.release()));
*resolveAttachment = std::move(texture);
} else {
*colorAttachment = std::move(texture);
}
return true;
}
sk_sp<GrMtlTextureRenderTarget> GrMtlTextureRenderTarget::MakeNewTextureRenderTarget(
GrMtlGpu* gpu,
SkBudgeted budgeted,
@ -51,34 +72,29 @@ sk_sp<GrMtlTextureRenderTarget> GrMtlTextureRenderTarget::MakeNewTextureRenderTa
MTLPixelFormat format,
uint32_t mipLevels,
GrMipmapStatus mipmapStatus) {
sk_sp<GrMtlAttachment> texture =
sk_sp<GrMtlAttachment> textureAttachment =
GrMtlAttachment::MakeTexture(gpu, dimensions, format, mipLevels, GrRenderable::kYes,
/*numSamples=*/1, budgeted);
if (!texture) {
if (!textureAttachment) {
return nullptr;
}
if (@available(macOS 10.11, iOS 9.0, *)) {
SkASSERT((MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget) &
texture->mtlTexture().usage);
textureAttachment->mtlTexture().usage);
}
if (sampleCnt > 1) {
// TODO: create in resourceprovider
sk_sp<GrMtlAttachment> colorAttachment =
GrMtlAttachment::MakeMSAA(gpu, dimensions, sampleCnt, texture->mtlFormat());
if (!colorAttachment) {
return nullptr;
}
sk_sp<GrMtlAttachment> resolveAttachment = texture;
return sk_sp<GrMtlTextureRenderTarget>(new GrMtlTextureRenderTarget(
gpu, budgeted, dimensions, std::move(texture), std::move(colorAttachment),
std::move(resolveAttachment), mipmapStatus));
} else {
sk_sp<GrMtlAttachment> colorAttachment = texture;
return sk_sp<GrMtlTextureRenderTarget>(
new GrMtlTextureRenderTarget(gpu, budgeted, dimensions, std::move(texture),
std::move(colorAttachment), nullptr, mipmapStatus));
sk_sp<GrMtlAttachment> colorAttachment;
sk_sp<GrMtlAttachment> resolveAttachment;
if (!create_rt_attachments(gpu, dimensions, format, sampleCnt, textureAttachment,
&colorAttachment, &resolveAttachment)) {
return nullptr;
}
SkASSERT(colorAttachment);
SkASSERT(sampleCnt == 1 || resolveAttachment);
return sk_sp<GrMtlTextureRenderTarget>(new GrMtlTextureRenderTarget(
gpu, budgeted, dimensions, std::move(textureAttachment), std::move(colorAttachment),
std::move(resolveAttachment), mipmapStatus));
}
sk_sp<GrMtlTextureRenderTarget> GrMtlTextureRenderTarget::MakeWrappedTextureRenderTarget(
@ -102,24 +118,42 @@ sk_sp<GrMtlTextureRenderTarget> GrMtlTextureRenderTarget::MakeWrappedTextureRend
return nullptr;
}
if (sampleCnt > 1) {
// TODO: create in resourceprovider
sk_sp<GrMtlAttachment> colorAttachment =
GrMtlAttachment::MakeMSAA(gpu, dimensions, sampleCnt, texture.pixelFormat);
if (!colorAttachment) {
return nullptr;
}
sk_sp<GrMtlAttachment> resolveAttachment = textureAttachment;
return sk_sp<GrMtlTextureRenderTarget>(new GrMtlTextureRenderTarget(
gpu, dimensions, std::move(textureAttachment), std::move(colorAttachment),
std::move(resolveAttachment), mipmapStatus, cacheable));
} else {
sk_sp<GrMtlAttachment> colorAttachment = textureAttachment;
return sk_sp<GrMtlTextureRenderTarget>(
new GrMtlTextureRenderTarget(gpu, dimensions, std::move(textureAttachment),
std::move(colorAttachment), nullptr, mipmapStatus,
cacheable));
sk_sp<GrMtlAttachment> colorAttachment;
sk_sp<GrMtlAttachment> resolveAttachment;
if (!create_rt_attachments(gpu, dimensions, texture.pixelFormat, sampleCnt, textureAttachment,
&colorAttachment, &resolveAttachment)) {
return nullptr;
}
SkASSERT(colorAttachment);
SkASSERT(sampleCnt == 1 || resolveAttachment);
return sk_sp<GrMtlTextureRenderTarget>(new GrMtlTextureRenderTarget(
gpu, dimensions, std::move(textureAttachment), std::move(colorAttachment),
std::move(resolveAttachment), mipmapStatus, cacheable));
}
size_t GrMtlTextureRenderTarget::onGpuMemorySize() const {
// The GrTexture[RenderTarget] is built up by a bunch of attachments each of which are their
// own GrGpuResource. Ideally the GrRenderTarget would not be a GrGpuResource and the GrTexture
// would just merge with the new GrSurface/Attachment world. Then we could just depend on each
// attachment to give its own size since we don't have GrGpuResources owning other
// GrGpuResources. Until we get to that point we need to live in some hybrid world. We will let
// the msaa and stencil attachments track their own size because they do get cached separately.
// For all GrTexture* based things we will continue to to use the GrTexture* to report size and
// the owned attachments will have no size and be uncached.
#ifdef SK_DEBUG
// The nonMSAA attachment (either color or resolve depending on numSamples should have size of
// zero since it is a texture attachment.
SkASSERT(this->nonMSAAAttachment()->gpuMemorySize() == 0);
if (this->numSamples() > 1) {
// Msaa attachment should have a valid size
SkASSERT(this->colorAttachment()->gpuMemorySize() ==
GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
this->numSamples(), GrMipMapped::kNo));
}
#endif
return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
1 /*colorSamplesPerPixel*/, this->mipmapped());
}
GR_NORETAIN_END