You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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?
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.
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).
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.
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.
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.
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).
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.
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).
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:
dictionaryGPUColorTargetState{requiredGPUIndex32location;// <<<<<< HERE <<<<<requiredGPUTextureFormatformat;GPUBlendStateblend;GPUColorWriteFlagswriteMask=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 :)
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.
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
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.
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
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.
When creating VkPipeline we need to know the exact VkRenderPass and its subpass. So we have 2 choices:
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.
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.
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.
Activity
kvark commentedon Nov 23, 2020
Could you provide a use case that requires that?
kainino0x commentedon Nov 23, 2020
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?kainino0x commentedon Nov 23, 2020
cc @Popov72
kvark commentedon Nov 23, 2020
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.
kainino0x commentedon Nov 23, 2020
But you would need such a texture allocated in order to bind it there, just for a no-op, right?
Popov72 commentedon Nov 23, 2020
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.drawBufferscan have somegl.NONEentries.[...] 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.NONEbut you get the point).kvark commentedon Nov 24, 2020
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:
We need a proper investigation to see how "true" holes in
colorAttachmentscan be ported to our backend APIs.Kangz commentedon Nov 25, 2020
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:
I wasn't able to find anything for the other APIs so we'll have to write tests.
magcius commentedon Nov 25, 2020
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:
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.
Kangz commentedon Dec 7, 2020
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).
kdashg commentedon Dec 7, 2020
It sounds like this is useful enough, given that our native APIs all support it then?
kainino0x commentedon Dec 7, 2020
Tentative plan to change render {pipeline, pass, bundle encoder} descriptors to take
sequence<thing?>for the attachment lists. (Needs PR)grorg commentedon Dec 7, 2020
Discussed at the 2020-12-07 meeting.
2 remaining items
Allow sparse color attachments and ignored FS outputs
kvark commentedon Feb 4, 2022
That's good to know, but we still need an investigation here.
Kangz commentedon Feb 7, 2022
For Vulkan:
For D3D12 (from @magcius above):
For Metal, the documentation doesn't say anything, however there is
MTLPixelFormatInvalid:That you specify attachments that are valid individually without giving the number of attachments for
MTLRenderPipelinesuggests that sparse attachments are supported. (plus debug devices don't complain).kainino0x commentedon Feb 7, 2022
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
@locationattributes) to start at least.[-]Sparse colorAttachments[/-][+]Sparse colorAttachments in a render pass[/+]kvark commentedon Feb 7, 2022
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:
Why this would be a good idea:
locationvalues.locationis 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 :)kainino0x commentedon Feb 8, 2022
Editors: Considered the following, and we'd ideally like them to be consistent across bind group/layout
entries, andcolorAttachments/targets. Maybe vertex attributes as well, but they're pretty different. PipelinebindGroupLayoutscan'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.sequence<T?>[x, null, undefined, y]orconst a = []; a[0] = x, a[3] = y;shaderLocations are not going to be contiguous starting at 0.bindGroupLayouts: Worksrequired GPUIndex32 location;[{ location: 0, ...x }, { location: 3, ...y }]binding: nmatches@binding(n).location: nalways matches@location(n).shaderLocation: nmatches@location(n).bindGroupLayouts: Works, but we would have to insert a dictionary in between to hold thegroup: nmember, which is unfortunate.record<DOMString, T?>where the key gets parsed as int[x, null, undefined, y]orconst 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.
bindGroupLayouts: WorksWe were overall leaning strongly toward 2, considering 3.
We considered variants of 2 where, for color attachments, (2a)
locationdefaults to0and (2b)locationdefaults 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
bindGroupLayoutskainino0x commentedon Feb 8, 2022
FWIW I verified that you can in fact pass arrays into
record<>types, at least in Blink's implementation.Kangz commentedon Feb 8, 2022
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.
kvark commentedon Feb 8, 2022
I think my proposal isn't going to work.
When creating
VkPipelinewe need to know the exactVkRenderPassand its subpass. So we have 2 choices:createShaderModuletime. 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.VkRenderPassbecause 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.kainino0x commentedon Feb 8, 2022
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.
Allow sparse color attachments and ignored FS outputs
Allow sparse color attachments and ignored FS outputs (#2562)
wgsl: Stub tests for dpdxCoarse builtin (gpuweb#1358)