Skip to content

Sparse colorAttachments in a render pass #1250

Closed

Description

@kainino0x

Probably it should be possible to have "holes" in the colorAttachments array (e.g. have attachments 0 and 2, but not 1).

I know we discussed this a VERY long time ago, but right now in the API this is not expressible. We should make sure it's possible and change the API to support it (e.g. sequence of nullable, or put the attachment location inside the GPURenderPassColorAttachmentDescriptor like with GPUBindGroupLayoutEntry).

Activity

  1. kvark commented on Nov 23, 2020

    @kvark
    Contributor

    Could you provide a use case that requires that?

  2. kainino0x commented on Nov 23, 2020

    @kainino0x
    ContributorAuthor

    Perhaps an attachment may or may not be used depending on whether some rendering feature is enabled, but the author doesn't want to have to dynamically generate the [[location(n)]] in the shader based on what features are enabled?

  3. kainino0x commented on Nov 23, 2020

    @kainino0x
    ContributorAuthor
  4. kvark commented on Nov 23, 2020

    @kvark
    Contributor

    A render pipeline can be configured in a way that it outputs to, say, color 0 and color 2 only, leaving the color mask for color 1 as 0. That doesn't require sparse color attachments in the render pass descriptor.

  5. kainino0x commented on Nov 23, 2020

    @kainino0x
    ContributorAuthor

    But you would need such a texture allocated in order to bind it there, just for a no-op, right?

  6. Popov72 commented on Nov 23, 2020

    @Popov72

    The use case in Babylon.js is that we have a pre-pass renderer that can generates albedo/normal/position/irradiance/... textures based on some config parameters. We may not generate all those data, it depends on what the app requires. However, the shader code itself is fixed and expects albedo to be texture 0, normal to be texture 1 and so on. If we have to generate only normal and irradiance, for eg, we only need to bind a texture to buffer 1 and 3 and leave 0 and 2 empty/unbound in webgl.

    [...] Actually it could be a bit more complicated than that because I can see in the code that in some circumstances we "pack" the textures so that there's no holes, but I definitely can also see in the code that the attachment array built for gl.drawBuffers can have some gl.NONE entries.

    [...] So, NONE bindings are used primarily for clearing purpose: the first texture is cleared with some specific values, and the other textures are cleared with another set of values. So, for the 1st binding, [X,0,0,0] is passed to drawBuffers (if we have 4 textures in the color attachments) and for the 2nd binding we pass [0,Y,Z,W] (actually it is not 0 but gl.NONE but you get the point).

  7. kvark commented on Nov 24, 2020

    @kvark
    Contributor

    So what you can already do today is creating dummy textures for the components you don't need to generate, and use them in that render pass with load operation = Clear and store operation = Clear, and having the pipelines not writing to them.
    This approach should work fine, having a few (rather small) issues:

    • the mobile GPU's tile cache would still contain the data for those attachments, even though we aren't using them
    • VRAM is still allocated for those textures, at least for the duration of that initialization pass

    We need a proper investigation to see how "true" holes in colorAttachments can be ported to our backend APIs.

  8. Kangz commented on Nov 25, 2020

    @Kangz
    Contributor

    I'm pretty sure that the pipeline compilers in various APIs are able to pack sparse attachments: they already pack attachments with variable-sized formats inside tile caches.

    For subpass descriptions Vulkan says:

    Each element of the pColorAttachments array corresponds to an output location in the shader, i.e. if the shader declares an output variable decorated with a Location value of X, then it uses the attachment provided in pColorAttachments[X]. If the attachment member of any element of pColorAttachments is VK_ATTACHMENT_UNUSED, writes to the corresponding location by a fragment are discarded.

    I wasn't able to find anything for the other APIs so we'll have to write tests.

  9. magcius commented on Nov 25, 2020

    @magcius

    For D3D12, to bind a gap, you pick an arbitrary format for the RTV, and then fill its descriptor with a null descriptor with the same format -- one created with nullptr passed to CreateRenderTargetView, as documented:

    At least one of pResource or pDesc must be provided. A null pResource is used to initialize a null descriptor, which guarantees D3D11-like null binding behavior (reading 0s, writes are discarded), but must have a valid pDesc in order to determine the descriptor type.

    Note that the D3D12 validation layer has had, in the past, a bug, that would complain about this, but this has been fixed in recent versions of Windows according to Microsoft, and the behavior itself was always safe and spec'd properly.

    Not sure about Metal.

  10. Kangz commented on Dec 7, 2020

    @Kangz
    Contributor

    I did a quick test on Metal with the validation layers on and they didn't complain (and the test passed as in it correctly wrote different values to sparse MRT).

  11. kdashg commented on Dec 7, 2020

    @kdashg
    Contributor

    It sounds like this is useful enough, given that our native APIs all support it then?

  12. kainino0x commented on Dec 7, 2020

    @kainino0x
    ContributorAuthor

    Tentative plan to change render {pipeline, pass, bundle encoder} descriptors to take sequence<thing?> for the attachment lists. (Needs PR)

  13. grorg commented on Dec 7, 2020

    @grorg
    Contributor

    Discussed at the 2020-12-07 meeting.

  14. added this to the V1.0 milestone on Sep 2, 2021
  15. 2 remaining items

  16. added a commit that references this issue on Feb 3, 2022
    4db41e6
  17. kvark commented on Feb 4, 2022

    @kvark
    Contributor

    That's good to know, but we still need an investigation here.

  18. Kangz commented on Feb 7, 2022

    @Kangz
    Contributor

    For Vulkan:

    Each element of the pColorAttachments array corresponds to an output location in the shader, i.e. if the shader declares an output variable decorated with a Location value of X, then it uses the attachment provided in pColorAttachments[X]. If the attachment member of any element of pColorAttachments is VK_ATTACHMENT_UNUSED, writes to the corresponding location by a fragment are discarded.

    For D3D12 (from @magcius above):

    At least one of pResource or pDesc must be provided. A null pResource is used to initialize a null descriptor, which guarantees D3D11-like null binding behavior (reading 0s, writes are discarded), but must have a valid pDesc in order to determine the descriptor type.

    For Metal, the documentation doesn't say anything, however there is MTLPixelFormatInvalid:

    The default value of the pixel format for the MTLRenderPipelineState. You cannot create a texture with this value.

    That you specify attachments that are valid individually without giving the number of attachments for MTLRenderPipeline suggests that sparse attachments are supported. (plus debug devices don't complain).

  19. kainino0x commented on Feb 7, 2022

    @kainino0x
    ContributorAuthor

    Should the limit on the number of color attachments (hardcoded as 8) be on the size of the sequence, or the number of attachments total?

    Easiest to assume it's on the size of the sequence (the values of the @location attributes) to start at least.

  20. changed the title [-]Sparse colorAttachments[/-] [+]Sparse colorAttachments in a render pass[/+] on Feb 7, 2022
  21. kvark commented on Feb 7, 2022

    @kvark
    Contributor

    Thank you for compiling the investigation bits together. I agree that this is sufficient evidence for us to consider this supported.
    In terms of validation, do I understand correctly that each pipeline has to have exactly the same "holes" in the attachment list as the pipeline?

    I'm thinking that this API may not necessarily be the best to serve the user needs. An alternative proposal would be to add a "location" to the color targets:

    dictionary GPUColorTargetState {
        required GPUIndex32 location;  // <<<<<< HERE <<<<<
        required GPUTextureFormat format;
        GPUBlendState blend;
        GPUColorWriteFlags writeMask = 0xF;  // GPUColorWrite.ALL
    };

    Why this would be a good idea:

    • "holes" in the attachment list come naturally here. If I need a render pass for 2 attachments, I create a render pass with 2 attachments. If my shaders happen to write to locations 0 and 3, I can set this up at the pipeline with these location values.
    • effects of modifying shaders are more local. If I change shader output locations, I only need to touch the pipeline, not the whole render pass with all other pipelines used in this render pass.
    • location is consistent with vertex input locations, which we specify in a similar way. With this, we can say that all shader-visible locations are consistently defined by the pipeline. I.e. it makes the API a bit more wholesome :)
  22. kainino0x commented on Feb 8, 2022

    @kainino0x
    ContributorAuthor

    Editors: Considered the following, and we'd ideally like them to be consistent across bind group/layout entries, and colorAttachments/targets. Maybe vertex attributes as well, but they're pretty different. Pipeline bindGroupLayouts can't be sparse right now and I think we tentatively decided not to add that for now, but still reasonable to try to make it the same.

    1. Nullable: sequence<T?>
      [x, null, undefined, y] or const a = []; a[0] = x, a[3] = y;
      • Bind groups: Works, but probably reasonably common to want discontiguous bindings. Also binding numbers can be up to 65535.
      • Attachments: Works, discontiguous attachments probably less common?
      • Attributes: Basically never makes sense. As soon as you have more than 2 vertex buffers, shaderLocations are not going to be contiguous starting at 0.
      • bindGroupLayouts: Works
    2. Index member (kvark's suggestion): required GPUIndex32 location;
      [{ location: 0, ...x }, { location: 3, ...y }]
      • For bind groups: Current solution, pretty good. binding: n matches @binding(n).
      • For attachments: Slightly heavy-handed especially for the most common case (one color attachment), but has the benefit that location: n always matches @location(n).
      • For attributes: Current solution, good. shaderLocation: n matches @location(n).
      • bindGroupLayouts: Works, but we would have to insert a dictionary in between to hold the group: n member, which is unfortunate.
    3. Record+nullable: record<DOMString, T?> where the key gets parsed as int
      [x, null, undefined, y] or const a = []; a[0] = x, a[3] = y; or { 0: x, 3: y }
      Introduces "optional" syntax in the form of being able to use an array OR an object, which is not great for readability especially for anyone who doesn't deeply understand that arrays are actually just objects. Also sort of breaks the consistency argument because now the devs can be inconsistent if they want.
      • Bind groups: Works
      • Attachments: Works
      • Attributes: Using the array syntax would never really be useful.
      • bindGroupLayouts: Works

    We were overall leaning strongly toward 2, considering 3.

    We considered variants of 2 where, for color attachments, (2a) location defaults to 0 and (2b) location defaults to the index in the array. We decided against 2b and were weakly against 2a (it probably is less readable).

    EDIT: forgot to fill out pros/cons for 3
    EDIT: pros/cons for bindGroupLayouts

  23. kainino0x commented on Feb 8, 2022

    @kainino0x
    ContributorAuthor

    FWIW I verified that you can in fact pass arrays into record<> types, at least in Blink's implementation.

    await a.requestDevice({ requiredLimits: [2] }) // Uncaught DOMException: The limit "0" is not recognized.
    await a.requestDevice({ requiredLimits: [] }) // OK
  24. Kangz commented on Feb 8, 2022

    @Kangz
    Contributor

    Given the various proposals above and the discussions of how they would work for the various lists in the API, I don't understand why editors are leaning towards 2).

    For both the list of BGLs and the list of attachments, the large majority of cases will have dense arrays of BGLs or attachments. Intuitively the reason why this would happen is that the space for these things is limited (4 for BGLs, 8 for attachments, 8 for vertex buffers) so developers want to pack them as much as possible. On the contrary binding numbers or attribute locations are just names in the shader that don't really have any concrete meaning outside of "make this refer to that". They have a much larger space of valid values and it makes sense to give developers the flexibility to choose whatever they want (and provide what they wanted in the descriptors).

    This is also a breaking and somewhat structural change, with unclear gains, right as we're trying to tie off loose ends for the API. The bar for making structural changes is getting pretty high and I think this one clearly doesn't pass the bar.

  25. kvark commented on Feb 8, 2022

    @kvark
    Contributor

    I think my proposal isn't going to work.

    When creating VkPipeline we need to know the exact VkRenderPass and its subpass. So we have 2 choices:

    1. Patch the produced shader's output locations to match the ones of the expected render pass. This comes across our ability to generate shader code at createShaderModule time. Things would definitely be more flexible if we'd given up on this, but I believe we are doing the right thing by keeping this ability around.
    2. Create a different VkRenderPass because its subpass would have different routing to the attachments. This would make the render pass incompatible with the one a pipeline is used in. So it's a hard stop here.
  26. kainino0x commented on Feb 8, 2022

    @kainino0x
    ContributorAuthor

    Given the various proposals above and the discussions of how they would work for the various lists in the API, I don't understand why editors are leaning towards 2).

    I should clarify we were leaning toward 2 for attachments mainly, and considering which other things could change to be more consistent. I don't think we will change vertex attributes.

  27. added a commit that references this issue on Feb 9, 2022
    46869b8
  28. added a commit that references this issue on Feb 10, 2022
    b9b28d6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions