Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sokol_gfx.h metal: add debug labels and support push/pop debug group #890

Merged
merged 3 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
## Updates

#### 17-Sep-2023

- The sokol-gfx Metal backend now adds debug labels to Metal resource objects and
also passes through the `sg_push/pop_debug_group()` calls. If you use the push/pop
debug group calls, please be aware of the following limitations:

- a push inside a render pass must have an associated pop inside the same render pass
- a push outside any render pass must have an associated pop outside any render pass
- Metal will ignore any push/pop calls outside render passes (this is because in Metal
these are MTLCommandEncoder methods)

Associated issue: https://github.com/floooh/sokol/issues/889, and PR: https://github.com/floooh/sokol/pull/890.

#### 09-Sep-2023

- a small PR has been merged which fixes a redundant glBindFramebuffer() in the GLES3 backend
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Simple
[STB-style](https://github.com/nothings/stb/blob/master/docs/stb_howto.txt)
cross-platform libraries for C and C++, written in C.

[**See what's new**](https://github.com/floooh/sokol/blob/master/CHANGELOG.md) (**26-Jul-2023** proper image/sampler pair support in sokol_nuklear.h)
[**See what's new**](https://github.com/floooh/sokol/blob/master/CHANGELOG.md) (**17-Set-2023** debug label support in sokol_gfx.h Metal backend)

[![Build](/../../actions/workflows/main.yml/badge.svg)](/../../actions/workflows/main.yml) [![Bindings](/../../actions/workflows/gen_bindings.yml/badge.svg)](/../../actions/workflows/gen_bindings.yml) [![build](https://github.com/floooh/sokol-zig/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-zig/actions/workflows/main.yml) [![build](https://github.com/floooh/sokol-nim/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-nim/actions/workflows/main.yml) [![Odin](https://github.com/floooh/sokol-odin/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-odin/actions/workflows/main.yml)[![Rust](https://github.com/floooh/sokol-rust/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-rust/actions/workflows/main.yml)

Expand Down
66 changes: 65 additions & 1 deletion sokol_gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -10883,12 +10883,17 @@ _SOKOL_PRIVATE void _sg_mtl_setup_backend(const sg_desc* desc) {
_sg.mtl.sem = dispatch_semaphore_create(SG_NUM_INFLIGHT_FRAMES);
_sg.mtl.device = (__bridge id<MTLDevice>) desc->context.metal.device;
_sg.mtl.cmd_queue = [_sg.mtl.device newCommandQueue];

for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
_sg.mtl.uniform_buffers[i] = [_sg.mtl.device
newBufferWithLength:(NSUInteger)_sg.mtl.ub_size
options:MTLResourceCPUCacheModeWriteCombined|MTLResourceStorageModeShared
];
#if defined(SOKOL_DEBUG)
_sg.mtl.uniform_buffers[i].label = [NSString stringWithFormat:@"sg-uniform-buffer.%d", i];
#endif
}

if (@available(macOS 10.15, iOS 13.0, *)) {
_sg.mtl.has_unified_memory = _sg.mtl.device.hasUnifiedMemory;
} else {
Expand Down Expand Up @@ -10983,6 +10988,11 @@ _SOKOL_PRIVATE sg_resource_state _sg_mtl_create_buffer(_sg_buffer_t* buf, const
mtl_buf = [_sg.mtl.device newBufferWithLength:(NSUInteger)buf->cmn.size options:mtl_options];
}
}
#if defined(SOKOL_DEBUG)
if (desc->label) {
mtl_buf.label = [NSString stringWithFormat:@"%s.%d", desc->label, slot];
}
#endif
buf->mtl.buf[slot] = _sg_mtl_add_resource(mtl_buf);
_SG_OBJC_RELEASE(mtl_buf);
}
Expand Down Expand Up @@ -11126,6 +11136,11 @@ _SOKOL_PRIVATE sg_resource_state _sg_mtl_create_image(_sg_image_t* img, const sg
_sg_mtl_copy_image_data(img, mtl_tex, &desc->data);
}
}
#if defined(SOKOL_DEBUG)
if (desc->label) {
mtl_tex.label = [NSString stringWithFormat:@"%s.%d", desc->label, slot];
}
#endif
img->mtl.tex[slot] = _sg_mtl_add_resource(mtl_tex);
_SG_OBJC_RELEASE(mtl_tex);
}
Expand Down Expand Up @@ -11167,6 +11182,11 @@ _SOKOL_PRIVATE sg_resource_state _sg_mtl_create_sampler(_sg_sampler_t* smp, cons
mtl_desc.maxAnisotropy = desc->max_anisotropy;
mtl_desc.normalizedCoordinates = YES;
mtl_desc.compareFunction = _sg_mtl_compare_func(desc->compare);
#if defined(SOKOL_DEBUG)
if (desc->label) {
mtl_desc.label = [NSString stringWithUTF8String:desc->label];
}
#endif
mtl_smp = [_sg.mtl.device newSamplerStateWithDescriptor:mtl_desc];
_SG_OBJC_RELEASE(mtl_desc);
}
Expand Down Expand Up @@ -11246,6 +11266,12 @@ _SOKOL_PRIVATE sg_resource_state _sg_mtl_create_shader(_sg_shader_t* shd, const
_SG_ERROR(METAL_FRAGMENT_SHADER_ENTRY_NOT_FOUND);
goto failed;
}
#if defined(SOKOL_DEBUG)
if (desc->label) {
vs_lib.label = [NSString stringWithFormat:@"%s.vs", desc->label];
fs_lib.label = [NSString stringWithFormat:@"%s.fs", desc->label];
}
#endif
// it is legal to call _sg_mtl_add_resource with a nil value, this will return a special 0xFFFFFFFF index
shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_lib = _sg_mtl_add_resource(vs_lib);
_SG_OBJC_RELEASE(vs_lib);
Expand Down Expand Up @@ -11361,6 +11387,11 @@ _SOKOL_PRIVATE sg_resource_state _sg_mtl_create_pipeline(_sg_pipeline_t* pip, _s
rp_desc.colorAttachments[i].sourceAlphaBlendFactor = _sg_mtl_blend_factor(cs->blend.src_factor_alpha);
rp_desc.colorAttachments[i].sourceRGBBlendFactor = _sg_mtl_blend_factor(cs->blend.src_factor_rgb);
}
#if defined(SOKOL_DEBUG)
if (desc->label) {
rp_desc.label = [NSString stringWithFormat:@"%s", desc->label];
}
#endif
NSError* err = NULL;
id<MTLRenderPipelineState> mtl_rps = [_sg.mtl.device newRenderPipelineStateWithDescriptor:rp_desc error:&err];
_SG_OBJC_RELEASE(rp_desc);
Expand Down Expand Up @@ -11393,6 +11424,11 @@ _SOKOL_PRIVATE sg_resource_state _sg_mtl_create_pipeline(_sg_pipeline_t* pip, _s
ds_desc.frontFaceStencil.readMask = desc->stencil.read_mask;
ds_desc.frontFaceStencil.writeMask = desc->stencil.write_mask;
}
#if defined(SOKOL_DEBUG)
if (desc->label) {
ds_desc.label = [NSString stringWithFormat:@"%s.dss", desc->label];
}
#endif
// FIXME: can this actually fail?
id<MTLDepthStencilState> mtl_dss = [_sg.mtl.device newDepthStencilStateWithDescriptor:ds_desc];
_SG_OBJC_RELEASE(ds_desc);
Expand Down Expand Up @@ -11931,6 +11967,19 @@ _SOKOL_PRIVATE void _sg_mtl_update_image(_sg_image_t* img, const sg_image_data*
_sg_mtl_copy_image_data(img, mtl_tex, data);
}

_SOKOL_PRIVATE void _sg_mtl_push_debug_group(const char* name) {
SOKOL_ASSERT(name);
if (_sg.mtl.cmd_encoder) {
[_sg.mtl.cmd_encoder pushDebugGroup:[NSString stringWithUTF8String:name]];
}
}

_SOKOL_PRIVATE void _sg_mtl_pop_debug_group(void) {
if (_sg.mtl.cmd_encoder) {
[_sg.mtl.cmd_encoder popDebugGroup];
}
}

// ██ ██ ███████ ██████ ██████ ██████ ██ ██ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██
// ██ █ ██ █████ ██████ ██ ███ ██████ ██ ██ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██
Expand Down Expand Up @@ -14160,6 +14209,20 @@ static inline void _sg_update_image(_sg_image_t* img, const sg_image_data* data)
#endif
}

static inline void _sg_push_debug_group(const char* name) {
#if defined(SOKOL_METAL)
_sg_mtl_push_debug_group(name);
#else
_SOKOL_UNUSED(name);
#endif
}

static inline void _sg_pop_debug_group(void) {
#if defined(SOKOL_METAL)
_sg_mtl_pop_debug_group();
#endif
}

// ██████ ██████ ██████ ██
// ██ ██ ██ ██ ██ ██ ██
// ██████ ██ ██ ██ ██ ██
Expand Down Expand Up @@ -17002,12 +17065,13 @@ SOKOL_API_IMPL void sg_update_image(sg_image img_id, const sg_image_data* data)
SOKOL_API_IMPL void sg_push_debug_group(const char* name) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(name);
_SOKOL_UNUSED(name);
_sg_push_debug_group(name);
_SG_TRACE_ARGS(push_debug_group, name);
}

SOKOL_API_IMPL void sg_pop_debug_group(void) {
SOKOL_ASSERT(_sg.valid);
_sg_pop_debug_group();
_SG_TRACE_NOARGS(pop_debug_group);
}

Expand Down
Loading