Skip to content

perf(texture): reuse compatible sampled VkImageView for 2D attachments (P2-A) - #5

Merged
Apdelrahman1911 merged 2 commits into
x86simfrom
perf/reuse-sampled-attachment-view
Dec 6, 2026
Merged

Apdelrahman1911 merged 2 commits into
x86simfrom
perf/reuse-sampled-attachment-view

Conversation

@Apdelrahman1911

Copy link
Copy Markdown

Summary

Eliminates redundant vkCreateImageView calls on 1-mip, 1-layer 2D render targets (~42-45% of all remaining image_view_creates on Lavapipe, where vkCreateImageView serializes behind the single rasterizer thread for ~13.4-13.9s per 150s ABBA leg).

Changes

  • crates/metal2vulkan-runtime/src/texture.rs:
    • Pass explicit level_count: 1, layer_count: 1 (instead of REMAINING_MIP_LEVELS/REMAINING_ARRAY_LAYERS) when creating the sampled view of 1-mip, 1-layer 2D images so it satisfies Vulkan framebuffer attachment VUIDs verbatim.
    • Record sampled_is_attachment: bool on Image and View only when all Vulkan attachment invariants hold: TYPE_2D, 1 mip, 1 layer, identity ComponentMapping, full image.aspects, attachment image usage, matching format (for Image), and unrestricted views_usage (for View).
    • Short-circuit TextureObject::attachment_view to return Attachment { view, held: None, .. } when eligible — zero vkCreateImageView, zero attachments mutex contention, zero AttachmentViews gauge churn, and lifetime guaranteed by the owning Texture.
  • crates/metal2vulkan-runtime/src/tests/attachment_views.rs:
    • Add every_device_reuses_sampled_view_for_single_slice_2d_color_attachment (asserts image_view_creates delta == 0, attachment_views gauge delta == 0, views(&tex).is_empty()).
    • Add every_device_allocates_dedicated_attachment_view_when_sampled_view_is_ineligible (asserts dedicated views for Depth32FloatStencil8, swizzled 2D view, and 2-mip 2D target).
  • crates/metal2vulkan-runtime/src/tests/pass.rs:
    • Update expected attachment_views delta from 3 to 2 (words is a 1-mip 2D color target with a compatible sampled view; colors (2 mips) and stencil (no sampled view) still allocate dedicated attachment views).

Verification

  • Verified via GitHub Actions Fork CI (Ubuntu 24.04) (no local compilation/testing per policy).

On CPU Vulkan ICDs (Lavapipe), vkCreateImageView serializes behind the single rasterizer thread and accounts for ~13.4-13.9s of CPU stall per 150s ABBA leg (max single-call latency ~846-1,075ms).

For 1-mip, 1-layer 2D render targets whose sampled VkImageView already has explicit level_count=1, layer_count=1, identity swizzle, full image aspects, and attachment usage, reuse that sampled VkImageView directly in TextureObject::attachment_view with Attachment.held = None instead of creating a duplicate AttachmentView.

Preserves dedicated AttachmentView allocation for combined depth-stencil (where sampled is DEPTH-only), swizzled views, multi-mip/array/cube/3D textures, and usage-restricted cross-format views.

Adds on-device tests verifying both zero-create reuse and dedicated allocation for ineligible textures, and updates tests/pass.rs for the reused words attachment view.
@Apdelrahman1911
Apdelrahman1911 merged commit 0a45daf into x86sim Dec 6, 2026
1 of 3 checks passed
@Apdelrahman1911
Apdelrahman1911 deleted the perf/reuse-sampled-attachment-view branch December 6, 2026 11:38
Apdelrahman1911 added a commit to ios-linux/x86sim that referenced this pull request Dec 6, 2026
…ment view reuse)

Updates external/metal2vulkan submodule from fdaa1dfb7 to 0a45daf1d3cdc25a3d66e64d8162605330e987a5 (ios-linux/metal2vulkan#5: reuse compatible sampled VkImageView for 2D render-pass attachments) so P2-A and P2-B land together.
Apdelrahman1911 added a commit to ios-linux/x86sim that referenced this pull request Dec 6, 2026
…ed views, and bump metal2vulkan (P2) (#23)

* perf(mtlvk): reuse identity tex_view handles and cache BrokerPlane derived views (P2-B)

Eliminates repeated vkCreateImageView churn on Lavapipe from:
1. Identity newTextureViewWithPixelFormat calls in State::tex_view (reuses parent Texture handle with refcounted HeldIndex so OBJ_DEL of parent cannot release the set-1 descriptor index while an identity view remains alive).
2. Repeated non-identity derived views over shared BrokerPlane surfaces (e.g. BGRA8UnormSRGB over BGRA8Unorm) via a bounded per-BrokerPlane Mutex<HashMap<BrokerViewKey, CachedBrokerView>> cache (cap 8, automatically freed with BrokerPlane) consulted by both tex_new_buf and tex_view.

Adds unit tests for identity view detection and broker view key differentiation, plus a Lavapipe integration test verifying zero extra vkCreateImageView calls across repeated sRGB broker views and identity tex_view.

* chore(deps): bump external/metal2vulkan to PR #5 (P2-A sampled attachment view reuse)

Updates external/metal2vulkan submodule from fdaa1dfb7 to 0a45daf1d3cdc25a3d66e64d8162605330e987a5 (ios-linux/metal2vulkan#5: reuse compatible sampled VkImageView for 2D render-pass attachments) so P2-A and P2-B land together.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant