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

Metal: Performance improvements and bug fixes #98212

Merged
merged 1 commit into from
Oct 21, 2024

Conversation

stuartcarnie
Copy link
Contributor

@stuartcarnie stuartcarnie commented Oct 15, 2024

This PR includes general improvements to the Metal rendering driver.

  • Improves rendering performance when a single MTLRenderCommandEncoder includes a large number of id<MTLResources> by removing redundant useResource: calls when binding the same resources over multiple draw calls
    • This was identified with @clayjohn's drawcalltest project that alternates between 2D rect and line canvas items
    • Frames went from 75 fps to 93 fps with this improvement
  • Fixes a bug that arrays of samplers were not supported by the Metal driver implementation which is needed for 2D: Combine texture state to batch more subsequent commands together #97340

@stuartcarnie stuartcarnie requested a review from a team as a code owner October 15, 2024 20:18
Copy link
Contributor Author

@stuartcarnie stuartcarnie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few comments

@@ -58,7 +58,7 @@

void MDCommandBuffer::begin() {
DEV_ASSERT(commandBuffer == nil);
commandBuffer = queue.commandBuffer;
commandBuffer = queue.commandBufferWithUnretainedReferences;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Metal driver maintains references to the Metal resources, so there is no need for the command buffer to perform add ref and release calls.

Comment on lines +2063 to +2066
primary.arrayLength = 1;
for (uint32_t const &a : a_type.array) {
primary.arrayLength *= a;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix to support arrays of samplers

Copy link
Member

@clayjohn clayjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love to see that performance improvement!

@@ -271,8 +288,14 @@ class API_AVAILABLE(macos(11.0), ios(14.0)) MDCommandBuffer {
blend_constants.reset();
vertex_buffers.clear();
vertex_offsets.clear();
// keep the keys, as they are likely to be used again
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// keep the keys, as they are likely to be used again
// Keep the keys, as they are likely to be used again.

_FORCE_INLINE_ void reset() {
pipeline = nil;
encoder = nil;
// keep the keys, as they are likely to be used again
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// keep the keys, as they are likely to be used again
// Keep the keys, as they are likely to be used again.

if (resources == nullptr) {
resources = &p_dst.insert(keyval.key, ResourceVector())->value;
}
// reserve space for the new resources, assuming they are all added
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// reserve space for the new resources, assuming they are all added
// Reserve space for the new resources, assuming they are all added.

uint32_t i = 0, j = 0;
__unsafe_unretained id<MTLResource> *resources_ptr = resources->ptr();
const __unsafe_unretained id<MTLResource> *keyval_ptr = keyval.value.ptr();
// 2-way merge
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 2-way merge
// 2-way merge.

return;
}

// bind all resources
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// bind all resources
// Bind all resources.

return;
}

// bind all resources
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// bind all resources
// Bind all resources.

@stuartcarnie stuartcarnie force-pushed the sgc/metal_improvements branch from 10b9041 to 83ac274 Compare October 20, 2024 00:15
@stuartcarnie
Copy link
Contributor Author

@AThousandShips thanks for the feedback – all those changes are in

@Repiteo Repiteo merged commit 4630cbc into godotengine:master Oct 21, 2024
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Oct 21, 2024

Thanks!

@stuartcarnie stuartcarnie deleted the sgc/metal_improvements branch October 25, 2024 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants