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
Let's say that for some reason I would like to create a pipeline that only uses bind groups 0 and 2. This could happen for example if I choose BG 1 to be for the fragment data and BG 2 for vertex data, and want to render without an FS. (but many other reasons).
Because GPUPipelineLayoutDescriptor.bindGroupLayout is non-sparse, right now to create the pipeline I need to do the following:
constbgl0=device.createBindGroupLayout(...);constbgl2=device.createBindGroupLayout(...);constlayout=device.createPipelineLayout({bindGroupLayouts: [bgl0,device.createBindGroupLayout({entries: []}),// This is the important partbgl2,],});constpipeline= ...;
But then to draw with this pipeline I need to set an empty GPUBindGroup at group index 1 to satisfy "validate encoder bindgroups". This is an annoying to just "unset" a bindgroup.
I'd like to suggest that we allow either GPUPipelineLayoutDescriptor.bindGroupLayout to be sparse, or during validation, treat the empty bindgrouplayout the same way as requiring no bind groups.
Vulkan requires const VkDescriptorSetLayout* pSetLayouts; to be provided in VkPipelineLayoutCreateInfo. So it requires consecutive bind groups, essentially.
Metal doesn't have a pipeline layout concept natively
DX12's root signature is virtualized, so it doesn't care about the gaps in the pipeline layout.
Given that WebGPU's binding model is closest to Vulkan, it makes sense to consider how it's used in Vulkan land. Since it's impossible to create a gap in the pipeline layout, people aren't doing it. Therefore, what motivates us to consider having special logic here?
Those coming from Vulkan aren't doing it. For those who want a gab, they can create an empty BGL/BG and use it.
Since it's impossible to create a gap in the pipeline layout, people aren't doing it. Therefore, what motivates us to consider having special logic here?
In Vulkan land people have to make holes using empty VkDescriptorSetLayouts. Compared to Vulkan, developer-friendliness is more important in WebGPU and why I raised this issue.
Is the developer-friendliness argument based on real feedback, or just threoretical?
I'd put this feature into "worth looking after MVP" category, since it's non-essential and involves API complexity.
As discussed in the last meeting, here's an example of what the validation looks like at draw time at the moment (in pseudo code, actual validation is different in Dawn for a mix of optimization and historical reasons):
classState {
voidOnSetBindGroup(int index, BindGroup* group) {
mBindGroups[i] = group;
}
voidOnSetPipeline(PipelineBase* pipeline) {
mPipeline = pipeline;
}
MaybeError ValidateBindGroupsForDraw() {
int bglCount = mPipeline->GetBindGroupLayoutCount();
for (int i = 0; i < bglCount; i++) {
if (mBindGroups[i]->GetLayout() != mPipeline->GetBindGroupLayout(i)) {
returnVALIDATION_ERROR("Blah");
}
// Check the buffer sizes if needed if minBindingSize wasn't used.
}
return {};
}
BindGroup* mBindGroups[4];
PipelineBase* mPipeline;
};
Adding the fact that a null bindgroup can be used with the empty pipeline layout (or null layout) requires changing the condition to something like:
However if we decided to canonicalize null BGLs to the empty BGL at pipeline creation, then we can do the following, which isn't too bad (it tracks BGLs in OnSetBindGroup and canonicalize to the empty bind group layout when possible):
classState {
voidOnSetBindGroup(int i, BindGroup* group) {
mBindGroups[i] = group;
if (group == nullptr) {
mBGLs[i] = EmptyBindGroupLayout;
} else {
mBGLs[i] = group->GetLayout();
}
}
voidOnSetPipeline(PipelineBase* pipeline) {
mPipeline = pipeline;
}
MaybeError ValidateBindGroupsForDraw() {
int bglCount = mPipeline->GetBindGroupLayoutCount();
for (int i = 0; i < bglCount; i++) {
if (mBGLs[i] != mPipeline->GetBindGroupLayout(i)) {
returnVALIDATION_ERROR("Blah");
}
// Check the buffer sizes if needed if minBindingSize wasn't used.
}
return {};
}
BindGroup* mBindGroups[4];
BindGroupLayout* mBGLs[4];
PipelineBase* mPipeline;
};
There's a very slight difference in the validation cost which I don't think will be measurable, so this shouldn't be a concern.
I think, conceptually, we already have null BGL entries - it's all of the entries after the set provided by the user. Correspondingly, the behavior of setBindGroup(N, X) for N >= number of entries of the current pipeline would be the same as for N corresponding to a null entry. So this change is not complicating the pass state in theory.
It's not 100% clear if these are the same: currently WebGPU doesn't require that unused bindgroups past pipeline->bindgroupLayoutCount are the empty bindgroup. However it is not clear if for holes in the pipeline the bindgroup should be ignored, or if we require them to be null/empty.
I've hit this issue in our engine as well but it might be because of a bad design decision regarding bindgroups. Moving from WebGL is not trivial, please let me know if I missed a simpler solution here.
The first thing I did to support bindgroups was to attribute a static index to all of our bindgroups. There's an enum somewhere that looks like PER_PASS : 0, PER_MATERIAL : 1, PER_DRAW : 2. These are used to identify data when building pipeline layouts for example. Then I wrote a bunch of ubershaders that will use some or all of these bindgroups. The ubershaders are specialized at runtime based on what the current pass is or what features the material has. ( Think #ifdef MUST_TONEMAP, #ifdef HAS_CLEARCOAT or whatever )
Because of this runtime specialization, I cannot know in advance which bindgroups will be used. It is entirely possible that a shader module will use the PER_PASS and PER_DRAW bindgroups but not the PER_MATERIAL bindgroup. This means that if I write these static indices in the shader code, there will be a hole in the indices.
The first solution I used to solve this problem was to generate dynamic indices and to procedurally write the IO code for our shaders at runtime. In the previous example, PER_PASS would remain 0 but PER_DRAW would be remapped to 1. At the time I was using this solution, I was not caching shader modules separately but rather couples of vs + fs, a shader "program" in the WebGL terminology.
This was convenient because I would get the pipeline layout at module creation time. I would look at the code of both vs and fs, identify the bindgroups needed by both, remap the indices and then create two shader modules. It worked well but would trigger redundant shader module compilations. For example, for the two programs vs0 + fs0 and vs0 + fs1, I would compile vs0 twice.
To avoid the redundant compilations, I decided to cache shader modules separately. But if I do that, I don't get the entire pipeline layout at module creation time, which means it is impossible to remap bindgroup indices. Let's say for example the VS uses PER_PASS and the FS uses PER_DRAW. At module creation time, both would get their indices remapped to 0 because each shader only knows about its own layout.
I would defer this index remapping operation at pipeline creation time but since I need to hardcode bindgroup indices in the shader code, that would mean deferring shader modules creation at pipeline creation. My solution to this was to go back to using static indices. At pipeline creation time I figure out the pipeline layout based on the layouts of my two modules, I figure out if there are holes and whenever I'm drawing I bind an empty bindgroup where there is a hole.
Because of the way my code is written, allowing empty bindgroups was super easy, it just feels weird to do it. Also, if you don't have proper caching in place you might end up creating quite a few empty bindgroups.
Thank you for the detailed explanation of how you hit this in practice! I think that having PER_PASS/MATERIAL/DRAW statically assigned to 0/1/2 is a good approach but like you said it will give rise to empty bindgrouplayouts that need to match with an empty bindgroup bound at runtime. This is a small papercut that we could remove from the API for minimal additional validation cost.
@frguthmann thank you for describing!
Frankly speaking, it looks like you are using the pipeline layout in a non-idiomatic way, and we shouldn't adjust the API to cater to this kind of usage. Let me explain.
The idea of a pipeline layout in general is to separate resource binding from pipelines. An idiomatic application would first figure out what pipeline layouts it needs, and then for each pipeline it would know exactly what layout it expects. What you are trying to do is to derive the layouts based on the shader code, instead. So this is asking for trouble.
Because of this runtime specialization, I cannot know in advance which bindgroups will be used. It is entirely possible that a shader module will use the PER_PASS and PER_DRAW bindgroups but not the PER_MATERIAL bindgroup. This means that if I write these static indices in the shader code, there will be a hole in the indices.
This sounds totally fine. You just described that you effectively have a pipeline layout with 3 BGL entries: PER_PASS, PER_MATERIAL, PER_DRAW. If the shader doesn't use some of these (leaving holes), that is totally normal, and doesn't require any spec adjustments.
Everything that is written after it appears to try solving this "hole in the indices" as a problem. So that confuses me. Why do you find this a problem? You effectively have one pipeline layout with these 3 BGL entries, and all of your shaders are compatible with it, so why do you need to make it any more complicated?
From my understanding, the issue is that they need to fill the missing bind group with resources they don't have, because there's no way to bind nothing. If PER_MATERIAL has slots for 16 textures, they must fill in a bind group with 16 textures even if the shader does not use that bind group, which is unfortunate.
A non-sparse pipeline layout ends up being troublesome because there is no limit on maxBindGroups. Say it's 1000. Suddenly I can't fill up bindGroups because other settings limit the number of each type of resource (maxSamplersPerShaderStage, maxSampledTexturesPerShaderStage, maxUniformBuffersPerShaderStage, maxStorageBuffersPerShaderStage, maxStorageTexturesPerShaderStage).
It should be possible to fill it up with empty bind groups. The "auto" default pipeline layout algorithm doesn't actually generate pipeline layouts with holes, it fills the holes with empty bind group layouts.
It should be possible to fill it up with empty bind groups. The "auto" default pipeline layout algorithm doesn't actually generate pipeline layouts with holes, it fills the holes with empty bind group layouts.
Wouldn't that then require binding empty groups to pass validation at draw time?
Wouldn't that then require binding empty groups to pass validation at draw time?
It does, which is what this issue is about: the pipeline layout should get empty bind group layouts, but we should require users to actually bind empty bind groups there. However I don't think we should count the number of non-empty BGLs for maxBindGroupsPlusVertexBuffers, only the largest group index used (+1) IMHO. (plus I still believe this limit will never actually be able to be hit in any browser)
Ok, I was unaware of the empty bind groups stuff. Thanks for clarifying.
Is it spelled out that all empty bindgroups are compatible with each other? Usually, bind groups from a layout: 'auto' are not compatible even if they would appear to be. But, in the case of an empty bindgroup, it appears any empty bind group will do. That's great! Just didn't find it in the spec.
NOTE: Default layouts are provided as a convenience for simple pipelines, but use of explicit layouts is recommended in most cases. Bind groups created from default layouts cannot be used with other pipelines, and the structure of the default layout may change when altering shaders, causing unexpected bind group creation errors.
But a quick test seems to be that if the bind group layout is empty then even from a default layout it's compatible.
Actually, layouts from 2 different pipelines generated with default layouts are compatible with each other even though the spec seems to say they are not supposed to be. Is that a bug in Chrome?
Is it spelled out that all empty bindgroups are compatible with each other? Usually, bind groups from a layout: 'auto' are not compatible even if they would appear to be. But, in the case of an empty bindgroup, it appears any empty bind group will do. That's great! Just didn't find it in the spec.
I don't think that's spelled out, I don't even think it was intentional. Both things you found are probably bugs in Chrome - though for empty bind groups we might consider making an exception in the spec for convenience.
CW: If you have a GPU pipeline layout, and you want to leave an empty bind group layout, you have to add a bindgroup with zero bindings. That’s annoying. Proposal has three parts:
Change GPUPipelineLayoutDescriptor.bindGroupLayouts to be required sequence<GPUBindGroupLayout?> (the change is the addition of the ?)
Change the algorithm for createPipelineLayout to reify an empty BGL to "null" instead.
Activity
kvark commentedon Aug 17, 2021
Let's see how native APIs deal with this:
const VkDescriptorSetLayout* pSetLayouts;to be provided inVkPipelineLayoutCreateInfo. So it requires consecutive bind groups, essentially.Given that WebGPU's binding model is closest to Vulkan, it makes sense to consider how it's used in Vulkan land. Since it's impossible to create a gap in the pipeline layout, people aren't doing it. Therefore, what motivates us to consider having special logic here?
Those coming from Vulkan aren't doing it. For those who want a gab, they can create an empty BGL/BG and use it.
kainino0x commentedon Aug 17, 2021
vaguely related: #1250
Kangz commentedon Aug 18, 2021
In Vulkan land people have to make holes using empty VkDescriptorSetLayouts. Compared to Vulkan, developer-friendliness is more important in WebGPU and why I raised this issue.
kvark commentedon Aug 18, 2021
Is the developer-friendliness argument based on real feedback, or just threoretical?
I'd put this feature into "worth looking after MVP" category, since it's non-essential and involves API complexity.
Kangz commentedon Aug 18, 2021
For now it is mostly theoretical. I ran into it a couple times but that was to write tests so not very real-world.
Kangz commentedon Aug 24, 2021
As discussed in the last meeting, here's an example of what the validation looks like at draw time at the moment (in pseudo code, actual validation is different in Dawn for a mix of optimization and historical reasons):
Adding the fact that a null bindgroup can be used with the empty pipeline layout (or null layout) requires changing the condition to something like:
However if we decided to canonicalize null BGLs to the empty BGL at pipeline creation, then we can do the following, which isn't too bad (it tracks BGLs in
OnSetBindGroupand canonicalize to the empty bind group layout when possible):There's a very slight difference in the validation cost which I don't think will be measurable, so this shouldn't be a concern.
kvark commentedon Aug 24, 2021
I think, conceptually, we already have null BGL entries - it's all of the entries after the set provided by the user. Correspondingly, the behavior of
setBindGroup(N, X)for N >= number of entries of the current pipeline would be the same as for N corresponding to a null entry. So this change is not complicating the pass state in theory.Kangz commentedon Aug 24, 2021
It's not 100% clear if these are the same: currently WebGPU doesn't require that unused bindgroups past
pipeline->bindgroupLayoutCountare the empty bindgroup. However it is not clear if for holes in the pipeline the bindgroup should be ignored, or if we require them to be null/empty.frguthmann commentedon Aug 25, 2021
I've hit this issue in our engine as well but it might be because of a bad design decision regarding bindgroups. Moving from WebGL is not trivial, please let me know if I missed a simpler solution here.
The first thing I did to support bindgroups was to attribute a static index to all of our bindgroups. There's an enum somewhere that looks like PER_PASS : 0, PER_MATERIAL : 1, PER_DRAW : 2. These are used to identify data when building pipeline layouts for example. Then I wrote a bunch of ubershaders that will use some or all of these bindgroups. The ubershaders are specialized at runtime based on what the current pass is or what features the material has. ( Think #ifdef MUST_TONEMAP, #ifdef HAS_CLEARCOAT or whatever )
Because of this runtime specialization, I cannot know in advance which bindgroups will be used. It is entirely possible that a shader module will use the PER_PASS and PER_DRAW bindgroups but not the PER_MATERIAL bindgroup. This means that if I write these static indices in the shader code, there will be a hole in the indices.
The first solution I used to solve this problem was to generate dynamic indices and to procedurally write the IO code for our shaders at runtime. In the previous example, PER_PASS would remain 0 but PER_DRAW would be remapped to 1. At the time I was using this solution, I was not caching shader modules separately but rather couples of vs + fs, a shader "program" in the WebGL terminology.
This was convenient because I would get the pipeline layout at module creation time. I would look at the code of both vs and fs, identify the bindgroups needed by both, remap the indices and then create two shader modules. It worked well but would trigger redundant shader module compilations. For example, for the two programs vs0 + fs0 and vs0 + fs1, I would compile vs0 twice.
To avoid the redundant compilations, I decided to cache shader modules separately. But if I do that, I don't get the entire pipeline layout at module creation time, which means it is impossible to remap bindgroup indices. Let's say for example the VS uses PER_PASS and the FS uses PER_DRAW. At module creation time, both would get their indices remapped to 0 because each shader only knows about its own layout.
I would defer this index remapping operation at pipeline creation time but since I need to hardcode bindgroup indices in the shader code, that would mean deferring shader modules creation at pipeline creation. My solution to this was to go back to using static indices. At pipeline creation time I figure out the pipeline layout based on the layouts of my two modules, I figure out if there are holes and whenever I'm drawing I bind an empty bindgroup where there is a hole.
Because of the way my code is written, allowing empty bindgroups was super easy, it just feels weird to do it. Also, if you don't have proper caching in place you might end up creating quite a few empty bindgroups.
Kangz commentedon Aug 25, 2021
Thank you for the detailed explanation of how you hit this in practice! I think that having PER_PASS/MATERIAL/DRAW statically assigned to 0/1/2 is a good approach but like you said it will give rise to empty bindgrouplayouts that need to match with an empty bindgroup bound at runtime. This is a small papercut that we could remove from the API for minimal additional validation cost.
kvark commentedon Aug 25, 2021
@frguthmann thank you for describing!
Frankly speaking, it looks like you are using the pipeline layout in a non-idiomatic way, and we shouldn't adjust the API to cater to this kind of usage. Let me explain.
The idea of a pipeline layout in general is to separate resource binding from pipelines. An idiomatic application would first figure out what pipeline layouts it needs, and then for each pipeline it would know exactly what layout it expects. What you are trying to do is to derive the layouts based on the shader code, instead. So this is asking for trouble.
This sounds totally fine. You just described that you effectively have a pipeline layout with 3 BGL entries: PER_PASS, PER_MATERIAL, PER_DRAW. If the shader doesn't use some of these (leaving holes), that is totally normal, and doesn't require any spec adjustments.
Everything that is written after it appears to try solving this "hole in the indices" as a problem. So that confuses me. Why do you find this a problem? You effectively have one pipeline layout with these 3 BGL entries, and all of your shaders are compatible with it, so why do you need to make it any more complicated?
magcius commentedon Dec 21, 2021
From my understanding, the issue is that they need to fill the missing bind group with resources they don't have, because there's no way to bind nothing. If PER_MATERIAL has slots for 16 textures, they must fill in a bind group with 16 textures even if the shader does not use that bind group, which is unfortunate.
14 remaining items
kainino0x commentedon Feb 6, 2024
It should be possible to fill it up with empty bind groups. The "auto" default pipeline layout algorithm doesn't actually generate pipeline layouts with holes, it fills the holes with empty bind group layouts.
greggman commentedon Feb 6, 2024
Wouldn't that then require binding empty groups to pass validation at draw time?
https://gpuweb.github.io/gpuweb/#abstract-opdef-validate-encoder-bind-groups
Kangz commentedon Feb 7, 2024
It does, which is what this issue is about: the pipeline layout should get empty bind group layouts, but we should require users to actually bind empty bind groups there. However I don't think we should count the number of non-empty BGLs for
maxBindGroupsPlusVertexBuffers, only the largest group index used (+1) IMHO. (plus I still believe this limit will never actually be able to be hit in any browser)greggman commentedon Feb 7, 2024
Ok, I was unaware of the empty bind groups stuff. Thanks for clarifying.
Is it spelled out that all empty bindgroups are compatible with each other? Usually, bind groups from a
layout: 'auto'are not compatible even if they would appear to be. But, in the case of an empty bindgroup, it appears any empty bind group will do. That's great! Just didn't find it in the spec.Here (https://gpuweb.github.io/gpuweb/#default-pipeline-layout) there's a note:
But a quick test seems to be that if the bind group layout is empty then even from a default layout it's compatible.
Actually, layouts from 2 different pipelines generated with default layouts are compatible with each other even though the spec seems to say they are not supposed to be. Is that a bug in Chrome?
kainino0x commentedon Feb 7, 2024
I don't think that's spelled out, I don't even think it was intentional. Both things you found are probably bugs in Chrome - though for empty bind groups we might consider making an exception in the spec for convenience.
Kangz commentedon Oct 25, 2024
Since I'm hoping to discuss this at the upcoming F2F, here is a concrete proposal.
GPUPipelineLayoutDescriptor.bindGroupLayoutsto berequired sequence<GPUBindGroupLayout?>(the change is the addition of the?)beaufortfrancois commentedon Oct 31, 2024
@Kangz I've started #4946
Kangz commentedon Nov 19, 2024
GPU Web WG 2024-10-29/30 Mountain View F2F
GPUPipelineLayoutDescriptor.bindGroupLayoutsto berequired sequence<GPUBindGroupLayout?>(the change is the addition of the?)