GLSL : How to bind thousands of buffers properly?

2019-08-01 05:19发布

问题:

I came up with an idea that requires to bind thousands of buffers (Atomic counters and Shader storage ones) to one GLSL program. I first checked if this would make any sense in the limitations of openGL and it seems possible for two reasons :

  1. On my laptop GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS and GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS both are around 32k. So openGL is enclined to let me bind thousands of buffers for one pass.
  2. openGL 4.4 comes up with :

    void BindBuffersBase(enum target, uint first, sizei count, const uint *buffers);

On the C code it seems easy, but I have no clue on how to do my shaders. When binding indexed buffers, we are supposed to use :

layout (std430, binding = 0) buffer Objects
{
  Object objects[];
};

When binding a couple of buffers it is ok. But in my case :

  1. I don't know in advance how many buffers will be bound
  2. Even if I knew, am I supposed to write some lines of code for EACH buffer and define in the layout binding 0, 1, 2, ..., n ?

The question is then : Is there a way to make the shader unaware of the number of buffer that will be bound to it ? Assuming they are ALL containing the same type of data.

Answer :

Thanks to Andon M. Coleman for the quick and clear answers.

First of all, I was wrong to assume my hardware supported thousands of bindings. Rookie mistakes, I took the defined value instead of the runtime one.

On my machine :

  1. GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS = 96
  2. GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS = 8
  3. GL_MAX_UNIFORM_BUFFER_BINDINGS = 84

Which is far from what I based my question on.

However, there is a mean to define multiple buffers in the shader :

A
{
  B b[];
}
layout (std430, binding = 0) buffer A a[32];

The amount of maximum bindings must be known in advance but those buffers will occupy the bindings from 0 to 31.

回答1:

Thanks to Andon M. Coleman for the quick and clear answers.

First of all, I was wrong to assume my hardware supported thousands of bindings. Rookie mistakes, I took the defined value instead of the runtime one.

On my machine :

  1. GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS = 96
  2. GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS = 8
  3. GL_MAX_UNIFORM_BUFFER_BINDINGS = 84

Which is far from what I based my question on.

However, there is a mean to define multiple buffers in the shader :

A
{
  B b[];
}
layout (std430, binding = 0) buffer A a[32];

The amount of maximum bindings must be known in advance but those buffers will occupy the bindings from 0 to 31.