Skip to content

Implement texture-component-swizzle - #9553

Open
beicause wants to merge 27 commits into
gfx-rs:trunkfrom
beicause:texture-component-swizzle
Open

Implement texture-component-swizzle#9553
beicause wants to merge 27 commits into
gfx-rs:trunkfrom
beicause:texture-component-swizzle

Conversation

@beicause

@beicause beicause commented May 14, 2026

Copy link
Copy Markdown
Contributor

Connections
Fixes #1028.

Description
Implementn texture component swizzle feature on vulkan, metal and dx12 (not implemented on gles)

Testing
I added texture component swizzle CTS

cargo xtask cts 'webgpu:api,operation,texture_view,texture_component_swizzle:*'

Squash or Rebase?
Squash

Checklist

  • I self-reviewed and fully understand this PR.
  • WebGPU implementations built with wgpu may be affected behaviorally.
  • Validation and feature gates are in place to confine behavioral changes.
  • Tests demonstrate the validation and altered logic works.
  • CHANGELOG.md entries for the user-facing effects of this change are present.
  • The PR is minimal, and doesn't make sense to land as multiple PRs.
  • Commits are logically scoped and individually reviewable.
  • The PR description has enough context to understand the motivation and solution implemented.

@beicause
beicause force-pushed the texture-component-swizzle branch from 56c7392 to a1c8647 Compare May 15, 2026 06:54
@beicause beicause changed the title (WIP) Implement texture-component-swizzle Implement texture-component-swizzle on Vulkan May 15, 2026
@beicause

This comment was marked as resolved.

@beicause
beicause force-pushed the texture-component-swizzle branch from a1c8647 to b397976 Compare May 15, 2026 07:24
@beicause
beicause marked this pull request as ready for review May 15, 2026 07:25
@beicause beicause changed the title Implement texture-component-swizzle on Vulkan Implement texture-component-swizzle on Vulkan and DX12 May 15, 2026
@beicause
beicause force-pushed the texture-component-swizzle branch 2 times, most recently from 9533b09 to 226392c Compare May 15, 2026 16:40
@beicause
beicause force-pushed the texture-component-swizzle branch from 226392c to 64b2ed3 Compare May 17, 2026 02:03
@beicause beicause changed the title Implement texture-component-swizzle on Vulkan and DX12 Implement texture-component-swizzle May 18, 2026
Comment thread cts_runner/test.lst Outdated
@inner-daemons
inner-daemons self-requested a review May 20, 2026 15:31
@inner-daemons inner-daemons self-assigned this May 20, 2026
@jimblandy jimblandy self-assigned this May 27, 2026
@beicause
beicause force-pushed the texture-component-swizzle branch from 9e12d46 to 9a25432 Compare May 28, 2026 01:53
@beicause
beicause force-pushed the texture-component-swizzle branch from 9a25432 to 0fb4f24 Compare May 28, 2026 01:56
@inner-daemons

Copy link
Copy Markdown
Collaborator

Agh same here, another long delay. Will also prioritize this PR early this week

@inner-daemons inner-daemons left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm tired so not everything I wrote will be coherent or correct. Please ask if you are not sure what I mean anywhere.

This is a useful feature but there are some behavioral changes, particularly on metal, that need to be justified.

Sorry for the delay, hopefully my next review will be quicker.

Comment thread CHANGELOG.md Outdated
Comment thread wgpu/src/backend/webgpu.rs Outdated
/// - Metal on Apple2+ or Mac2+
///
/// Not yet implemented:
/// - OpenGL

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you note whether it would be possible on OpenGL

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OpenGL has glTexParameter GL_TEXTURE_SWIZZLE_*

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ok, maybe file an issue and link it in the feature docs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This remains to be done I think

Comment thread wgpu-types/src/texture.rs
Comment thread wgpu-hal/src/vulkan/mod.rs
Comment thread wgpu-hal/src/metal/device.rs Outdated
usage.intersects(
Tu::RESOURCE | Tu::DEPTH_STENCIL_READ | Tu::STORAGE_READ_ONLY | Tu::STORAGE_READ_WRITE,
),
usage.intersects(Tu::RESOURCE | Tu::STORAGE_READ_ONLY | Tu::STORAGE_READ_WRITE),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a weird change, can you explain?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is needed by https://github.com/beicause/wgpu/blob/0fb4f24f52e432174057ddc63c9f6e3a3d63285e/wgpu-hal/src/metal/device.rs#L632-L635

I think DEPTH_STENCIL_READ doesn't require ShaderRead. Only DEPTH_STENCIL_READ|RESOURCE (RENDER_ATTACHMENT|TEXTURE_BINDING) together requires ShaderRead.

Comment on lines +914 to +916
at_descriptor.setTexture(Some(&at.target.view.attachment.texture));
at_descriptor.setLevel(at.target.view.attachment.base_mip_level as _);
at_descriptor.setSlice(at.target.view.attachment.base_array_layer as _);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why is this change necessary? It warrants some comments in code

@beicause beicause Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I commented in https://github.com/beicause/wgpu/blob/0fb4f24f52e432174057ddc63c9f6e3a3d63285e/wgpu-hal/src/metal/device.rs#L708-L709

When used as a texture binding, depth-stencil needs to use the r001 view, but when used as an attachment, depth-stencil cannot use the swizzled view because it's non-renderable. This is a problem if depth-stencil texture has both RENDER_ATTACHMENT and TEXTURE_BINDING, so we always use the original texture + setLevel/setSlice to avoid the non-renderable view.

Comment thread wgpu-hal/src/metal/adapter.rs Outdated
Comment thread cts_runner/test.lst Outdated

@inner-daemons inner-daemons left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Vast majority of comments are the same renames. Im not super attached to the names I chose but I definitely think they are an improvement.

Nothing here that should be blocking.

Apologies again for the delay, lets try to get this in soon!

Comment thread cts_runner/test.lst Outdated
Comment thread deno_webgpu/lib.rs
}
}

fn map_texture_component_swizzle(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
fn map_texture_component_swizzle(
fn map_all_texture_components_swizzle(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The function names are following the structs TextureComponentSwizzle ComponentSwizzle.

Perhaps the structs should be renamed, but I haven't thought of better names yet.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes I think they should be renamed, since it is not immediatley clear which is which or why IMO.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Current names is from webgpu.h:
WGPUTextureComponentSwizzle
WGPUComponentSwizzle

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Interesting. I wonder why they made that decision. Regardless, you can ignore all my comments on this topic

@beicause beicause Jul 12, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The alternative names I thought of are TextureSwizzle + Swizzle which are shorter with "Component" omitted

But I'll wait for more feedback if we want to change them.

Edit: But webgpu already used texture_component_swizzle everywhere so probably we should stick to TextureComponentSwizzle?

Comment thread deno_webgpu/lib.rs
Ok(wgpu_types::TextureComponentSwizzle { r, g, b, a })
}

fn map_component_swizzle(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
fn map_component_swizzle(
fn map_texture_component_swizzle(

Comment thread deno_webgpu/lib.rs Outdated
Comment thread wgpu-hal/src/dx12/conv.rs
Comment thread wgpu-hal/src/vulkan/conv.rs
Comment thread wgpu-hal/src/vulkan/conv.rs
/// - Metal on Apple2+ or Mac2+
///
/// Not yet implemented:
/// - OpenGL

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This remains to be done I think

Comment thread wgpu/src/backend/webgpu.rs Outdated
Comment thread wgpu/src/backend/webgpu.rs
@inner-daemons

Copy link
Copy Markdown
Collaborator

Can you ping me or request a re-review when you think I should take another look?

Co-authored-by: Inner Daemons <85136135+inner-daemons@users.noreply.github.com>
@beicause
beicause requested a review from inner-daemons July 12, 2026 05:35

@inner-daemons inner-daemons left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Assuming you haven't changed anything major since my last review, approving. Sorry once again for the delay.

@beicause
beicause force-pushed the texture-component-swizzle branch from cef3286 to 33e3a16 Compare July 31, 2026 09:08
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.

Implement texture-component-swizzle feature

3 participants