Skip to content

Support for arrays of textures #822

Open

Description

@muni-corn

I just wanted to get the ball rolling on support for arrays of textures in shaders. Coming from this issue on the wgpu-rs repo: gfx-rs/wgpu#106

The idea is to be able to use an array of 2d textures in shaders, as originally shown in the issue above:

layout(set = 0, binding = 1) uniform texture2D textures[1024];

Activity

  1. added
    feature request
    on Jun 1, 2020
  2. krogovin commented on Aug 21, 2020

    @krogovin

    What are the expectations for the index into the array? Can it be anything, or must it be "dynamically uniform", i.e. dependent only on values that are derived from uniforms only?

  3. kvark commented on Aug 21, 2020

    @kvark
    Contributor

    wgpu on native exposes the restrictions on the index as different extra features:

    • SAMPLED_TEXTURE_ARRAY_DYNAMIC_INDEXING
    • SAMPLED_TEXTURE_ARRAY_NON_UNIFORM_INDEXING

    cc @cwfitzgerald

  4. jozanza commented on Mar 7, 2021

    @jozanza

    In WGSL, how are we able to access a single texture in an array of textures? A binding such as this seems valid:

    [[group(1), binding(0)]] var textures: array<texture_2d<f32>, 2>;
    

    However, any attempt to access an index via textures[n] throws various compiler errors. Is there some special array access notation or am I just missing something?

    The texture arrays example only has glsl shaders, and I couldn't find info about this in the spec other than the basic array notation syntax.

    Edit: This non-uniform shader is the one I'm interested in writing in WGSL. That "flat" specifier also seems to cause errors everywhere I've tried to place it.

  5. litherum commented on Mar 7, 2021

    @litherum
    Contributor

    I remember that we had a resolution in the group to not allow arrays of textures (as distinct from texture arrays) in the first version of the language, because of uniformity rules. Texture arrays are allowed, though.

  6. jozanza commented on Mar 7, 2021

    @jozanza

    I did see that this was supported by wgpu on native with certain extensions. But you're saying there's currently no valid syntax for working with an array of textures in WGSL?

  7. Kangz commented on Mar 8, 2021

    @Kangz
    Contributor

    I think you're referring to bindless, and that hasn't been discussed for WebGPU at this point. Because the API doesn't support bindless (even as an extension), it doesn't make sense to add support for it in WGSL. Experimental syntax can be added to wgpu-rs / Naga though.

  8. kvark commented on Mar 8, 2021

    @kvark
    Contributor

    Correct, we'll need to work on WGSL syntax for this internally (prototyping in Naga), and then potentially returning to the group with a proposal.

  9. jozanza commented on Mar 8, 2021

    @jozanza

    Thanks for the comments!

    I'm not sure what "bindless" means tbh 👀, but to clarify, is it correct to say there's no way to achieve dynamic non-uniform texture indexing/sampling in WGSL at this point? Like those example shaders cannot be converted to WGSL yet?

  10. kvark commented on Mar 8, 2021

    @kvark
    Contributor

    That's correct, yes. At least, not in the "standard" WGSL.

  11. kainino0x commented on Mar 9, 2021

    @kainino0x
    Contributor

    You should be able to use conditionals to choose dynamically between branches that sample different textures (or perhaps even assign different textures into a pointer variable?). But we don't have a way to bind arrays of textures at the API surface, so you still have to have a separate binding point for each texture.

  12. jozanza commented on Mar 10, 2021

    @jozanza

    @kainino0x I had trouble taking that approach because, when compiling my shader, it apparently violated some non-uniformity restriction to selectively use a texture based on an in var from the vertex shader. So I ended up just rolling with a texture atlas.

  13. kainino0x commented on Mar 11, 2021

    @kainino0x
    Contributor

    Got it. I think it should* be valid if the conditional is uniform, but otherwise it will not be.

    * I don't know if the uniformity rules are actually written out yet, but ultimately I think this will be the case.

  14. added this to the post-V1 milestone on Sep 2, 2021
  15. aloucks commented on Dec 31, 2021

    @aloucks

    Not sure if the support was added to WGSL after this issue, but textureSample and textureSampleLevel both allow you to specify the array layer index when using texture_2d<f32>.

  16. 25 remaining items

  17. kainino0x commented on Nov 2, 2023

    @kainino0x
    Contributor

    Similarly in Vulkan 1.2:

    • shaderInputAttachmentArrayNonUniformIndexing
    • shaderSampledImageArrayNonUniformIndexing
    • shaderStorageImageArrayNonUniformIndexing
    • shaderUniformBufferArrayNonUniformIndexing
    • shaderStorageBufferArrayNonUniformIndexing
    • shaderUniformTexelBufferArrayNonUniformIndexing
    • shaderStorageTexelBufferArrayNonUniformIndexing
  18. greggman commented on Nov 2, 2023

    @greggman
    Member

    From the metal docs

    2.12.1 Arrays of Textures, Texture Buffers, and Samplers
    iOS: Arrays of textures supported since Metal 1.2; arrays of samplers since Metal 2; arrays of
    texture buffers since Metal 2.1.
    macOS: Arrays of textures supported since Metal 2; arrays of samplers since Metal 2; arrays of
    texture buffers since Metal 2.1.

  19. jimblandy commented on Nov 2, 2023

    @jimblandy
    Contributor
    Agenda / Minutes for GPU Web meeting 2023-11-01
    • JB: quick summary of learnings / next steps.
    • KR: haven't thought about this for a month, but Gregg pointed out that we need this for feature parity for WebGL. Can't expand out arrays of textures into single individual texture bindings.
    • JB: is this easier for textures?
    • KN: think it's a hardware feature.
    • GT: think you'll need arrays of samplers too for feature parity with WebGL, since in WebGL they're combined.
    • JB: next steps - figure out what this looks like in backends.
    • KN: not sure how you do this in Vk. Vk has dynamic indexing features for various things… Oh: Vk spec says, shaderSampledImageArrayDynamicIndexing provides both samplers and textures. So yes, we should bundle these together too.
    • JB: for WebGL parity, sounds important, so let's pick this up in the next meeting.
  20. kdashg commented on Dec 6, 2023

    @kdashg
    Contributor
    WGSL 2023-12-05 Minutes
    • DN: Think it’s important gap in functionality now. Coupled to sketching design of array of buffer bindings. Have design issues with uniformity of indexing, but let’s keep that to design.
    • AB: We also think it’s important to do this at the same time as wgsl: Sketch design for arrays of buffer-bindings #2105.
    • JB: Mozilla was expecting to discuss this early in 2024. Either M1 or M2 is fine with us.
    • ->M1
  21. TheMostDiligent commented on Jun 24, 2024

    @TheMostDiligent

    Any updates on the issue?
    WebGPU is the only graphics API in existence that does not support array of resources.
    While dynamically indexing resources may be involved (though all modern graphics APIs do support it), statically indexing resources should not be a problem and is supported supported in GLES1.0 and WebGL1.0.

  22. gitbetter commented on Jun 27, 2024

    @gitbetter

    Wanted to bump this as well. Bindless techniques are pretty common these days for writing GPU driven renderers and it's kind of a bust to not have arrays of textures support. I can be sort of faked by writing image data into storage buffers but then sampler filtering goes out the window and you have to roll out your own filtering solutions in a shader. Would love to know if there's any headway on this. I have no idea what the bureaucracy of open source standards is like 😅

  23. Kangz commented on Jun 27, 2024

    @Kangz
    Contributor

    This issue is not for bindless arrays of textures and buffers (that would be #380), only for fixed-size, bindfull arrays. We'd love to have bindless one day and it's something that's very likely to happen, but the amount of work to make it happen is massive so we aren't entertaining starting it until WebGPU is shipped everywhere.

  24. gitbetter commented on Jun 27, 2024

    @gitbetter

    Oh nice, didn't know there was a separate issue for it! Given that it possibly has a simpler roadmap, statically size bindfull arrays of textures or buffers can give us a sort of starting point for custom bindless implementations (i.e. single global bind group created ahead of time with large arrays of textures and buffers that can be modified on the fly, without recreating bind groups), which is I guess why I'm more interested in this issue for the time being. It seems more tractable and still sort of gives graphics programmer the power of "custom" bindless (albeit not the truest form of bindless).

  25. added a commit that references this issue on Oct 25, 2024
  26. Kangz commented on Nov 19, 2024

    @Kangz
    Contributor
    GPU Web WG 2024-10-29/30 Mountain View F2F
    • (AB requests after 1030a)
    • CW: add an array size in the bind group; it’s inline with binding numbers (not a separate index like in Vulkan). On the WGSL side, there’s a new array type binding_array<type, number> (constant or override). And array indexing. The validation you would expect.
    • CW: What do we do with a GPU external texture. It’s a composite of bindings. WGSL could do a structure of arrays;
    • CW: Texel buffers could be supported.
    • CW: Question about uniformity of indexing. There’s some Vulkan HW which only allows constant indices. Worked around in ANGLE with a giant switch. I think we should support uniform indexing of every binding type. Question is should we support nonuniform indexing.
    • JSP: No preference on nonuniform. Can wait for bindless if we have to.
    • JSP: 1. Do we support the thing with dynamic offsets into these arrays.
    • CW: No.
    • JSP: Can you explain the binding number tithing. Can I put texture in binding0, then storage buffer starting at 10 to 20.
    • CW: Bindgropus can have holes. So that’s ok.
    • CW: Vulkan has a 3D binding model, but here I suggest we flatten it, collapse the array entries as individual binding numbers.
    • JSP: SGTM.
    • BJ: Unlike bindless proposal these don’t have to be last in the bind group.
    • CW: Right. And you can have multiple fixed size arrays in the bindgroup.
    • BJ: So basically this is syntactic sugar over having a bunch of individual bindings. Advantage is you get to index them in the shader.
    • CW: Yes.
    • AB: Looking at your GL hack again, GL couldn’t do storage buffers and things like that. How you’d want to do this in SPIR-V is you’d want to return a pointer, and then you need varaiblePointers. So maybe you have to specialize these functions (inlining etc.) to a great extent, which might get ugly. What’s allowed in base vulkan is arrays of textures.
    • CW: Let’s check gpuinfo for arrays of storage buffers; i think it doesn’t remove any devices. For nonuniform indexing it doesn’t remove many devices.
    • AB: Each nonuniform indexing is its own bit per resource type.
    • JSP: If I say I have 10 textures in the array I have to bind all of them.
    • CW: yes.
    • CW: We have to check availability in Vulkan: when they allow uniform indexing of these; try nonuniform indexing later.
    • KR: There were different rules for 1 and 2. 2 was restricted. Caused problems upgrading 1 to 2.
    • GT: Does the switch statement break derivatives?
    • KN: It works because you assume the index is uniform.
    • CW: Want to get agreement on direction.
    • JSP +1
    • JB: +1
    • TT: How prevalent is this feature. It’s not in Vulkan core.
    • CW: It’s in core in Vulkan.
    • KG: The only reason we punted on it before was because it was work.
    • JSP: you already have that WGSL language feature in wgpu.
    • CW: ok, sounds like we land the proposal doc, then check Vulkan coverage of uniform indexing, and go with that for now.
  27. Kbz-8 commented on May 18, 2025

    @Kbz-8

    Any updates ? This issue has been opened for 5 years on a feature that even WebGL 1.0 supports

  28. Kangz commented on May 19, 2025

    @Kangz
    Contributor
  29. modified the milestones: Milestone 1, Milestone 2 on Oct 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    apifeature request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions