[Metal] Use Shared rather than Managed storageMode for M1 dynamic buffers

Change-Id: I53cb4306a9715f75e7b15f8d368c7d0b4007b7a1
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/458956
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
This commit is contained in:
Jim Van Verth 2021-10-13 12:08:14 -04:00 committed by SkCQ
parent 558aff92f9
commit 3c2160470e
3 changed files with 38 additions and 48 deletions

View File

@ -52,7 +52,11 @@ GrMtlBuffer::GrMtlBuffer(GrMtlGpu* gpu, size_t size, GrGpuBufferType intendedTyp
if (@available(macOS 10.11, iOS 9.0, *)) {
if (fIsDynamic) {
#ifdef SK_BUILD_FOR_MAC
options |= MTLResourceStorageModeManaged;
if (gpu->mtlCaps().isMac()) {
options |= MTLResourceStorageModeManaged;
} else {
options |= MTLResourceStorageModeShared;
}
#else
options |= MTLResourceStorageModeShared;
#endif
@ -60,11 +64,8 @@ GrMtlBuffer::GrMtlBuffer(GrMtlGpu* gpu, size_t size, GrGpuBufferType intendedTyp
options |= MTLResourceStorageModePrivate;
}
}
#ifdef SK_BUILD_FOR_MAC
// Mac requires 4-byte alignment for copies so we need
// to ensure we have space for the extra data
size = SkAlign4(size);
#endif
size = GrAlignTo(size, gpu->mtlCaps().getMinBufferAlignment());
fMtlBuffer = size == 0 ? nil :
[gpu->device() newBufferWithLength: size
options: options];
@ -99,15 +100,8 @@ bool GrMtlBuffer::onUpdateData(const void* src, size_t sizeInBytes) {
} else {
// copy data to gpu buffer
GrStagingBufferManager::Slice slice;
#ifdef SK_BUILD_FOR_MAC
// Mac requires 4-byte alignment for copies
// TODO: true for Apple Silicon?
static const size_t kMinAlignment = 4;
#else
static const size_t kMinAlignment = 1;
#endif
slice = this->mtlGpu()->stagingBufferManager()->allocateStagingBufferSlice(sizeInBytes,
kMinAlignment);
slice = this->mtlGpu()->stagingBufferManager()->allocateStagingBufferSlice(
sizeInBytes, this->mtlGpu()->mtlCaps().getMinBufferAlignment());
if (!slice.fBuffer) {
return false;
}
@ -166,7 +160,9 @@ void GrMtlBuffer::internalUnmap(size_t sizeInBytes) {
SkASSERT(sizeInBytes <= this->size());
SkASSERT(this->isMapped());
#ifdef SK_BUILD_FOR_MAC
[fMtlBuffer didModifyRange: NSMakeRange(0, sizeInBytes)];
if (this->mtlGpu()->mtlCaps().isMac()) {
[fMtlBuffer didModifyRange: NSMakeRange(0, sizeInBytes)];
}
#endif
fMapPtr = nullptr;
}

View File

@ -86,6 +86,11 @@ public:
ProgramDescOverrideFlags) const override;
MTLPixelFormat getStencilPixelFormat(const GrProgramDesc& desc);
bool isMac() const { return fGPUFamily == GPUFamily::kMac; }
bool isApple() const { return fGPUFamily == GPUFamily::kApple; }
size_t getMinBufferAlignment() const { return this->isMac() ? 4 : 1; }
// if true, MTLStoreActionStoreAndMultiplesampleResolve is available
bool storeAndMultisampleResolveSupport() const { return fStoreAndMultisampleResolveSupport; }
@ -184,8 +189,6 @@ private:
kMac,
kApple,
};
bool isMac() { return fGPUFamily == GPUFamily::kMac; }
bool isApple() { return fGPUFamily == GPUFamily::kApple; }
bool getGPUFamily(id<MTLDevice> device, GPUFamily* gpuFamily, int* group);
bool getGPUFamilyFromFeatureSet(id<MTLDevice> device, GrMtlCaps::GPUFamily* gpuFamily,
int* group);

View File

@ -357,12 +357,7 @@ bool GrMtlGpu::uploadToTexture(GrMtlTexture* tex,
// offset value must be a multiple of the destination texture's pixel size in bytes
#ifdef SK_BUILD_FOR_MAC
static const size_t kMinAlignment = 4;
#else
static const size_t kMinAlignment = 1;
#endif
size_t alignment = std::max(bpp, kMinAlignment);
size_t alignment = std::max(bpp, this->mtlCaps().getMinBufferAlignment());
GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
combinedBufferSize, alignment);
if (!slice.fBuffer) {
@ -407,7 +402,9 @@ bool GrMtlGpu::uploadToTexture(GrMtlTexture* tex,
SkDEBUGCODE(layerHeight = currentHeight);
}
#ifdef SK_BUILD_FOR_MAC
[mtlBuffer->mtlBuffer() didModifyRange: NSMakeRange(slice.fOffset, combinedBufferSize)];
if (this->mtlCaps().isMac()) {
[mtlBuffer->mtlBuffer() didModifyRange: NSMakeRange(slice.fOffset, combinedBufferSize)];
}
#endif
#ifdef SK_ENABLE_MTL_DEBUG_INFO
[blitCmdEncoder popDebugGroup];
@ -458,12 +455,7 @@ bool GrMtlGpu::clearTexture(GrMtlTexture* tex, size_t bpp, uint32_t levelMask) {
}
SkASSERT(combinedBufferSize > 0 && !individualMipOffsets.empty());
#ifdef SK_BUILD_FOR_MAC
static const size_t kMinAlignment = 4;
#else
static const size_t kMinAlignment = 1;
#endif
size_t alignment = std::max(bpp, kMinAlignment);
size_t alignment = std::max(bpp, this->mtlCaps().getMinBufferAlignment());
GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
combinedBufferSize, alignment);
if (!slice.fBuffer) {
@ -679,7 +671,9 @@ sk_sp<GrTexture> GrMtlGpu::onCreateCompressedTexture(SkISize dimensions,
std::max(1, levelDimensions.height()/2)};
}
#ifdef SK_BUILD_FOR_MAC
[mtlBuffer->mtlBuffer() didModifyRange: NSMakeRange(slice.fOffset, dataSize)];
if (this->mtlCaps().isMac()) {
[mtlBuffer->mtlBuffer() didModifyRange: NSMakeRange(slice.fOffset, dataSize)];
}
#endif
#ifdef SK_ENABLE_MTL_DEBUG_INFO
[blitCmdEncoder popDebugGroup];
@ -935,12 +929,7 @@ bool GrMtlGpu::onClearBackendTexture(const GrBackendTexture& backendTexture,
// Reuse the same buffer for all levels. Should be ok since we made the row bytes tight.
combinedBufferSize = bytesPerPixel*backendTexture.width()*backendTexture.height();
#ifdef SK_BUILD_FOR_MAC
static const size_t kMinAlignment = 4;
#else
static const size_t kMinAlignment = 1;
#endif
size_t alignment = std::max(bytesPerPixel, kMinAlignment);
size_t alignment = std::max(bytesPerPixel, this->mtlCaps().getMinBufferAlignment());
GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
combinedBufferSize, alignment);
if (!slice.fBuffer) {
@ -995,7 +984,9 @@ bool GrMtlGpu::onClearBackendTexture(const GrBackendTexture& backendTexture,
std::max(1, levelDimensions.height() / 2)};
}
#ifdef SK_BUILD_FOR_MAC
[mtlBuffer->mtlBuffer() didModifyRange: NSMakeRange(slice.fOffset, combinedBufferSize)];
if (this->mtlCaps().isMac()) {
[mtlBuffer->mtlBuffer() didModifyRange: NSMakeRange(slice.fOffset, combinedBufferSize)];
}
#endif
[blitCmdEncoder popDebugGroup];
@ -1045,12 +1036,8 @@ bool GrMtlGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendT
mipMapped == GrMipmapped::kYes);
SkASSERT(individualMipOffsets.count() == numMipLevels);
#ifdef SK_BUILD_FOR_MAC
static const size_t kMinAlignment = 4;
#else
static const size_t kMinAlignment = 1;
#endif
size_t alignment = std::max(SkCompressedBlockSize(compression), kMinAlignment);
size_t alignment = std::max(SkCompressedBlockSize(compression),
this->mtlCaps().getMinBufferAlignment());
GrStagingBufferManager::Slice slice =
fStagingBufferManager.allocateStagingBufferSlice(combinedBufferSize, alignment);
if (!slice.fBuffer) {
@ -1095,7 +1082,9 @@ bool GrMtlGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendT
std::max(1, levelDimensions.height() / 2)};
}
#ifdef SK_BUILD_FOR_MAC
[mtlBuffer->mtlBuffer() didModifyRange:NSMakeRange(slice.fOffset, combinedBufferSize)];
if (this->mtlCaps().isMac()) {
[mtlBuffer->mtlBuffer() didModifyRange:NSMakeRange(slice.fOffset, combinedBufferSize)];
}
#endif
[blitCmdEncoder popDebugGroup];
@ -1507,8 +1496,10 @@ bool GrMtlGpu::readOrTransferPixels(GrSurface* surface,
destinationBytesPerRow: rowBytes
destinationBytesPerImage: imageBytes];
#ifdef SK_BUILD_FOR_MAC
// Sync GPU data back to the CPU
[blitCmdEncoder synchronizeResource: transferBuffer];
if (this->mtlCaps().isMac()) {
// Sync GPU data back to the CPU
[blitCmdEncoder synchronizeResource: transferBuffer];
}
#endif
#ifdef SK_ENABLE_MTL_DEBUG_INFO
[blitCmdEncoder popDebugGroup];