Skip to content

Commit

Permalink
Implement debug names on Metal
Browse files Browse the repository at this point in the history
  • Loading branch information
thatcosmonaut committed Jan 13, 2025
1 parent 4c65453 commit d31f497
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
96 changes: 61 additions & 35 deletions src/gpu/metal/SDL_gpu_metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,15 @@ static void METAL_ReleaseGraphicsPipeline(
return NULL;
}

handle = [renderer->device newComputePipelineStateWithFunction:libraryFunction.function error:&error];
MTLComputePipelineDescriptor *descriptor = [MTLComputePipelineDescriptor new];
descriptor.computeFunction = libraryFunction.function;
descriptor.label = @("Compute Pipeline"); // label can't be nil for some reason
if (renderer->debugMode && SDL_HasProperty(createinfo->props, SDL_PROP_GPU_CREATECOMPUTEPIPELINE_NAME_STRING)) {
const char *name = SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_CREATECOMPUTEPIPELINE_NAME_STRING, NULL);
descriptor.label = @(name);
}

handle = [renderer->device newComputePipelineStateWithDescriptor:descriptor options:MTLPipelineOptionNone reflection: nil error:&error];
if (error != NULL) {
SET_ERROR_AND_RETURN("Creating compute pipeline failed: %s", [[error description] UTF8String], NULL);
}
Expand Down Expand Up @@ -1183,6 +1191,12 @@ static void METAL_ReleaseGraphicsPipeline(
pipelineDescriptor.vertexDescriptor = vertexDescriptor;
}

pipelineDescriptor.label = @("Graphics Pipeline"); // label can't be nil for some reason
if (renderer->debugMode && SDL_HasProperty(createinfo->props, SDL_PROP_GPU_CREATEGRAPHICSPIPELINE_NAME_STRING)) {
const char *name = SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_CREATEGRAPHICSPIPELINE_NAME_STRING, NULL);
pipelineDescriptor.label = @(name);
}

// Create the graphics pipeline

pipelineState = [renderer->device newRenderPipelineStateWithDescriptor:pipelineDescriptor error:&error];
Expand Down Expand Up @@ -1222,17 +1236,13 @@ static void METAL_SetBufferName(
@autoreleasepool {
MetalRenderer *renderer = (MetalRenderer *)driverData;
MetalBufferContainer *container = (MetalBufferContainer *)buffer;
size_t textLength = SDL_strlen(text) + 1;

if (renderer->debugMode) {
container->debugName = SDL_realloc(
container->debugName,
textLength);
if (renderer->debugMode && text != NULL) {
if (container->debugName != NULL) {
SDL_free(container->debugName);
}

SDL_utf8strlcpy(
container->debugName,
text,
textLength);
container->debugName = SDL_strdup(text);

for (Uint32 i = 0; i < container->bufferCount; i += 1) {
container->buffers[i]->handle.label = @(text);
Expand All @@ -1249,17 +1259,13 @@ static void METAL_SetTextureName(
@autoreleasepool {
MetalRenderer *renderer = (MetalRenderer *)driverData;
MetalTextureContainer *container = (MetalTextureContainer *)texture;
size_t textLength = SDL_strlen(text) + 1;

if (renderer->debugMode) {
container->debugName = SDL_realloc(
container->debugName,
textLength);
if (renderer->debugMode && text != NULL) {
if (container->debugName != NULL) {
SDL_free(container->debugName);
}

SDL_utf8strlcpy(
container->debugName,
text,
textLength);
container->debugName = SDL_strdup(text);

for (Uint32 i = 0; i < container->textureCount; i += 1) {
container->textures[i]->handle.label = @(text);
Expand Down Expand Up @@ -1457,6 +1463,11 @@ static void METAL_PopDebugGroup(
metalTexture = (MetalTexture *)SDL_calloc(1, sizeof(MetalTexture));
metalTexture->handle = texture;
SDL_SetAtomicInt(&metalTexture->referenceCount, 0);

if (renderer->debugMode && SDL_HasProperty(createinfo->props, SDL_PROP_GPU_CREATETEXTURE_NAME_STRING)) {
metalTexture->handle.label = @(SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_CREATETEXTURE_NAME_STRING, NULL));
}

return metalTexture;
}

Expand Down Expand Up @@ -1500,6 +1511,10 @@ static bool METAL_SupportsSampleCount(
container->textures[0] = texture;
container->debugName = NULL;

if (SDL_HasProperty(createinfo->props, SDL_PROP_GPU_CREATETEXTURE_NAME_STRING)) {
container->debugName = SDL_strdup(SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_CREATETEXTURE_NAME_STRING, NULL));
}

return (SDL_GPUTexture *)container;
}
}
Expand Down Expand Up @@ -1534,10 +1549,6 @@ static bool METAL_SupportsSampleCount(
container->textureCount += 1;

container->activeTexture = container->textures[container->textureCount - 1];

if (renderer->debugMode && container->debugName != NULL) {
container->activeTexture->handle.label = @(container->debugName);
}
}

return container->activeTexture;
Expand All @@ -1547,7 +1558,8 @@ static bool METAL_SupportsSampleCount(
static MetalBuffer *METAL_INTERNAL_CreateBuffer(
MetalRenderer *renderer,
Uint32 size,
MTLResourceOptions resourceOptions)
MTLResourceOptions resourceOptions,
const char *debugName)
{
id<MTLBuffer> bufferHandle;
MetalBuffer *metalBuffer;
Expand All @@ -1565,6 +1577,10 @@ static bool METAL_SupportsSampleCount(
metalBuffer->handle = bufferHandle;
SDL_SetAtomicInt(&metalBuffer->referenceCount, 0);

if (debugName != NULL) {
metalBuffer->handle.label = @(debugName);
}

return metalBuffer;
}

Expand All @@ -1573,7 +1589,8 @@ static bool METAL_SupportsSampleCount(
MetalRenderer *renderer,
Uint32 size,
bool isPrivate,
bool isWriteOnly)
bool isWriteOnly,
const char *debugName)
{
MetalBufferContainer *container = SDL_calloc(1, sizeof(MetalBufferContainer));
MTLResourceOptions resourceOptions;
Expand All @@ -1586,6 +1603,9 @@ static bool METAL_SupportsSampleCount(
container->isPrivate = isPrivate;
container->isWriteOnly = isWriteOnly;
container->debugName = NULL;
if (container->debugName != NULL) {
container->debugName = SDL_strdup(debugName);
}

if (isPrivate) {
resourceOptions = MTLResourceStorageModePrivate;
Expand All @@ -1600,7 +1620,9 @@ static bool METAL_SupportsSampleCount(
container->buffers[0] = METAL_INTERNAL_CreateBuffer(
renderer,
size,
resourceOptions);
resourceOptions,
debugName);

container->activeBuffer = container->buffers[0];

return container;
Expand All @@ -1609,28 +1631,32 @@ static bool METAL_SupportsSampleCount(
static SDL_GPUBuffer *METAL_CreateBuffer(
SDL_GPURenderer *driverData,
SDL_GPUBufferUsageFlags usage,
Uint32 size)
Uint32 size,
const char *debugName)
{
@autoreleasepool {
return (SDL_GPUBuffer *)METAL_INTERNAL_CreateBufferContainer(
(MetalRenderer *)driverData,
size,
true,
false);
false,
debugName);
}
}

static SDL_GPUTransferBuffer *METAL_CreateTransferBuffer(
SDL_GPURenderer *driverData,
SDL_GPUTransferBufferUsage usage,
Uint32 size)
Uint32 size,
const char *debugName)
{
@autoreleasepool {
return (SDL_GPUTransferBuffer *)METAL_INTERNAL_CreateBufferContainer(
(MetalRenderer *)driverData,
size,
false,
usage == SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD);
usage == SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
debugName);
}
}

Expand Down Expand Up @@ -1694,14 +1720,11 @@ static bool METAL_SupportsSampleCount(
container->buffers[container->bufferCount] = METAL_INTERNAL_CreateBuffer(
renderer,
container->size,
resourceOptions);
resourceOptions,
container->debugName);
container->bufferCount += 1;

container->activeBuffer = container->buffers[container->bufferCount - 1];

if (renderer->debugMode && container->debugName != NULL) {
container->activeBuffer->handle.label = @(container->debugName);
}
}

return container->activeBuffer;
Expand Down Expand Up @@ -4386,6 +4409,8 @@ static void METAL_INTERNAL_DestroyBlitResources(
}

#ifdef SDL_PLATFORM_MACOS
hasHardwareSupport = true;
/*
if (@available(macOS 10.15, *)) {
hasHardwareSupport = [device supportsFamily:MTLGPUFamilyMac2];
} else if (@available(macOS 10.14, *)) {
Expand All @@ -4395,6 +4420,7 @@ static void METAL_INTERNAL_DestroyBlitResources(
if (@available(iOS 13.0, tvOS 13.0, *)) {
hasHardwareSupport = [device supportsFamily:MTLGPUFamilyApple3];
}
*/
#endif

if (!hasHardwareSupport) {
Expand Down
26 changes: 0 additions & 26 deletions src/gpu/vulkan/SDL_gpu_vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -5851,13 +5851,6 @@ static void VULKAN_INTERNAL_CycleActiveBuffer(
container->bufferCount += 1;

container->activeBuffer = buffer;

if (renderer->debugMode && renderer->supportsDebugUtils && container->debugName != NULL) {
VULKAN_INTERNAL_SetBufferName(
renderer,
container->activeBuffer,
container->debugName);
}
}

static void VULKAN_INTERNAL_CycleActiveTexture(
Expand Down Expand Up @@ -5898,13 +5891,6 @@ static void VULKAN_INTERNAL_CycleActiveTexture(
container->textureCount += 1;

container->activeTexture = texture;

if (renderer->debugMode && renderer->supportsDebugUtils && container->debugName != NULL) {
VULKAN_INTERNAL_SetTextureName(
renderer,
container->activeTexture,
container->debugName);
}
}

static VulkanBuffer *VULKAN_INTERNAL_PrepareBufferForWrite(
Expand Down Expand Up @@ -10708,18 +10694,6 @@ static bool VULKAN_INTERNAL_DefragmentMemory(
srcSubresource = &currentRegion->vulkanTexture->subresources[subresourceIndex];
dstSubresource = &newTexture->subresources[subresourceIndex];

// Set debug name if it exists
if (
renderer->debugMode &&
renderer->supportsDebugUtils &&
srcSubresource->parent->container != NULL &&
srcSubresource->parent->container->debugName != NULL) {
VULKAN_INTERNAL_SetTextureName(
renderer,
currentRegion->vulkanTexture,
srcSubresource->parent->container->debugName);
}

VULKAN_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
renderer,
commandBuffer,
Expand Down

0 comments on commit d31f497

Please sign in to comment.