Plumbs labels for the following Metal objects:

- Buffers
- Pipelines
- QuerySets
- Samplers
- Shaders
- Textures

Change also includes:
- Metal debug name utilities

Bug: dawn:1784
Change-Id: Iddd2546f842c794570c225577d35c82e56809c6c
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/130940
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Loko Kung <lokokung@google.com>
This commit is contained in:
Loko Kung 2023-05-03 05:57:06 +00:00 committed by Dawn LUCI CQ
parent 83ffcdecc2
commit 2552dd3c9f
11 changed files with 92 additions and 8 deletions

View File

@ -53,6 +53,7 @@ class Buffer final : public BufferBase {
MaybeError MapAsyncImpl(wgpu::MapMode mode, size_t offset, size_t size) override;
void UnmapImpl() override;
void DestroyImpl() override;
void SetLabelImpl() override;
void* GetMappedPointer() override;
bool IsCPUWritableAtCreation() const override;
MaybeError MapAtCreationImpl() override;

View File

@ -19,6 +19,7 @@
#include "dawn/native/CommandBuffer.h"
#include "dawn/native/metal/CommandRecordingContext.h"
#include "dawn/native/metal/DeviceMTL.h"
#include "dawn/native/metal/UtilsMetal.h"
#include <limits>
@ -109,6 +110,7 @@ MaybeError Buffer::Initialize(bool mappedAtCreation) {
if (mMtlBuffer == nullptr) {
return DAWN_OUT_OF_MEMORY_ERROR("Buffer allocation failed");
}
SetLabelImpl();
// The buffers with mappedAtCreation == true will be initialized in
// BufferBase::MapAtCreation().
@ -235,4 +237,8 @@ void Buffer::ClearBuffer(CommandRecordingContext* commandContext,
value:clearValue];
}
void Buffer::SetLabelImpl() {
SetDebugName(GetDevice(), mMtlBuffer.Get(), "Dawn_Buffer", GetLabel());
}
} // namespace dawn::native::metal

View File

@ -47,8 +47,19 @@ MaybeError ComputePipeline::Initialize() {
&computeData));
NSError* error = nullptr;
mMtlComputePipelineState.Acquire(
[mtlDevice newComputePipelineStateWithFunction:computeData.function.Get() error:&error]);
NSRef<NSString> label = MakeDebugName(GetDevice(), "Dawn_ComputePipeline", GetLabel());
NSRef<MTLComputePipelineDescriptor> descriptorRef =
AcquireNSRef([MTLComputePipelineDescriptor new]);
MTLComputePipelineDescriptor* descriptor = descriptorRef.Get();
descriptor.computeFunction = computeData.function.Get();
descriptor.label = label.Get();
mMtlComputePipelineState.Acquire([mtlDevice
newComputePipelineStateWithDescriptor:descriptor
options:MTLPipelineOptionNone
reflection:nil
error:&error]);
if (error != nullptr) {
return DAWN_INTERNAL_ERROR("Error creating pipeline state " +
std::string([error.localizedDescription UTF8String]));

View File

@ -17,18 +17,21 @@
#include "dawn/common/Math.h"
#include "dawn/common/Platform.h"
#include "dawn/native/metal/DeviceMTL.h"
#include "dawn/native/metal/UtilsMetal.h"
namespace dawn::native::metal {
namespace {
ResultOrError<id<MTLCounterSampleBuffer>> CreateCounterSampleBuffer(Device* device,
NSString* label,
MTLCommonCounterSet counterSet,
uint32_t count)
API_AVAILABLE(macos(10.15), ios(14.0)) {
NSRef<MTLCounterSampleBufferDescriptor> descriptorRef =
AcquireNSRef([MTLCounterSampleBufferDescriptor new]);
MTLCounterSampleBufferDescriptor* descriptor = descriptorRef.Get();
descriptor.label = label;
// To determine which counters are available from a device, we need to iterate through
// the counterSets property of a MTLDevice. Then configure which counters will be
@ -81,6 +84,8 @@ MaybeError QuerySet::Initialize() {
mVisibilityBuffer = AcquireNSPRef([device->GetMTLDevice()
newBufferWithLength:bufferSize
options:MTLResourceStorageModePrivate]);
SetDebugName(GetDevice(), mVisibilityBuffer.Get(), "Dawn_QuerySet_VisibilityBuffer",
GetLabel());
if (mVisibilityBuffer == nil) {
return DAWN_OUT_OF_MEMORY_ERROR("Failed to allocate query set.");
@ -89,18 +94,24 @@ MaybeError QuerySet::Initialize() {
}
case wgpu::QueryType::PipelineStatistics:
if (@available(macOS 10.15, iOS 14.0, *)) {
DAWN_TRY_ASSIGN(mCounterSampleBuffer,
CreateCounterSampleBuffer(device, MTLCommonCounterSetStatistic,
GetQueryCount()));
NSRef<NSString> label = MakeDebugName(
GetDevice(), "Dawn_QuerySet_PipelineStatisticCounterSampleBuffer", GetLabel());
DAWN_TRY_ASSIGN(
mCounterSampleBuffer,
CreateCounterSampleBuffer(device, label.Get(), MTLCommonCounterSetStatistic,
GetQueryCount()));
} else {
UNREACHABLE();
}
break;
case wgpu::QueryType::Timestamp:
if (@available(macOS 10.15, iOS 14.0, *)) {
DAWN_TRY_ASSIGN(mCounterSampleBuffer,
CreateCounterSampleBuffer(device, MTLCommonCounterSetTimestamp,
GetQueryCount()));
NSRef<NSString> label = MakeDebugName(
GetDevice(), "Dawn_QuerySet_TimestampCounterSampleBuffer", GetLabel());
DAWN_TRY_ASSIGN(
mCounterSampleBuffer,
CreateCounterSampleBuffer(device, label.Get(), MTLCommonCounterSetTimestamp,
GetQueryCount()));
} else {
UNREACHABLE();
}

View File

@ -341,6 +341,9 @@ MaybeError RenderPipeline::Initialize() {
AcquireNSRef([MTLRenderPipelineDescriptor new]);
MTLRenderPipelineDescriptor* descriptorMTL = descriptorMTLRef.Get();
NSRef<NSString> label = MakeDebugName(GetDevice(), "Dawn_RenderPipeline", GetLabel());
descriptorMTL.label = label.Get();
NSRef<MTLVertexDescriptor> vertexDesc;
if (GetDevice()->IsToggleEnabled(Toggle::MetalEnableVertexPulling)) {
vertexDesc = AcquireNSRef([MTLVertexDescriptor new]);

View File

@ -72,6 +72,9 @@ MaybeError Sampler::Initialize(const SamplerDescriptor* descriptor) {
NSRef<MTLSamplerDescriptor> mtlDescRef = AcquireNSRef([MTLSamplerDescriptor new]);
MTLSamplerDescriptor* mtlDesc = mtlDescRef.Get();
NSRef<NSString> label = MakeDebugName(GetDevice(), "Dawn_Sampler", GetLabel());
mtlDesc.label = label.Get();
mtlDesc.minFilter = FilterModeToMinMagFilter(descriptor->minFilter);
mtlDesc.magFilter = FilterModeToMinMagFilter(descriptor->magFilter);
mtlDesc.mipFilter = FilterModeToMipFilter(descriptor->mipmapFilter);

View File

@ -21,6 +21,7 @@
#include "dawn/native/metal/DeviceMTL.h"
#include "dawn/native/metal/PipelineLayoutMTL.h"
#include "dawn/native/metal/RenderPipelineMTL.h"
#include "dawn/native/metal/UtilsMetal.h"
#include "dawn/native/stream/BlobSource.h"
#include "dawn/native/stream/ByteVectorSink.h"
#include "dawn/platform/DawnPlatform.h"
@ -376,6 +377,9 @@ MaybeError ShaderModule::CreateFunction(SingleShaderStage stage,
out->function = AcquireNSPRef([*library newFunctionWithName:name.Get()]);
}
std::ostringstream labelStream;
labelStream << GetLabel() << "::" << entryPointName;
SetDebugName(GetDevice(), out->function.Get(), "Dawn_ShaderModule", labelStream.str());
GetDevice()->GetBlobCache()->EnsureStored(mslCompilation);
if (GetDevice()->IsToggleEnabled(Toggle::MetalEnableVertexPulling) &&

View File

@ -78,6 +78,7 @@ class Texture final : public TextureBase {
void InitializeAsWrapping(const TextureDescriptor* descriptor, NSPRef<id<MTLTexture>> wrapped);
void DestroyImpl() override;
void SetLabelImpl() override;
MaybeError ClearTexture(CommandRecordingContext* commandContext,
const SubresourceRange& range,
@ -108,6 +109,7 @@ class TextureView final : public TextureViewBase {
using TextureViewBase::TextureViewBase;
MaybeError Initialize(const TextureViewDescriptor* descriptor);
void DestroyImpl() override;
void SetLabelImpl() override;
NSPRef<id<MTLTexture>> mMtlTextureView;
};

View File

@ -760,6 +760,7 @@ MaybeError Texture::InitializeAsInternalTexture(const TextureDescriptor* descrip
if (mMtlTexture == nil) {
return DAWN_OUT_OF_MEMORY_ERROR("Failed to allocate texture.");
}
SetLabelImpl();
if (device->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting)) {
DAWN_TRY(ClearTexture(device->GetPendingCommandContext(), GetAllSubresources(),
@ -777,6 +778,7 @@ void Texture::InitializeAsWrapping(const TextureDescriptor* descriptor,
NSRef<MTLTextureDescriptor> mtlDesc = CreateMetalTextureDescriptor();
mMtlUsage = [*mtlDesc usage];
mMtlTexture = std::move(wrapped);
SetLabelImpl();
}
MaybeError Texture::InitializeFromIOSurface(const ExternalImageDescriptor* descriptor,
@ -807,6 +809,7 @@ MaybeError Texture::InitializeFromIOSurface(const ExternalImageDescriptor* descr
mMtlTexture = AcquireNSPRef([device->GetMTLDevice() newTextureWithDescriptor:mtlDesc.Get()
iosurface:ioSurface
plane:0]);
SetLabelImpl();
}
SetIsSubresourceContentInitialized(descriptor->isInitialized, GetAllSubresources());
return {};
@ -848,6 +851,10 @@ void Texture::DestroyImpl() {
mIOSurface = nullptr;
}
void Texture::SetLabelImpl() {
SetDebugName(GetDevice(), mMtlTexture.Get(), "Dawn_Texture", GetLabel());
}
id<MTLTexture> Texture::GetMTLTexture() const {
return mMtlTexture.Get();
}
@ -1187,6 +1194,7 @@ MaybeError TextureView::Initialize(const TextureViewDescriptor* descriptor) {
}
}
SetLabelImpl();
return {};
}
@ -1194,6 +1202,10 @@ void TextureView::DestroyImpl() {
mMtlTextureView = nil;
}
void TextureView::SetLabelImpl() {
SetDebugName(GetDevice(), mMtlTextureView.Get(), "Dawn_TextureView", GetLabel());
}
id<MTLTexture> TextureView::GetMTLTexture() const {
ASSERT(mMtlTextureView != nullptr);
return mMtlTextureView.Get();

View File

@ -15,6 +15,9 @@
#ifndef SRC_DAWN_NATIVE_METAL_UTILSMETAL_H_
#define SRC_DAWN_NATIVE_METAL_UTILSMETAL_H_
#include <string>
#include "dawn/common/NSRef.h"
#include "dawn/common/StackContainer.h"
#include "dawn/native/dawn_platform.h"
#include "dawn/native/metal/DeviceMTL.h"
@ -32,6 +35,21 @@ enum class SingleShaderStage;
namespace dawn::native::metal {
NSRef<NSString> MakeDebugName(DeviceBase* device, const char* prefix, std::string label = "");
// Templating for setting the label on MTL objects because not all MTL objects are of the same base
// class. For example MTLBuffer and MTLTexture inherit MTLResource, but MTLFunction does not. Note
// that we allow a nullable Metal object because APISetLabel does not currently do any checks on
// backend resources.
template <typename T>
void SetDebugName(DeviceBase* device, T* mtlObj, const char* prefix, std::string label = "") {
if (mtlObj == nullptr) {
return;
}
NSRef<NSString> debugName = MakeDebugName(device, prefix, label);
[mtlObj setLabel:debugName.Get()];
}
Aspect GetDepthStencilAspects(MTLPixelFormat format);
MTLCompareFunction ToMetalCompareFunction(wgpu::CompareFunction compareFunction);

View File

@ -143,6 +143,19 @@ void ResolveInAnotherRenderPass(
} // anonymous namespace
NSRef<NSString> MakeDebugName(DeviceBase* device, const char* prefix, std::string label) {
std::ostringstream objectNameStream;
objectNameStream << prefix;
if (!label.empty() && device->IsToggleEnabled(Toggle::UseUserDefinedLabelsInBackend)) {
objectNameStream << "_" << label;
}
const std::string debugName = objectNameStream.str();
NSRef<NSString> nsDebugName =
AcquireNSRef([[NSString alloc] initWithUTF8String:debugName.c_str()]);
return nsDebugName;
}
Aspect GetDepthStencilAspects(MTLPixelFormat format) {
switch (format) {
case MTLPixelFormatDepth16Unorm: